1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import {
3 adminHeaders,
4 createAdminUser,
5 } from "../../../../helpers/create-admin-user"
6
7 jest.setTimeout(30000)
8
9 medusaIntegrationTestRunner({
10 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
11 let region1
12 let region2
13 let container
14
15 beforeAll(async () => {
16 container = getContainer()
17 await createAdminUser(dbConnection, adminHeaders, container)
18
19 region1 = (
20 await api.post(
21 "/admin/regions",
22 {
23 name: "United Kingdom",
24 currency_code: "gbp",
25 },
26 adminHeaders
27 )
28 ).data.region
29
30 region2 = (
31 await api.post(
32 "/admin/regions",
33 {
34 name: "United States",
35 currency_code: "usd",
36 },
37 adminHeaders
38 )
39 ).data.region
40
41 await dbUtils.snapshot()
42 })
43
44 // BREAKING: There is no more `tax_rate` field on the region.
45 // BREAKING: There are no more fulfillment providers list on a region.
46 describe("GET /admin/regions", () => {
47 it("should list regions", async () => {
48 const response = await api.get("/admin/regions", adminHeaders)
49
50 expect(response.status).toEqual(200)
51 expect(response.data.regions).toEqual([
52 expect.objectContaining({
53 name: "United Kingdom",
54 id: region1.id,
55 currency_code: "gbp",
56 automatic_taxes: region1.automatic_taxes,
57 countries: [],
58 }),
59 expect.objectContaining({
60 name: "United States",
61 id: region2.id,
62 currency_code: "usd",
63 automatic_taxes: region2.automatic_taxes,
64 countries: [],
65 }),
66 ])
67 })
68
69 it("should list the regions with q and order params", async () => {
70 const response = await api.get(
71 "/admin/regions?q=united&order=-currency_code",
72 adminHeaders
73 )
74
75 expect(response.status).toEqual(200)
76
77 expect(response.data.regions).toEqual([
78 expect.objectContaining({
79 name: "United States",
80 currency_code: "usd",
81 }),
82 expect.objectContaining({
83 name: "United Kingdom",
84 currency_code: "gbp",
85 }),
86 ])
87 })
88 })
89
90 describe("GET /admin/regions/:id", () => {
91 it("should retrieve the region from ID", async () => {
92 const response = await api.get(
93 `/admin/regions/${region1.id}`,
94 adminHeaders
95 )
96
97 expect(response.status).toEqual(200)
98 expect(response.data.region).toEqual(
99 expect.objectContaining({
100 id: region1.id,
101 })
102 )
103 })
104
105 it("should throw an error when region ID is invalid", async () => {
106 await api
107 .get(`/admin/regions/invalid-region-id`, adminHeaders)
108 .catch((e) => {
109 expect(e.response.status).toEqual(404)
110 expect(e.response.data.type).toEqual("not_found")
111 expect(e.response.data.message).toEqual(
112 `Region with id: invalid-region-id not found`
113 )
114 })
115 })
116 })
117
118 describe("POST /admin/regions", () => {
119 it("should create a region", async () => {
120 const region = (
121 await api.post(
122 "/admin/regions",
123 {
124 name: "Test",
125 currency_code: "usd",
126 },
127 adminHeaders
128 )
129 ).data.region
130
131 expect(region).toEqual(
132 expect.objectContaining({
133 name: "Test",
134 currency_code: "usd",
135 })
136 )
137 })
138
139 it("should create a region with tax inclusivity setting", async () => {
140 const region = (
141 await api.post(
142 "/admin/regions",
143 {
144 name: "Test",
145 currency_code: "usd",
146 // BREAKING: The property used to be called `includes_tax`
147 is_tax_inclusive: true,
148 },
149 adminHeaders
150 )
151 ).data.region
152
153 const response = await api.get(`/admin/price-preferences`, adminHeaders)
154
155 expect(response.data.price_preferences).toEqual(
156 expect.arrayContaining([
157 expect.objectContaining({
158 attribute: "region_id",
159 value: region.id,
160 is_tax_inclusive: true,
161 }),
162 ])
163 )
164 })
165
166 it("should fails to create when countries exists in different region", async () => {
167 try {
168 await api.post(
169 `/admin/regions`,
170 {
171 name: "World",
172 currency_code: "usd",
173 countries: ["us"],
174 },
175 adminHeaders
176 )
177 } catch (error) {
178 expect(error.response.status).toEqual(400)
179 expect(error.response.data.message).toEqual(
180 `Countries with codes: \"us\" are already assigned to a region`
181 )
182 }
183 })
184 })
185
186 describe("POST /admin/regions/:id", () => {
187 it("should update a region", async () => {
188 const region = (
189 await api.post(
190 `/admin/regions/${region1.id}`,
191 {
192 name: "New test",
193 currency_code: "eur",
194 },
195 adminHeaders
196 )
197 ).data.region
198
199 expect(region).toEqual(
200 expect.objectContaining({
201 name: "New test",
202 currency_code: "eur",
203 })
204 )
205 })
206
207 it("should update a region with tax inclusivity setting", async () => {
208 const beforeResponse = await api.get(
209 `/admin/price-preferences`,
210 adminHeaders
211 )
212 expect(beforeResponse.data.price_preferences).toEqual(
213 expect.arrayContaining([
214 expect.objectContaining({
215 attribute: "region_id",
216 value: region1.id,
217 is_tax_inclusive: false,
218 }),
219 ])
220 )
221
222 await api.post(
223 `/admin/regions/${region1.id}`,
224 {
225 is_tax_inclusive: true,
226 },
227 adminHeaders
228 )
229
230 const afterResponse = await api.get(
231 `/admin/price-preferences`,
232 adminHeaders
233 )
234 expect(afterResponse.data.price_preferences).toEqual(
235 expect.arrayContaining([
236 expect.objectContaining({
237 attribute: "region_id",
238 value: region1.id,
239 is_tax_inclusive: true,
240 }),
241 ])
242 )
243 })
244 })
245 },
246 })