1 import { createRegionsWorkflow } from "@medusajs/core-flows"
2 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
3 import { MedusaContainer, RegionDTO } from "@medusajs/types"
4 import {
5 ContainerRegistrationKeys,
6 remoteQueryObjectFromString,
7 } from "@medusajs/utils"
8 import { createAdminUser } from "../../../../helpers/create-admin-user"
9
10 jest.setTimeout(200000)
11
12 const env = {}
13 const adminHeaders = {
14 headers: { "x-medusa-access-token": "test_token" },
15 }
16
17 medusaIntegrationTestRunner({
18 env,
19 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
20 let container: MedusaContainer
21 let region: RegionDTO
22
23 beforeAll(() => {
24 container = getContainer()
25 })
26
27 beforeAll(async () => {
28 await createAdminUser(dbConnection, adminHeaders, container)
29
30 await dbUtils.snapshot()
31 })
32
33 describe("create region workflow", () => {
34 it("should create a region", async () => {
35 const paymentProviderId = "pp_system_default"
36
37 const data = {
38 name: "Test Region",
39 currency_code: "usd",
40 countries: ["us", "ca"],
41 metadata: { foo: "bar" },
42 payment_providers: [paymentProviderId],
43 }
44
45 let {
46 result: [createRegion],
47 } = await createRegionsWorkflow(container).run({
48 input: {
49 regions: [data],
50 },
51 })
52
53 const remoteQuery = container.resolve(
54 ContainerRegistrationKeys.REMOTE_QUERY
55 )
56
57 ;[createRegion] = await remoteQuery(
58 remoteQueryObjectFromString({
59 entryPoint: "region",
60 variables: {
61 filters: {
62 id: createRegion.id,
63 },
64 },
65 fields: [
66 "id",
67 "name",
68 "currency_code",
69 "metadata",
70 "countries.*",
71 "payment_providers.*",
72 ],
73 })
74 )
75
76 expect(createRegion).toEqual(
77 expect.objectContaining({
78 id: expect.any(String),
79 name: data.name,
80 currency_code: data.currency_code,
81 countries: expect.arrayContaining(
82 data.countries.map((iso_2) => expect.objectContaining({ iso_2 }))
83 ),
84 metadata: data.metadata,
85 payment_providers: [
86 expect.objectContaining({
87 id: paymentProviderId,
88 }),
89 ],
90 })
91 )
92 })
93
94 it("should revert the created region and payment providers", async () => {
95 const paymentProviderId = "pp_system_default"
96
97 const data = {
98 name: "Test Region",
99 currency_code: "usd",
100 countries: ["us", "ca"],
101 metadata: { foo: "bar" },
102 payment_providers: [paymentProviderId],
103 }
104
105 const workflow = createRegionsWorkflow(container)
106 workflow.addAction(
107 "throw",
108 {
109 invoke: async function failStep() {
110 throw new Error(`Failed to create region`)
111 },
112 },
113 {
114 noCompensation: true,
115 }
116 )
117
118 const { errors } = await workflow.run({
119 input: {
120 regions: [data],
121 },
122 throwOnError: false,
123 })
124
125 expect(errors).toHaveLength(1)
126 expect(errors[0].error.message).toEqual(`Failed to create region`)
127
128 const remoteQuery = container.resolve(
129 ContainerRegistrationKeys.REMOTE_QUERY
130 )
131
132 const createRegions = await remoteQuery(
133 remoteQueryObjectFromString({
134 entryPoint: "region",
135 fields: [
136 "id",
137 "name",
138 "currency_code",
139 "metadata",
140 "countries.*",
141 "payment_providers.*",
142 ],
143 })
144 )
145
146 expect(createRegions).toHaveLength(0)
147 })
148 })
149 },
150 })