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 env: {},
11 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
12 let baseCollection
13 let baseCollection1
14 let baseCollection2
15
16 let baseProduct
17 let baseProduct1
18
19 let shippingProfile
20
21 beforeAll(async () => {
22 const container = getContainer()
23 await createAdminUser(dbConnection, adminHeaders, container)
24
25 baseCollection = (
26 await api.post(
27 "/admin/collections",
28 { title: "test-collection", external_id: "ext-collection-01" },
29 adminHeaders
30 )
31 ).data.collection
32
33 baseCollection1 = (
34 await api.post(
35 "/admin/collections",
36 { title: "test-collection1" },
37 adminHeaders
38 )
39 ).data.collection
40
41 baseCollection2 = (
42 await api.post(
43 "/admin/collections",
44 { title: "test-collection2" },
45 adminHeaders
46 )
47 ).data.collection
48
49 shippingProfile = (
50 await api.post(
51 `/admin/shipping-profiles`,
52 { name: "Test", type: "default" },
53 adminHeaders
54 )
55 ).data.shipping_profile
56
57 baseProduct = (
58 await api.post(
59 "/admin/products",
60 {
61 title: "test-product",
62 options: [{ title: "size", values: ["x", "l"] }],
63 shipping_profile_id: shippingProfile.id,
64 },
65 adminHeaders
66 )
67 ).data.product
68
69 baseProduct1 = (
70 await api.post(
71 "/admin/products",
72 {
73 title: "test-product1",
74 options: [{ title: "size", values: ["x", "l"] }],
75 shipping_profile_id: shippingProfile.id,
76 },
77 adminHeaders
78 )
79 ).data.product
80
81 await dbUtils.snapshot()
82 })
83
84 describe("/admin/collections", () => {
85 it("creates a collection", async () => {
86 const response = await api.post(
87 "/admin/collections",
88 {
89 title: "New collection",
90 handle: "test-new-collection",
91 external_id: "ext-collection-new",
92 },
93 adminHeaders
94 )
95
96 expect(response.status).toEqual(200)
97 expect(response.data).toEqual({
98 collection: {
99 id: expect.stringMatching(/^pcol_*/),
100 title: "New collection",
101 handle: "test-new-collection",
102 external_id: "ext-collection-new",
103 created_at: expect.any(String),
104 updated_at: expect.any(String),
105 metadata: null,
106 },
107 })
108 })
109
110 it("lists collections", async () => {
111 const response = await api.get("/admin/collections", adminHeaders)
112
113 expect(response.data).toEqual(
114 expect.objectContaining({
115 count: 3,
116 limit: 10,
117 offset: 0,
118 collections: expect.arrayContaining([
119 {
120 id: baseCollection.id,
121 created_at: expect.any(String),
122 external_id: "ext-collection-01",
123 updated_at: expect.any(String),
124 handle: "test-collection",
125 metadata: null,
126 title: "test-collection",
127 },
128 {
129 id: baseCollection1.id,
130 created_at: expect.any(String),
131 external_id: null,
132 updated_at: expect.any(String),
133 handle: "test-collection1",
134 metadata: null,
135 title: "test-collection1",
136 },
137 {
138 id: baseCollection2.id,
139 created_at: expect.any(String),
140 external_id: null,
141 updated_at: expect.any(String),
142 handle: "test-collection2",
143 metadata: null,
144 title: "test-collection2",
145 },
146 ]),
147 })
148 )
149 })
150
151 it("filters collections by title", async () => {
152 const response = await api.get(
153 "/admin/collections?title=test-collection",
154 adminHeaders
155 )
156
157 expect(response.data).toEqual(
158 expect.objectContaining({
159 count: 1,
160 collections: expect.arrayContaining([
161 {
162 id: baseCollection.id,
163 created_at: expect.any(String),
164 external_id: "ext-collection-01",
165 updated_at: expect.any(String),
166 handle: "test-collection",
167 metadata: null,
168 title: "test-collection",
169 },
170 ]),
171 })
172 )
173 })
174
175 it("filters collection by external_id", async () => {
176 const response = await api.get(
177 "/admin/collections?external_id=ext-collection-01",
178 adminHeaders
179 )
180
181 expect(response.data).toEqual(
182 expect.objectContaining({
183 count: 1,
184 collections: expect.arrayContaining([
185 {
186 id: baseCollection.id,
187 created_at: expect.any(String),
188 external_id: "ext-collection-01",
189 updated_at: expect.any(String),
190 handle: "test-collection",
191 metadata: null,
192 title: "test-collection",
193 },
194 ]),
195 })
196 )
197 })
198 // BREAKING: There is no longer discount condition ID filtering for collections (test case: "returns a list of collections filtered by discount condition id")
199 })
200
201 describe("/admin/collections/:id", () => {
202 it("updates a collection", async () => {
203 const response = await api.post(
204 `/admin/collections/${baseCollection.id}`,
205 {
206 title: "test collection creation",
207 handle: "test-handle-creation",
208 },
209 adminHeaders
210 )
211
212 expect(response.status).toEqual(200)
213 expect(response.data).toEqual(
214 expect.objectContaining({
215 collection: {
216 id: expect.stringMatching(/^pcol_*/),
217 title: "test collection creation",
218 handle: "test-handle-creation",
219 external_id: "ext-collection-01",
220 created_at: expect.any(String),
221 updated_at: expect.any(String),
222 metadata: null,
223 },
224 })
225 )
226 })
227
228 it("deletes a collection", async () => {
229 const response = await api.delete(
230 `/admin/collections/${baseCollection.id}`,
231 adminHeaders
232 )
233
234 expect(response.status).toEqual(200)
235 expect(response.data).toEqual({
236 id: baseCollection.id,
237 object: "collection",
238 deleted: true,
239 })
240 })
241
242 it("gets collection", async () => {
243 const response = await api.get(
244 `/admin/collections/${baseCollection.id}`,
245 adminHeaders
246 )
247
248 expect(response.data).toEqual(
249 expect.objectContaining({
250 collection: expect.objectContaining({
251 id: baseCollection.id,
252 created_at: expect.any(String),
253 updated_at: expect.any(String),
254 }),
255 })
256 )
257 })
258
259 // BREAKING: URL and payload changes for adding products to a collection (there is no more "batch" suffix)
260 it("adds products to collection", async () => {
261 const response = await api.post(
262 `/admin/collections/${baseCollection.id}/products?fields=*products`,
263 { add: [baseProduct.id, baseProduct1.id] },
264 adminHeaders
265 )
266
267 expect(response.status).toEqual(200)
268 expect(response.data.collection).toEqual(
269 expect.objectContaining({
270 id: baseCollection.id,
271 created_at: expect.any(String),
272 updated_at: expect.any(String),
273 products: expect.arrayContaining([
274 expect.objectContaining({
275 collection_id: baseCollection.id,
276 title: "test-product",
277 }),
278 expect.objectContaining({
279 collection_id: baseCollection.id,
280 title: "test-product1",
281 }),
282 ]),
283 })
284 )
285 })
286
287 it("should not remove products from collection when updating collection", async () => {
288 const addProductsResponse = await api.post(
289 `/admin/collections/${baseCollection.id}/products?fields=*products`,
290 { add: [baseProduct.id, baseProduct1.id] },
291 adminHeaders
292 )
293
294 expect(addProductsResponse.status).toEqual(200)
295 expect(addProductsResponse.data.collection).toEqual(
296 expect.objectContaining({
297 id: baseCollection.id,
298 products: expect.arrayContaining([
299 expect.objectContaining({
300 collection_id: baseCollection.id,
301 title: "test-product",
302 }),
303 expect.objectContaining({
304 collection_id: baseCollection.id,
305 title: "test-product1",
306 }),
307 ]),
308 })
309 )
310
311 const updateCollectionResponse = await api.post(
312 `/admin/collections/${baseCollection.id}?fields=*products`,
313 { title: "test collection update" },
314 adminHeaders
315 )
316
317 expect(updateCollectionResponse.status).toEqual(200)
318 expect(updateCollectionResponse.data.collection).toEqual(
319 expect.objectContaining({
320 title: "test collection update",
321 products: expect.arrayContaining([
322 expect.objectContaining({
323 collection_id: baseCollection.id,
324 title: "test-product",
325 }),
326 expect.objectContaining({
327 collection_id: baseCollection.id,
328 title: "test-product1",
329 }),
330 ]),
331 })
332 )
333 })
334
335 it("removes products from collection", async () => {
336 await api.post(
337 `/admin/collections/${baseCollection.id}/products`,
338 {
339 add: [baseProduct.id, baseProduct1.id],
340 },
341 adminHeaders
342 )
343
344 const response = await api.post(
345 `/admin/collections/${baseCollection.id}/products?fields=*products`,
346 {
347 remove: [baseProduct1.id],
348 },
349 adminHeaders
350 )
351
352 expect(response.data.collection).toEqual(
353 expect.objectContaining({
354 id: baseCollection.id,
355 products: [
356 expect.objectContaining({
357 collection_id: baseCollection.id,
358 title: "test-product",
359 }),
360 ],
361 })
362 )
363
364 expect(response.status).toEqual(200)
365 })
366 })
367 },
368 })