1 import {
2 CreateGeoZoneDTO,
3 IFulfillmentModuleService,
4 UpdateGeoZoneDTO,
5 } from "@medusajs/framework/types"
6 import {
7 FulfillmentEvents,
8 GeoZoneType,
9 Modules,
10 } from "@medusajs/framework/utils"
11 import {
12 MockEventBusService,
13 moduleIntegrationTestRunner,
14 } from "@medusajs/test-utils"
15 import { buildExpectedEventMessageShape } from "../../__fixtures__"
16
17 jest.setTimeout(100000)
18
19 moduleIntegrationTestRunner<IFulfillmentModuleService>({
20 moduleName: Modules.FULFILLMENT,
21 testSuite: ({ service }) => {
22 let eventBusEmitSpy
23
24 beforeEach(() => {
25 eventBusEmitSpy = jest.spyOn(MockEventBusService.prototype, "emit")
26 })
27
28 afterEach(() => {
29 jest.clearAllMocks()
30 })
31
32 describe("Fulfillment Module Service", () => {
33 describe("read", () => {
34 it("should list geo zones with a filter", async function () {
35 const fulfillmentSet = await service.createFulfillmentSets({
36 name: "test",
37 type: "test-type",
38 })
39 const serviceZone = await service.createServiceZones({
40 name: "test",
41 fulfillment_set_id: fulfillmentSet.id,
42 })
43
44 const createdZone1 = await service.createGeoZones({
45 service_zone_id: serviceZone.id,
46 type: GeoZoneType.COUNTRY,
47 country_code: "fr",
48 })
49 const createdZone2 = await service.createGeoZones({
50 service_zone_id: serviceZone.id,
51 type: GeoZoneType.COUNTRY,
52 country_code: "us",
53 })
54
55 let listedZones = await service.listGeoZones({
56 type: createdZone1.type,
57 })
58
59 expect(listedZones).toEqual(
60 expect.arrayContaining([
61 expect.objectContaining({ id: createdZone1.id }),
62 expect.objectContaining({ id: createdZone2.id }),
63 ])
64 )
65
66 listedZones = await service.listGeoZones({
67 country_code: createdZone2.country_code,
68 })
69
70 expect(listedZones).toEqual(
71 expect.arrayContaining([
72 expect.objectContaining({ id: createdZone2.id }),
73 ])
74 )
75 expect(listedZones).not.toEqual(
76 expect.arrayContaining([
77 expect.objectContaining({ id: createdZone1.id }),
78 ])
79 )
80 })
81 })
82
83 describe("mutations", () => {
84 describe("on create", () => {
85 it("should create a new geo zone", async function () {
86 const fulfillmentSet = await service.createFulfillmentSets({
87 name: "test",
88 type: "test-type",
89 })
90 const serviceZone = await service.createServiceZones({
91 name: "test",
92 fulfillment_set_id: fulfillmentSet.id,
93 })
94
95 const data: CreateGeoZoneDTO = {
96 service_zone_id: serviceZone.id,
97 type: GeoZoneType.COUNTRY,
98 country_code: "fr",
99 }
100
101 jest.clearAllMocks()
102
103 const geoZone = await service.createGeoZones(data)
104
105 expect(geoZone).toEqual(
106 expect.objectContaining({
107 id: expect.any(String),
108 type: data.type,
109 country_code: data.country_code,
110 })
111 )
112
113 expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(1)
114 expect(eventBusEmitSpy).toHaveBeenCalledWith(
115 [
116 buildExpectedEventMessageShape({
117 eventName: FulfillmentEvents.GEO_ZONE_CREATED,
118 action: "created",
119 object: "geo_zone",
120 data: { id: geoZone.id },
121 }),
122 ],
123 {
124 internal: true,
125 }
126 )
127 })
128
129 it("should create a collection of geo zones", async function () {
130 const fulfillmentSet = await service.createFulfillmentSets({
131 name: "test",
132 type: "test-type",
133 })
134 const serviceZone = await service.createServiceZones({
135 name: "test",
136 fulfillment_set_id: fulfillmentSet.id,
137 })
138
139 const data: CreateGeoZoneDTO[] = [
140 {
141 service_zone_id: serviceZone.id,
142 type: GeoZoneType.COUNTRY,
143 country_code: "fr",
144 },
145 {
146 service_zone_id: serviceZone.id,
147 type: GeoZoneType.COUNTRY,
148 country_code: "us",
149 },
150 ]
151
152 jest.clearAllMocks()
153
154 const geoZones = await service.createGeoZones(data)
155
156 expect(geoZones).toHaveLength(2)
157 expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(2)
158
159 let i = 0
160 for (const data_ of data) {
161 expect(geoZones[i]).toEqual(
162 expect.objectContaining({
163 id: expect.any(String),
164 type: data_.type,
165 country_code: data_.country_code,
166 })
167 )
168
169 expect(eventBusEmitSpy).toHaveBeenCalledWith(
170 expect.arrayContaining([
171 buildExpectedEventMessageShape({
172 eventName: FulfillmentEvents.GEO_ZONE_CREATED,
173 action: "created",
174 object: "geo_zone",
175 data: { id: geoZones[i].id },
176 }),
177 ]),
178 {
179 internal: true,
180 }
181 )
182
183 ++i
184 }
185 })
186
187 it("should fail to create new geo zones that are not valid", async function () {
188 const fulfillmentSet = await service.createFulfillmentSets({
189 name: "test",
190 type: "test-type",
191 })
192 const serviceZone = await service.createServiceZones({
193 name: "test",
194 fulfillment_set_id: fulfillmentSet.id,
195 })
196
197 let data: CreateGeoZoneDTO = {
198 service_zone_id: serviceZone.id,
199 type: GeoZoneType.PROVINCE,
200 country_code: "fr",
201 } as any
202
203 let err = await service.createGeoZones(data).catch((e) => e)
204 expect(err.message).toBe(
205 "Missing required property province_code for geo zone type province"
206 )
207
208 data = {
209 service_zone_id: serviceZone.id,
210 type: GeoZoneType.CITY,
211 country_code: "fr",
212 province_code: "test",
213 } as any
214
215 err = await service.createGeoZones(data).catch((e) => e)
216 expect(err.message).toBe(
217 "Missing required property city for geo zone type city"
218 )
219
220 data = {
221 service_zone_id: serviceZone.id,
222 type: GeoZoneType.ZIP,
223 postal_expression: "test",
224 } as any
225
226 err = await service.createGeoZones(data).catch((e) => e)
227 expect(err.message).toBe(
228 "Missing required property country_code for geo zone type zip"
229 )
230
231 data = {
232 service_zone_id: serviceZone.id,
233 type: "unknown",
234 postal_expression: "test",
235 } as any
236
237 err = await service.createGeoZones(data).catch((e) => e)
238 expect(err.message).toBe(`Invalid geo zone type: unknown`)
239 })
240 })
241
242 describe("on update", () => {
243 it("should update an existing geo zone", async function () {
244 const fulfillmentSet = await service.createFulfillmentSets({
245 name: "test",
246 type: "test-type",
247 })
248
249 const serviceZone = await service.createServiceZones({
250 name: "test",
251 fulfillment_set_id: fulfillmentSet.id,
252 })
253
254 const createData: CreateGeoZoneDTO = {
255 service_zone_id: serviceZone.id,
256 type: GeoZoneType.COUNTRY,
257 country_code: "fr",
258 }
259
260 const createdGeoZone = await service.createGeoZones(createData)
261
262 const updateData: UpdateGeoZoneDTO = {
263 id: createdGeoZone.id,
264 type: GeoZoneType.COUNTRY,
265 country_code: "us",
266 }
267
268 jest.clearAllMocks()
269
270 const updatedGeoZone = await service.updateGeoZones(updateData)
271
272 expect(updatedGeoZone).toEqual(
273 expect.objectContaining({
274 id: updateData.id,
275 type: updateData.type,
276 country_code: updateData.country_code,
277 })
278 )
279
280 expect(eventBusEmitSpy).toHaveBeenCalledWith(
281 [
282 buildExpectedEventMessageShape({
283 eventName: FulfillmentEvents.GEO_ZONE_UPDATED,
284 action: "updated",
285 object: "geo_zone",
286 data: { id: updatedGeoZone.id },
287 }),
288 ],
289 {
290 internal: true,
291 }
292 )
293 })
294
295 it("should update a collection of geo zones", async function () {
296 const fulfillmentSet = await service.createFulfillmentSets({
297 name: "test",
298 type: "test-type",
299 })
300
301 const serviceZone = await service.createServiceZones({
302 name: "test",
303 fulfillment_set_id: fulfillmentSet.id,
304 })
305
306 const createData: CreateGeoZoneDTO[] = [
307 {
308 service_zone_id: serviceZone.id,
309 type: GeoZoneType.COUNTRY,
310 country_code: "fr",
311 },
312 {
313 service_zone_id: serviceZone.id,
314 type: GeoZoneType.COUNTRY,
315 country_code: "us",
316 },
317 ]
318
319 const createdGeoZones = await service.createGeoZones(createData)
320
321 const updateData: UpdateGeoZoneDTO[] = createdGeoZones.map(
322 (geoZone, index) => ({
323 id: geoZone.id,
324 type: GeoZoneType.COUNTRY,
325 country_code: index % 2 === 0 ? "us" : "fr",
326 })
327 )
328
329 jest.clearAllMocks()
330
331 const updatedGeoZones = await service.updateGeoZones(updateData)
332
333 expect(updatedGeoZones).toHaveLength(2)
334 expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(2)
335
336 for (const data_ of updateData) {
337 const expectedGeoZone = updatedGeoZones.find(
338 (geoZone) => geoZone.id === data_.id
339 )!
340
341 expect(expectedGeoZone).toEqual(
342 expect.objectContaining({
343 id: data_.id,
344 type: data_.type,
345 country_code: data_.country_code,
346 })
347 )
348
349 expect(eventBusEmitSpy).toHaveBeenCalledWith(
350 expect.arrayContaining([
351 buildExpectedEventMessageShape({
352 eventName: FulfillmentEvents.GEO_ZONE_UPDATED,
353 action: "updated",
354 object: "geo_zone",
355 data: { id: expectedGeoZone.id },
356 }),
357 ]),
358 {
359 internal: true,
360 }
361 )
362 }
363 })
364 })
365 })
366 })
367 },
368 })