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 baseRegion
15 let shippingProfile
16 beforeAll(async () => {
17 await createAdminUser(dbConnection, adminHeaders, getContainer())
18 // BREAKING: Creating a region no longer takes tax_rate, payment_providers, fulfillment_providers, countriesr
19 baseRegion = (
20 await api.post(
21 "/admin/regions",
22 {
23 name: "Test region",
24 currency_code: "USD",
25 },
26 adminHeaders
27 )
28 ).data.region
29
30 shippingProfile = (
31 await api.post(
32 `/admin/shipping-profiles`,
33 { name: "default", type: "default" },
34 adminHeaders
35 )
36 ).data.shipping_profile
37
38 baseProduct = (
39 await api.post(
40 "/admin/products",
41 getProductFixture({
42 title: "Base product",
43 shipping_profile_id: shippingProfile.id,
44 }),
45 adminHeaders
46 )
47 ).data.product
48
49 await dbUtils.snapshot()
50 })
51
52 // BREAKING: We no longer have `/admin/variants` endpoint. Instead, variant is scoped by product ID, `/admin/products/:id/variants`
53 describe("GET /admin/products/:id/variants", () => {
54 it("should return the variants related to the requested product", async () => {
55 const res = await api
56 .get(`/admin/products/${baseProduct.id}/variants`, adminHeaders)
57 .catch((err) => {
58 console.log(err)
59 })
60
61 expect(res.status).toEqual(200)
62 expect(res.data.variants.length).toBe(1)
63 expect(res.data.variants).toEqual(
64 expect.arrayContaining([
65 expect.objectContaining({
66 id: baseProduct.variants[0].id,
67 product_id: baseProduct.id,
68 }),
69 ])
70 )
71 })
72
73 it("should allow searching of variants", async () => {
74 const newProduct = (
75 await api.post(
76 "/admin/products",
77 getProductFixture({
78 variants: [
79 { title: "First variant", prices: [] },
80 { title: "Second variant", prices: [] },
81 ],
82 shipping_profile_id: shippingProfile.id,
83 }),
84 adminHeaders
85 )
86 ).data.product
87
88 const res = await api
89 .get(
90 `/admin/products/${newProduct.id}/variants?q=first`,
91 adminHeaders
92 )
93 .catch((err) => {
94 console.log(err)
95 })
96
97 expect(res.status).toEqual(200)
98 expect(res.data.variants).toHaveLength(1)
99 expect(res.data.variants).toEqual(
100 expect.arrayContaining([
101 expect.objectContaining({
102 title: "First variant",
103 product_id: newProduct.id,
104 }),
105 ])
106 )
107 })
108 })
109
110 describe("updates a variant's default prices (ignores prices associated with a Price List)", () => {
111 it("successfully updates a variant's default prices by changing an existing price (currency_code)", async () => {
112 const data = {
113 prices: [
114 {
115 currency_code: "usd",
116 amount: 1500,
117 rules: {
118 region_id: "na",
119 },
120 },
121 ],
122 }
123
124 const response = await api.post(
125 `/admin/products/${baseProduct.id}/variants/${baseProduct.variants[0].id}`,
126 data,
127 adminHeaders
128 )
129
130 expect(
131 baseProduct.variants[0].prices.find((p) => p.currency_code === "usd")
132 .amount
133 ).toEqual(100)
134
135 expect(response.status).toEqual(200)
136 expect(response.data).toEqual({
137 product: expect.objectContaining({
138 id: baseProduct.id,
139 variants: expect.arrayContaining([
140 expect.objectContaining({
141 id: baseProduct.variants[0].id,
142 prices: expect.arrayContaining([
143 expect.objectContaining({
144 amount: 1500,
145 currency_code: "usd",
146 rules: { region_id: "na" },
147 }),
148 ]),
149 }),
150 ]),
151 }),
152 })
153 })
154
155 // TODO: Do we want to add support for region prices through the product APIs?
156 it.skip("successfully updates a variant's price by changing an existing price (given a region_id)", async () => {
157 const data = {
158 prices: [
159 {
160 region_id: "test-region",
161 amount: 1500,
162 },
163 ],
164 }
165
166 const response = await api
167 .post(
168 "/admin/products/test-product1/variants/test-variant_3",
169 data,
170 adminHeaders
171 )
172 .catch((err) => {
173 console.log(err)
174 })
175
176 expect(response.status).toEqual(200)
177
178 expect(response.data.product).toEqual(
179 expect.objectContaining({
180 variants: expect.arrayContaining([
181 expect.objectContaining({
182 id: "test-variant_3",
183 prices: expect.arrayContaining([
184 expect.objectContaining({
185 amount: 1500,
186 currency_code: "usd",
187 region_id: "test-region",
188 }),
189 ]),
190 }),
191 ]),
192 })
193 )
194 })
195
196 it("successfully updates a variant's prices by adding a new price", async () => {
197 const usdPrice = baseProduct.variants[0].prices.find(
198 (p) => p.currency_code === "usd"
199 )
200 const data = {
201 title: "Test variant prices",
202 prices: [
203 {
204 id: usdPrice.id,
205 currency_code: "usd",
206 amount: 100,
207 },
208 {
209 currency_code: "eur",
210 amount: 4500,
211 },
212 ],
213 }
214
215 const response = await api.post(
216 `/admin/products/${baseProduct.id}/variants/${baseProduct.variants[0].id}`,
217 data,
218 adminHeaders
219 )
220
221 expect(response.status).toEqual(200)
222
223 expect(response.data).toEqual(
224 expect.objectContaining({
225 product: expect.objectContaining({
226 id: baseProduct.id,
227 variants: expect.arrayContaining([
228 expect.objectContaining({
229 id: baseProduct.variants[0].id,
230 prices: expect.arrayContaining([
231 expect.objectContaining({
232 amount: 100,
233 currency_code: "usd",
234 id: usdPrice.id,
235 }),
236 expect.objectContaining({
237 amount: 4500,
238 currency_code: "eur",
239 }),
240 ]),
241 }),
242 ]),
243 }),
244 })
245 )
246 })
247
248 it("successfully updates a variant's prices by deleting a price and adding another price", async () => {
249 const data = {
250 prices: [
251 {
252 currency_code: "dkk",
253 amount: 8000,
254 },
255 {
256 currency_code: "eur",
257 amount: 900,
258 },
259 ],
260 }
261
262 const response = await api
263 .post(
264 `/admin/products/${baseProduct.id}/variants/${baseProduct.variants[0].id}`,
265 data,
266 adminHeaders
267 )
268 .catch((err) => {
269 console.log(err)
270 })
271
272 expect(response.status).toEqual(200)
273
274 const variant = response.data.product.variants[0]
275 expect(variant.prices.length).toEqual(2)
276
277 expect(variant.prices).toEqual(
278 expect.arrayContaining([
279 expect.objectContaining({
280 amount: 8000,
281 currency_code: "dkk",
282 }),
283 expect.objectContaining({
284 amount: 900,
285 currency_code: "eur",
286 }),
287 ])
288 )
289 })
290
291 // TODO: Similarly we need to decide how to handle regions
292 it.skip("successfully updates a variant's prices by updating an existing price (using region_id) and adding another price", async () => {
293 const data = {
294 prices: [
295 {
296 region_id: "test-region",
297 amount: 8000,
298 },
299 {
300 currency_code: "eur",
301 amount: 900,
302 },
303 ],
304 }
305
306 const variantId = "test-variant_3"
307 const response = await api
308 .post(
309 `/admin/products/test-product1/variants/${variantId}`,
310 data,
311 adminHeaders
312 )
313 .catch((err) => {
314 console.log(err)
315 })
316
317 expect(response.status).toEqual(200)
318
319 const variant = response.data.product.variants.find(
320 (v) => v.id === variantId
321 )
322 expect(variant.prices.length).toEqual(data.prices.length)
323
324 expect(variant.prices).toEqual(
325 expect.arrayContaining([
326 expect.objectContaining({
327 amount: 8000,
328 currency_code: "usd",
329 region_id: "test-region",
330 }),
331 expect.objectContaining({
332 amount: 900,
333 currency_code: "eur",
334 }),
335 ])
336 )
337 })
338
339 // TODO: Similarly we need to decide how to handle regions
340 it.skip("successfully deletes a region price", async () => {
341 const createRegionPricePayload = {
342 prices: [
343 {
344 currency_code: "usd",
345 amount: 1000,
346 },
347 {
348 region_id: "test-region",
349 amount: 8000,
350 },
351 {
352 currency_code: "eur",
353 amount: 900,
354 },
355 ],
356 }
357
358 const variantId = "test-variant_3"
359
360 const createRegionPriceResponse = await api.post(
361 "/admin/products/test-product1/variants/test-variant_3",
362 createRegionPricePayload,
363 adminHeaders
364 )
365
366 const initialPriceArray =
367 createRegionPriceResponse.data.product.variants.find(
368 (v) => v.id === variantId
369 ).prices
370
371 expect(createRegionPriceResponse.status).toEqual(200)
372 expect(initialPriceArray).toHaveLength(3)
373 expect(initialPriceArray).toEqual(
374 expect.arrayContaining([
375 expect.objectContaining({
376 amount: 1000,
377 currency_code: "usd",
378 }),
379 expect.objectContaining({
380 amount: 8000,
381 currency_code: "usd",
382 region_id: "test-region",
383 }),
384 expect.objectContaining({
385 amount: 900,
386 currency_code: "eur",
387 }),
388 ])
389 )
390
391 const deleteRegionPricePayload = {
392 prices: [
393 {
394 currency_code: "usd",
395 amount: 1000,
396 },
397 {
398 currency_code: "eur",
399 amount: 900,
400 },
401 ],
402 }
403
404 const deleteRegionPriceResponse = await api.post(
405 "/admin/products/test-product1/variants/test-variant_3",
406 deleteRegionPricePayload,
407 adminHeaders
408 )
409
410 const finalPriceArray =
411 deleteRegionPriceResponse.data.product.variants.find(
412 (v) => v.id === variantId
413 ).prices
414
415 expect(deleteRegionPriceResponse.status).toEqual(200)
416 expect(finalPriceArray).toHaveLength(2)
417 expect(finalPriceArray).toEqual(
418 expect.arrayContaining([
419 expect.objectContaining({
420 amount: 1000,
421 currency_code: "usd",
422 }),
423 expect.objectContaining({
424 amount: 900,
425 currency_code: "eur",
426 }),
427 ])
428 )
429 })
430
431 // TODO: Similarly we need to decide how to handle regions
432 it.skip("successfully updates a variants prices by deleting both a currency and region price", async () => {
433 // await Promise.all(
434 // ["reg_1", "reg_2", "reg_3"].map(async (regionId) => {
435 // return await simpleRegionFactory(dbConnection, {
436 // id: regionId,
437 // currency_code: regionId === "reg_1" ? "eur" : "usd",
438 // })
439 // })
440 // )
441
442 const createPrices = {
443 prices: [
444 {
445 region_id: "reg_1",
446 amount: 1,
447 },
448 {
449 region_id: "reg_2",
450 amount: 2,
451 },
452 {
453 currency_code: "usd",
454 amount: 3,
455 },
456 {
457 region_id: "reg_3",
458 amount: 4,
459 },
460 {
461 currency_code: "eur",
462 amount: 5,
463 },
464 ],
465 }
466
467 const variantId = "test-variant_3"
468
469 await api
470 .post(
471 `/admin/products/test-product1/variants/${variantId}`,
472 createPrices,
473 adminHeaders
474 )
475 .catch((err) => {
476 console.log(err)
477 })
478
479 const updatePrices = {
480 prices: [
481 {
482 region_id: "reg_1",
483 amount: 100,
484 },
485 {
486 region_id: "reg_2",
487 amount: 200,
488 },
489 {
490 currency_code: "usd",
491 amount: 300,
492 },
493 ],
494 }
495
496 const response = await api.post(
497 `/admin/products/test-product1/variants/${variantId}`,
498 updatePrices,
499 adminHeaders
500 )
501
502 const finalPriceArray = response.data.product.variants.find(
503 (v) => v.id === variantId
504 ).prices
505
506 expect(response.status).toEqual(200)
507 expect(finalPriceArray).toHaveLength(3)
508 expect(finalPriceArray).toEqual(
509 expect.arrayContaining([
510 expect.objectContaining({
511 amount: 100,
512 region_id: "reg_1",
513 }),
514 expect.objectContaining({
515 amount: 200,
516 region_id: "reg_2",
517 }),
518 expect.objectContaining({
519 amount: 300,
520 currency_code: "usd",
521 }),
522 ])
523 )
524 })
525 })
526
527 // TODO: Do we want to support price calculation on the admin endpoints? Enable this suite if we do, otherwise remove it
528 describe.skip("variant pricing calculations", () => {
529 it("selects prices based on the passed currency code", async () => {
530 const response = await api.get(
531 `/admin/products/${baseProduct.id}/variants/${baseProduct.variants[0].id}?fields=calculated_price&currency_code=usd`,
532 adminHeaders
533 )
534
535 expect(response.data.variant).toEqual({
536 id: baseProduct.variants[0].id,
537 original_price: 100,
538 calculated_price: 80,
539 calculated_price_type: "sale",
540 original_price_incl_tax: null,
541 calculated_price_incl_tax: null,
542 original_tax: null,
543 calculated_tax: null,
544 options: expect.any(Array),
545 prices: expect.any(Array),
546 product: expect.any(Object),
547 created_at: expect.any(String),
548 updated_at: expect.any(String),
549 })
550 })
551
552 it("selects prices based on the passed region id", async () => {
553 const response = await api.get(
554 `/admin/products/${baseProduct.id}/variants/${baseProduct.variants[0].id}?fields=calculated_price&region_id=${baseRegion.id}`,
555 adminHeaders
556 )
557
558 expect(response.data.variant).toEqual({
559 id: "test-variant",
560 original_price: 100,
561 calculated_price: 80,
562 calculated_price_type: "sale",
563 original_price_incl_tax: 100,
564 calculated_price_incl_tax: 80,
565 original_tax: 0,
566 calculated_tax: 0,
567 options: expect.any(Array),
568 prices: expect.any(Array),
569 product: expect.any(Object),
570 created_at: expect.any(String),
571 updated_at: expect.any(String),
572 })
573 })
574
575 it("selects prices based on the passed region id and customer id", async () => {
576 const response = await api.get(
577 `/admin/products/${baseProduct.id}/variants/${
578 baseProduct.variants[0].id
579 }?fields=calculated_price&region_id=${
580 baseRegion.id
581 }&customer_id=${""}`,
582 adminHeaders
583 )
584
585 expect(response.data.variant).toEqual({
586 id: "test-variant",
587 original_price: 100,
588 calculated_price: 40,
589 calculated_price_type: "sale",
590 original_price_incl_tax: 100,
591 calculated_price_incl_tax: 40,
592 original_tax: 0,
593 calculated_tax: 0,
594 prices: expect.any(Array),
595 options: expect.any(Array),
596 product: expect.any(Object),
597 created_at: expect.any(String),
598 updated_at: expect.any(String),
599 })
600 })
601 })
602
603 describe("variant creation", () => {
604 it("create a product variant with prices (regional and currency)", async () => {
605 const payload = {
606 title: "Created variant",
607 sku: "new-sku",
608 ean: "new-ean",
609 upc: "new-upc",
610 barcode: "new-barcode",
611 prices: [
612 {
613 currency_code: "usd",
614 amount: 100,
615 },
616 {
617 currency_code: "eur",
618 amount: 200,
619 },
620 ],
621 }
622
623 const res = await api
624 .post(
625 `/admin/products/${baseProduct.id}/variants`,
626 payload,
627 adminHeaders
628 )
629 .catch((err) => console.log(err))
630
631 const insertedVariant = res.data.product.variants.find(
632 (v) => v.sku === "new-sku"
633 )
634
635 expect(res.status).toEqual(200)
636
637 expect(insertedVariant.prices).toHaveLength(2)
638 expect(insertedVariant.prices).toEqual(
639 expect.arrayContaining([
640 expect.objectContaining({
641 currency_code: "usd",
642 amount: 100,
643 variant_id: insertedVariant.id,
644 }),
645 expect.objectContaining({
646 currency_code: "eur",
647 amount: 200,
648 variant_id: insertedVariant.id,
649 }),
650 ])
651 )
652 })
653 })
654
655 describe("POST /admin/products/:id/variants/:variant_id", () => {
656 it("should update variant to manage_inventory false and unlink inventory items", async () => {
657 const inventoryItem1 = (
658 await api.post(
659 `/admin/inventory-items`,
660 { sku: "inventory-item-variant-1" },
661 adminHeaders
662 )
663 ).data.inventory_item
664
665 const inventoryItem2 = (
666 await api.post(
667 `/admin/inventory-items`,
668 { sku: "inventory-item-variant-2" },
669 adminHeaders
670 )
671 ).data.inventory_item
672
673 const createPayload = {
674 title: "Test product for variant update",
675 handle: "test-product-variant-update",
676 options: [{ title: "size", values: ["large"] }],
677 shipping_profile_id: shippingProfile.id,
678 variants: [
679 {
680 title: "Variant with inventory",
681 prices: [{ currency_code: "usd", amount: 100 }],
682 manage_inventory: true,
683 options: { size: "large" },
684 inventory_items: [
685 {
686 inventory_item_id: inventoryItem1.id,
687 required_quantity: 5,
688 },
689 {
690 inventory_item_id: inventoryItem2.id,
691 required_quantity: 10,
692 },
693 ],
694 },
695 ],
696 }
697
698 const createdProduct = (
699 await api.post("/admin/products", createPayload, {
700 ...adminHeaders,
701 params: {
702 fields:
703 "+variants.inventory_items.*,+variants.inventory_items.inventory.*",
704 },
705 })
706 ).data.product
707
708 const variantWithInventory = createdProduct.variants[0]
709
710 expect(variantWithInventory.manage_inventory).toBe(true)
711 expect(variantWithInventory.inventory_items).toHaveLength(2)
712 expect(variantWithInventory.inventory_items).toEqual(
713 expect.arrayContaining([
714 expect.objectContaining({
715 inventory_item_id: inventoryItem1.id,
716 required_quantity: 5,
717 }),
718 expect.objectContaining({
719 inventory_item_id: inventoryItem2.id,
720 required_quantity: 10,
721 }),
722 ])
723 )
724
725 const updatePayload = {
726 manage_inventory: false,
727 }
728
729 const updatedProduct = (
730 await api.post(
731 `/admin/products/${createdProduct.id}/variants/${variantWithInventory.id}`,
732 updatePayload,
733 {
734 ...adminHeaders,
735 params: {
736 fields:
737 "+variants.inventory_items.*,+variants.inventory_items.inventory.*",
738 },
739 }
740 )
741 ).data.product
742
743 const updatedVariant = updatedProduct.variants.find(
744 (v) => v.id === variantWithInventory.id
745 )
746
747 expect(updatedVariant.manage_inventory).toBe(false)
748
749 expect(updatedVariant.inventory_items).toHaveLength(0)
750 expect(updatedVariant.inventory_items).toEqual([])
751
752 const inventoryItem1Response = await api.get(
753 `/admin/inventory-items/${inventoryItem1.id}`,
754 adminHeaders
755 )
756 const inventoryItem2Response = await api.get(
757 `/admin/inventory-items/${inventoryItem2.id}`,
758 adminHeaders
759 )
760
761 expect(inventoryItem1Response.status).toEqual(200)
762 expect(inventoryItem2Response.status).toEqual(200)
763 expect(inventoryItem1Response.data.inventory_item.id).toBe(
764 inventoryItem1.id
765 )
766 expect(inventoryItem2Response.data.inventory_item.id).toBe(
767 inventoryItem2.id
768 )
769 })
770
771 it("should create an inventory item and link it when manage_inventory is enabled on an existing variant", async () => {
772 const createdProduct = (
773 await api.post(
774 "/admin/products",
775 {
776 title: "Test product for enabling manage inventory",
777 handle: "test-product-enable-manage-inventory",
778 options: [{ title: "size", values: ["large"] }],
779 shipping_profile_id: shippingProfile.id,
780 variants: [
781 {
782 title: "Variant without inventory",
783 sku: "variant-enable-manage-inventory",
784 prices: [{ currency_code: "usd", amount: 100 }],
785 manage_inventory: false,
786 options: { size: "large" },
787 },
788 ],
789 },
790 {
791 ...adminHeaders,
792 params: {
793 fields:
794 "+variants.inventory_items.*,+variants.inventory_items.inventory.*",
795 },
796 }
797 )
798 ).data.product
799
800 const variant = createdProduct.variants[0]
801
802 expect(variant.manage_inventory).toBe(false)
803 expect(variant.inventory_items).toHaveLength(0)
804
805 const updatedProduct = (
806 await api.post(
807 `/admin/products/${createdProduct.id}/variants/${variant.id}`,
808 { manage_inventory: true },
809 {
810 ...adminHeaders,
811 params: {
812 fields:
813 "+variants.inventory_items.*,+variants.inventory_items.inventory.*",
814 },
815 }
816 )
817 ).data.product
818
819 const updatedVariant = updatedProduct.variants.find(
820 (v) => v.id === variant.id
821 )
822
823 expect(updatedVariant.manage_inventory).toBe(true)
824 expect(updatedVariant.inventory_items).toHaveLength(1)
825 expect(updatedVariant.inventory_items[0]).toEqual(
826 expect.objectContaining({
827 required_quantity: 1,
828 inventory: expect.objectContaining({
829 sku: "variant-enable-manage-inventory",
830 title: "Variant without inventory",
831 requires_shipping: true,
832 }),
833 })
834 )
835 })
836
837 it("should not create a second inventory item when manage_inventory is enabled again", async () => {
838 const createdProduct = (
839 await api.post(
840 "/admin/products",
841 {
842 title: "Test product for idempotent manage inventory",
843 handle: "test-product-idempotent-manage-inventory",
844 options: [{ title: "size", values: ["large"] }],
845 shipping_profile_id: shippingProfile.id,
846 variants: [
847 {
848 title: "Variant toggled twice",
849 sku: "variant-idempotent-manage-inventory",
850 prices: [{ currency_code: "usd", amount: 100 }],
851 manage_inventory: false,
852 options: { size: "large" },
853 },
854 ],
855 },
856 adminHeaders
857 )
858 ).data.product
859
860 const variant = createdProduct.variants[0]
861
862 await api.post(
863 `/admin/products/${createdProduct.id}/variants/${variant.id}`,
864 { manage_inventory: true },
865 adminHeaders
866 )
867
868 const updatedProduct = (
869 await api.post(
870 `/admin/products/${createdProduct.id}/variants/${variant.id}`,
871 { manage_inventory: true },
872 {
873 ...adminHeaders,
874 params: {
875 fields:
876 "+variants.inventory_items.*,+variants.inventory_items.inventory.*",
877 },
878 }
879 )
880 ).data.product
881
882 const updatedVariant = updatedProduct.variants.find(
883 (v) => v.id === variant.id
884 )
885
886 expect(updatedVariant.manage_inventory).toBe(true)
887 expect(updatedVariant.inventory_items).toHaveLength(1)
888 })
889 })
890
891 describe("POST /admin/products/:id/variants/:variant_id/inventory-items", () => {
892 it("should throw an error when required attributes are not passed", async () => {
893 const { response } = await api
894 .post(
895 `/admin/products/${baseProduct.id}/variants/${baseProduct.variants[0].id}/inventory-items`,
896 {},
897 adminHeaders
898 )
899 .catch((e) => e)
900
901 expect(response.status).toEqual(400)
902 expect(response.data).toEqual({
903 type: "invalid_data",
904 message:
905 "Invalid request: Field 'required_quantity' is required; Field 'inventory_item_id' is required",
906 })
907 })
908
909 it("successfully adds inventory item to a variant", async () => {
910 const inventoryItem = (
911 await api.post(
912 `/admin/inventory-items`,
913 { sku: "12345" },
914 adminHeaders
915 )
916 ).data.inventory_item
917
918 const res = await api.post(
919 `/admin/products/${baseProduct.id}/variants/${baseProduct.variants[0].id}/inventory-items?fields=inventory_items.inventory.*,inventory_items.*`,
920 {
921 inventory_item_id: inventoryItem.id,
922 required_quantity: 5,
923 },
924 adminHeaders
925 )
926
927 expect(res.status).toEqual(200)
928 expect(res.data.variant.inventory_items).toHaveLength(2)
929 expect(res.data.variant.inventory_items).toEqual(
930 expect.arrayContaining([
931 expect.objectContaining({
932 required_quantity: 5,
933 inventory_item_id: inventoryItem.id,
934 }),
935 ])
936 )
937 })
938
939 it("successfully adds inventory item to a variant with a fractional required quantity", async () => {
940 const inventoryItem = (
941 await api.post(
942 `/admin/inventory-items`,
943 { sku: "fractional-sku" },
944 adminHeaders
945 )
946 ).data.inventory_item
947
948 const res = await api.post(
949 `/admin/products/${baseProduct.id}/variants/${baseProduct.variants[0].id}/inventory-items?fields=inventory_items.inventory.*,inventory_items.*`,
950 {
951 inventory_item_id: inventoryItem.id,
952 required_quantity: 0.25,
953 },
954 adminHeaders
955 )
956
957 expect(res.status).toEqual(200)
958 expect(res.data.variant.inventory_items).toEqual(
959 expect.arrayContaining([
960 expect.objectContaining({
961 required_quantity: 0.25,
962 inventory_item_id: inventoryItem.id,
963 }),
964 ])
965 )
966 })
967 })
968
969 describe("POST /admin/products/:id/variants/:variant_id/inventory-items/:inventory_id", () => {
970 let inventoryItem
971
972 beforeEach(async () => {
973 inventoryItem = (
974 await api.post(
975 `/admin/inventory-items`,
976 { sku: "12345" },
977 adminHeaders
978 )
979 ).data.inventory_item
980
981 await api.post(
982 `/admin/products/${baseProduct.id}/variants/${baseProduct.variants[0].id}/inventory-items`,
983 {
984 inventory_item_id: inventoryItem.id,
985 required_quantity: 5,
986 },
987 adminHeaders
988 )
989 })
990
991 it("should throw an error when required attributes are not passed", async () => {
992 const { response } = await api
993 .post(
994 `/admin/products/${baseProduct.id}/variants/${baseProduct.variants[0].id}/inventory-items/${inventoryItem.id}`,
995 {},
996 adminHeaders
997 )
998 .catch((e) => e)
999
1000 expect(response.status).toEqual(400)
1001 expect(response.data).toEqual({
1002 type: "invalid_data",
1003 message: "Invalid request: Field 'required_quantity' is required",
1004 })
1005 })
1006
1007 it("successfully updates an inventory item link to a variant", async () => {
1008 const res = await api.post(
1009 `/admin/products/${baseProduct.id}/variants/${baseProduct.variants[0].id}/inventory-items/${inventoryItem.id}?fields=inventory_items.inventory.*,inventory_items.*`,
1010 { required_quantity: 10 },
1011 adminHeaders
1012 )
1013
1014 expect(res.status).toEqual(200)
1015 expect(res.data.variant.inventory_items).toHaveLength(2)
1016 expect(res.data.variant.inventory_items).toEqual(
1017 expect.arrayContaining([
1018 expect.objectContaining({
1019 required_quantity: 10,
1020 inventory_item_id: inventoryItem.id,
1021 }),
1022 ])
1023 )
1024 })
1025 })
1026
1027 describe("DELETE /admin/products/:id/variants/:variant_id/inventory-items/:inventory_id", () => {
1028 let inventoryItem
1029
1030 beforeEach(async () => {
1031 inventoryItem = (
1032 await api.post(
1033 `/admin/inventory-items`,
1034 { sku: "12345" },
1035 adminHeaders
1036 )
1037 ).data.inventory_item
1038
1039 await api.post(
1040 `/admin/products/${baseProduct.id}/variants/${baseProduct.variants[0].id}/inventory-items`,
1041 {
1042 inventory_item_id: inventoryItem.id,
1043 required_quantity: 5,
1044 },
1045 adminHeaders
1046 )
1047 })
1048
1049 it("successfully deletes an inventory item link from a variant", async () => {
1050 await api.post(
1051 `/admin/products/${baseProduct.id}/variants/${baseProduct.variants[0].id}/inventory-items`,
1052 { inventory_item_id: inventoryItem.id, required_quantity: 5 },
1053 adminHeaders
1054 )
1055
1056 const res = await api.delete(
1057 `/admin/products/${baseProduct.id}/variants/${baseProduct.variants[0].id}/inventory-items/${inventoryItem.id}?fields=inventory_items.inventory.*,inventory_items.*`,
1058 adminHeaders
1059 )
1060
1061 expect(res.status).toEqual(200)
1062 expect(res.data.parent.inventory_items).toHaveLength(1)
1063 expect(res.data.parent.inventory_items[0].id).not.toBe(inventoryItem.id)
1064 })
1065 })
1066
1067 describe("POST /admin/products/:id/variants/:variant_id/inventory-items/batch", () => {
1068 let inventoryItemToUpdate
1069 let inventoryItemToDelete
1070 let inventoryItemToCreate
1071 let inventoryProduct
1072 let inventoryVariant1
1073 let inventoryVariant2
1074 let inventoryVariant3
1075
1076 beforeEach(async () => {
1077 inventoryProduct = (
1078 await api.post(
1079 "/admin/products",
1080 {
1081 title: "product 1",
1082 shipping_profile_id: shippingProfile.id,
1083 options: [
1084 { title: "size", values: ["large", "medium", "small"] },
1085 ],
1086 variants: [
1087 {
1088 title: "variant 1",
1089 prices: [{ currency_code: "usd", amount: 100 }],
1090 options: { size: "large" },
1091 },
1092 {
1093 title: "variant 2",
1094 prices: [{ currency_code: "usd", amount: 100 }],
1095 options: { size: "small" },
1096 },
1097 {
1098 title: "variant 3",
1099 prices: [{ currency_code: "usd", amount: 100 }],
1100 options: { size: "medium" },
1101 },
1102 ],
1103 },
1104 adminHeaders
1105 )
1106 ).data.product
1107
1108 inventoryVariant1 = inventoryProduct.variants[0]
1109 inventoryVariant2 = inventoryProduct.variants[1]
1110 inventoryVariant3 = inventoryProduct.variants[2]
1111
1112 inventoryItemToCreate = (
1113 await api.post(
1114 `/admin/inventory-items`,
1115 { sku: "to-create" },
1116 adminHeaders
1117 )
1118 ).data.inventory_item
1119
1120 inventoryItemToUpdate = (
1121 await api.post(
1122 `/admin/inventory-items`,
1123 { sku: "to-update" },
1124 adminHeaders
1125 )
1126 ).data.inventory_item
1127
1128 inventoryItemToDelete = (
1129 await api.post(
1130 `/admin/inventory-items`,
1131 { sku: "to-delete" },
1132 adminHeaders
1133 )
1134 ).data.inventory_item
1135
1136 await api.post(
1137 `/admin/products/${baseProduct.id}/variants/${inventoryVariant1.id}/inventory-items`,
1138 {
1139 inventory_item_id: inventoryItemToUpdate.id,
1140 required_quantity: 5,
1141 },
1142 adminHeaders
1143 )
1144
1145 await api.post(
1146 `/admin/products/${baseProduct.id}/variants/${inventoryVariant2.id}/inventory-items`,
1147 {
1148 inventory_item_id: inventoryItemToDelete.id,
1149 required_quantity: 10,
1150 },
1151 adminHeaders
1152 )
1153 })
1154
1155 it("successfully creates, updates and deletes an inventory item link from a variant", async () => {
1156 const res = await api.post(
1157 `/admin/products/${inventoryProduct.id}/variants/inventory-items/batch`,
1158 {
1159 create: [
1160 {
1161 required_quantity: 15,
1162 inventory_item_id: inventoryItemToCreate.id,
1163 variant_id: inventoryVariant3.id,
1164 },
1165 ],
1166 update: [
1167 {
1168 required_quantity: 25,
1169 inventory_item_id: inventoryItemToUpdate.id,
1170 variant_id: inventoryVariant1.id,
1171 },
1172 ],
1173 delete: [
1174 {
1175 inventory_item_id: inventoryItemToDelete.id,
1176 variant_id: inventoryVariant2.id,
1177 },
1178 ],
1179 },
1180 adminHeaders
1181 )
1182
1183 expect(res.status).toEqual(200)
1184
1185 const createdLinkVariant = (
1186 await api.get(
1187 `/admin/products/${inventoryProduct.id}/variants/${inventoryVariant3.id}?fields=inventory_items.inventory.*,inventory_items.*`,
1188 adminHeaders
1189 )
1190 ).data.variant
1191
1192 expect(createdLinkVariant.inventory_items).toHaveLength(2)
1193 expect(createdLinkVariant.inventory_items).toEqual(
1194 expect.arrayContaining([
1195 expect.objectContaining({
1196 required_quantity: 15,
1197 inventory_item_id: inventoryItemToCreate.id,
1198 }),
1199 ])
1200 )
1201
1202 const updatedLinkVariant = (
1203 await api.get(
1204 `/admin/products/${inventoryProduct.id}/variants/${inventoryVariant1.id}?fields=inventory_items.inventory.*,inventory_items.*`,
1205 adminHeaders
1206 )
1207 ).data.variant
1208
1209 expect(updatedLinkVariant.inventory_items).toHaveLength(2)
1210 expect(updatedLinkVariant.inventory_items).toEqual(
1211 expect.arrayContaining([
1212 expect.objectContaining({
1213 required_quantity: 25,
1214 inventory_item_id: inventoryItemToUpdate.id,
1215 }),
1216 ])
1217 )
1218
1219 const deletedLinkVariant = (
1220 await api.get(
1221 `/admin/products/${inventoryProduct.id}/variants/${inventoryVariant2.id}?fields=inventory_items.inventory.*,inventory_items.*`,
1222 adminHeaders
1223 )
1224 ).data.variant
1225
1226 expect(deletedLinkVariant.inventory_items).toHaveLength(1)
1227 expect(deletedLinkVariant.inventory_items[0].id).not.toEqual(
1228 inventoryItemToDelete.id
1229 )
1230 })
1231 })
1232
1233 describe("DELETE /admin/products/:id/variants/:variant_id", () => {
1234 it("successfully deletes a variant when a linked inventory item was already deleted", async () => {
1235 const stockLocation = (
1236 await api.post(
1237 `/admin/stock-locations`,
1238 { name: "loc" },
1239 adminHeaders
1240 )
1241 ).data.stock_location
1242
1243 const inventoryItem1 = (
1244 await api.post(
1245 `/admin/inventory-items`,
1246 { sku: "variant-orphan-1" },
1247 adminHeaders
1248 )
1249 ).data.inventory_item
1250
1251 const inventoryItem2 = (
1252 await api.post(
1253 `/admin/inventory-items`,
1254 { sku: "variant-orphan-2" },
1255 adminHeaders
1256 )
1257 ).data.inventory_item
1258
1259 await api.post(
1260 `/admin/inventory-items/${inventoryItem1.id}/location-levels`,
1261 {
1262 location_id: stockLocation.id,
1263 stocked_quantity: 10,
1264 },
1265 adminHeaders
1266 )
1267
1268 await api.post(
1269 `/admin/inventory-items/${inventoryItem2.id}/location-levels`,
1270 {
1271 location_id: stockLocation.id,
1272 stocked_quantity: 5,
1273 },
1274 adminHeaders
1275 )
1276
1277 const product = (
1278 await api.post(
1279 `/admin/products`,
1280 {
1281 title: "Product with orphan variant link",
1282 handle: "product-orphan-variant-link",
1283 options: [{ title: "size", values: ["m"] }],
1284 shipping_profile_id: shippingProfile.id,
1285 variants: [
1286 {
1287 title: "Variant with orphan inventory link",
1288 prices: [{ currency_code: "usd", amount: 100 }],
1289 manage_inventory: true,
1290 options: { size: "m" },
1291 inventory_items: [
1292 {
1293 inventory_item_id: inventoryItem1.id,
1294 required_quantity: 1,
1295 },
1296 {
1297 inventory_item_id: inventoryItem2.id,
1298 required_quantity: 1,
1299 },
1300 ],
1301 },
1302 ],
1303 },
1304 adminHeaders
1305 )
1306 ).data.product
1307
1308 const variantId = product.variants[0].id
1309
1310 // Delete one inventory item directly via the module service,
1311 // bypassing the workflow that would clean up the link.
1312 // This creates an orphan link (variant still references a
1313 // deleted inventory item).
1314 const inventoryModule = getContainer().resolve(Modules.INVENTORY)
1315 await inventoryModule.deleteInventoryItems(inventoryItem1.id)
1316
1317 // Deleting the variant should succeed despite the orphan link
1318 const response = await api.delete(
1319 `/admin/products/${product.id}/variants/${variantId}`,
1320 adminHeaders
1321 )
1322
1323 expect(response.status).toEqual(200)
1324 expect(response.data).toEqual(
1325 expect.objectContaining({
1326 id: variantId,
1327 deleted: true,
1328 })
1329 )
1330
1331 // The remaining inventory item should also be deleted since it
1332 // was only associated with this variant
1333 const item2Response = await api
1334 .get(`/admin/inventory-items/${inventoryItem2.id}`, adminHeaders)
1335 .catch((err) => err.response)
1336
1337 expect(item2Response.status).toEqual(404)
1338 })
1339 })
1340 },
1341 })