|
1
|
import { IRegionModuleService } from "@medusajs/framework/types" |
|
2
|
import { Module, Modules } from "@medusajs/framework/utils" |
|
3
|
import { moduleIntegrationTestRunner } from "@medusajs/test-utils" |
|
4
|
import { RegionModuleService } from "@services" |
|
5
|
|
|
6
|
jest.setTimeout(30000) |
|
7
|
|
|
8
|
moduleIntegrationTestRunner<IRegionModuleService>({ |
|
9
|
moduleName: Modules.REGION, |
|
10
|
testSuite: ({ service }) => { |
|
11
|
describe("Region Module Service", () => { |
|
12
|
it(`should export the appropriate linkable configuration`, () => { |
|
13
|
const linkable = Module(Modules.REGION, { |
|
14
|
service: RegionModuleService, |
|
15
|
}).linkable |
|
16
|
|
|
17
|
expect(Object.keys(linkable)).toEqual(["region", "country"]) |
|
18
|
|
|
19
|
Object.keys(linkable).forEach((key) => { |
|
20
|
delete linkable[key].toJSON |
|
21
|
}) |
|
22
|
|
|
23
|
expect(linkable).toEqual({ |
|
24
|
region: { |
|
25
|
id: { |
|
26
|
linkable: "region_id", |
|
27
|
entity: "Region", |
|
28
|
primaryKey: "id", |
|
29
|
serviceName: "region", |
|
30
|
field: "region", |
|
31
|
}, |
|
32
|
}, |
|
33
|
country: { |
|
34
|
iso_2: { |
|
35
|
linkable: "country_iso_2", |
|
36
|
entity: "Country", |
|
37
|
primaryKey: "iso_2", |
|
38
|
serviceName: "region", |
|
39
|
field: "country", |
|
40
|
}, |
|
41
|
}, |
|
42
|
}) |
|
43
|
}) |
|
44
|
|
|
45
|
it("should create countries on application start", async () => { |
|
46
|
const countries = await service.listCountries({}, {}) |
|
47
|
expect(countries.length).toEqual(250) |
|
48
|
}) |
|
49
|
|
|
50
|
it("should create and list a region", async () => { |
|
51
|
const createdRegion = await service.createRegions({ |
|
52
|
name: "Europe", |
|
53
|
currency_code: "EUR", |
|
54
|
automatic_taxes: false, |
|
55
|
}) |
|
56
|
|
|
57
|
expect(createdRegion).toEqual( |
|
58
|
expect.objectContaining({ |
|
59
|
id: createdRegion.id, |
|
60
|
name: "Europe", |
|
61
|
currency_code: "eur", |
|
62
|
countries: [], |
|
63
|
automatic_taxes: false, |
|
64
|
}) |
|
65
|
) |
|
66
|
|
|
67
|
const region = await service.retrieveRegion(createdRegion.id, { |
|
68
|
relations: ["countries"], |
|
69
|
}) |
|
70
|
|
|
71
|
expect(region).toEqual( |
|
72
|
expect.objectContaining({ |
|
73
|
id: region.id, |
|
74
|
name: "Europe", |
|
75
|
currency_code: "eur", |
|
76
|
countries: [], |
|
77
|
}) |
|
78
|
) |
|
79
|
}) |
|
80
|
|
|
81
|
it("should create a region with countries", async () => { |
|
82
|
const createdRegion = await service.createRegions({ |
|
83
|
name: "North America", |
|
84
|
currency_code: "USD", |
|
85
|
countries: ["us", "ca"], |
|
86
|
}) |
|
87
|
|
|
88
|
const region = await service.retrieveRegion(createdRegion.id, { |
|
89
|
relations: ["countries"], |
|
90
|
}) |
|
91
|
|
|
92
|
expect(region).toEqual( |
|
93
|
expect.objectContaining({ |
|
94
|
id: region.id, |
|
95
|
name: "North America", |
|
96
|
currency_code: "usd", |
|
97
|
automatic_taxes: true, |
|
98
|
countries: [ |
|
99
|
expect.objectContaining({ |
|
100
|
display_name: "Canada", |
|
101
|
iso_2: "ca", |
|
102
|
}), |
|
103
|
expect.objectContaining({ |
|
104
|
display_name: "United States", |
|
105
|
iso_2: "us", |
|
106
|
}), |
|
107
|
], |
|
108
|
}) |
|
109
|
) |
|
110
|
}) |
|
111
|
|
|
112
|
it("should throw when country doesn't exist", async () => { |
|
113
|
await expect( |
|
114
|
service.createRegions({ |
|
115
|
name: "North America", |
|
116
|
currency_code: "USD", |
|
117
|
countries: ["neverland"], |
|
118
|
}) |
|
119
|
).rejects.toThrowError('Countries with codes: "neverland" do not exist') |
|
120
|
}) |
|
121
|
|
|
122
|
it("should throw when country is already assigned to a region", async () => { |
|
123
|
await service.createRegions({ |
|
124
|
name: "North America", |
|
125
|
currency_code: "USD", |
|
126
|
countries: ["us"], |
|
127
|
}) |
|
128
|
|
|
129
|
await expect( |
|
130
|
service.createRegions({ |
|
131
|
name: "United States", |
|
132
|
currency_code: "USD", |
|
133
|
countries: ["us"], |
|
134
|
}) |
|
135
|
).rejects.toThrowError( |
|
136
|
'Countries with codes: "us" are already assigned to a region' |
|
137
|
) |
|
138
|
}) |
|
139
|
|
|
140
|
it("should throw when country is being assigned to multiple regions", async () => { |
|
141
|
await expect( |
|
142
|
service.createRegions([ |
|
143
|
{ |
|
144
|
name: "United States", |
|
145
|
currency_code: "USD", |
|
146
|
countries: ["us"], |
|
147
|
}, |
|
148
|
{ |
|
149
|
name: "North America", |
|
150
|
currency_code: "USD", |
|
151
|
countries: ["us"], |
|
152
|
}, |
|
153
|
]) |
|
154
|
).rejects.toThrowError( |
|
155
|
'Countries with codes: "us" are already assigned to a region' |
|
156
|
) |
|
157
|
}) |
|
158
|
|
|
159
|
it("should upsert the region successfully", async () => { |
|
160
|
const createdRegion = await service.upsertRegions({ |
|
161
|
name: "North America", |
|
162
|
currency_code: "USD", |
|
163
|
countries: ["us", "ca"], |
|
164
|
}) |
|
165
|
|
|
166
|
await service.upsertRegions({ |
|
167
|
id: createdRegion.id, |
|
168
|
name: "Americas", |
|
169
|
currency_code: "MXN", |
|
170
|
countries: ["us", "mx"], |
|
171
|
automatic_taxes: false, |
|
172
|
}) |
|
173
|
|
|
174
|
const latestRegion = await service.retrieveRegion(createdRegion.id, { |
|
175
|
relations: ["countries"], |
|
176
|
}) |
|
177
|
|
|
178
|
expect(latestRegion).toMatchObject({ |
|
179
|
id: createdRegion.id, |
|
180
|
name: "Americas", |
|
181
|
currency_code: "mxn", |
|
182
|
automatic_taxes: false, |
|
183
|
}) |
|
184
|
expect(latestRegion.countries.map((c) => c.iso_2)).toEqual(["mx", "us"]) |
|
185
|
}) |
|
186
|
|
|
187
|
it("should allow mixing create and update operations in upsert", async () => { |
|
188
|
const createdRegion = await service.upsertRegions({ |
|
189
|
name: "North America", |
|
190
|
currency_code: "USD", |
|
191
|
countries: ["us", "ca"], |
|
192
|
}) |
|
193
|
|
|
194
|
const upserted = await service.upsertRegions([ |
|
195
|
{ |
|
196
|
id: createdRegion.id, |
|
197
|
name: "Americas", |
|
198
|
currency_code: "USD", |
|
199
|
countries: ["us", "ca"], |
|
200
|
}, |
|
201
|
{ |
|
202
|
name: "Central America", |
|
203
|
currency_code: "MXN", |
|
204
|
countries: ["mx"], |
|
205
|
}, |
|
206
|
]) |
|
207
|
|
|
208
|
expect(upserted).toEqual( |
|
209
|
expect.arrayContaining([ |
|
210
|
expect.objectContaining({ |
|
211
|
id: createdRegion.id, |
|
212
|
name: "Americas", |
|
213
|
currency_code: "usd", |
|
214
|
}), |
|
215
|
expect.objectContaining({ |
|
216
|
name: "Central America", |
|
217
|
currency_code: "mxn", |
|
218
|
}), |
|
219
|
]) |
|
220
|
) |
|
221
|
}) |
|
222
|
|
|
223
|
it("should update the region successfully", async () => { |
|
224
|
const createdRegion = await service.createRegions({ |
|
225
|
name: "North America", |
|
226
|
currency_code: "USD", |
|
227
|
countries: ["us", "ca"], |
|
228
|
}) |
|
229
|
|
|
230
|
const shouldNotBeChangedRegion = await service.createRegions({ |
|
231
|
name: "Europe", |
|
232
|
currency_code: "EUR", |
|
233
|
countries: ["hr"], |
|
234
|
}) |
|
235
|
|
|
236
|
await service.updateRegions(createdRegion.id, { |
|
237
|
name: "Americas", |
|
238
|
currency_code: "MXN", |
|
239
|
countries: ["us", "mx"], |
|
240
|
}) |
|
241
|
|
|
242
|
const latestRegion = await service.retrieveRegion(createdRegion.id, { |
|
243
|
relations: ["countries"], |
|
244
|
}) |
|
245
|
|
|
246
|
const shouldNotBeChangedRegionAfter = await service.retrieveRegion( |
|
247
|
shouldNotBeChangedRegion.id, |
|
248
|
{ |
|
249
|
relations: ["countries"], |
|
250
|
} |
|
251
|
) |
|
252
|
|
|
253
|
expect(latestRegion).toMatchObject({ |
|
254
|
id: createdRegion.id, |
|
255
|
name: "Americas", |
|
256
|
currency_code: "mxn", |
|
257
|
}) |
|
258
|
|
|
259
|
expect( |
|
260
|
shouldNotBeChangedRegionAfter.countries.map((c) => c.iso_2) |
|
261
|
).toEqual(["hr"]) |
|
262
|
|
|
263
|
expect(latestRegion.countries.map((c) => c.iso_2)).toEqual( |
|
264
|
expect.arrayContaining(["mx", "us"]) |
|
265
|
) |
|
266
|
}) |
|
267
|
|
|
268
|
it("should update the region without affecting countries if countries are undefined", async () => { |
|
269
|
const createdRegion = await service.createRegions({ |
|
270
|
name: "North America", |
|
271
|
currency_code: "USD", |
|
272
|
countries: ["us", "ca"], |
|
273
|
}) |
|
274
|
|
|
275
|
await service.updateRegions(createdRegion.id, { |
|
276
|
name: "Americas", |
|
277
|
currency_code: "MXN", |
|
278
|
}) |
|
279
|
|
|
280
|
const updatedRegion = await service.retrieveRegion(createdRegion.id, { |
|
281
|
relations: ["countries"], |
|
282
|
}) |
|
283
|
|
|
284
|
expect(updatedRegion).toMatchObject({ |
|
285
|
id: createdRegion.id, |
|
286
|
name: "Americas", |
|
287
|
currency_code: "mxn", |
|
288
|
}) |
|
289
|
|
|
290
|
expect(updatedRegion.countries.map((c) => c.iso_2)).toEqual([ |
|
291
|
"ca", |
|
292
|
"us", |
|
293
|
]) |
|
294
|
}) |
|
295
|
|
|
296
|
it("should remove the countries in a region successfully", async () => { |
|
297
|
const createdRegion = await service.createRegions({ |
|
298
|
name: "North America", |
|
299
|
currency_code: "USD", |
|
300
|
countries: ["us", "ca"], |
|
301
|
}) |
|
302
|
|
|
303
|
await service.updateRegions(createdRegion.id, { |
|
304
|
name: "Americas", |
|
305
|
currency_code: "MXN", |
|
306
|
countries: [], |
|
307
|
}) |
|
308
|
|
|
309
|
const updatedRegion = await service.retrieveRegion(createdRegion.id, { |
|
310
|
relations: ["countries"], |
|
311
|
}) |
|
312
|
|
|
313
|
expect(updatedRegion).toMatchObject({ |
|
314
|
id: createdRegion.id, |
|
315
|
name: "Americas", |
|
316
|
currency_code: "mxn", |
|
317
|
}) |
|
318
|
|
|
319
|
expect(updatedRegion.countries).toHaveLength(0) |
|
320
|
}) |
|
321
|
|
|
322
|
it("should fail updating the region countries to non-existent ones", async () => { |
|
323
|
const createdRegion = await service.createRegions({ |
|
324
|
name: "North America", |
|
325
|
currency_code: "USD", |
|
326
|
countries: ["us", "ca"], |
|
327
|
}) |
|
328
|
|
|
329
|
await expect( |
|
330
|
service.updateRegions( |
|
331
|
{ id: createdRegion.id }, |
|
332
|
{ |
|
333
|
countries: ["us", "neverland"], |
|
334
|
} |
|
335
|
) |
|
336
|
).rejects.toThrowError('Countries with codes: "neverland" do not exist') |
|
337
|
}) |
|
338
|
|
|
339
|
it("should fail updating the region if there are duplicate countries", async () => { |
|
340
|
const createdRegion = await service.createRegions({ |
|
341
|
name: "North America", |
|
342
|
currency_code: "USD", |
|
343
|
countries: ["us", "ca"], |
|
344
|
}) |
|
345
|
|
|
346
|
await expect( |
|
347
|
service.updateRegions( |
|
348
|
{ id: createdRegion.id }, |
|
349
|
{ |
|
350
|
countries: ["us", "us"], |
|
351
|
} |
|
352
|
) |
|
353
|
).rejects.toThrowError( |
|
354
|
'Countries with codes: "us" are already assigned to a region' |
|
355
|
) |
|
356
|
}) |
|
357
|
|
|
358
|
it("should fail updating the region if country is already used", async () => { |
|
359
|
const [createdRegion] = await service.createRegions([ |
|
360
|
{ |
|
361
|
name: "North America", |
|
362
|
currency_code: "USD", |
|
363
|
countries: ["us", "ca"], |
|
364
|
}, |
|
365
|
{ |
|
366
|
name: "Americas", |
|
367
|
currency_code: "USD", |
|
368
|
countries: ["mx"], |
|
369
|
}, |
|
370
|
]) |
|
371
|
|
|
372
|
await expect( |
|
373
|
service.updateRegions( |
|
374
|
{ id: createdRegion.id }, |
|
375
|
{ |
|
376
|
countries: ["us", "mx"], |
|
377
|
} |
|
378
|
) |
|
379
|
).rejects.toThrowError( |
|
380
|
'Countries with codes: "mx" are already assigned to a region' |
|
381
|
) |
|
382
|
}) |
|
383
|
|
|
384
|
it("should unset the region ID on the country when deleting a region", async () => { |
|
385
|
const createdRegion = await service.createRegions({ |
|
386
|
name: "North America", |
|
387
|
currency_code: "USD", |
|
388
|
countries: ["us", "ca"], |
|
389
|
}) |
|
390
|
|
|
391
|
await service.deleteRegions(createdRegion.id) |
|
392
|
|
|
393
|
const newRegion = await service.createRegions({ |
|
394
|
name: "North America", |
|
395
|
currency_code: "USD", |
|
396
|
countries: ["us", "ca"], |
|
397
|
}) |
|
398
|
|
|
399
|
const resp = await service.retrieveRegion(newRegion.id, { |
|
400
|
relations: ["countries"], |
|
401
|
}) |
|
402
|
|
|
403
|
expect(resp.countries).toHaveLength(2) |
|
404
|
}) |
|
405
|
|
|
406
|
it("should delete a region without countries", async () => { |
|
407
|
const createdRegion = await service.createRegions({ |
|
408
|
name: "North America", |
|
409
|
currency_code: "USD", |
|
410
|
countries: [], |
|
411
|
}) |
|
412
|
|
|
413
|
await service.softDeleteRegions([createdRegion.id]) |
|
414
|
|
|
415
|
const [deletedRegion] = await service.listRegions( |
|
416
|
{ id: createdRegion.id }, |
|
417
|
{ withDeleted: true } |
|
418
|
) |
|
419
|
|
|
420
|
expect(deletedRegion).toEqual({ |
|
421
|
id: createdRegion.id, |
|
422
|
name: createdRegion.name, |
|
423
|
currency_code: createdRegion.currency_code, |
|
424
|
metadata: null, |
|
425
|
automatic_taxes: createdRegion.automatic_taxes, |
|
426
|
created_at: expect.any(Date), |
|
427
|
updated_at: expect.any(Date), |
|
428
|
deleted_at: expect.any(Date), |
|
429
|
}) |
|
430
|
}) |
|
431
|
|
|
432
|
it("should unset the region ID on the country when soft deleting a region", async () => { |
|
433
|
const createdRegion = await service.createRegions({ |
|
434
|
name: "North America", |
|
435
|
currency_code: "USD", |
|
436
|
countries: ["us", "ca"], |
|
437
|
}) |
|
438
|
|
|
439
|
await service.softDeleteRegions([createdRegion.id]) |
|
440
|
|
|
441
|
const newRegion = await service.createRegions({ |
|
442
|
name: "North America", |
|
443
|
currency_code: "USD", |
|
444
|
countries: ["us", "ca"], |
|
445
|
}) |
|
446
|
|
|
447
|
const resp = await service.retrieveRegion(newRegion.id, { |
|
448
|
relations: ["countries"], |
|
449
|
}) |
|
450
|
|
|
451
|
expect(resp.countries).toHaveLength(2) |
|
452
|
}) |
|
453
|
}) |
|
454
|
}, |
|
455
|
}) |