1 import {
2 CreateShippingProfileDTO,
3 IFulfillmentModuleService,
4 } from "@medusajs/framework/types"
5 import {
6 MockEventBusService,
7 moduleIntegrationTestRunner,
8 } from "@medusajs/test-utils"
9 import {
10 buildExpectedEventMessageShape,
11 generateCreateShippingOptionsData,
12 } from "../../__fixtures__"
13 import { FulfillmentEvents, Modules } from "@medusajs/framework/utils"
14 import { resolve } from "path"
15 import { FulfillmentProviderService } from "@services"
16 import { FulfillmentProviderServiceFixtures } from "../../__fixtures__/providers"
17
18 jest.setTimeout(100000)
19
20 const moduleOptions = {
21 providers: [
22 {
23 resolve: resolve(
24 process.cwd() +
25 "/integration-tests/__fixtures__/providers/default-provider"
26 ),
27 id: "test-provider",
28 },
29 ],
30 }
31
32 const providerId = FulfillmentProviderService.getRegistrationIdentifier(
33 FulfillmentProviderServiceFixtures,
34 "test-provider"
35 )
36
37 moduleIntegrationTestRunner<IFulfillmentModuleService>({
38 moduleName: Modules.FULFILLMENT,
39 moduleOptions,
40 testSuite: ({ service }) => {
41 let eventBusEmitSpy
42
43 beforeEach(() => {
44 eventBusEmitSpy = jest.spyOn(MockEventBusService.prototype, "emit")
45 })
46
47 afterEach(() => {
48 jest.clearAllMocks()
49 })
50
51 describe("Fulfillment Module Service", () => {
52 describe("mutations", () => {
53 describe("on create", () => {
54 it("should create a new shipping profile", async function () {
55 const createData: CreateShippingProfileDTO = {
56 name: "test-default-profile",
57 type: "default",
58 }
59
60 const createdShippingProfile = await service.createShippingProfiles(
61 createData
62 )
63
64 expect(createdShippingProfile).toEqual(
65 expect.objectContaining({
66 name: createData.name,
67 type: createData.type,
68 })
69 )
70
71 expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(1)
72 expect(eventBusEmitSpy).toHaveBeenCalledWith(
73 [
74 buildExpectedEventMessageShape({
75 eventName: FulfillmentEvents.SHIPPING_PROFILE_CREATED,
76 action: "created",
77 object: "shipping_profile",
78 data: { id: createdShippingProfile.id },
79 }),
80 ],
81 {
82 internal: true,
83 }
84 )
85 })
86
87 it("should create multiple new shipping profiles", async function () {
88 const createData: CreateShippingProfileDTO[] = [
89 {
90 name: "test-profile-1",
91 type: "default",
92 },
93 {
94 name: "test-profile-2",
95 type: "custom",
96 },
97 ]
98
99 const createdShippingProfiles =
100 await service.createShippingProfiles(createData)
101
102 expect(createdShippingProfiles).toHaveLength(2)
103 expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(2)
104
105 let i = 0
106 for (const data_ of createData) {
107 expect(createdShippingProfiles[i]).toEqual(
108 expect.objectContaining({
109 name: data_.name,
110 type: data_.type,
111 })
112 )
113
114 expect(eventBusEmitSpy).toHaveBeenCalledWith(
115 expect.arrayContaining([
116 buildExpectedEventMessageShape({
117 eventName: FulfillmentEvents.SHIPPING_PROFILE_CREATED,
118 action: "created",
119 object: "shipping_profile",
120 data: {
121 id: createdShippingProfiles[i].id,
122 },
123 }),
124 ]),
125 {
126 internal: true,
127 }
128 )
129
130 ++i
131 }
132 })
133
134 it("should fail on duplicated shipping profile name", async function () {
135 const createData: CreateShippingProfileDTO = {
136 name: "test-default-profile",
137 type: "default",
138 }
139
140 await service.createShippingProfiles(createData)
141
142 const err = await service
143 .createShippingProfiles(createData)
144 .catch((e) => e)
145
146 expect(err).toBeDefined()
147 expect(err.message).toContain("exists")
148 })
149 })
150
151 describe("on delete", () => {
152 it("should delete a shipping profile", async function () {
153 const createData: CreateShippingProfileDTO = {
154 name: "test-default-profile",
155 type: "default",
156 }
157
158 const createdShippingProfile = await service.createShippingProfiles(
159 createData
160 )
161
162 await service.deleteShippingProfiles(createdShippingProfile.id)
163
164 const [shippingProfile] = await service.listShippingProfiles({
165 id: createdShippingProfile.id,
166 })
167
168 expect(shippingProfile).toBeUndefined()
169 })
170
171 it("should not allow to delete a shipping profile that is associated to any shipping options", async function () {
172 const createData: CreateShippingProfileDTO = {
173 name: "test-default-profile",
174 type: "default",
175 }
176
177 const createdShippingProfile = await service.createShippingProfiles(
178 createData
179 )
180
181 const fulfillmentSet = await service.createFulfillmentSets({
182 name: "test",
183 type: "test-type",
184 service_zones: [
185 {
186 name: "test",
187 },
188 ],
189 })
190
191 await service.createShippingOptions([
192 generateCreateShippingOptionsData({
193 service_zone_id: fulfillmentSet.service_zones[0].id,
194 shipping_profile_id: createdShippingProfile.id,
195 provider_id: providerId,
196 rules: [
197 {
198 attribute: "test-attribute",
199 operator: "in",
200 value: ["test"],
201 },
202 ],
203 }),
204 ])
205
206 const err = await service
207 .deleteShippingProfiles(createdShippingProfile.id)
208 .catch((e) => e)
209
210 expect(err).toBeTruthy()
211 expect(err.message).toContain(
212 `Cannot delete Shipping Profiles ${createdShippingProfile.id} with associated Shipping Options. Delete Shipping Options first and try again.`
213 )
214 })
215 })
216 })
217 })
218 },
219 })