|
1
|
import { updateRegionsWorkflow } from "@medusajs/core-flows" |
|
2
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
3
|
import { MedusaContainer, RegionDTO } from "@medusajs/types" |
|
4
|
import { createAdminUser } from "../../../../helpers/create-admin-user" |
|
5
|
|
|
6
|
jest.setTimeout(200000) |
|
7
|
|
|
8
|
const env = {} |
|
9
|
const adminHeaders = { |
|
10
|
headers: { "x-medusa-access-token": "test_token" }, |
|
11
|
} |
|
12
|
|
|
13
|
medusaIntegrationTestRunner({ |
|
14
|
env, |
|
15
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
16
|
let container: MedusaContainer |
|
17
|
let region: RegionDTO |
|
18
|
|
|
19
|
beforeAll(() => { |
|
20
|
container = getContainer() |
|
21
|
}) |
|
22
|
|
|
23
|
beforeAll(async () => { |
|
24
|
await createAdminUser(dbConnection, adminHeaders, container) |
|
25
|
|
|
26
|
region = ( |
|
27
|
await api.post( |
|
28
|
`/admin/regions`, |
|
29
|
{ |
|
30
|
name: "Test Region", |
|
31
|
currency_code: "usd", |
|
32
|
countries: ["us", "ca"], |
|
33
|
metadata: { foo: "bar" }, |
|
34
|
}, |
|
35
|
adminHeaders |
|
36
|
) |
|
37
|
).data.region |
|
38
|
|
|
39
|
await dbUtils.snapshot() |
|
40
|
}) |
|
41
|
|
|
42
|
describe("update region workflow", () => { |
|
43
|
it("should update a region", async () => { |
|
44
|
const updateData = { |
|
45
|
name: "United States update", |
|
46
|
} |
|
47
|
|
|
48
|
const { |
|
49
|
result: [updatedRegion], |
|
50
|
} = await updateRegionsWorkflow(container).run({ |
|
51
|
input: { |
|
52
|
selector: { id: region.id }, |
|
53
|
update: updateData, |
|
54
|
}, |
|
55
|
}) |
|
56
|
|
|
57
|
expect(updatedRegion).toEqual( |
|
58
|
expect.objectContaining({ |
|
59
|
id: region.id, |
|
60
|
name: updateData.name, |
|
61
|
}) |
|
62
|
) |
|
63
|
}) |
|
64
|
|
|
65
|
it("should revert region update and payment providers update when it fails", async () => { |
|
66
|
const paymentProviderId = "pp_system_default" |
|
67
|
const paymentProvider2Id = "pp_system_default_2" |
|
68
|
const updateData = { |
|
69
|
name: "United States update", |
|
70
|
payment_providers: [paymentProviderId, paymentProvider2Id], |
|
71
|
} |
|
72
|
|
|
73
|
const workflow = updateRegionsWorkflow(container) |
|
74
|
workflow.addAction( |
|
75
|
"throw", |
|
76
|
{ |
|
77
|
invoke: async function failStep() { |
|
78
|
throw new Error(`Failed to update region`) |
|
79
|
}, |
|
80
|
}, |
|
81
|
{ |
|
82
|
noCompensation: true, |
|
83
|
} |
|
84
|
) |
|
85
|
|
|
86
|
const { errors } = await workflow.run({ |
|
87
|
input: { |
|
88
|
selector: { id: region.id }, |
|
89
|
update: updateData, |
|
90
|
}, |
|
91
|
throwOnError: false, |
|
92
|
}) |
|
93
|
|
|
94
|
expect(errors).toHaveLength(1) |
|
95
|
expect(errors[0].error.message).toEqual(`Failed to update region`) |
|
96
|
|
|
97
|
const updatedRegion = ( |
|
98
|
await api.get( |
|
99
|
`/admin/regions/${region.id}?fields=*payment_providers`, |
|
100
|
adminHeaders |
|
101
|
) |
|
102
|
).data.region |
|
103
|
|
|
104
|
expect(updatedRegion).toEqual( |
|
105
|
expect.objectContaining({ |
|
106
|
id: region.id, |
|
107
|
name: region.name, |
|
108
|
payment_providers: [], |
|
109
|
}) |
|
110
|
) |
|
111
|
}) |
|
112
|
}) |
|
113
|
}, |
|
114
|
}) |