1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import { Modules } from "@medusajs/framework/utils"
3 import {
4 adminHeaders,
5 createAdminUser,
6 } from "../../../../helpers/create-admin-user"
7 import { getProductFixture } from "../../../../helpers/fixtures"
8
9 jest.setTimeout(50000)
10
11 medusaIntegrationTestRunner({
12 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
13 let baseProduct
14 let proposedProduct
15 let publishedProduct
16 let deletedProduct
17
18 let baseCollection
19 let publishedCollection
20
21 let baseType
22 let baseTag1
23 let baseTag2
24 let newTag
25
26 let shippingProfile
27
28 beforeAll(async () => {
29 await createAdminUser(dbConnection, adminHeaders, getContainer())
30
31 baseCollection = (
32 await api.post(
33 "/admin/collections",
34 { title: "base-collection" },
35 adminHeaders
36 )
37 ).data.collection
38
39 publishedCollection = (
40 await api.post(
41 "/admin/collections",
42 { title: "proposed-collection" },
43 adminHeaders
44 )
45 ).data.collection
46
47 baseType = (
48 await api.post(
49 "/admin/product-types",
50 { value: "test-type" },
51 adminHeaders
52 )
53 ).data.product_type
54
55 baseTag1 = (
56 await api.post("/admin/product-tags", { value: "123" }, adminHeaders)
57 ).data.product_tag
58
59 baseTag2 = (
60 await api.post("/admin/product-tags", { value: "456" }, adminHeaders)
61 ).data.product_tag
62
63 newTag = (
64 await api.post(
65 "/admin/product-tags",
66 { value: "new-tag" },
67 adminHeaders
68 )
69 ).data.product_tag
70
71 shippingProfile = (
72 await api.post(
73 `/admin/shipping-profiles`,
74 { name: "default", type: "default" },
75 adminHeaders
76 )
77 ).data.shipping_profile
78
79 baseProduct = (
80 await api.post(
81 "/admin/products",
82 getProductFixture({
83 title: "Base product",
84 collection_id: baseCollection.id,
85 // BREAKING: Type input changed from {type: {value: string}} to {type_id: string}
86 type_id: baseType.id,
87 tags: [{ id: baseTag1.id }, { id: baseTag2.id }],
88 shipping_profile_id: shippingProfile.id,
89 images: [
90 {
91 url: "image-one",
92 },
93 {
94 url: "image-two",
95 },
96 ],
97 }),
98 adminHeaders
99 )
100 ).data.product
101
102 proposedProduct = (
103 await api.post(
104 "/admin/products",
105 getProductFixture({
106 title: "Proposed product",
107 status: "proposed",
108 tags: [{ id: newTag.id }],
109 type_id: baseType.id,
110 shipping_profile_id: shippingProfile.id,
111 }),
112 adminHeaders
113 )
114 ).data.product
115
116 publishedProduct = (
117 await api.post(
118 "/admin/products",
119 getProductFixture({
120 title: "Published product",
121 status: "published",
122 collection_id: publishedCollection.id,
123 tags: [{ id: baseTag1.id }, { id: baseTag2.id }],
124 shipping_profile_id: shippingProfile.id,
125 }),
126 adminHeaders
127 )
128 ).data.product
129 deletedProduct = (
130 await api.post(
131 "/admin/products",
132 getProductFixture({
133 title: "Deleted product",
134 shipping_profile_id: shippingProfile.id,
135 }),
136 adminHeaders
137 )
138 ).data.product
139 await api.delete(`/admin/products/${deletedProduct.id}`, adminHeaders)
140
141 await dbUtils.snapshot()
142 })
143
144 describe("/admin/products", () => {
145 describe("GET /admin/products", () => {
146 it("returns a list of products with images ordered by rank", async () => {
147 const res = await api.get("/admin/products", adminHeaders)
148
149 expect(res.status).toEqual(200)
150 expect(res.data.products).toEqual(
151 expect.arrayContaining([
152 expect.objectContaining({
153 id: baseProduct.id,
154 images: expect.arrayContaining([
155 expect.objectContaining({ url: "image-one", rank: 0 }),
156 expect.objectContaining({ url: "image-two", rank: 1 }),
157 ]),
158 }),
159 ])
160 )
161 })
162
163 it("returns a list of products with all statuses when no status or invalid status is provided", async () => {
164 const res = await api
165 .get("/admin/products", adminHeaders)
166 .catch((err) => {
167 console.log(err)
168 })
169
170 expect(res.status).toEqual(200)
171 expect(res.data.products).toEqual(
172 expect.arrayContaining([
173 expect.objectContaining({
174 id: baseProduct.id,
175 status: baseProduct.status,
176 }),
177 expect.objectContaining({
178 id: proposedProduct.id,
179 status: proposedProduct.status,
180 }),
181 ])
182 )
183 })
184
185 it("returns a list of all products when no query is provided", async () => {
186 const res = await api
187 .get("/admin/products?q=", adminHeaders)
188 .catch((err) => {
189 console.log(err)
190 })
191
192 expect(res.status).toEqual(200)
193 expect(res.data.products).toEqual(
194 expect.arrayContaining([
195 expect.objectContaining({
196 id: baseProduct.id,
197 }),
198 expect.objectContaining({
199 id: proposedProduct.id,
200 }),
201 ])
202 )
203 })
204
205 // TODO: replace factory with API call
206 it.skip("should return prices not in price list for list product endpoint", async () => {
207 // await simplePriceListFactory(dbConnection, {
208 // prices: [
209 // {
210 // variant_id: "test-variant",
211 // amount: 100,
212 // currency_code: "usd",
213 // },
214 // ],
215 // })
216
217 const res = await api.get(
218 "/admin/products?id=test-product",
219 adminHeaders
220 )
221
222 const prices = res.data.products[0].variants
223 .map((v) => v.prices)
224 .flat()
225
226 expect(res.status).toEqual(200)
227 expect(res.data.products).toEqual(
228 expect.arrayContaining([
229 expect.objectContaining({
230 id: "test-product",
231 status: "draft",
232 }),
233 ])
234 )
235 expect(prices).toEqual(
236 expect.not.arrayContaining([
237 expect.objectContaining({ price_list_id: expect.any(String) }),
238 ])
239 )
240 })
241
242 it("returns a list of products where status is proposed", async () => {
243 const payload = {
244 status: "proposed",
245 }
246
247 await api
248 .post(`/admin/products/${baseProduct.id}`, payload, adminHeaders)
249 .catch((err) => {
250 console.log(err)
251 })
252
253 const response = await api
254 .get("/admin/products?status[]=proposed", adminHeaders)
255 .catch((err) => {
256 console.log(err)
257 })
258
259 expect(response.status).toEqual(200)
260 expect(response.data.products).toEqual(
261 expect.arrayContaining([
262 expect.objectContaining({
263 id: baseProduct.id,
264 status: "proposed",
265 }),
266 ])
267 )
268 })
269
270 it("returns a list of products where status is proposed or published", async () => {
271 const notExpected = [
272 expect.objectContaining({ status: "draft" }),
273 expect.objectContaining({ status: "rejected" }),
274 expect.objectContaining({
275 id: baseProduct.id,
276 }),
277 ]
278
279 const response = await api
280 .get("/admin/products?status[]=published,proposed", adminHeaders)
281 .catch((err) => {
282 console.log(err)
283 })
284
285 expect(response.status).toEqual(200)
286 expect(response.data.products).toHaveLength(2)
287 expect(response.data.products).toEqual(
288 expect.arrayContaining([
289 expect.objectContaining({
290 id: proposedProduct.id,
291 status: "proposed",
292 }),
293 expect.objectContaining({
294 id: publishedProduct.id,
295 status: "published",
296 }),
297 ])
298 )
299
300 for (const notExpect of notExpected) {
301 expect(response.data.products).toEqual(
302 expect.not.arrayContaining([notExpect])
303 )
304 }
305 })
306
307 it("returns a list of products where type_id is filtered", async () => {
308 const response = await api
309 .get(`/admin/products?type_id[]=${baseType.id}`, adminHeaders)
310 .catch((err) => {
311 console.log(err)
312 })
313
314 expect(response.status).toEqual(200)
315 expect(response.data.products).toHaveLength(2)
316 expect(response.data.products).toEqual(
317 expect.arrayContaining([
318 expect.objectContaining({
319 type_id: baseType.id,
320 }),
321 ])
322 )
323 })
324
325 it("returns a list of products where id is a list", async () => {
326 const response = await api
327 .get(
328 `/admin/products?id[]=${baseProduct.id},${proposedProduct.id}`,
329 adminHeaders
330 )
331 .catch((err) => {
332 console.log(err)
333 })
334
335 expect(response.status).toEqual(200)
336 expect(response.data.products).toHaveLength(2)
337 expect(response.data.products).toEqual(
338 expect.arrayContaining([
339 expect.objectContaining({
340 id: baseProduct.id,
341 }),
342 expect.objectContaining({
343 id: proposedProduct.id,
344 }),
345 ])
346 )
347 })
348
349 // TODO: Decide how this should be handled in v2
350 it.skip("returns a list of products filtered by discount condition id", async () => {
351 // const resProd = await api.get("/admin/products", adminHeaders)
352 // const prod1 = resProd.data.products[0]
353 // const prod2 = resProd.data.products[2]
354 // const buildDiscountData = (code, conditionId, products) => {
355 // return {
356 // code,
357 // rule: {
358 // type: DiscountRuleType.PERCENTAGE,
359 // value: 10,
360 // allocation: AllocationType.TOTAL,
361 // conditions: [
362 // {
363 // id: conditionId,
364 // type: DiscountConditionType.PRODUCTS,
365 // operator: DiscountConditionOperator.IN,
366 // product_tags: products,
367 // },
368 // ],
369 // },
370 // }
371 // }
372 // const discountConditionId = IdMap.getId("discount-condition-prod-1")
373 // await simpleDiscountFactory(
374 // dbConnection,
375 // buildDiscountData("code-1", discountConditionId, [prod1.id])
376 // )
377 // const discountConditionId2 = IdMap.getId("discount-condition-prod-2")
378 // await simpleDiscountFactory(
379 // dbConnection,
380 // buildDiscountData("code-2", discountConditionId2, [prod2.id])
381 // )
382 // let res = await api.get(
383 // `/admin/products?discount_condition_id=${discountConditionId}`,
384 // adminHeaders
385 // )
386 // expect(res.status).toEqual(200)
387 // expect(res.data.products).toHaveLength(1)
388 // expect(res.data.products).toEqual(
389 // expect.arrayContaining([expect.objectContaining({ id: prod1.id })])
390 // )
391 // res = await api.get(
392 // `/admin/products?discount_condition_id=${discountConditionId2}`,
393 // adminHeaders
394 // )
395 // expect(res.status).toEqual(200)
396 // expect(res.data.products).toHaveLength(1)
397 // expect(res.data.products).toEqual(
398 // expect.arrayContaining([expect.objectContaining({ id: prod2.id })])
399 // )
400 // res = await api.get(`/admin/products`, adminHeaders)
401 // expect(res.status).toEqual(200)
402 // expect(res.data.products).toHaveLength(5)
403 // expect(res.data.products).toEqual(
404 // expect.arrayContaining([
405 // expect.objectContaining({ id: prod1.id }),
406 // expect.objectContaining({ id: prod2.id }),
407 // ])
408 // )
409 })
410
411 it("doesn't expand collection and types", async () => {
412 const notExpected = [
413 expect.objectContaining({
414 collection: expect.any(Object),
415 type: expect.any(Object),
416 }),
417 ]
418
419 // BREAKING: The expand query parameter is no longer supported, instead we use `fields`.
420 const response = await api
421 .get(
422 `/admin/products?status[]=published,proposed&fields=id,status,*tags`,
423 adminHeaders
424 )
425 .catch((err) => {
426 console.log(err)
427 })
428
429 expect(response.status).toEqual(200)
430 expect(response.data.products).toHaveLength(2)
431 expect(response.data.products).toEqual(
432 expect.arrayContaining([
433 expect.objectContaining({
434 id: proposedProduct.id,
435 status: "proposed",
436 }),
437 expect.objectContaining({
438 id: publishedProduct.id,
439 status: "published",
440 }),
441 ])
442 )
443
444 for (const notExpect of notExpected) {
445 expect(response.data.products).toEqual(
446 expect.not.arrayContaining([notExpect])
447 )
448 }
449 })
450
451 it("returns a list of deleted products with free text query", async () => {
452 // BREAKING: Comparison operators changed, so eg. `gt` became `$gt`
453 const response = await api
454 .get(
455 `/admin/products?deleted_at[$gt]=01-26-1990&with_deleted=true&q=test`,
456 adminHeaders
457 )
458 .catch((err) => {
459 console.log(err)
460 })
461
462 expect(response.status).toEqual(200)
463 expect(response.data.count).toEqual(1)
464 expect(response.data.products).toHaveLength(1)
465 expect(response.data.products).toEqual(
466 expect.arrayContaining([
467 expect.objectContaining({
468 id: deletedProduct.id,
469 }),
470 ])
471 )
472 })
473
474 it("returns a list of products with free text query and limit", async () => {
475 const response = await api
476 .get("/admin/products?q=t&limit=2", adminHeaders)
477 .catch((err) => {
478 console.log(err)
479 })
480
481 expect(response.status).toEqual(200)
482 expect(response.data.products.length).toEqual(2)
483 })
484
485 it("returns a list of products with free text query including variant prices", async () => {
486 const response = await api
487 .get(`/admin/products?q=${baseProduct.description}`, adminHeaders)
488 .catch((err) => {
489 console.log(err)
490 })
491
492 const expectedVariantPrices = response.data.products[0].variants
493 .map((v) => v.prices)
494 .flat(1)
495
496 expect(response.status).toEqual(200)
497 expect(expectedVariantPrices).toEqual(
498 expect.arrayContaining([
499 expect.objectContaining({
500 id: expect.stringMatching(/^price_*/),
501 }),
502 expect.objectContaining({
503 id: expect.stringMatching(/^price_*/),
504 }),
505 expect.objectContaining({
506 id: expect.stringMatching(/^price_*/),
507 }),
508 ])
509 )
510 })
511
512 it("returns a list of products with free text query and offset", async () => {
513 const response = await api
514 .get("/admin/products?q=t&offset=1", adminHeaders)
515 .catch((err) => {
516 console.log(err)
517 })
518
519 expect(response.status).toEqual(200)
520 expect(response.data.products.length).toEqual(2)
521 })
522
523 it("returns a list of deleted products", async () => {
524 const response = await api
525 .get(
526 `/admin/products?deleted_at[$gt]=01-26-1990&with_deleted=true`,
527 adminHeaders
528 )
529 .catch((err) => {
530 console.log(err)
531 })
532
533 expect(response.status).toEqual(200)
534 expect(response.data.products).toHaveLength(1)
535 expect(response.data.products).toEqual(
536 expect.arrayContaining([
537 expect.objectContaining({
538 id: deletedProduct.id,
539 }),
540 ])
541 )
542 })
543
544 it("returns a list of products in collection", async () => {
545 const notExpected = [
546 expect.objectContaining({ collection_id: publishedCollection.id }),
547 ]
548
549 const response = await api
550 .get(
551 `/admin/products?collection_id[]=${baseCollection.id}`,
552 adminHeaders
553 )
554 .catch((err) => {
555 console.log(err)
556 })
557
558 expect(response.status).toEqual(200)
559 expect(response.data.products).toHaveLength(1)
560 expect(response.data.products).toEqual(
561 expect.arrayContaining([
562 expect.objectContaining({
563 id: baseProduct.id,
564 collection_id: baseCollection.id,
565 }),
566 ])
567 )
568
569 for (const notExpect of notExpected) {
570 expect(response.data.products).toEqual(
571 expect.not.arrayContaining([notExpect])
572 )
573 }
574 })
575
576 it("returns a list of products with tags", async () => {
577 const response = await api.get(
578 `/admin/products?tag_id[]=${baseProduct.tags[0].id}`,
579 adminHeaders
580 )
581
582 expect(response.status).toEqual(200)
583 expect(response.data.products).toHaveLength(2)
584 expect(response.data.products).toEqual(
585 expect.arrayContaining([
586 expect.objectContaining({
587 id: baseProduct.id,
588 tags: expect.arrayContaining([
589 expect.objectContaining({ id: baseProduct.tags[0].id }),
590 ]),
591 }),
592 expect.objectContaining({
593 id: publishedProduct.id,
594 // It should be the same tag instance in both products
595 tags: expect.arrayContaining([
596 expect.objectContaining({ id: baseProduct.tags[0].id }),
597 ]),
598 }),
599 ])
600 )
601 })
602
603 it("returns a list of products with tags in a collection", async () => {
604 const response = await api.get(
605 `/admin/products?collection_id[]=${baseCollection.id}&tag_id[]=${baseProduct.tags[0].id}`,
606 adminHeaders
607 )
608
609 expect(response.status).toEqual(200)
610 expect(response.data.products).toHaveLength(1)
611 expect(response.data.products).toEqual(
612 expect.arrayContaining([
613 expect.objectContaining({
614 id: baseProduct.id,
615 collection_id: baseCollection.id,
616 tags: expect.arrayContaining([
617 expect.objectContaining({ id: baseProduct.tags[0].id }),
618 ]),
619 }),
620 ])
621 )
622 })
623
624 it("returns a list of products with only giftcard in list", async () => {
625 const payload = {
626 title: "Test Giftcard",
627 is_giftcard: true,
628 description: "test-giftcard-description",
629 options: [{ title: "Denominations", values: ["100"] }],
630 shipping_profile_id: shippingProfile.id,
631 variants: [
632 {
633 title: "Test variant",
634 prices: [{ currency_code: "usd", amount: 100 }],
635 options: {
636 Denominations: "100",
637 },
638 },
639 ],
640 }
641
642 await api
643 .post("/admin/products", payload, adminHeaders)
644 .catch((err) => {
645 console.log(err)
646 })
647
648 const response = await api
649 .get("/admin/products?is_giftcard=true", adminHeaders)
650 .catch((err) => {
651 console.log(err)
652 })
653
654 expect(response.data.products).toEqual(
655 expect.not.arrayContaining([
656 expect.objectContaining({ is_giftcard: false }),
657 ])
658 )
659
660 expect(response.status).toEqual(200)
661 expect(response.data.products).toEqual(
662 expect.arrayContaining([
663 expect.objectContaining({
664 title: "Test Giftcard",
665 id: expect.stringMatching(/^prod_*/),
666 is_giftcard: true,
667 description: "test-giftcard-description",
668 // profile_id: expect.stringMatching(/^sp_*/),
669 options: expect.arrayContaining([
670 expect.objectContaining({
671 title: "Denominations",
672 values: expect.arrayContaining([
673 expect.objectContaining({ value: "100" }),
674 ]),
675 id: expect.stringMatching(/^opt_*/),
676 created_at: expect.any(String),
677 updated_at: expect.any(String),
678 }),
679 ]),
680 variants: expect.arrayContaining([
681 expect.objectContaining({
682 title: "Test variant",
683 id: expect.stringMatching(/^variant_*/),
684 product_id: expect.stringMatching(/^prod_*/),
685 created_at: expect.any(String),
686 updated_at: expect.any(String),
687 prices: expect.arrayContaining([
688 expect.objectContaining({
689 id: expect.any(String),
690 currency_code: "usd",
691 amount: 100,
692 variant_id: expect.stringMatching(/^variant_*/),
693 created_at: expect.any(String),
694 updated_at: expect.any(String),
695 }),
696 ]),
697 options: expect.arrayContaining([
698 expect.objectContaining({
699 id: expect.stringMatching(/^optval_*/),
700 value: "100",
701 }),
702 ]),
703 }),
704 ]),
705 created_at: expect.any(String),
706 updated_at: expect.any(String),
707 }),
708 ])
709 )
710 })
711
712 it("returns a list of products not containing a giftcard in list", async () => {
713 const payload = {
714 title: "Test Giftcard",
715 options: [{ title: "Denominations", values: ["100"] }],
716 is_giftcard: true,
717 description: "test-giftcard-description",
718 shipping_profile_id: shippingProfile.id,
719 }
720
721 await api
722 .post("/admin/products", payload, adminHeaders)
723 .catch((err) => {
724 console.log(err)
725 })
726
727 const response = await api
728 .get("/admin/products?is_giftcard=false", adminHeaders)
729 .catch((err) => {
730 console.log(err)
731 })
732
733 expect(response.data.products).toHaveLength(3)
734 expect(response.data.products).toEqual(
735 expect.not.arrayContaining([
736 expect.objectContaining({ is_giftcard: true }),
737 ])
738 )
739 })
740
741 it("returns a list of products with child entities", async () => {
742 const response = await api
743 .get("/admin/products?order=created_at", adminHeaders)
744 .catch((err) => {
745 console.log(err)
746 })
747
748 expect(response.data.products).toHaveLength(3)
749 expect(response.data.products).toEqual(
750 expect.arrayContaining([
751 expect.objectContaining({
752 id: baseProduct.id,
753 options: expect.arrayContaining([
754 expect.objectContaining({
755 id: expect.stringMatching(/^opt_*/),
756 created_at: expect.any(String),
757 updated_at: expect.any(String),
758 }),
759 ]),
760 images: expect.arrayContaining([
761 expect.objectContaining({
762 id: expect.stringMatching(/^img_*/),
763 created_at: expect.any(String),
764 updated_at: expect.any(String),
765 }),
766 ]),
767 variants: expect.arrayContaining([
768 expect.objectContaining({
769 id: baseProduct.variants[0].id,
770 created_at: expect.any(String),
771 updated_at: expect.any(String),
772 product_id: expect.stringMatching(/^prod_*/),
773 prices: expect.arrayContaining([
774 expect.objectContaining({
775 id: baseProduct.variants[0].prices[0].id,
776 variant_id: expect.stringMatching(/^variant_*/),
777 created_at: expect.any(String),
778 updated_at: expect.any(String),
779 }),
780 ]),
781 options: expect.arrayContaining([
782 expect.objectContaining({
783 id: expect.stringMatching(/^optval_*/),
784 value: "large",
785 }),
786 ]),
787 }),
788 ]),
789 tags: expect.arrayContaining([
790 expect.objectContaining({
791 id: expect.stringMatching(/^ptag_*/),
792 created_at: expect.any(String),
793 updated_at: expect.any(String),
794 }),
795 ]),
796 type: expect.objectContaining({
797 id: expect.stringMatching(/^ptyp_*/),
798 created_at: expect.any(String),
799 updated_at: expect.any(String),
800 }),
801 collection: expect.objectContaining({
802 id: expect.stringMatching(/^pcol_*/),
803 created_at: expect.any(String),
804 updated_at: expect.any(String),
805 }),
806 // // profile_id: expect.stringMatching(/^sp_*/),
807 created_at: expect.any(String),
808 updated_at: expect.any(String),
809 }),
810 expect.objectContaining({
811 id: proposedProduct.id,
812 created_at: expect.any(String),
813 variants: expect.arrayContaining([
814 expect.objectContaining({
815 id: proposedProduct.variants[0].id,
816 created_at: expect.any(String),
817 updated_at: expect.any(String),
818 product_id: expect.stringMatching(/^prod_*/),
819 options: expect.arrayContaining([
820 expect.objectContaining({
821 id: expect.stringMatching(/^optval_*/),
822 value: "green",
823 }),
824 ]),
825 }),
826 ]),
827 tags: expect.arrayContaining([
828 expect.objectContaining({
829 id: expect.stringMatching(/^ptag*/),
830 created_at: expect.any(String),
831 updated_at: expect.any(String),
832 }),
833 ]),
834 type: expect.objectContaining({
835 id: expect.stringMatching(/^ptyp-*/),
836 created_at: expect.any(String),
837 updated_at: expect.any(String),
838 }),
839 collection: null,
840 // profile_id: expect.stringMatching(/^sp_*/),
841 updated_at: expect.any(String),
842 }),
843 expect.objectContaining({
844 id: publishedProduct.id,
845 // profile_id: expect.stringMatching(/^sp_*/),
846 created_at: expect.any(String),
847 type: expect.any(Object),
848 collection: expect.any(Object),
849 options: expect.any(Array),
850 tags: expect.any(Array),
851 variants: expect.any(Array),
852 updated_at: expect.any(String),
853 }),
854 ])
855 )
856 })
857
858 // TODO: Enable through http
859 it.skip("should return products filtered by price_list_id", async () => {
860 // const priceList = await breaking(
861 // async () => {
862 // return await simplePriceListFactory(dbConnection, {
863 // prices: [
864 // {
865 // variant_id: baseProduct.variants[0].id,
866 // amount: 100,
867 // currency_code: "usd",
868 // },
869 // ],
870 // })
871 // },
872 // async () => {
873 // const variantId = baseProduct.variants[0].id
874 // const priceSet = await createVariantPriceSet({
875 // container,
876 // variantId,
877 // })
878 // const [priceList] = await pricingService.createPriceLists([
879 // {
880 // title: "Test price list",
881 // description: "Test",
882 // status: PriceListStatus.ACTIVE,
883 // type: PriceListType.OVERRIDE,
884 // prices: [
885 // {
886 // amount: 5000,
887 // currency_code: "usd",
888 // price_set_id: priceSet.id,
889 // rules: {
890 // region_id: "test-region",
891 // },
892 // },
893 // ],
894 // },
895 // ])
896 // return priceList
897 // }
898 // )
899 // const res = await api.get(
900 // `/admin/products?price_list_id[]=${priceList.id}`,
901 // adminHeaders
902 // )
903 // expect(res.status).toEqual(200)
904 // expect(res.data.products.length).toEqual(1)
905 // expect(res.data.products).toEqual([
906 // expect.objectContaining({
907 // id: baseProduct.id,
908 // status: baseProduct.status,
909 // }),
910 // ])
911 })
912
913 it("should return products filtered by sales_channel_id", async () => {
914 const salesChannel = (
915 await api.post(
916 "/admin/sales-channels",
917 {
918 name: "Sales",
919 },
920 adminHeaders
921 )
922 ).data.sales_channel
923
924 const newProduct = (
925 await api.post(
926 "/admin/products",
927 getProductFixture({
928 title: "Test saleschannel",
929 sales_channels: [{ id: salesChannel.id }],
930 shipping_profile_id: shippingProfile.id,
931 }),
932 adminHeaders
933 )
934 ).data.product
935
936 const res = await api.get(
937 `/admin/products?sales_channel_id[]=${salesChannel.id}`,
938 adminHeaders
939 )
940
941 expect(res.status).toEqual(200)
942 expect(res.data.products.length).toEqual(1)
943 expect(res.data.products).toEqual([
944 expect.objectContaining({
945 id: newProduct.id,
946 }),
947 ])
948 })
949
950 it("should return products filtered by external_id", async () => {
951 const newProduct = (
952 await api.post(
953 "/admin/products",
954 getProductFixture({
955 title: "Test saleschannel",
956 external_id: "test-external-id",
957 shipping_profile_id: shippingProfile.id,
958 }),
959 adminHeaders
960 )
961 ).data.product
962
963 const res = await api.get(
964 `/admin/products?external_id[]=test-external-id`,
965 adminHeaders
966 )
967
968 expect(res.status).toEqual(200)
969 expect(res.data.products.length).toEqual(1)
970 expect(res.data.products).toEqual([
971 expect.objectContaining({
972 id: newProduct.id,
973 }),
974 ])
975 })
976
977 it("returns a list of products filtered by variants[sku]", async () => {
978 const productWithSku = await api.post(
979 "/admin/products",
980 getProductFixture({
981 title: "Product with SKU",
982 shipping_profile_id: shippingProfile.id,
983 variants: [
984 {
985 title: "Test variant",
986 sku: "SKU1234567890123",
987 prices: [{ currency_code: "usd", amount: 100 }],
988 options: {
989 size: "large",
990 color: "green",
991 },
992 },
993 ],
994 }),
995 adminHeaders
996 )
997
998 await api.post(
999 "/admin/products",
1000 getProductFixture({
1001 title: "Product with different SKU",
1002 shipping_profile_id: shippingProfile.id,
1003 variants: [
1004 {
1005 title: "Test variant 2",
1006 sku: "SKU9876543210987",
1007 prices: [{ currency_code: "usd", amount: 150 }],
1008 options: {
1009 size: "large",
1010 color: "green",
1011 },
1012 },
1013 ],
1014 }),
1015 adminHeaders
1016 )
1017
1018 const response = await api
1019 .get("/admin/products?variants[sku]=SKU1234567890123", adminHeaders)
1020 .catch((err) => {
1021 console.log(err)
1022 })
1023
1024 expect(response.status).toEqual(200)
1025 expect(response.data.products).toHaveLength(1)
1026 expect(response.data.products).toEqual(
1027 expect.arrayContaining([
1028 expect.objectContaining({
1029 id: productWithSku.data.product.id,
1030 title: "Product with SKU",
1031 variants: expect.arrayContaining([
1032 expect.objectContaining({
1033 sku: "SKU1234567890123",
1034 }),
1035 ]),
1036 }),
1037 ])
1038 )
1039 })
1040
1041 it("returns a list of products filtered by variants[ean]", async () => {
1042 const productWithEan = await api.post(
1043 "/admin/products",
1044 getProductFixture({
1045 title: "Product with EAN",
1046 shipping_profile_id: shippingProfile.id,
1047 variants: [
1048 {
1049 title: "Test variant",
1050 ean: "1234567890123",
1051 prices: [{ currency_code: "usd", amount: 100 }],
1052 options: {
1053 size: "large",
1054 color: "green",
1055 },
1056 },
1057 ],
1058 }),
1059 adminHeaders
1060 )
1061
1062 await api.post(
1063 "/admin/products",
1064 getProductFixture({
1065 title: "Product with different EAN",
1066 shipping_profile_id: shippingProfile.id,
1067 variants: [
1068 {
1069 title: "Test variant 2",
1070 ean: "9876543210987",
1071 prices: [{ currency_code: "usd", amount: 150 }],
1072 options: {
1073 size: "large",
1074 color: "green",
1075 },
1076 },
1077 ],
1078 }),
1079 adminHeaders
1080 )
1081
1082 const response = await api
1083 .get("/admin/products?variants[ean]=1234567890123", adminHeaders)
1084 .catch((err) => {
1085 console.log(err)
1086 })
1087
1088 expect(response.status).toEqual(200)
1089 expect(response.data.products).toHaveLength(1)
1090 expect(response.data.products).toEqual(
1091 expect.arrayContaining([
1092 expect.objectContaining({
1093 id: productWithEan.data.product.id,
1094 title: "Product with EAN",
1095 variants: expect.arrayContaining([
1096 expect.objectContaining({
1097 ean: "1234567890123",
1098 }),
1099 ]),
1100 }),
1101 ])
1102 )
1103 })
1104
1105 it("returns a list of products filtered by variants[upc]", async () => {
1106 const productWithUpc = await api.post(
1107 "/admin/products",
1108 getProductFixture({
1109 title: "Product with UPC",
1110 shipping_profile_id: shippingProfile.id,
1111 variants: [
1112 {
1113 title: "Test variant",
1114 upc: "123456789012",
1115 prices: [{ currency_code: "usd", amount: 200 }],
1116 options: {
1117 size: "large",
1118 color: "green",
1119 },
1120 },
1121 ],
1122 }),
1123 adminHeaders
1124 )
1125
1126 await api.post(
1127 "/admin/products",
1128 getProductFixture({
1129 title: "Product with different UPC",
1130 shipping_profile_id: shippingProfile.id,
1131 variants: [
1132 {
1133 title: "Test variant 2",
1134 upc: "098765432109",
1135 prices: [{ currency_code: "usd", amount: 250 }],
1136 options: {
1137 size: "large",
1138 color: "green",
1139 },
1140 },
1141 ],
1142 }),
1143 adminHeaders
1144 )
1145
1146 const response = await api
1147 .get("/admin/products?variants[upc]=123456789012", adminHeaders)
1148 .catch((err) => {
1149 console.log(err)
1150 })
1151
1152 expect(response.status).toEqual(200)
1153 expect(response.data.products).toHaveLength(1)
1154 expect(response.data.products).toEqual(
1155 expect.arrayContaining([
1156 expect.objectContaining({
1157 id: productWithUpc.data.product.id,
1158 title: "Product with UPC",
1159 variants: expect.arrayContaining([
1160 expect.objectContaining({
1161 upc: "123456789012",
1162 }),
1163 ]),
1164 }),
1165 ])
1166 )
1167 })
1168
1169 it("returns a list of products filtered by variants[barcode]", async () => {
1170 const productWithBarcode = await api.post(
1171 "/admin/products",
1172 getProductFixture({
1173 title: "Product with Barcode",
1174 shipping_profile_id: shippingProfile.id,
1175 variants: [
1176 {
1177 title: "Test variant",
1178 barcode: "1234567890",
1179 prices: [{ currency_code: "usd", amount: 300 }],
1180 options: {
1181 size: "large",
1182 color: "green",
1183 },
1184 },
1185 ],
1186 }),
1187 adminHeaders
1188 )
1189
1190 await api.post(
1191 "/admin/products",
1192 getProductFixture({
1193 title: "Product with different Barcode",
1194 shipping_profile_id: shippingProfile.id,
1195 variants: [
1196 {
1197 title: "Test variant 2",
1198 barcode: "0987654321",
1199 prices: [{ currency_code: "usd", amount: 350 }],
1200 options: {
1201 size: "large",
1202 color: "green",
1203 },
1204 },
1205 ],
1206 }),
1207 adminHeaders
1208 )
1209
1210 const response = await api
1211 .get("/admin/products?variants[barcode]=1234567890", adminHeaders)
1212 .catch((err) => {
1213 console.log(err)
1214 })
1215
1216 expect(response.status).toEqual(200)
1217 expect(response.data.products).toHaveLength(1)
1218 expect(response.data.products).toEqual(
1219 expect.arrayContaining([
1220 expect.objectContaining({
1221 id: productWithBarcode.data.product.id,
1222 title: "Product with Barcode",
1223 variants: expect.arrayContaining([
1224 expect.objectContaining({
1225 barcode: "1234567890",
1226 }),
1227 ]),
1228 }),
1229 ])
1230 )
1231 })
1232
1233 it("returns empty list when filtering by non-existent variants[ean]", async () => {
1234 const response = await api
1235 .get("/admin/products?variants[ean]=5555555555555", adminHeaders)
1236 .catch((err) => {
1237 console.log(err)
1238 })
1239
1240 expect(response.status).toEqual(200)
1241 expect(response.data.products).toHaveLength(0)
1242 })
1243
1244 it("returns empty list when filtering by non-existent variants[upc]", async () => {
1245 const response = await api
1246 .get("/admin/products?variants[upc]=555555555555", adminHeaders)
1247 .catch((err) => {
1248 console.log(err)
1249 })
1250
1251 expect(response.status).toEqual(200)
1252 expect(response.data.products).toHaveLength(0)
1253 })
1254
1255 it("returns empty list when filtering by non-existent variants[barcode]", async () => {
1256 const response = await api
1257 .get("/admin/products?variants[barcode]=5555555555", adminHeaders)
1258 .catch((err) => {
1259 console.log(err)
1260 })
1261
1262 expect(response.status).toEqual(200)
1263 expect(response.data.products).toHaveLength(0)
1264 })
1265 })
1266
1267 describe("GET /admin/products/:id", () => {
1268 it("should get a product with default relations", async () => {
1269 const res = await api
1270 .get(`/admin/products/${baseProduct.id}`, adminHeaders)
1271 .catch((err) => {
1272 console.log(err)
1273 })
1274
1275 const keysInResponse = Object.keys(res.data.product)
1276
1277 expect(res.status).toEqual(200)
1278 expect(res.data.product.id).toEqual(baseProduct.id)
1279 expect(keysInResponse).toEqual(
1280 expect.arrayContaining([
1281 "id",
1282 "created_at",
1283 "updated_at",
1284 "deleted_at",
1285 "title",
1286 "subtitle",
1287 "description",
1288 "handle",
1289 "is_giftcard",
1290 "status",
1291 "thumbnail",
1292 "weight",
1293 "length",
1294 "height",
1295 "width",
1296 "hs_code",
1297 "origin_country",
1298 "mid_code",
1299 "material",
1300 "collection_id",
1301 "type_id",
1302 "discountable",
1303 "external_id",
1304 "metadata",
1305 // "categories",
1306 "collection",
1307 "images",
1308 "options",
1309 // "profiles",
1310 // "profile",
1311 // "profile_id",
1312 // "sales_channels",
1313 "tags",
1314 "type",
1315 "variants",
1316 ])
1317 )
1318
1319 const variants = res.data.product.variants
1320 const hasPrices = variants.some((variant) => !!variant.prices)
1321
1322 expect(hasPrices).toBe(true)
1323 })
1324
1325 it("should get a product with images ordered by rank", async () => {
1326 const res = await api.get(
1327 `/admin/products/${baseProduct.id}`,
1328 adminHeaders
1329 )
1330
1331 expect(res.data.product.images).toEqual(
1332 expect.arrayContaining([
1333 expect.objectContaining({ url: "image-one", rank: 0 }),
1334 expect.objectContaining({ url: "image-two", rank: 1 }),
1335 ])
1336 )
1337 })
1338
1339 it("should get a product with prices", async () => {
1340 const res = await api
1341 .get(
1342 `/admin/products/${baseProduct.id}?fields=*variants,*variants.prices`,
1343 adminHeaders
1344 )
1345 .catch((err) => {
1346 console.log(err)
1347 })
1348
1349 const { id, variants } = res.data.product
1350
1351 expect(id).toEqual(baseProduct.id)
1352 expect(variants[0].prices).toEqual(
1353 expect.arrayContaining([
1354 expect.objectContaining({
1355 amount: 100,
1356 currency_code: "usd",
1357 }),
1358 ])
1359 )
1360 })
1361
1362 it("should get a product only with variants expanded", async () => {
1363 const res = await api
1364 .get(
1365 `/admin/products/${baseProduct.id}?fields=title,*variants`,
1366 adminHeaders
1367 )
1368 .catch((err) => {
1369 console.log(err)
1370 })
1371
1372 const { id, variants } = res.data.product
1373
1374 expect(id).toEqual(baseProduct.id)
1375 expect(variants[0]).toEqual(
1376 expect.objectContaining({
1377 title: "Test variant",
1378 })
1379 )
1380 // prices is one of many properties that should not be expanded
1381 expect(variants[0].prices).toBeUndefined()
1382 })
1383 })
1384
1385 describe("GET /admin/products/:id/variants", () => {
1386 it("should get product variants with inventory quantity computed", async () => {
1387 const stockLocation = (
1388 await api.post(
1389 `/admin/stock-locations`,
1390 { name: "loc" },
1391 adminHeaders
1392 )
1393 ).data.stock_location
1394
1395 const inventoryItem1 = (
1396 await api.post(
1397 `/admin/inventory-items`,
1398 { sku: "inventory-1" },
1399 adminHeaders
1400 )
1401 ).data.inventory_item
1402
1403 const inventoryItem2 = (
1404 await api.post(
1405 `/admin/inventory-items`,
1406 { sku: "inventory-2" },
1407 adminHeaders
1408 )
1409 ).data.inventory_item
1410
1411 await api.post(
1412 `/admin/inventory-items/${inventoryItem1.id}/location-levels`,
1413 {
1414 location_id: stockLocation.id,
1415 stocked_quantity: 8,
1416 },
1417 adminHeaders
1418 )
1419
1420 await api.post(
1421 `/admin/inventory-items/${inventoryItem2.id}/location-levels`,
1422 {
1423 location_id: stockLocation.id,
1424 stocked_quantity: 4,
1425 },
1426 adminHeaders
1427 )
1428
1429 const payload = {
1430 title: "Test product - 1",
1431 handle: "test-1",
1432 options: [{ title: "size", values: ["x", "l"] }],
1433 shipping_profile_id: shippingProfile.id,
1434 variants: [
1435 {
1436 title: "Custom inventory 1",
1437 prices: [{ currency_code: "usd", amount: 100 }],
1438 options: { size: "x" },
1439 manage_inventory: true,
1440 inventory_items: [
1441 {
1442 inventory_item_id: inventoryItem1.id,
1443 required_quantity: 4,
1444 },
1445 {
1446 inventory_item_id: inventoryItem2.id,
1447 required_quantity: 2,
1448 },
1449 ],
1450 },
1451 ],
1452 }
1453
1454 const product = (
1455 await api.post(`/admin/products`, payload, adminHeaders)
1456 ).data.product
1457
1458 const variants = (
1459 await api.get(
1460 `/admin/products/${product.id}/variants?fields=+inventory_quantity`,
1461 adminHeaders
1462 )
1463 ).data.variants
1464
1465 expect(variants).toEqual([
1466 expect.objectContaining({
1467 inventory_quantity: 2,
1468 }),
1469 ])
1470 })
1471
1472 it("should get product variants filtered by sku", async () => {
1473 const payload = {
1474 title: "Test product - 1",
1475 handle: "test-1",
1476 options: [{ title: "size", values: ["x", "l"] }],
1477 shipping_profile_id: shippingProfile.id,
1478 variants: [
1479 {
1480 title: "Custom inventory 1",
1481 prices: [{ currency_code: "usd", amount: 100 }],
1482 options: { size: "x" },
1483 sku: "sku-123",
1484 },
1485 {
1486 title: "Custom inventory 2",
1487 prices: [{ currency_code: "usd", amount: 100 }],
1488 options: { size: "l" },
1489 sku: "sku-456",
1490 },
1491 ],
1492 }
1493
1494 const product = (
1495 await api.post(`/admin/products`, payload, adminHeaders)
1496 ).data.product
1497
1498 const variants = (
1499 await api.get(
1500 `/admin/products/${product.id}/variants?sku=sku-123`,
1501 adminHeaders
1502 )
1503 ).data.variants
1504
1505 expect(variants).toEqual([
1506 expect.objectContaining({
1507 title: "Custom inventory 1",
1508 product_id: product.id,
1509 }),
1510 ])
1511 })
1512
1513 it("should get product variants filtered by manage_inventory", async () => {
1514 const payload = {
1515 title: "Test product - 1",
1516 handle: "test-1",
1517 options: [{ title: "size", values: ["x", "l"] }],
1518 shipping_profile_id: shippingProfile.id,
1519 variants: [
1520 {
1521 title: "Custom inventory 1",
1522 prices: [{ currency_code: "usd", amount: 100 }],
1523 manage_inventory: true,
1524 options: { size: "x" },
1525 inventory_items: [],
1526 },
1527 {
1528 title: "Custom inventory 2",
1529 prices: [{ currency_code: "usd", amount: 100 }],
1530 options: { size: "l" },
1531 manage_inventory: false,
1532 },
1533 ],
1534 }
1535
1536 const product = (
1537 await api.post(`/admin/products`, payload, adminHeaders)
1538 ).data.product
1539
1540 let variants = (
1541 await api.get(
1542 `/admin/products/${product.id}/variants?manage_inventory=true`,
1543 adminHeaders
1544 )
1545 ).data.variants
1546
1547 expect(variants).toEqual([
1548 expect.objectContaining({
1549 title: "Custom inventory 1",
1550 product_id: product.id,
1551 }),
1552 ])
1553
1554 variants = (
1555 await api.get(
1556 `/admin/products/${product.id}/variants?manage_inventory=false`,
1557 adminHeaders
1558 )
1559 ).data.variants
1560
1561 expect(variants).toEqual([
1562 expect.objectContaining({
1563 title: "Custom inventory 2",
1564 product_id: product.id,
1565 }),
1566 ])
1567 })
1568 })
1569
1570 describe("POST /admin/products", () => {
1571 it("creates a product", async () => {
1572 const response = await api
1573 .post(
1574 "/admin/products",
1575 getProductFixture({
1576 title: "Test create",
1577 collection_id: baseCollection.id,
1578 type_id: baseType.id,
1579 tags: [{ id: baseTag1.id }, { id: baseTag2.id }],
1580 shipping_profile_id: shippingProfile.id,
1581 }),
1582
1583 adminHeaders
1584 )
1585 .catch((err) => {
1586 console.log(err)
1587 })
1588
1589 const priceIdSelector = /^price_*/
1590
1591 expect(response.status).toEqual(200)
1592 expect(response.data.product).toEqual(
1593 expect.objectContaining({
1594 id: expect.stringMatching(/^prod_*/),
1595 title: "Test create",
1596 discountable: true,
1597 is_giftcard: false,
1598 handle: "test-create",
1599 status: "draft",
1600 created_at: expect.any(String),
1601 updated_at: expect.any(String),
1602 // profile_id: expect.stringMatching(/^sp_*/),
1603 images: expect.arrayContaining([
1604 expect.objectContaining({
1605 id: expect.any(String),
1606 url: "test-image.png",
1607 created_at: expect.any(String),
1608 updated_at: expect.any(String),
1609 }),
1610 expect.objectContaining({
1611 id: expect.any(String),
1612 url: "test-image-2.png",
1613 created_at: expect.any(String),
1614 updated_at: expect.any(String),
1615 }),
1616 ]),
1617 thumbnail: "test-image.png",
1618 tags: expect.arrayContaining([
1619 expect.objectContaining({
1620 id: expect.any(String),
1621 value: "123",
1622 created_at: expect.any(String),
1623 updated_at: expect.any(String),
1624 }),
1625 expect.objectContaining({
1626 id: expect.any(String),
1627 value: "456",
1628 created_at: expect.any(String),
1629 updated_at: expect.any(String),
1630 }),
1631 ]),
1632 type: expect.objectContaining({
1633 value: baseType.value,
1634 created_at: expect.any(String),
1635 updated_at: expect.any(String),
1636 }),
1637 collection: expect.objectContaining({
1638 id: baseCollection.id,
1639 title: baseCollection.title,
1640 created_at: expect.any(String),
1641 updated_at: expect.any(String),
1642 }),
1643 options: expect.arrayContaining([
1644 expect.objectContaining({
1645 id: expect.stringMatching(/^opt_*/),
1646 title: "size",
1647 values: expect.arrayContaining([
1648 expect.objectContaining({ value: "large" }),
1649 ]),
1650 created_at: expect.any(String),
1651 updated_at: expect.any(String),
1652 }),
1653 expect.objectContaining({
1654 id: expect.stringMatching(/^opt_*/),
1655 title: "color",
1656 values: expect.arrayContaining([
1657 expect.objectContaining({ value: "green" }),
1658 ]),
1659 created_at: expect.any(String),
1660 updated_at: expect.any(String),
1661 }),
1662 ]),
1663 variants: expect.arrayContaining([
1664 expect.objectContaining({
1665 id: expect.stringMatching(/^variant_*/),
1666 product_id: expect.stringMatching(/^prod_*/),
1667 updated_at: expect.any(String),
1668 created_at: expect.any(String),
1669 title: "Test variant",
1670 prices: expect.arrayContaining([
1671 expect.objectContaining({
1672 id: expect.stringMatching(priceIdSelector),
1673 currency_code: "usd",
1674 amount: 100,
1675 created_at: expect.any(String),
1676 updated_at: expect.any(String),
1677 variant_id: expect.stringMatching(/^variant_*/),
1678 }),
1679 expect.objectContaining({
1680 id: expect.stringMatching(priceIdSelector),
1681 currency_code: "eur",
1682 amount: 45,
1683 created_at: expect.any(String),
1684 updated_at: expect.any(String),
1685 variant_id: expect.stringMatching(/^variant_*/),
1686 }),
1687 expect.objectContaining({
1688 id: expect.stringMatching(priceIdSelector),
1689 currency_code: "dkk",
1690 amount: 30,
1691 created_at: expect.any(String),
1692 updated_at: expect.any(String),
1693 variant_id: expect.stringMatching(/^variant_*/),
1694 }),
1695 ]),
1696 options: expect.arrayContaining([
1697 expect.objectContaining({
1698 id: expect.stringMatching(/^optval_*/),
1699 value: "large",
1700 }),
1701 expect.objectContaining({
1702 id: expect.stringMatching(/^optval_*/),
1703 value: "green",
1704 }),
1705 ]),
1706 }),
1707 ]),
1708 })
1709 )
1710 })
1711
1712 it("creates a product variant with price rules", async () => {
1713 const response = await api.post(
1714 "/admin/products",
1715 {
1716 title: "Test create",
1717 options: [{ title: "size", values: ["x", "l"] }],
1718 shipping_profile_id: shippingProfile.id,
1719 variants: [
1720 {
1721 title: "Price with rules",
1722 options: { size: "l" },
1723 prices: [
1724 {
1725 currency_code: "usd",
1726 amount: 100,
1727 rules: { region_id: "eur" },
1728 },
1729 ],
1730 },
1731 ],
1732 },
1733 adminHeaders
1734 )
1735
1736 const priceIdSelector = /^price_*/
1737
1738 expect(response.status).toEqual(200)
1739 expect(response.data.product).toEqual(
1740 expect.objectContaining({
1741 id: expect.stringMatching(/^prod_*/),
1742 variants: expect.arrayContaining([
1743 expect.objectContaining({
1744 id: expect.stringMatching(/^variant_*/),
1745 title: "Price with rules",
1746 prices: expect.arrayContaining([
1747 expect.objectContaining({
1748 id: expect.stringMatching(priceIdSelector),
1749 currency_code: "usd",
1750 amount: 100,
1751 created_at: expect.any(String),
1752 updated_at: expect.any(String),
1753 variant_id: expect.stringMatching(/^variant_*/),
1754 rules: { region_id: "eur" },
1755 }),
1756 ]),
1757 }),
1758 ]),
1759 })
1760 )
1761 })
1762
1763 it("creates a product that is not discountable", async () => {
1764 const payload = {
1765 title: "Test",
1766 discountable: false,
1767 description: "test-product-description",
1768 images: [{ url: "test-image.png" }, { url: "test-image-2.png" }],
1769 collection_id: baseCollection.id,
1770 tags: [{ id: baseTag1.id }, { id: baseTag2.id }],
1771 options: [{ title: "size", values: ["large"] }],
1772 shipping_profile_id: shippingProfile.id,
1773 variants: [
1774 {
1775 title: "Test variant",
1776 prices: [{ currency_code: "usd", amount: 100 }],
1777 options: { size: "large" },
1778 },
1779 ],
1780 }
1781
1782 const response = await api
1783 .post("/admin/products", payload, adminHeaders)
1784 .catch((err) => {
1785 console.log(err)
1786 })
1787
1788 expect(response.status).toEqual(200)
1789 expect(response.data.product).toEqual(
1790 expect.objectContaining({
1791 discountable: false,
1792 })
1793 )
1794 })
1795
1796 it.skip("Sets variant ranks when creating a product", async () => {
1797 const payload = {
1798 title: "Test product - 1",
1799 handle: "test-1",
1800 description: "test-product-description 1",
1801 images: [{ url: "test-image.png" }, { url: "test-image-2.png" }],
1802 collection_id: baseCollection.id,
1803 tags: [{ id: baseTag1.id }, { id: baseTag2.id }],
1804 options: [{ title: "size", values: ["l", "x"] }],
1805 shipping_profile_id: shippingProfile.id,
1806 variants: [
1807 {
1808 title: "Test variant 1",
1809 prices: [{ currency_code: "usd", amount: 100 }],
1810 options: { size: "x" },
1811 },
1812 {
1813 title: "Test variant 2",
1814 prices: [{ currency_code: "usd", amount: 100 }],
1815 options: { size: "l" },
1816 },
1817 ],
1818 }
1819
1820 const creationResponse = await api
1821 .post("/admin/products", payload, adminHeaders)
1822 .catch((err) => {
1823 console.log(err)
1824 })
1825
1826 expect(creationResponse.status).toEqual(200)
1827
1828 const productId = creationResponse.data.product.id
1829
1830 const response = await api
1831 .get(`/admin/products/${productId}`, adminHeaders)
1832 .catch((err) => {
1833 console.log(err)
1834 })
1835
1836 expect(response.data.product).toEqual(
1837 expect.objectContaining({
1838 title: "Test product - 1",
1839 variants: [
1840 expect.objectContaining({
1841 title: "Test variant 1",
1842 }),
1843 expect.objectContaining({
1844 title: "Test variant 2",
1845 }),
1846 ],
1847 })
1848 )
1849 })
1850
1851 it("creates a giftcard", async () => {
1852 const payload = {
1853 title: "Test Giftcard",
1854 is_giftcard: true,
1855 description: "test-giftcard-description",
1856 options: [{ title: "size", values: ["large"] }],
1857 shipping_profile_id: shippingProfile.id,
1858 variants: [
1859 {
1860 title: "Test variant",
1861 prices: [{ currency_code: "usd", amount: 100 }],
1862 options: { size: "large" },
1863 },
1864 ],
1865 }
1866
1867 const response = await api
1868 .post("/admin/products", payload, adminHeaders)
1869 .catch((err) => {
1870 console.log(err)
1871 })
1872
1873 expect(response.status).toEqual(200)
1874
1875 expect(response.data.product).toEqual(
1876 expect.objectContaining({
1877 title: "Test Giftcard",
1878 discountable: false,
1879 })
1880 )
1881 })
1882
1883 it("updates products with shipping profiles", async () => {
1884 const shippingProfile2 = (
1885 await api.post(
1886 `/admin/shipping-profiles`,
1887 { name: "heavy", type: "heavy" },
1888 adminHeaders
1889 )
1890 ).data.shipping_profile
1891
1892 let fetchProduct = await api.get(
1893 `/admin/products/${baseProduct.id}?fields=+shipping_profile.id`,
1894 adminHeaders
1895 )
1896
1897 let payload: Record<string, any> = {
1898 shipping_profile_id: shippingProfile2.id,
1899 }
1900
1901 let response = await api
1902 .post(`/admin/products/${baseProduct.id}`, payload, adminHeaders)
1903 .catch((err) => {
1904 console.log(err)
1905 })
1906
1907 expect(response.status).toEqual(200)
1908
1909 fetchProduct = await api.get(
1910 `/admin/products/${baseProduct.id}?fields=+shipping_profile.id`,
1911 adminHeaders
1912 )
1913
1914 expect(fetchProduct.data.product.shipping_profile.id).toEqual(
1915 shippingProfile2.id
1916 )
1917
1918 payload = {
1919 subtitle: "new subtitle",
1920 }
1921
1922 response = await api
1923 .post(`/admin/products/${baseProduct.id}`, payload, adminHeaders)
1924 .catch((err) => {
1925 console.log(err)
1926 })
1927
1928 expect(response.status).toEqual(200)
1929
1930 fetchProduct = await api.get(
1931 `/admin/products/${baseProduct.id}?fields=+shipping_profile.id`,
1932 adminHeaders
1933 )
1934
1935 expect(fetchProduct.data.product.shipping_profile.id).toEqual(
1936 shippingProfile2.id
1937 )
1938 })
1939
1940 it("updates products relations and attributes", async () => {
1941 const shortsCategory = (
1942 await api.post(
1943 "/admin/product-categories",
1944 { name: "Shorts", is_internal: false, is_active: true },
1945 adminHeaders
1946 )
1947 ).data.product_category
1948
1949 const pantsCategory = (
1950 await api.post(
1951 "/admin/product-categories",
1952 { name: "Pants", is_internal: false, is_active: true },
1953 adminHeaders
1954 )
1955 ).data.product_category
1956
1957 const payload = {
1958 title: "Test an update",
1959 weight: 100,
1960 length: 100,
1961 width: 100,
1962 height: 100,
1963 options: [{ title: "size", values: ["large", "small"] }],
1964 variants: [
1965 {
1966 options: { size: "large" },
1967 title: "New variant",
1968 prices: [
1969 {
1970 currency_code: "usd",
1971 amount: 200,
1972 },
1973 ],
1974 },
1975 ],
1976 }
1977
1978 const createdProduct = (
1979 await api.post("/admin/products", payload, adminHeaders)
1980 ).data.product
1981
1982 let updatedProduct = (
1983 await api.post(
1984 `/admin/products/${createdProduct.id}`,
1985 { weight: 20, length: null },
1986 adminHeaders
1987 )
1988 ).data.product
1989
1990 expect(updatedProduct).toEqual(
1991 expect.objectContaining({
1992 weight: 20,
1993 length: null,
1994 width: 100,
1995 height: 100,
1996 })
1997 )
1998
1999 updatedProduct = (
2000 await api.post(
2001 `/admin/products/${createdProduct.id}?fields=+categories.id`,
2002 { categories: [{ id: pantsCategory.id }] },
2003 adminHeaders
2004 )
2005 ).data.product
2006
2007 expect(updatedProduct).toEqual(
2008 expect.objectContaining({
2009 weight: 20,
2010 length: null,
2011 width: 100,
2012 height: 100,
2013 categories: expect.arrayContaining([
2014 expect.objectContaining({
2015 id: pantsCategory.id,
2016 }),
2017 ]),
2018 })
2019 )
2020
2021 updatedProduct = (
2022 await api.post(
2023 `/admin/products/${createdProduct.id}?fields=+categories.id`,
2024 { weight: null, length: 20, width: 50 },
2025 adminHeaders
2026 )
2027 ).data.product
2028
2029 expect(updatedProduct).toEqual(
2030 expect.objectContaining({
2031 weight: null,
2032 length: 20,
2033 width: 50,
2034 height: 100,
2035 categories: expect.arrayContaining([
2036 expect.objectContaining({
2037 id: pantsCategory.id,
2038 }),
2039 ]),
2040 })
2041 )
2042 })
2043
2044 it("updates a product (update prices, tags, update status, delete collection, delete type, replaces images)", async () => {
2045 const payload = {
2046 collection_id: null,
2047 title: "Test an update",
2048 variants: [
2049 {
2050 id: baseProduct.variants[0].id,
2051 title: "New variant",
2052 barcode: "test-barcode",
2053 ean: "test-ean",
2054 upc: "test-upc",
2055 prices: [
2056 {
2057 currency_code: "usd",
2058 amount: 200,
2059 },
2060 {
2061 currency_code: "eur",
2062 amount: 65,
2063 },
2064 {
2065 currency_code: "dkk",
2066 amount: 50,
2067 },
2068 ],
2069 },
2070 ],
2071 tags: [{ id: baseTag1.id }],
2072 images: [{ url: "test-image-2.png" }],
2073 status: "published",
2074 }
2075
2076 const response = await api
2077 .post(`/admin/products/${baseProduct.id}`, payload, adminHeaders)
2078 .catch((err) => {
2079 console.log(err)
2080 })
2081
2082 expect(response.status).toEqual(200)
2083
2084 expect(response.data.product).toEqual(
2085 expect.objectContaining({
2086 id: baseProduct.id,
2087 title: "Test an update",
2088 created_at: expect.any(String),
2089 description: "test-product-description",
2090 discountable: true,
2091 // TODO: Do an update on the name, see if the handle is constant
2092 handle: "base-product",
2093 images: expect.arrayContaining([
2094 expect.objectContaining({
2095 created_at: expect.any(String),
2096 id: expect.stringMatching(/^img_*/),
2097 updated_at: expect.any(String),
2098 url: "test-image-2.png",
2099 }),
2100 ]),
2101 is_giftcard: false,
2102 options: expect.arrayContaining([
2103 expect.objectContaining({
2104 created_at: expect.any(String),
2105 id: expect.stringMatching(/^opt_*/),
2106 title: "size",
2107 values: expect.arrayContaining([
2108 expect.objectContaining({ value: "large" }),
2109 ]),
2110 updated_at: expect.any(String),
2111 }),
2112 ]),
2113 // // profile_id: expect.stringMatching(/^sp_*/),
2114 status: "published",
2115 tags: expect.arrayContaining([
2116 expect.objectContaining({
2117 created_at: expect.any(String),
2118 // TODO: Check how v1 tags update worked. Is it a full replacement, or something else? Why do we expect tag1 here?
2119 // id: "tag1",
2120 updated_at: expect.any(String),
2121 value: "123",
2122 }),
2123 ]),
2124 // TODO: Decide how to handle the thumbnail on update
2125 // thumbnail: "test-image-2.png",
2126 type: expect.objectContaining({
2127 id: baseType.id,
2128 created_at: expect.any(String),
2129 updated_at: expect.any(String),
2130 value: baseType.value,
2131 }),
2132 type_id: baseType.id,
2133 updated_at: expect.any(String),
2134 variants: expect.arrayContaining([
2135 expect.objectContaining({
2136 allow_backorder: false,
2137 barcode: "test-barcode",
2138 ean: "test-ean",
2139 upc: "test-upc",
2140 created_at: expect.any(String),
2141 id: baseProduct.variants[0].id,
2142 manage_inventory: true,
2143 options: expect.arrayContaining([
2144 expect.objectContaining({
2145 id: expect.stringMatching(/^optval_*/),
2146 value: "large",
2147 }),
2148 ]),
2149 origin_country: null,
2150 prices: expect.arrayContaining([
2151 expect.objectContaining({
2152 amount: 200,
2153 created_at: expect.any(String),
2154 currency_code: "usd",
2155 }),
2156 expect.objectContaining({
2157 amount: 65,
2158 created_at: expect.any(String),
2159 currency_code: "eur",
2160 }),
2161 expect.objectContaining({
2162 amount: 50,
2163 created_at: expect.any(String),
2164 currency_code: "dkk",
2165 }),
2166 ]),
2167 product_id: baseProduct.id,
2168 title: "New variant",
2169 updated_at: expect.any(String),
2170 }),
2171 ]),
2172 })
2173 )
2174 })
2175
2176 it("updates product variants (update price on existing variant, create new variant)", async () => {
2177 const payload = {
2178 shipping_profile_id: shippingProfile.id,
2179 variants: [
2180 {
2181 id: baseProduct.variants[0].id,
2182 prices: [
2183 {
2184 currency_code: "usd",
2185 amount: 200,
2186 },
2187 {
2188 currency_code: "dkk",
2189 amount: 50,
2190 },
2191 ],
2192 },
2193 {
2194 title: "New variant",
2195 prices: [
2196 {
2197 currency_code: "usd",
2198 amount: 150,
2199 },
2200 {
2201 currency_code: "dkk",
2202 amount: 20,
2203 },
2204 ],
2205 },
2206 ],
2207 }
2208
2209 const response = await api
2210 .post(`/admin/products/${baseProduct.id}`, payload, adminHeaders)
2211 .catch((err) => {
2212 console.log(err)
2213 })
2214
2215 expect(response.status).toEqual(200)
2216
2217 expect(response.data.product).toEqual(
2218 expect.objectContaining({
2219 id: baseProduct.id,
2220 variants: expect.arrayContaining([
2221 expect.objectContaining({
2222 id: baseProduct.variants[0].id,
2223 prices: expect.arrayContaining([
2224 expect.objectContaining({
2225 amount: 200,
2226 created_at: expect.any(String),
2227 currency_code: "usd",
2228 }),
2229 expect.objectContaining({
2230 amount: 50,
2231 created_at: expect.any(String),
2232 currency_code: "dkk",
2233 }),
2234 ]),
2235 product_id: baseProduct.id,
2236 }),
2237 expect.objectContaining({
2238 id: expect.any(String),
2239 title: "New variant",
2240 prices: expect.arrayContaining([
2241 expect.objectContaining({
2242 amount: 150,
2243 created_at: expect.any(String),
2244 currency_code: "usd",
2245 }),
2246 expect.objectContaining({
2247 amount: 20,
2248 created_at: expect.any(String),
2249 currency_code: "dkk",
2250 }),
2251 ]),
2252 product_id: baseProduct.id,
2253 }),
2254 ]),
2255 })
2256 )
2257 })
2258
2259 it("updates product (removes images when empty array included)", async () => {
2260 const payload = {
2261 images: [],
2262 }
2263
2264 const response = await api
2265 .post(`/admin/products/${baseProduct.id}`, payload, adminHeaders)
2266 .catch((err) => {
2267 console.log(err)
2268 })
2269
2270 expect(response.status).toEqual(200)
2271 expect(response.data.product.images.length).toEqual(0)
2272 })
2273
2274 it("updating the product without variants keeps the variants and prices intact", async () => {
2275 const payload = {
2276 title: "Test an update",
2277 }
2278
2279 await api
2280 .post(`/admin/products/${baseProduct.id}`, payload, adminHeaders)
2281 .catch((err) => {
2282 console.log(err)
2283 })
2284
2285 const updatedProduct = (
2286 await api.get(`/admin/products/${baseProduct.id}`, adminHeaders)
2287 ).data.product
2288
2289 expect(updatedProduct.variants).toEqual([
2290 expect.objectContaining({
2291 id: baseProduct.variants[0].id,
2292 prices: expect.arrayContaining([
2293 expect.objectContaining({
2294 currency_code: "usd",
2295 amount: 100,
2296 }),
2297 expect.objectContaining({
2298 currency_code: "eur",
2299 amount: 45,
2300 }),
2301 expect.objectContaining({
2302 currency_code: "dkk",
2303 amount: 30,
2304 }),
2305 ]),
2306 }),
2307 ])
2308 })
2309
2310 it("updating the product variants without prices keeps the prices intact", async () => {
2311 const payload = {
2312 title: "Test an update",
2313 variants: baseProduct.variants.map((variant) => ({
2314 id: variant.id,
2315 title: variant.id,
2316 })),
2317 }
2318
2319 await api
2320 .post(`/admin/products/${baseProduct.id}`, payload, adminHeaders)
2321 .catch((err) => {
2322 console.log(err)
2323 })
2324
2325 const updatedProduct = (
2326 await api.get(`/admin/products/${baseProduct.id}`, adminHeaders)
2327 ).data.product
2328
2329 expect(updatedProduct.variants).toEqual([
2330 expect.objectContaining({
2331 id: baseProduct.variants[0].id,
2332 title: baseProduct.variants[0].id,
2333 prices: expect.arrayContaining([
2334 expect.objectContaining({
2335 currency_code: "usd",
2336 amount: 100,
2337 }),
2338 expect.objectContaining({
2339 currency_code: "eur",
2340 amount: 45,
2341 }),
2342 expect.objectContaining({
2343 currency_code: "dkk",
2344 amount: 30,
2345 }),
2346 ]),
2347 }),
2348 ])
2349 })
2350
2351 it("updates a product by deleting a field from metadata", async () => {
2352 const created = (
2353 await api.post(
2354 "/admin/products",
2355 getProductFixture({
2356 title: "Test metadata",
2357 shipping_profile_id: shippingProfile.id,
2358 metadata: {
2359 "test-key": "test-value",
2360 "test-key-2": "test-value-2",
2361 "test-key-3": "test-value-3",
2362 },
2363 }),
2364 adminHeaders
2365 )
2366 ).data.product
2367
2368 const payload = {
2369 metadata: {
2370 "test-key": "", // item is deleted by setting to empty string
2371 "test-key-2": null,
2372 },
2373 }
2374
2375 const response = await api.post(
2376 "/admin/products/" + created.id,
2377 payload,
2378 adminHeaders
2379 )
2380
2381 expect(response.status).toEqual(200)
2382 expect(response.data.product.metadata).toEqual({
2383 // "test-key" is deleted
2384 "test-key-2": null, // updated
2385 "test-key-3": "test-value-3", // preserved
2386 })
2387 })
2388
2389 it("should recreate a variant with the same sku after deletion", async () => {
2390 const payload = {
2391 title: "Test sku",
2392 shipping_profile_id: shippingProfile.id,
2393 variants: [
2394 {
2395 sku: "test-sku-should-persist",
2396 manage_inventory: true,
2397 title: "Recreate variant",
2398 prices: [
2399 {
2400 currency_code: "usd",
2401 amount: 100,
2402 },
2403 ],
2404 },
2405 ],
2406 }
2407
2408 const created = (
2409 await api.post(
2410 "/admin/products?fields=*variants.inventory_items.inventory",
2411 getProductFixture(payload),
2412 adminHeaders
2413 )
2414 ).data.product
2415
2416 const createdVariant = created.variants.find(
2417 (v) => v.sku === "test-sku-should-persist"
2418 )
2419
2420 const inventoryItem = createdVariant.inventory_items[0]
2421
2422 expect(created).toEqual(
2423 expect.objectContaining({
2424 variants: expect.arrayContaining([
2425 expect.objectContaining({
2426 sku: "test-sku-should-persist",
2427 inventory_items: expect.arrayContaining([
2428 expect.objectContaining({
2429 inventory: expect.objectContaining({
2430 sku: "test-sku-should-persist",
2431 }),
2432 }),
2433 ]),
2434 title: "Recreate variant",
2435 }),
2436 ]),
2437 })
2438 )
2439
2440 await api.delete(
2441 `/admin/products/${created.id}/variants/${createdVariant.id}`,
2442 adminHeaders
2443 )
2444
2445 const recreated = (
2446 await api.post(
2447 `/admin/products/${created.id}/variants?fields=+variants.inventory_items.inventory.sku`,
2448 {
2449 sku: "test-sku-should-persist",
2450 manage_inventory: true,
2451 title: "Recreate variant",
2452 prices: [
2453 {
2454 currency_code: "usd",
2455 amount: 100,
2456 },
2457 ],
2458 },
2459 adminHeaders
2460 )
2461 ).data.product
2462
2463 expect(recreated).toEqual(
2464 expect.objectContaining({
2465 variants: expect.arrayContaining([
2466 expect.objectContaining({
2467 sku: "test-sku-should-persist",
2468 inventory_items: expect.arrayContaining([
2469 expect.objectContaining({
2470 inventory: expect.objectContaining({
2471 sku: "test-sku-should-persist",
2472 }),
2473 }),
2474 ]),
2475 title: "Recreate variant",
2476 }),
2477 ]),
2478 })
2479 )
2480 })
2481
2482 it("should update variant to manage_inventory false and unlink inventory items", async () => {
2483 const inventoryItem1 = (
2484 await api.post(
2485 `/admin/inventory-items`,
2486 { sku: "inventory-item-1" },
2487 adminHeaders
2488 )
2489 ).data.inventory_item
2490
2491 const inventoryItem2 = (
2492 await api.post(
2493 `/admin/inventory-items`,
2494 { sku: "inventory-item-2" },
2495 adminHeaders
2496 )
2497 ).data.inventory_item
2498
2499 const createPayload = {
2500 title: "Test product with inventory",
2501 handle: "test-product-inventory",
2502 options: [{ title: "size", values: ["large"] }],
2503 shipping_profile_id: shippingProfile.id,
2504 variants: [
2505 {
2506 title: "Variant with inventory",
2507 prices: [{ currency_code: "usd", amount: 100 }],
2508 manage_inventory: true,
2509 options: { size: "large" },
2510 inventory_items: [
2511 {
2512 inventory_item_id: inventoryItem1.id,
2513 required_quantity: 5,
2514 },
2515 {
2516 inventory_item_id: inventoryItem2.id,
2517 required_quantity: 10,
2518 },
2519 ],
2520 },
2521 ],
2522 }
2523
2524 const createdProduct = (
2525 await api.post("/admin/products", createPayload, {
2526 ...adminHeaders,
2527 params: {
2528 fields:
2529 "variants.inventory_items.*,variants.inventory_items.inventory.*",
2530 },
2531 })
2532 ).data.product
2533
2534 const variantWithInventory = createdProduct.variants[0]
2535
2536 expect(variantWithInventory.manage_inventory).toBe(true)
2537 expect(variantWithInventory.inventory_items).toHaveLength(2)
2538 expect(variantWithInventory.inventory_items).toEqual(
2539 expect.arrayContaining([
2540 expect.objectContaining({
2541 inventory_item_id: inventoryItem1.id,
2542 required_quantity: 5,
2543 }),
2544 expect.objectContaining({
2545 inventory_item_id: inventoryItem2.id,
2546 required_quantity: 10,
2547 }),
2548 ])
2549 )
2550
2551 const updatePayload = {
2552 variants: [
2553 {
2554 id: variantWithInventory.id,
2555 manage_inventory: false,
2556 },
2557 ],
2558 }
2559
2560 const updatedProduct = (
2561 await api.post(
2562 `/admin/products/${createdProduct.id}`,
2563 updatePayload,
2564 {
2565 ...adminHeaders,
2566 params: {
2567 fields:
2568 "variants.inventory_items.*,variants.inventory_items.inventory.*",
2569 },
2570 }
2571 )
2572 ).data.product
2573
2574 const updatedVariant = updatedProduct.variants.find(
2575 (v) => v.id === variantWithInventory.id
2576 )
2577
2578 expect(updatedVariant.manage_inventory).toBe(false)
2579 expect(updatedVariant.inventory_items).toHaveLength(0)
2580 expect(updatedVariant.inventory_items).toEqual([])
2581
2582 const inventoryItem1Response = await api.get(
2583 `/admin/inventory-items/${inventoryItem1.id}`,
2584 adminHeaders
2585 )
2586 const inventoryItem2Response = await api.get(
2587 `/admin/inventory-items/${inventoryItem2.id}`,
2588 adminHeaders
2589 )
2590
2591 expect(inventoryItem1Response.status).toEqual(200)
2592 expect(inventoryItem2Response.status).toEqual(200)
2593 expect(inventoryItem1Response.data.inventory_item.id).toBe(
2594 inventoryItem1.id
2595 )
2596 expect(inventoryItem2Response.data.inventory_item.id).toBe(
2597 inventoryItem2.id
2598 )
2599 })
2600
2601 it("updates products sales channels", async () => {
2602 const salesChannel1 = (
2603 await api.post(
2604 "/admin/sales-channels",
2605 {
2606 name: "test name 1",
2607 description: "test description",
2608 },
2609 adminHeaders
2610 )
2611 ).data.sales_channel
2612
2613 const salesChannel2 = (
2614 await api.post(
2615 "/admin/sales-channels",
2616 {
2617 name: "test name 2",
2618 description: "test description",
2619 },
2620 adminHeaders
2621 )
2622 ).data.sales_channel
2623
2624 const newProduct = (
2625 await api.post(
2626 "/admin/products",
2627 getProductFixture({
2628 title: "Test saleschannel",
2629 sales_channels: [{ id: salesChannel1.id }],
2630 shipping_profile_id: shippingProfile.id,
2631 }),
2632 adminHeaders
2633 )
2634 ).data.product
2635
2636 await api.post(
2637 `/admin/products/${newProduct.id}`,
2638 {
2639 title: "new name",
2640 sales_channels: [
2641 { id: salesChannel1.id },
2642 { id: salesChannel2.id },
2643 ],
2644 },
2645 adminHeaders
2646 )
2647
2648 let res = await api.get(
2649 `/admin/products/${newProduct.id}?fields=*sales_channels`,
2650 adminHeaders
2651 )
2652
2653 expect(res.status).toEqual(200)
2654 expect(res.data.product).toEqual(
2655 expect.objectContaining({
2656 id: newProduct.id,
2657 title: "new name",
2658 sales_channels: expect.arrayContaining([
2659 expect.objectContaining({
2660 id: salesChannel1.id,
2661 name: "test name 1",
2662 }),
2663 expect.objectContaining({
2664 id: salesChannel2.id,
2665 name: "test name 2",
2666 }),
2667 ]),
2668 })
2669 )
2670
2671 await api.post(
2672 `/admin/products/${newProduct.id}`,
2673 {
2674 title: "new name 2",
2675 sales_channels: [{ id: salesChannel2.id }], // update channels again to remove the first one
2676 },
2677 adminHeaders
2678 )
2679
2680 res = await api.get(
2681 `/admin/products/${newProduct.id}?fields=*sales_channels`,
2682 adminHeaders
2683 )
2684
2685 expect(res.status).toEqual(200)
2686 expect(res.data.product).toEqual(
2687 expect.objectContaining({
2688 id: newProduct.id,
2689 title: "new name 2",
2690 sales_channels: expect.arrayContaining([
2691 expect.objectContaining({
2692 id: salesChannel2.id,
2693 name: "test name 2",
2694 }),
2695 ]),
2696 })
2697 )
2698 })
2699
2700 it("updates multiple products that have the same sales channel", async () => {
2701 const salesChannel = (
2702 await api.post(
2703 "/admin/sales-channels",
2704 {
2705 name: "Sales",
2706 },
2707 adminHeaders
2708 )
2709 ).data.sales_channel
2710
2711 await api.post(
2712 `/admin/products/${baseProduct.id}`,
2713 {
2714 sales_channels: [{ id: salesChannel.id }],
2715 },
2716 adminHeaders
2717 )
2718 await api.post(
2719 `/admin/products/${proposedProduct.id}`,
2720 {
2721 sales_channels: [{ id: salesChannel.id }],
2722 },
2723 adminHeaders
2724 )
2725
2726 let res = await api.get(
2727 `/admin/products?fields=*sales_channels&sales_channel_id[]=${salesChannel.id}`,
2728 adminHeaders
2729 )
2730
2731 expect(res.status).toEqual(200)
2732 expect(res.data.products).toEqual([
2733 expect.objectContaining({
2734 id: baseProduct.id,
2735 sales_channels: expect.arrayContaining([
2736 expect.objectContaining({
2737 id: salesChannel.id,
2738 }),
2739 ]),
2740 }),
2741 expect.objectContaining({
2742 id: proposedProduct.id,
2743 sales_channels: expect.arrayContaining([
2744 expect.objectContaining({
2745 id: salesChannel.id,
2746 }),
2747 ]),
2748 }),
2749 ])
2750
2751 await api.post(
2752 `/admin/products/${proposedProduct.id}`,
2753 {
2754 sales_channels: [],
2755 },
2756 adminHeaders
2757 )
2758
2759 res = await api.get(
2760 `/admin/products?fields=*sales_channels&sales_channel_id[]=${salesChannel.id}`,
2761 adminHeaders
2762 )
2763
2764 expect(res.status).toEqual(200)
2765 expect(res.data.products).toEqual([
2766 expect.objectContaining({
2767 id: baseProduct.id,
2768 sales_channels: expect.arrayContaining([
2769 expect.objectContaining({
2770 id: salesChannel.id,
2771 }),
2772 ]),
2773 }),
2774 ])
2775 })
2776
2777 it("fails to update product with invalid status", async () => {
2778 const payload = {
2779 status: null,
2780 }
2781
2782 try {
2783 await api.post(
2784 `/admin/products/${baseProduct}`,
2785 payload,
2786 adminHeaders
2787 )
2788 } catch (e) {
2789 expect(e.response.status).toEqual(400)
2790 expect(e.response.data.type).toEqual("invalid_data")
2791 }
2792 })
2793
2794 // TODO: Apply variant ranking correctly
2795 it.skip("updates a product (variant ordering)", async () => {
2796 const plainProduct = (
2797 await api.post(
2798 "/admin/products",
2799 {
2800 title: "Test variant order",
2801 shipping_profile_id: shippingProfile.id,
2802 },
2803 adminHeaders
2804 )
2805 ).data.product
2806
2807 const payload = {
2808 variants: [
2809 {
2810 title: "first",
2811 },
2812 {
2813 title: "second",
2814 },
2815 {
2816 title: "third",
2817 },
2818 ],
2819 }
2820
2821 const response = await api
2822 .post(`/admin/products/${plainProduct.id}`, payload, adminHeaders)
2823 .catch((err) => {
2824 console.log(err)
2825 })
2826
2827 expect(response.status).toEqual(200)
2828
2829 expect(response.data.product).toEqual(
2830 expect.objectContaining({
2831 title: plainProduct.title,
2832 variants: [
2833 expect.objectContaining({
2834 title: "first",
2835 }),
2836 expect.objectContaining({
2837 title: "second",
2838 }),
2839 expect.objectContaining({
2840 title: "third",
2841 }),
2842 ],
2843 })
2844 )
2845 })
2846
2847 it("creates product with variant inventory kits", async () => {
2848 const inventoryItem1 = (
2849 await api.post(
2850 `/admin/inventory-items`,
2851 { sku: "inventory-1" },
2852 adminHeaders
2853 )
2854 ).data.inventory_item
2855
2856 const inventoryItem2 = (
2857 await api.post(
2858 `/admin/inventory-items`,
2859 { sku: "inventory-2" },
2860 adminHeaders
2861 )
2862 ).data.inventory_item
2863
2864 const inventoryItem3 = (
2865 await api.post(
2866 `/admin/inventory-items`,
2867 { sku: "inventory-3" },
2868 adminHeaders
2869 )
2870 ).data.inventory_item
2871
2872 const payload = {
2873 title: "Test product - 1",
2874 handle: "test-1",
2875 options: [{ title: "size", values: ["x", "l"] }],
2876 shipping_profile_id: shippingProfile.id,
2877 variants: [
2878 {
2879 title: "Custom inventory 1",
2880 prices: [{ currency_code: "usd", amount: 100 }],
2881 manage_inventory: true,
2882 options: { size: "l" },
2883 inventory_items: [
2884 {
2885 inventory_item_id: inventoryItem1.id,
2886 required_quantity: 4,
2887 },
2888 ],
2889 },
2890 {
2891 title: "No inventory",
2892 prices: [{ currency_code: "usd", amount: 100 }],
2893 manage_inventory: false,
2894 },
2895 {
2896 title: "Default Inventory",
2897 prices: [{ currency_code: "usd", amount: 100 }],
2898 manage_inventory: true,
2899 },
2900 {
2901 title: "Custom inventory 2",
2902 prices: [{ currency_code: "usd", amount: 100 }],
2903 manage_inventory: true,
2904 inventory_items: [
2905 {
2906 inventory_item_id: inventoryItem2.id,
2907 required_quantity: 5,
2908 },
2909 {
2910 inventory_item_id: inventoryItem3.id,
2911 required_quantity: 6,
2912 },
2913 ],
2914 },
2915 ],
2916 }
2917
2918 const response = await api
2919 .post(
2920 "/admin/products?fields=%2bvariants.inventory_items.inventory.*,%2bvariants.inventory_items.*",
2921 payload,
2922 adminHeaders
2923 )
2924 .catch((err) => {
2925 console.log(err)
2926 })
2927
2928 expect(response.status).toEqual(200)
2929 expect(response.data.product).toEqual(
2930 expect.objectContaining({
2931 title: "Test product - 1",
2932 variants: expect.arrayContaining([
2933 expect.objectContaining({
2934 title: "Custom inventory 1",
2935 manage_inventory: true,
2936 inventory_items: [
2937 expect.objectContaining({
2938 required_quantity: 4,
2939 inventory_item_id: inventoryItem1.id,
2940 }),
2941 ],
2942 }),
2943 expect.objectContaining({
2944 title: "No inventory",
2945 manage_inventory: false,
2946 inventory_items: [],
2947 }),
2948 expect.objectContaining({
2949 title: "Default Inventory",
2950 manage_inventory: true,
2951 inventory_items: [
2952 expect.objectContaining({
2953 required_quantity: 1,
2954 inventory_item_id: expect.any(String),
2955 }),
2956 ],
2957 }),
2958 expect.objectContaining({
2959 title: "Custom inventory 2",
2960 manage_inventory: true,
2961 inventory_items: expect.arrayContaining([
2962 expect.objectContaining({
2963 required_quantity: 5,
2964 inventory_item_id: inventoryItem2.id,
2965 }),
2966 expect.objectContaining({
2967 required_quantity: 6,
2968 inventory_item_id: inventoryItem3.id,
2969 }),
2970 ]),
2971 }),
2972 ]),
2973 })
2974 )
2975 })
2976
2977 it("creates throw when duplicated inventory items", async () => {
2978 const inventoryItem1 = (
2979 await api.post(
2980 `/admin/inventory-items`,
2981 { sku: "inventory-1" },
2982 adminHeaders
2983 )
2984 ).data.inventory_item
2985
2986 const payload = {
2987 title: "Test product - 1",
2988 handle: "test-1",
2989 options: [{ title: "size", values: ["x", "l"] }],
2990 shipping_profile_id: shippingProfile.id,
2991 variants: [
2992 {
2993 title: "Custom inventory 1",
2994 prices: [{ currency_code: "usd", amount: 100 }],
2995 options: { size: "l" },
2996 manage_inventory: true,
2997 inventory_items: [
2998 {
2999 inventory_item_id: inventoryItem1.id,
3000 required_quantity: 4,
3001 },
3002 {
3003 inventory_item_id: inventoryItem1.id,
3004 required_quantity: 2,
3005 },
3006 ],
3007 },
3008 {
3009 title: "No inventory",
3010 prices: [{ currency_code: "usd", amount: 100 }],
3011 manage_inventory: false,
3012 },
3013 {
3014 title: "Default Inventory",
3015 prices: [{ currency_code: "usd", amount: 100 }],
3016 manage_inventory: true,
3017 },
3018 ],
3019 }
3020
3021 const error = await api
3022 .post(
3023 "/admin/products?fields=%2bvariants.inventory_items.inventory.*,%2bvariants.inventory_items.*",
3024 payload,
3025 adminHeaders
3026 )
3027 .catch((err) => err)
3028
3029 expect(error.response.status).toEqual(400)
3030 expect(error.response.data.message).toMatch(
3031 new RegExp(
3032 "Cannot associate duplicate inventory items to variant\\(s\\) \\w+"
3033 )
3034 )
3035 })
3036
3037 it("should throw an error when inventory item does not exist", async () => {
3038 const payload = {
3039 title: "Test product - 1",
3040 handle: "test-1",
3041 options: [{ title: "size", values: ["x", "l"] }],
3042 shipping_profile_id: shippingProfile.id,
3043 variants: [
3044 {
3045 title: "Custom inventory 1",
3046 prices: [{ currency_code: "usd", amount: 100 }],
3047 options: { size: "l" },
3048 manage_inventory: true,
3049 inventory_items: [
3050 {
3051 inventory_item_id: "does-not-exist",
3052 required_quantity: 4,
3053 },
3054 ],
3055 },
3056 ],
3057 }
3058
3059 const error = await api
3060 .post("/admin/products", payload, adminHeaders)
3061 .catch((err) => err)
3062
3063 expect(error.response.status).toEqual(400)
3064 expect(error.response.data).toEqual(
3065 expect.objectContaining({
3066 type: "invalid_data",
3067 message: "Inventory Items with ids: does-not-exist was not found",
3068 })
3069 )
3070 })
3071 })
3072
3073 describe("POST /admin/products/:id/options/batch", () => {
3074 let colorOption
3075 let sizeOption
3076
3077 beforeEach(async () => {
3078 colorOption = (
3079 await api.post(
3080 "/admin/product-options",
3081 { title: "Color", values: ["Red", "Blue"] },
3082 adminHeaders
3083 )
3084 ).data.product_option
3085
3086 sizeOption = (
3087 await api.post(
3088 "/admin/product-options",
3089 { title: "Size", values: ["L", "M"] },
3090 adminHeaders
3091 )
3092 ).data.product_option
3093 })
3094
3095 it("should link existing options to product", async () => {
3096 const payload = {
3097 add: [colorOption.id, sizeOption.id],
3098 }
3099
3100 const response = await api.post(
3101 `/admin/products/${baseProduct.id}/options/batch`,
3102 payload,
3103 adminHeaders
3104 )
3105
3106 expect(response.status).toEqual(200)
3107 expect(response.data.product.options.length).toEqual(4) // 2 new ones and 2 it already had
3108 expect(response.data.product.options).toEqual(
3109 expect.arrayContaining([
3110 expect.objectContaining({
3111 id: baseProduct.options[0].id,
3112 }),
3113 expect.objectContaining({
3114 id: baseProduct.options[1].id,
3115 }),
3116 expect.objectContaining({
3117 id: colorOption.id,
3118 }),
3119 expect.objectContaining({
3120 id: sizeOption.id,
3121 }),
3122 ])
3123 )
3124 })
3125
3126 it("should unlink existing options from product", async () => {
3127 let response = await api.post(
3128 `/admin/products/${baseProduct.id}/options/batch`,
3129 {
3130 add: [colorOption.id, sizeOption.id],
3131 },
3132 adminHeaders
3133 )
3134
3135 expect(response.status).toEqual(200)
3136 expect(response.data.product.options.length).toEqual(4) // 2 new ones and 2 it already had
3137
3138 const payload = {
3139 remove: [colorOption.id, sizeOption.id],
3140 }
3141
3142 response = await api.post(
3143 `/admin/products/${baseProduct.id}/options/batch`,
3144 payload,
3145 adminHeaders
3146 )
3147
3148 expect(response.status).toEqual(200)
3149 expect(response.data.product.options.length).toEqual(2)
3150 })
3151
3152 it("should link a new options to product", async () => {
3153 const payload = {
3154 add: [
3155 colorOption.id,
3156 sizeOption.id,
3157 { title: "new", values: ["A", "B"] },
3158 ],
3159 }
3160
3161 const response = await api.post(
3162 `/admin/products/${baseProduct.id}/options/batch`,
3163 payload,
3164 adminHeaders
3165 )
3166
3167 expect(response.status).toEqual(200)
3168 expect(response.data.product.options.length).toEqual(5) // 3 new ones and 2 it already had
3169 expect(response.data.product.options).toEqual(
3170 expect.arrayContaining([
3171 expect.objectContaining({
3172 id: colorOption.id,
3173 }),
3174 expect.objectContaining({
3175 id: sizeOption.id,
3176 }),
3177 expect.objectContaining({
3178 title: "new",
3179 }),
3180 expect.objectContaining({
3181 id: baseProduct.options[0].id,
3182 }),
3183 expect.objectContaining({
3184 id: baseProduct.options[1].id,
3185 }),
3186 ])
3187 )
3188 })
3189 })
3190
3191 describe("testing for soft-deletion + uniqueness on handles, collection and variant properties", () => {
3192 it("successfully deletes a product", async () => {
3193 const response = await api
3194 .delete(`/admin/products/${baseProduct.id}`, adminHeaders)
3195 .catch((err) => {
3196 console.log(err)
3197 })
3198
3199 expect(response.status).toEqual(200)
3200
3201 expect(response.data).toEqual(
3202 expect.objectContaining({
3203 id: baseProduct.id,
3204 deleted: true,
3205 })
3206 )
3207 })
3208
3209 it("successfully deletes a product and variants", async () => {
3210 const variantPre = (
3211 await api.get(
3212 `/admin/products/${baseProduct.id}/variants/${baseProduct.variants[0].id}`,
3213 adminHeaders
3214 )
3215 ).data.variant
3216
3217 expect(variantPre).toBeTruthy()
3218
3219 const response = await api
3220 .delete(`/admin/products/${baseProduct.id}`, adminHeaders)
3221 .catch((err) => {
3222 console.log(err)
3223 })
3224
3225 expect(response.status).toEqual(200)
3226
3227 expect(response.data).toEqual(
3228 expect.objectContaining({
3229 id: baseProduct.id,
3230 deleted: true,
3231 })
3232 )
3233 const variantPost = (
3234 await api.get(
3235 `/admin/products/${baseProduct.id}/variants/${baseProduct.variants[0].id}`,
3236 adminHeaders
3237 )
3238 ).data.variant
3239
3240 expect(variantPost).not.toBeTruthy()
3241 })
3242
3243 it("deletes product and inventory items that are only associated with that product's variants", async () => {
3244 const stockLocation = (
3245 await api.post(
3246 `/admin/stock-locations`,
3247 { name: "loc" },
3248 adminHeaders
3249 )
3250 ).data.stock_location
3251
3252 const inventoryItem1 = (
3253 await api.post(
3254 `/admin/inventory-items`,
3255 { sku: "inventory-1" },
3256 adminHeaders
3257 )
3258 ).data.inventory_item
3259
3260 const inventoryItem2 = (
3261 await api.post(
3262 `/admin/inventory-items`,
3263 { sku: "inventory-2-reused-across-products" },
3264 adminHeaders
3265 )
3266 ).data.inventory_item
3267
3268 await api.post(
3269 `/admin/inventory-items/${inventoryItem1.id}/location-levels`,
3270 {
3271 location_id: stockLocation.id,
3272 stocked_quantity: 8,
3273 },
3274 adminHeaders
3275 )
3276
3277 await api.post(
3278 `/admin/inventory-items/${inventoryItem2.id}/location-levels`,
3279 {
3280 location_id: stockLocation.id,
3281 stocked_quantity: 4,
3282 },
3283 adminHeaders
3284 )
3285
3286 const productWithInventoryItems = (
3287 await api.post(
3288 `/admin/products`,
3289 {
3290 title: "Test product - 1",
3291 handle: "test-1",
3292 options: [{ title: "size", values: ["l"] }],
3293 shipping_profile_id: shippingProfile.id,
3294 variants: [
3295 {
3296 title: "Custom inventory 1",
3297 prices: [{ currency_code: "usd", amount: 100 }],
3298 manage_inventory: true,
3299 options: { size: "l" },
3300 inventory_items: [
3301 {
3302 inventory_item_id: inventoryItem1.id,
3303 required_quantity: 4,
3304 },
3305 {
3306 inventory_item_id: inventoryItem2.id,
3307 required_quantity: 2,
3308 },
3309 ],
3310 },
3311 ],
3312 },
3313 adminHeaders
3314 )
3315 ).data.product
3316
3317 // Another product that shares inventory item in the inventory kit
3318 await api.post(
3319 `/admin/products`,
3320 {
3321 title: "Test product - 2",
3322 handle: "test-2",
3323 options: [{ title: "size", values: ["l"] }],
3324 shipping_profile_id: shippingProfile.id,
3325 variants: [
3326 {
3327 title: "W/ shared inventory item",
3328 prices: [{ currency_code: "usd", amount: 100 }],
3329 manage_inventory: true,
3330 options: { size: "l" },
3331 inventory_items: [
3332 {
3333 inventory_item_id: inventoryItem2.id,
3334 required_quantity: 2,
3335 },
3336 ],
3337 },
3338 ],
3339 },
3340 adminHeaders
3341 )
3342
3343 const response = await api
3344 .delete(
3345 `/admin/products/${productWithInventoryItems.id}`,
3346 adminHeaders
3347 )
3348 .catch((err) => {
3349 console.log(err)
3350 })
3351
3352 expect(response.status).toEqual(200)
3353 expect(response.data).toEqual(
3354 expect.objectContaining({ deleted: true })
3355 )
3356
3357 const item1Response = await api
3358 .get(`/admin/inventory-items/${inventoryItem1.id}`, adminHeaders)
3359 .catch((err) => {
3360 return err.response
3361 })
3362
3363 const item2Response = await api
3364 .get(`/admin/inventory-items/${inventoryItem2.id}`, adminHeaders)
3365 .catch((err) => {
3366 console.log(err)
3367 })
3368
3369 expect(item1Response.status).toEqual(404) // deleted since it's used only by the deleted product
3370 expect(item2Response.status).toEqual(200) // not deleted since it belongs to other products
3371 expect(item2Response.data.inventory_item).toEqual(
3372 expect.objectContaining({ id: inventoryItem2.id })
3373 )
3374 })
3375
3376 it("successfully deletes a product when a linked inventory item was already deleted", async () => {
3377 const stockLocation = (
3378 await api.post(
3379 `/admin/stock-locations`,
3380 { name: "loc" },
3381 adminHeaders
3382 )
3383 ).data.stock_location
3384
3385 const inventoryItem1 = (
3386 await api.post(
3387 `/admin/inventory-items`,
3388 { sku: "inventory-orphan-1" },
3389 adminHeaders
3390 )
3391 ).data.inventory_item
3392
3393 const inventoryItem2 = (
3394 await api.post(
3395 `/admin/inventory-items`,
3396 { sku: "inventory-orphan-2" },
3397 adminHeaders
3398 )
3399 ).data.inventory_item
3400
3401 await api.post(
3402 `/admin/inventory-items/${inventoryItem1.id}/location-levels`,
3403 {
3404 location_id: stockLocation.id,
3405 stocked_quantity: 10,
3406 },
3407 adminHeaders
3408 )
3409
3410 await api.post(
3411 `/admin/inventory-items/${inventoryItem2.id}/location-levels`,
3412 {
3413 location_id: stockLocation.id,
3414 stocked_quantity: 5,
3415 },
3416 adminHeaders
3417 )
3418
3419 const productWithInventory = (
3420 await api.post(
3421 `/admin/products`,
3422 {
3423 title: "Product with orphan link",
3424 handle: "product-orphan-link",
3425 options: [{ title: "size", values: ["m"] }],
3426 shipping_profile_id: shippingProfile.id,
3427 variants: [
3428 {
3429 title: "Variant with orphan inventory link",
3430 prices: [{ currency_code: "usd", amount: 100 }],
3431 manage_inventory: true,
3432 options: { size: "m" },
3433 inventory_items: [
3434 {
3435 inventory_item_id: inventoryItem1.id,
3436 required_quantity: 1,
3437 },
3438 {
3439 inventory_item_id: inventoryItem2.id,
3440 required_quantity: 1,
3441 },
3442 ],
3443 },
3444 ],
3445 },
3446 adminHeaders
3447 )
3448 ).data.product
3449
3450 // Delete one inventory item directly via the module service,
3451 // bypassing the workflow that would clean up the link.
3452 // This creates an orphan link (variant still references a
3453 // deleted inventory item).
3454 const inventoryModule = getContainer().resolve(Modules.INVENTORY)
3455 await inventoryModule.deleteInventoryItems(inventoryItem1.id)
3456
3457 // Deleting the product should succeed despite the orphan link
3458 const response = await api.delete(
3459 `/admin/products/${productWithInventory.id}`,
3460 adminHeaders
3461 )
3462
3463 expect(response.status).toEqual(200)
3464 expect(response.data).toEqual(
3465 expect.objectContaining({ deleted: true })
3466 )
3467
3468 // The remaining inventory item should also be deleted since it
3469 // was only associated with this product's variant
3470 const item2Response = await api
3471 .get(`/admin/inventory-items/${inventoryItem2.id}`, adminHeaders)
3472 .catch((err) => err.response)
3473
3474 expect(item2Response.status).toEqual(404)
3475 })
3476
3477 it("should throw if product that has a reservation is being deleted", async () => {
3478 const stockLocation = (
3479 await api.post(
3480 `/admin/stock-locations`,
3481 { name: "loc" },
3482 adminHeaders
3483 )
3484 ).data.stock_location
3485
3486 const inventoryItem1 = (
3487 await api.post(
3488 `/admin/inventory-items`,
3489 { sku: "inventory-1" },
3490 adminHeaders
3491 )
3492 ).data.inventory_item
3493
3494 await api.post(
3495 `/admin/inventory-items/${inventoryItem1.id}/location-levels`,
3496 {
3497 location_id: stockLocation.id,
3498 stocked_quantity: 8,
3499 },
3500 adminHeaders
3501 )
3502
3503 const productWithInventoryItems = (
3504 await api.post(
3505 `/admin/products`,
3506 {
3507 title: "Test product - 1",
3508 handle: "test-1",
3509 options: [{ title: "size", values: ["l"] }],
3510 shipping_profile_id: shippingProfile.id,
3511 variants: [
3512 {
3513 title: "Custom inventory 1",
3514 prices: [{ currency_code: "usd", amount: 100 }],
3515 manage_inventory: true,
3516 options: { size: "l" },
3517 inventory_items: [
3518 {
3519 inventory_item_id: inventoryItem1.id,
3520 required_quantity: 4,
3521 },
3522 ],
3523 },
3524 ],
3525 },
3526 adminHeaders
3527 )
3528 ).data.product
3529
3530 const reservation = (
3531 await api.post(
3532 `/admin/reservations`,
3533 {
3534 line_item_id: "line-item-id-1",
3535 inventory_item_id: inventoryItem1.id,
3536 location_id: stockLocation.id,
3537 description: "test description",
3538 quantity: 1,
3539 },
3540 adminHeaders
3541 )
3542 ).data.reservation
3543
3544 const response = await api
3545 .delete(
3546 `/admin/products/${productWithInventoryItems.id}`,
3547 adminHeaders
3548 )
3549 .catch((err) => {
3550 return err.response
3551 })
3552
3553 expect(response.status).toEqual(400)
3554 expect(response.data.message).toEqual(
3555 `Cannot remove following inventory item(s) since they have reservations: [${inventoryItem1.id}].`
3556 )
3557 })
3558
3559 // TODO: Enable with http calls
3560 it.skip("successfully deletes a product variant and its associated prices", async () => {
3561 // // Validate that the price exists
3562 // const pricePre = await dbConnection.manager.findOne(MoneyAmount, {
3563 // where: { id: "test-price" },
3564 // })
3565 // expect(pricePre).toBeTruthy()
3566 // // Soft delete the variant
3567 // const response = await api.delete(
3568 // "/admin/products/test-product/variants/test-variant",
3569 // adminHeaders
3570 // )
3571 // expect(response.status).toEqual(200)
3572 // // Validate that the price was deleted
3573 // const pricePost = await dbConnection.manager.findOne(MoneyAmount, {
3574 // where: { id: "test-price" },
3575 // })
3576 // expect(pricePost).not.toBeTruthy()
3577 // // Validate that the price still exists in the DB with deleted_at
3578 // const optValDeleted = await dbConnection.manager.findOne(
3579 // MoneyAmount,
3580 // {
3581 // where: {
3582 // id: "test-price",
3583 // },
3584 // withDeleted: true,
3585 // }
3586 // )
3587 // expect(optValDeleted).toEqual(
3588 // expect.objectContaining({
3589 // deleted_at: expect.any(Date),
3590 // id: "test-price",
3591 // })
3592 // )
3593 })
3594
3595 // TODO: Enable with http calls
3596 it.skip("successfully deletes a product and any prices associated with one of its variants", async () => {
3597 // // Validate that the price exists
3598 // const pricePre = await dbConnection.manager.findOne(MoneyAmount, {
3599 // where: { id: "test-price" },
3600 // })
3601 // expect(pricePre).toBeTruthy()
3602 // // Soft delete the product
3603 // const response = await api.delete(
3604 // "/admin/products/test-product",
3605 // adminHeaders
3606 // )
3607 // expect(response.status).toEqual(200)
3608 // // Validate that the price has been deleted
3609 // const pricePost = await dbConnection.manager.findOne(MoneyAmount, {
3610 // where: { id: "test-price" },
3611 // })
3612 // expect(pricePost).not.toBeTruthy()
3613 // // Validate that the price still exists in the DB with deleted_at
3614 // const optValDeleted = await dbConnection.manager.findOne(
3615 // MoneyAmount,
3616 // {
3617 // where: {
3618 // id: "test-price",
3619 // },
3620 // withDeleted: true,
3621 // }
3622 // )
3623 // expect(optValDeleted).toEqual(
3624 // expect.objectContaining({
3625 // deleted_at: expect.any(Date),
3626 // id: "test-price",
3627 // })
3628 // )
3629 })
3630
3631 it("successfully creates product with soft-deleted product handle and deletes it again", async () => {
3632 // First we soft-delete the product
3633 const response = await api
3634 .delete(`/admin/products/${baseProduct.id}`, adminHeaders)
3635 .catch((err) => {
3636 console.log(err)
3637 })
3638
3639 expect(response.status).toEqual(200)
3640 expect(response.data.id).toEqual(baseProduct.id)
3641
3642 // Lets try to create a product with same handle as deleted one
3643 const payload = {
3644 title: baseProduct.title,
3645 handle: baseProduct.handle,
3646 options: [{ title: "size", values: ["x", "l"] }],
3647 shipping_profile_id: shippingProfile.id,
3648 variants: [
3649 {
3650 title: "Test variant",
3651 prices: [{ currency_code: "usd", amount: 100 }],
3652 options: { size: "x" },
3653 },
3654 ],
3655 }
3656
3657 const res = await api.post("/admin/products", payload, adminHeaders)
3658
3659 expect(res.status).toEqual(200)
3660 expect(res.data.product.handle).toEqual(baseProduct.handle)
3661
3662 // Delete product again to ensure uniqueness is enforced in all cases
3663 const response2 = await api
3664 .delete(`/admin/products/${res.data.product.id}`, adminHeaders)
3665 .catch((err) => {
3666 console.log(err)
3667 })
3668
3669 expect(response2.status).toEqual(200)
3670 expect(response2.data.id).toEqual(res.data.product.id)
3671 })
3672
3673 it("should fail when creating a product with a handle that already exists", async () => {
3674 // Lets try to create a product with same handle as deleted one
3675 const payload = {
3676 title: baseProduct.title,
3677 handle: baseProduct.handle,
3678 description: "test-product-description",
3679 options: [{ title: "size", values: ["x", "l"] }],
3680 shipping_profile_id: shippingProfile.id,
3681 variants: [
3682 {
3683 title: "Test variant",
3684 prices: [{ currency_code: "usd", amount: 100 }],
3685 options: { size: "x" },
3686 },
3687 ],
3688 }
3689
3690 try {
3691 await api.post("/admin/products", payload, adminHeaders)
3692 } catch (error) {
3693 expect(error.response.data.message).toMatch(
3694 "Product with handle: base-product, already exists."
3695 )
3696 }
3697 })
3698
3699 it("successfully deletes product collection", async () => {
3700 // First we soft-delete the product collection
3701 const response = await api
3702 .delete(`/admin/collections/${baseCollection.id}`, adminHeaders)
3703 .catch((err) => {
3704 console.log(err)
3705 })
3706
3707 expect(response.status).toEqual(200)
3708 expect(response.data.id).toEqual(baseCollection.id)
3709 })
3710
3711 it("successfully creates soft-deleted product collection", async () => {
3712 const response = await api.delete(
3713 `/admin/collections/${baseCollection.id}`,
3714 adminHeaders
3715 )
3716 expect(response.status).toEqual(200)
3717 expect(response.data.id).toEqual(baseCollection.id)
3718
3719 // Lets try to create a product collection with same handle as deleted one
3720 const payload = {
3721 title: "Another test collection",
3722 handle: baseCollection.handle,
3723 }
3724
3725 const res = await api.post(
3726 "/admin/collections",
3727 payload,
3728 adminHeaders
3729 )
3730
3731 expect(res.status).toEqual(200)
3732 expect(res.data.collection.handle).toEqual(baseCollection.handle)
3733 })
3734
3735 it("should fail when creating a collection with a handle that already exists", async () => {
3736 // Lets try to create a collection with same handle as deleted one
3737 const payload = {
3738 title: "Another test collection",
3739 handle: baseCollection.handle,
3740 }
3741
3742 try {
3743 await api.post("/admin/collections", payload, adminHeaders)
3744 } catch (error) {
3745 expect(error.response.data.message).toMatch(
3746 `Product collection with handle: ${baseCollection.handle}, already exists.`
3747 )
3748 }
3749 })
3750
3751 it("successfully creates soft-deleted product variant", async () => {
3752 const variant = baseProduct.variants[0]
3753 const response = await api.delete(
3754 `/admin/products/${baseProduct.id}/variants/${variant.id}`,
3755 adminHeaders
3756 )
3757
3758 expect(response.status).toEqual(200)
3759 expect(response.data.id).toEqual(baseProduct.variants[0].id)
3760
3761 const payload = {
3762 title: "Second variant",
3763 sku: "new-sku",
3764 ean: "new-ean",
3765 upc: "new-upc",
3766 barcode: "new-barcode",
3767 prices: [
3768 {
3769 currency_code: "usd",
3770 amount: 100,
3771 },
3772 ],
3773 }
3774
3775 const res = await api.post(
3776 `/admin/products/${baseProduct.id}/variants`,
3777 payload,
3778 adminHeaders
3779 )
3780
3781 expect(res.status).toEqual(200)
3782 expect(res.data.product.variants).toEqual(
3783 expect.arrayContaining([
3784 expect.objectContaining({
3785 title: "Second variant",
3786 sku: "new-sku",
3787 ean: "new-ean",
3788 upc: "new-upc",
3789 barcode: "new-barcode",
3790 }),
3791 ])
3792 )
3793 })
3794 })
3795
3796 describe("POST /admin/products/:id/variants/:id", () => {
3797 it("successfully updates variant without prices", async () => {
3798 const res = await api
3799 .post(
3800 `/admin/products/${baseProduct.id}/variants/${baseProduct.variants[0].id}`,
3801 {
3802 title: "Updated variant",
3803 },
3804 adminHeaders
3805 )
3806 .catch((err) => {
3807 console.log(err)
3808 })
3809
3810 expect(res.status).toEqual(200)
3811 expect(
3812 res.data.product.variants.find(
3813 (v) => v.id === baseProduct.variants[0].id
3814 )?.title
3815 ).toEqual("Updated variant")
3816 })
3817
3818 it("updates multiple options in the same call", async () => {
3819 const baseVariant = baseProduct.variants[0]
3820 const updatedProduct = (
3821 await api.post(
3822 `/admin/products/${baseProduct.id}/variants/${baseVariant.id}`,
3823 {
3824 title: "Updated variant",
3825 options: {
3826 size: "small",
3827 color: "green",
3828 },
3829 },
3830 adminHeaders
3831 )
3832 ).data.product
3833
3834 const updatedOptions = updatedProduct.variants.find(
3835 (v) => v.id === baseVariant.id
3836 ).options
3837
3838 expect(updatedOptions).toHaveLength(2)
3839 expect(updatedOptions).toEqual(
3840 expect.arrayContaining([
3841 expect.objectContaining({
3842 value: "small",
3843 }),
3844 expect.objectContaining({
3845 value: "green",
3846 }),
3847 ])
3848 )
3849 })
3850 })
3851
3852 describe("batch methods", () => {
3853 it("successfully creates, updates, and deletes products", async () => {
3854 const createPayload = getProductFixture({
3855 weight: 100,
3856 length: 200,
3857 height: 300,
3858 width: 400,
3859 title: "Test batch create",
3860 handle: "test-batch-create",
3861 shipping_profile_id: shippingProfile.id,
3862 })
3863
3864 const updatePayload = {
3865 weight: 101,
3866 length: 201,
3867 height: 301,
3868 width: 401,
3869 id: publishedProduct.id,
3870 title: "Test batch update",
3871 }
3872
3873 const response = await api.post(
3874 "/admin/products/batch",
3875 {
3876 create: [createPayload],
3877 update: [updatePayload],
3878 delete: [baseProduct.id],
3879 },
3880 adminHeaders
3881 )
3882
3883 expect(response.status).toEqual(200)
3884 expect(response.data.created).toHaveLength(1)
3885 expect(response.data.updated).toHaveLength(1)
3886 expect(response.data.deleted.ids).toHaveLength(1)
3887
3888 expect(response.data.created).toEqual([
3889 expect.objectContaining({
3890 title: "Test batch create",
3891 }),
3892 ])
3893
3894 expect(response.data.updated).toEqual([
3895 expect.objectContaining({
3896 title: "Test batch update",
3897 }),
3898 ])
3899
3900 expect(response.data.deleted).toEqual(
3901 expect.objectContaining({ ids: [baseProduct.id] })
3902 )
3903
3904 const dbData = (await api.get("/admin/products", adminHeaders)).data
3905 .products
3906
3907 expect(dbData).toHaveLength(3)
3908 expect(dbData).toEqual(
3909 expect.arrayContaining([
3910 expect.objectContaining({
3911 title: "Test batch create",
3912 }),
3913 expect.objectContaining({
3914 title: "Test batch update",
3915 }),
3916 ])
3917 )
3918 })
3919
3920 it("should successfully delete products", async () => {
3921 const response = await api.post(
3922 "/admin/products/batch",
3923 { delete: [baseProduct.id] },
3924 adminHeaders
3925 )
3926
3927 expect(response.status).toEqual(200)
3928 expect(response.data.created).toHaveLength(0)
3929 expect(response.data.updated).toHaveLength(0)
3930 expect(response.data.deleted.ids).toHaveLength(1)
3931
3932 expect(response.data.created).toEqual([])
3933 expect(response.data.updated).toEqual([])
3934 expect(response.data.deleted).toEqual(
3935 expect.objectContaining({ ids: [baseProduct.id] })
3936 )
3937 })
3938
3939 it("successfully creates, updates, and deletes product variants", async () => {
3940 const productWithMultipleVariants = getProductFixture({
3941 title: "Test batch variants",
3942 handle: "test-batch-variants",
3943 shipping_profile_id: shippingProfile.id,
3944 variants: [
3945 {
3946 title: "Variant 1",
3947 prices: [
3948 {
3949 currency_code: "usd",
3950 amount: 100,
3951 },
3952 ],
3953 },
3954 {
3955 title: "Variant 2",
3956 prices: [
3957 {
3958 currency_code: "usd",
3959 amount: 200,
3960 },
3961 ],
3962 },
3963 ],
3964 })
3965
3966 const createdProduct = (
3967 await api.post(
3968 "/admin/products",
3969 productWithMultipleVariants,
3970 adminHeaders
3971 )
3972 ).data.product
3973
3974 const createPayload = {
3975 title: "Test batch create variant",
3976 prices: [
3977 {
3978 currency_code: "usd",
3979 amount: 20,
3980 },
3981 {
3982 currency_code: "dkk",
3983 amount: 10,
3984 },
3985 ],
3986 }
3987
3988 const updatePayload = {
3989 id: createdProduct.variants[0].id,
3990 title: "Test batch update variant",
3991 }
3992
3993 const response = await api.post(
3994 `/admin/products/${createdProduct.id}/variants/batch`,
3995 {
3996 create: [createPayload],
3997 update: [updatePayload],
3998 delete: [createdProduct.variants[1].id],
3999 },
4000 adminHeaders
4001 )
4002
4003 const dbData = (
4004 await api.get(`/admin/products/${createdProduct.id}`, adminHeaders)
4005 ).data.product.variants
4006
4007 expect(response.status).toEqual(200)
4008 expect(dbData).toHaveLength(2)
4009 expect(dbData).toEqual(
4010 expect.arrayContaining([
4011 expect.objectContaining({
4012 title: "Test batch create variant",
4013 }),
4014 expect.objectContaining({
4015 title: "Test batch update variant",
4016 }),
4017 ])
4018 )
4019 })
4020
4021 it("should preserve variant prices when batch updating variants", async () => {
4022 const productWithMultipleVariants = getProductFixture({
4023 title: "Test batch variants",
4024 handle: "test-batch-variants",
4025 shipping_profile_id: shippingProfile.id,
4026 variants: [
4027 {
4028 title: "Variant 1",
4029 prices: [
4030 {
4031 currency_code: "usd",
4032 amount: 100,
4033 },
4034 ],
4035 },
4036 {
4037 title: "Variant 2",
4038 prices: [
4039 {
4040 currency_code: "usd",
4041 amount: 200,
4042 },
4043 ],
4044 },
4045 ],
4046 })
4047
4048 const createdProduct = (
4049 await api.post(
4050 "/admin/products",
4051 productWithMultipleVariants,
4052 adminHeaders
4053 )
4054 ).data.product
4055
4056 const variant1Id = createdProduct.variants.find(
4057 (v) => v.title === "Variant 1"
4058 ).id
4059
4060 const variant2Id = createdProduct.variants.find(
4061 (v) => v.title === "Variant 2"
4062 ).id
4063
4064 const updatePayload1 = {
4065 id: variant1Id,
4066 title: "Test batch update variant",
4067 }
4068
4069 const updatePayload2 = {
4070 id: variant2Id,
4071 prices: [{ currency_code: "usd", amount: 300 }],
4072 }
4073
4074 const response = await api.post(
4075 `/admin/products/${createdProduct.id}/variants/batch`,
4076 {
4077 update: [updatePayload1, updatePayload2],
4078 },
4079 adminHeaders
4080 )
4081
4082 const dbData = (
4083 await api.get(
4084 `/admin/products/${createdProduct.id}?fields=*variants.prices`,
4085 adminHeaders
4086 )
4087 ).data.product.variants
4088
4089 expect(response.status).toEqual(200)
4090 expect(dbData).toHaveLength(2)
4091 expect(dbData).toEqual(
4092 expect.arrayContaining([
4093 expect.objectContaining({
4094 id: variant1Id,
4095 title: "Test batch update variant",
4096 prices: expect.arrayContaining([
4097 // updated title but price remains the same
4098 expect.objectContaining({
4099 currency_code: "usd",
4100 amount: 100,
4101 }),
4102 ]),
4103 }),
4104 expect.objectContaining({
4105 id: variant2Id,
4106 prices: expect.arrayContaining([
4107 expect.objectContaining({
4108 // updated price
4109 currency_code: "usd",
4110 amount: 300,
4111 }),
4112 ]),
4113 }),
4114 ])
4115 )
4116 })
4117
4118 it("successfully adds and removes products to a collection", async () => {
4119 const response = await api.post(
4120 `/admin/collections/${baseCollection.id}/products`,
4121 {
4122 add: [publishedProduct.id],
4123 remove: [baseProduct.id],
4124 },
4125 adminHeaders
4126 )
4127
4128 expect(response.status).toEqual(200)
4129 expect(response.data.collection).toBeTruthy()
4130
4131 const collection = (
4132 await api.get(
4133 `/admin/collections/${baseCollection.id}?fields=*products`,
4134 adminHeaders
4135 )
4136 ).data.collection
4137
4138 expect(collection.products).toHaveLength(1)
4139 expect(collection.products[0]).toEqual(
4140 expect.objectContaining({
4141 id: publishedProduct.id,
4142 })
4143 )
4144 })
4145
4146 // https://linear.app/medusajs/issue/SUP-1631/price-differences
4147 it("should successfully handle successive updates on prices", async () => {
4148 const response = await api.post(
4149 "/admin/products/batch",
4150 {
4151 create: [
4152 getProductFixture({
4153 title: "title1",
4154 variants: [
4155 {
4156 title: "variant1",
4157 prices: [{ currency_code: "usd", amount: 100 }],
4158 },
4159 ],
4160 }),
4161 getProductFixture({
4162 title: "title2",
4163 variants: [
4164 {
4165 title: "variant2",
4166 prices: [{ currency_code: "usd", amount: 200 }],
4167 },
4168 ],
4169 }),
4170 getProductFixture({
4171 title: "title3",
4172 variants: [
4173 {
4174 title: "variant3",
4175 prices: [{ currency_code: "usd", amount: 300 }],
4176 },
4177 ],
4178 }),
4179 ],
4180 update: [],
4181 delete: [baseProduct.id],
4182 },
4183 adminHeaders
4184 )
4185 const created = response.data.created
4186 const created1 = created.find((p) => p.title === "title1")
4187 const created2 = created.find((p) => p.title === "title2")
4188 const created3 = created.find((p) => p.title === "title3")
4189
4190 // Update the first and third product price to shuffle their order in the DB
4191 await api.post(
4192 `/admin/products/batch`,
4193 {
4194 update: [
4195 {
4196 id: created1.id,
4197 variants: [
4198 {
4199 id: created1.variants[0].id,
4200 prices: [{ currency_code: "usd", amount: 101 }],
4201 },
4202 ],
4203 },
4204 {
4205 id: created3.id,
4206 variants: [
4207 {
4208 id: created3.variants[0].id,
4209 prices: [{ currency_code: "usd", amount: 301 }],
4210 },
4211 ],
4212 },
4213 ],
4214 },
4215 adminHeaders
4216 )
4217
4218 // Update all products now
4219 await api.post(
4220 `/admin/products/batch`,
4221 {
4222 update: [
4223 {
4224 id: created1.id,
4225 variants: [
4226 {
4227 id: created1.variants[0].id,
4228 prices: [{ currency_code: "usd", amount: 102 }],
4229 },
4230 ],
4231 },
4232 {
4233 id: created2.id,
4234 variants: [
4235 {
4236 id: created2.variants[0].id,
4237 prices: [{ currency_code: "usd", amount: 202 }],
4238 },
4239 ],
4240 },
4241 {
4242 id: created3.id,
4243 variants: [
4244 {
4245 id: created3.variants[0].id,
4246 prices: [{ currency_code: "usd", amount: 302 }],
4247 },
4248 ],
4249 },
4250 ],
4251 },
4252 adminHeaders
4253 )
4254
4255 // Get the first product
4256 const product1 = (
4257 await api.get(`/admin/products/${created1.id}`, adminHeaders)
4258 ).data.product
4259 const product2 = (
4260 await api.get(`/admin/products/${created2.id}`, adminHeaders)
4261 ).data.product
4262 const product3 = (
4263 await api.get(`/admin/products/${created3.id}`, adminHeaders)
4264 ).data.product
4265 expect(product1.variants[0].prices[0].amount).toEqual(102)
4266 expect(product2.variants[0].prices[0].amount).toEqual(202)
4267 expect(product3.variants[0].prices[0].amount).toEqual(302)
4268 })
4269 })
4270
4271 // TODO: Discuss how this should be handled
4272 describe.skip("GET /admin/products/tag-usage", () => {
4273 it("successfully gets the tags usage", async () => {
4274 const res = await api
4275 .get("/admin/products/tag-usage", adminHeaders)
4276 .catch((err) => {
4277 console.log(err)
4278 })
4279
4280 expect(res.status).toEqual(200)
4281 expect(res.data.tags.length).toEqual(3)
4282 expect(res.data.tags).toEqual(
4283 expect.arrayContaining([
4284 { id: "tag1", usage_count: "2", value: "123" },
4285 { id: "tag3", usage_count: "2", value: "1235" },
4286 { id: "tag4", usage_count: "1", value: "1234" },
4287 ])
4288 )
4289 })
4290 })
4291
4292 describe("POST /admin/products/:id/images/:image_id/variants/batch", () => {
4293 it("should batch assign and remove variants from images", async () => {
4294 // Create a product with multiple images
4295 const productWithMultipleImages = await api.post(
4296 "/admin/products",
4297 {
4298 title: "product with multiple images",
4299 status: "published",
4300 options: [
4301 {
4302 title: "size",
4303 values: ["large", "small"],
4304 },
4305 {
4306 title: "color",
4307 values: ["red", "blue"],
4308 },
4309 ],
4310 images: [
4311 {
4312 url: "https://via.placeholder.com/100",
4313 },
4314 {
4315 url: "https://via.placeholder.com/200",
4316 },
4317 {
4318 url: "https://via.placeholder.com/300",
4319 },
4320 ],
4321 },
4322 adminHeaders
4323 )
4324
4325 const product = productWithMultipleImages.data.product
4326
4327 const variant1Response = await api.post(
4328 `/admin/products/${product.id}/variants`,
4329 {
4330 title: "variant 1",
4331 options: { size: "large", color: "red" },
4332 prices: [{ currency_code: "usd", amount: 100 }],
4333 },
4334 adminHeaders
4335 )
4336
4337 const variant2Response = await api.post(
4338 `/admin/products/${product.id}/variants`,
4339 {
4340 title: "variant 2",
4341 options: { size: "small", color: "blue" },
4342 prices: [{ currency_code: "usd", amount: 200 }],
4343 },
4344 adminHeaders
4345 )
4346
4347 const variant1 = variant1Response.data.product.variants.find(
4348 (v) => v.title === "variant 1"
4349 )
4350 const variant2 = variant2Response.data.product.variants.find(
4351 (v) => v.title === "variant 2"
4352 )
4353
4354 const addResponse = await api.post(
4355 `/admin/products/${product.id}/images/${product.images[0].id}/variants/batch`,
4356 {
4357 add: [variant1.id, variant2.id],
4358 },
4359 adminHeaders
4360 )
4361
4362 expect(addResponse.status).toBe(200)
4363 expect(addResponse.data.added).toHaveLength(2)
4364 expect(addResponse.data.added).toContain(variant1.id)
4365 expect(addResponse.data.added).toContain(variant2.id)
4366
4367 const addResponse2 = await api.post(
4368 `/admin/products/${product.id}/images/${product.images[1].id}/variants/batch`,
4369 {
4370 add: [variant1.id],
4371 },
4372 adminHeaders
4373 )
4374
4375 expect(addResponse2.status).toBe(200)
4376 expect(addResponse2.data.added).toHaveLength(1)
4377 expect(addResponse2.data.added).toContain(variant1.id)
4378
4379 const variant1WithImages = await api.get(
4380 `/admin/products/${product.id}/variants/${variant1.id}?fields=*images`,
4381 adminHeaders
4382 )
4383
4384 const variant2WithImages = await api.get(
4385 `/admin/products/${product.id}/variants/${variant2.id}?fields=*images`,
4386 adminHeaders
4387 )
4388
4389 expect(variant1WithImages.data.variant.images).toHaveLength(3)
4390
4391 // Variant 1 should have both images (first and second)
4392 expect(variant1WithImages.data.variant.images).toEqual(
4393 expect.arrayContaining([
4394 expect.objectContaining({
4395 id: product.images[0].id, // Variant image
4396 }),
4397 expect.objectContaining({
4398 id: product.images[1].id, // Variant image
4399 }),
4400 expect.objectContaining({
4401 id: product.images[2].id, // General product image
4402 }),
4403 ])
4404 )
4405
4406 expect(variant2WithImages.data.variant.images).toHaveLength(2)
4407
4408 // Variant 2 should have the first image
4409 expect(variant2WithImages.data.variant.images).toEqual(
4410 expect.arrayContaining([
4411 expect.objectContaining({
4412 id: product.images[0].id, // Variant image
4413 }),
4414 expect.objectContaining({
4415 id: product.images[2].id, // General product image
4416 }),
4417 ])
4418 )
4419
4420 const removeResponse = await api.post(
4421 `/admin/products/${product.id}/images/${product.images[0].id}/variants/batch`,
4422 {
4423 remove: [variant1.id],
4424 },
4425 adminHeaders
4426 )
4427
4428 expect(removeResponse.status).toBe(200)
4429 expect(removeResponse.data.removed).toHaveLength(1)
4430 expect(removeResponse.data.removed).toContain(variant1.id)
4431
4432 const variant1WithImagesAfterRemove = await api.get(
4433 `/admin/products/${product.id}/variants/${variant1.id}?fields=*images`,
4434 adminHeaders
4435 )
4436
4437 expect(
4438 variant1WithImagesAfterRemove.data.variant.images
4439 ).toHaveLength(2)
4440 expect(variant1WithImagesAfterRemove.data.variant.images).toEqual(
4441 expect.arrayContaining([
4442 expect.objectContaining({
4443 id: product.images[1].id, // Variant image
4444 }),
4445 expect.objectContaining({
4446 id: product.images[2].id, // General product image
4447 }),
4448 ])
4449 )
4450
4451 const variant2WithImagesAfterRemove = await api.get(
4452 `/admin/products/${product.id}/variants/${variant2.id}?fields=*images`,
4453 adminHeaders
4454 )
4455
4456 expect(
4457 variant2WithImagesAfterRemove.data.variant.images
4458 ).toHaveLength(2)
4459 expect(variant2WithImagesAfterRemove.data.variant.images).toEqual(
4460 expect.arrayContaining([
4461 expect.objectContaining({
4462 // Removed from the first variant but still on the second
4463 id: product.images[0].id,
4464 }),
4465 expect.objectContaining({
4466 id: product.images[2].id,
4467 }),
4468 ])
4469 )
4470 })
4471 })
4472
4473 describe("POST /admin/products/:id/variants/:variant_id/images/batch", () => {
4474 it("should batch manage images for a specific variant", async () => {
4475 // Create a product with multiple images and variants
4476 const productWithMultipleImages = await api.post(
4477 "/admin/products",
4478 {
4479 title: "product for variant image batch management",
4480 status: "published",
4481 options: [
4482 {
4483 title: "size",
4484 values: ["large", "small"],
4485 },
4486 {
4487 title: "color",
4488 values: ["red", "blue"],
4489 },
4490 ],
4491 images: [
4492 {
4493 url: "https://via.placeholder.com/100",
4494 },
4495 {
4496 url: "https://via.placeholder.com/200",
4497 },
4498 {
4499 url: "https://via.placeholder.com/300",
4500 },
4501 {
4502 url: "https://via.placeholder.com/400",
4503 },
4504 ],
4505 variants: [
4506 {
4507 title: "variant 1",
4508 options: { size: "large", color: "red" },
4509 prices: [{ currency_code: "usd", amount: 100 }],
4510 },
4511 {
4512 title: "variant 2",
4513 options: { size: "small", color: "blue" },
4514 prices: [{ currency_code: "usd", amount: 200 }],
4515 },
4516 ],
4517 },
4518 adminHeaders
4519 )
4520
4521 const product = productWithMultipleImages.data.product
4522 const variant1 = product.variants.find((v) => v.title === "variant 1")
4523 const variant2 = product.variants.find((v) => v.title === "variant 2")
4524
4525 // First, assign some images to variant1
4526 const initialAssignResponse = await api.post(
4527 `/admin/products/${product.id}/variants/${variant1.id}/images/batch`,
4528 {
4529 add: [product.images[0].id, product.images[1].id],
4530 },
4531 adminHeaders
4532 )
4533
4534 expect(initialAssignResponse.status).toBe(200)
4535 expect(initialAssignResponse.data.added).toHaveLength(2)
4536 expect(initialAssignResponse.data.added).toEqual(
4537 expect.arrayContaining([product.images[0].id, product.images[1].id])
4538 )
4539
4540 // Now batch manage images for variant1: add one more, remove one
4541 const batchResponse = await api.post(
4542 `/admin/products/${product.id}/variants/${variant1.id}/images/batch`,
4543 {
4544 add: [product.images[2].id],
4545 remove: [product.images[0].id],
4546 },
4547 adminHeaders
4548 )
4549
4550 expect(batchResponse.status).toBe(200)
4551 expect(batchResponse.data.added).toHaveLength(1)
4552 expect(batchResponse.data.added).toEqual(
4553 expect.arrayContaining([product.images[2].id])
4554 )
4555 expect(batchResponse.data.removed).toHaveLength(1)
4556 expect(batchResponse.data.removed).toEqual(
4557 expect.arrayContaining([product.images[0].id])
4558 )
4559
4560 // Verify the final state by checking variant1 images
4561 const variant1WithImages = await api.get(
4562 `/admin/products/${product.id}/variants/${variant1.id}?fields=*images`,
4563 adminHeaders
4564 )
4565
4566 // Should have 3 images: images[0] and images[3] (general product image), images[1] and images[2] variant scoped
4567 expect(variant1WithImages.data.variant.images).toHaveLength(4)
4568 expect(variant1WithImages.data.variant.images).toEqual(
4569 expect.arrayContaining([
4570 expect.objectContaining({ id: product.images[0].id }),
4571 expect.objectContaining({ id: product.images[1].id }),
4572 expect.objectContaining({ id: product.images[2].id }),
4573 expect.objectContaining({ id: product.images[3].id }),
4574 ])
4575 )
4576
4577 // Verify variant2
4578 const variant2WithImages = await api.get(
4579 `/admin/products/${product.id}/variants/${variant2.id}?fields=*images`,
4580 adminHeaders
4581 )
4582
4583 // Should only have the general product image
4584 expect(variant2WithImages.data.variant.images).toHaveLength(2)
4585 expect(variant2WithImages.data.variant.images).toEqual(
4586 expect.arrayContaining([
4587 expect.objectContaining({ id: product.images[0].id }),
4588 expect.objectContaining({ id: product.images[3].id }),
4589 ])
4590 )
4591 })
4592 })
4593 })
4594 },
4595 })