1 import { ICartModuleService } from "@medusajs/framework/types"
2 import { BigNumber, Module, Modules } from "@medusajs/framework/utils"
3 import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
4 import { CheckConstraintViolationException } from "@medusajs/framework/mikro-orm/core"
5 import { CartModuleService } from "@services"
6 import { normalizeBigNumbers } from "@medusajs/test-utils"
7
8 jest.setTimeout(50000)
9
10 moduleIntegrationTestRunner<ICartModuleService>({
11 moduleName: Modules.CART,
12 testSuite: ({ service }) => {
13 describe("Cart Module Service", () => {
14 it(`should export the appropriate linkable configuration`, () => {
15 const linkable = Module(Modules.CART, {
16 service: CartModuleService,
17 }).linkable
18
19 expect(Object.keys(linkable)).toEqual([
20 "cart",
21 "creditLine",
22 "address",
23 "lineItem",
24 "lineItemAdjustment",
25 "lineItemTaxLine",
26 "shippingMethod",
27 "shippingMethodAdjustment",
28 "shippingMethodTaxLine",
29 ])
30
31 Object.keys(linkable).forEach((key) => {
32 delete linkable[key].toJSON
33 })
34
35 expect(linkable).toEqual({
36 cart: {
37 id: {
38 linkable: "cart_id",
39 entity: "Cart",
40 primaryKey: "id",
41 serviceName: "cart",
42 field: "cart",
43 },
44 },
45 creditLine: {
46 id: {
47 entity: "CreditLine",
48 field: "creditLine",
49 linkable: "credit_line_id",
50 primaryKey: "id",
51 serviceName: "cart",
52 },
53 },
54 address: {
55 id: {
56 linkable: "address_id",
57 entity: "Address",
58 primaryKey: "id",
59 serviceName: "cart",
60 field: "address",
61 },
62 },
63 lineItem: {
64 id: {
65 linkable: "line_item_id",
66 entity: "LineItem",
67 primaryKey: "id",
68 serviceName: "cart",
69 field: "lineItem",
70 },
71 },
72 lineItemAdjustment: {
73 id: {
74 linkable: "line_item_adjustment_id",
75 entity: "LineItemAdjustment",
76 primaryKey: "id",
77 serviceName: "cart",
78 field: "lineItemAdjustment",
79 },
80 },
81 lineItemTaxLine: {
82 id: {
83 linkable: "line_item_tax_line_id",
84 entity: "LineItemTaxLine",
85 primaryKey: "id",
86 serviceName: "cart",
87 field: "lineItemTaxLine",
88 },
89 },
90 shippingMethod: {
91 id: {
92 linkable: "shipping_method_id",
93 entity: "ShippingMethod",
94 primaryKey: "id",
95 serviceName: "cart",
96 field: "shippingMethod",
97 },
98 },
99 shippingMethodAdjustment: {
100 id: {
101 linkable: "shipping_method_adjustment_id",
102 entity: "ShippingMethodAdjustment",
103 primaryKey: "id",
104 serviceName: "cart",
105 field: "shippingMethodAdjustment",
106 },
107 },
108 shippingMethodTaxLine: {
109 id: {
110 linkable: "shipping_method_tax_line_id",
111 entity: "ShippingMethodTaxLine",
112 primaryKey: "id",
113 serviceName: "cart",
114 field: "shippingMethodTaxLine",
115 },
116 },
117 })
118 })
119
120 describe("create", () => {
121 it("should throw an error when required params are not passed", async () => {
122 const error = await service
123 .createCarts([
124 {
125 email: "test@email.com",
126 } as any,
127 ])
128 .catch((e) => e)
129
130 expect(error.message).toContain(
131 "Value for Cart.currency_code is required, 'undefined' found"
132 )
133 })
134
135 it("should create a cart successfully", async () => {
136 const [createdCart] = await service.createCarts([
137 {
138 currency_code: "eur",
139 },
140 ])
141
142 const [cart] = await service.listCarts({ id: [createdCart.id] })
143
144 expect(cart).toEqual(
145 expect.objectContaining({
146 id: createdCart.id,
147 currency_code: "eur",
148 })
149 )
150 })
151
152 it("should create a cart with billing + shipping address successfully", async () => {
153 const [createdCart] = await service.createCarts([
154 {
155 currency_code: "eur",
156 billing_address: {
157 first_name: "John",
158 last_name: "Doe",
159 },
160 shipping_address: {
161 first_name: "John",
162 last_name: "Doe",
163 },
164 },
165 ])
166
167 const [cart] = await service.listCarts(
168 { id: [createdCart.id] },
169 { relations: ["billing_address", "shipping_address"] }
170 )
171
172 expect(cart).toEqual(
173 expect.objectContaining({
174 id: createdCart.id,
175 currency_code: "eur",
176 billing_address: expect.objectContaining({
177 first_name: "John",
178 last_name: "Doe",
179 }),
180 shipping_address: expect.objectContaining({
181 first_name: "John",
182 last_name: "Doe",
183 }),
184 })
185 )
186 })
187
188 it("should create a cart with billing id + shipping id successfully", async () => {
189 const [createdAddress] = await service.createAddresses([
190 {
191 first_name: "John",
192 last_name: "Doe",
193 },
194 ])
195
196 const [createdCart] = await service.createCarts([
197 {
198 currency_code: "eur",
199 billing_address_id: createdAddress.id,
200 shipping_address_id: createdAddress.id,
201 },
202 ])
203
204 expect(createdCart).toEqual(
205 expect.objectContaining({
206 id: createdCart.id,
207 currency_code: "eur",
208 billing_address: expect.objectContaining({
209 id: createdAddress.id,
210 first_name: "John",
211 last_name: "Doe",
212 }),
213 shipping_address: expect.objectContaining({
214 id: createdAddress.id,
215 first_name: "John",
216 last_name: "Doe",
217 }),
218 })
219 )
220 })
221
222 it("should create a cart with items", async () => {
223 const createdCart = await service.createCarts({
224 currency_code: "eur",
225 items: [
226 {
227 title: "test",
228 quantity: 1,
229 unit_price: 100,
230 },
231 ],
232 })
233
234 const cart = await service.retrieveCart(createdCart.id, {
235 relations: ["items"],
236 })
237
238 expect(normalizeBigNumbers(cart)).toEqual(
239 expect.objectContaining({
240 id: createdCart.id,
241 currency_code: "eur",
242 items: expect.arrayContaining([
243 expect.objectContaining({
244 title: "test",
245 unit_price: 100,
246 }),
247 ]),
248 })
249 )
250 })
251
252 it("should create multiple carts with items", async () => {
253 const createdCarts = await service.createCarts([
254 {
255 currency_code: "eur",
256 items: [
257 {
258 title: "test",
259 quantity: 1,
260 unit_price: 100,
261 },
262 ],
263 },
264 {
265 currency_code: "usd",
266 items: [
267 {
268 title: "test-2",
269 quantity: 2,
270 unit_price: 200,
271 },
272 ],
273 },
274 ])
275
276 const carts = await service.listCarts(
277 { id: createdCarts.map((c) => c.id) },
278 {
279 relations: ["items"],
280 }
281 )
282
283 expect(normalizeBigNumbers(carts)).toEqual(
284 expect.arrayContaining([
285 expect.objectContaining({
286 currency_code: "eur",
287 items: expect.arrayContaining([
288 expect.objectContaining({
289 title: "test",
290 unit_price: 100,
291 }),
292 ]),
293 }),
294 expect.objectContaining({
295 currency_code: "usd",
296 items: expect.arrayContaining([
297 expect.objectContaining({
298 title: "test-2",
299 unit_price: 200,
300 }),
301 ]),
302 }),
303 ])
304 )
305 })
306 })
307
308 describe("update", () => {
309 it("should throw an error if cart does not exist", async () => {
310 const error = await service
311 .updateCarts([
312 {
313 id: "none-existing",
314 },
315 ])
316 .catch((e) => e)
317
318 expect(error.message).toContain(
319 'Cart with id "none-existing" not found'
320 )
321 })
322
323 it("should update a cart successfully", async () => {
324 const [createdCart] = await service.createCarts([
325 {
326 currency_code: "eur",
327 },
328 ])
329
330 const [updatedCart] = await service.updateCarts([
331 {
332 id: createdCart.id,
333 email: "test@email.com",
334 },
335 ])
336
337 const [cart] = await service.listCarts({ id: [createdCart.id] })
338
339 expect(cart).toEqual(
340 expect.objectContaining({
341 id: createdCart.id,
342 currency_code: "eur",
343 email: updatedCart.email,
344 })
345 )
346 })
347
348 it("should update a cart with selector successfully", async () => {
349 const [createdCart] = await service.createCarts([
350 {
351 currency_code: "eur",
352 },
353 ])
354
355 const [updatedCart] = await service.updateCarts(
356 { id: createdCart.id },
357 {
358 email: "test@email.com",
359 }
360 )
361
362 const [cart] = await service.listCarts({ id: [createdCart.id] })
363
364 expect(cart).toEqual(
365 expect.objectContaining({
366 id: createdCart.id,
367 currency_code: "eur",
368 email: updatedCart.email,
369 })
370 )
371 })
372
373 it("should update a cart with id successfully", async () => {
374 const [createdCart] = await service.createCarts([
375 {
376 currency_code: "eur",
377 },
378 ])
379
380 const updatedCart = await service.updateCarts(createdCart.id, {
381 email: "test@email.com",
382 })
383
384 const [cart] = await service.listCarts({ id: [createdCart.id] })
385
386 expect(cart).toEqual(
387 expect.objectContaining({
388 id: createdCart.id,
389 currency_code: "eur",
390 email: updatedCart.email,
391 })
392 )
393 })
394 })
395
396 describe("delete", () => {
397 it("should delete a cart successfully", async () => {
398 const [createdCart] = await service.createCarts([
399 {
400 currency_code: "eur",
401 },
402 ])
403
404 await service.deleteCarts([createdCart.id])
405
406 const carts = await service.listCarts({ id: [createdCart.id] })
407
408 expect(carts.length).toEqual(0)
409 })
410 })
411
412 describe("createAddresses", () => {
413 it("should create an address successfully", async () => {
414 const [createdAddress] = await service.createAddresses([
415 {
416 first_name: "John",
417 },
418 ])
419
420 const [address] = await service.listAddresses({
421 id: [createdAddress.id!],
422 })
423
424 expect(address).toEqual(
425 expect.objectContaining({
426 id: createdAddress.id,
427 first_name: "John",
428 })
429 )
430 })
431 })
432
433 describe("updateAddresses", () => {
434 it("should update an address successfully", async () => {
435 const [createdAddress] = await service.createAddresses([
436 {
437 first_name: "John",
438 },
439 ])
440
441 const [updatedAddress] = await service.updateAddresses([
442 { id: createdAddress.id!, first_name: "Jane" },
443 ])
444
445 expect(updatedAddress).toEqual(
446 expect.objectContaining({
447 id: createdAddress.id,
448 first_name: "Jane",
449 })
450 )
451 })
452 })
453
454 describe("deleteAddresses", () => {
455 it("should delete an address successfully", async () => {
456 const [createdAddress] = await service.createAddresses([
457 {
458 first_name: "John",
459 },
460 ])
461
462 await service.deleteAddresses([createdAddress.id!])
463
464 const [address] = await service.listAddresses({
465 id: [createdAddress.id!],
466 })
467
468 expect(address).toBe(undefined)
469 })
470 })
471
472 describe("addLineItems", () => {
473 it("should add a line item to cart succesfully", async () => {
474 const [createdCart] = await service.createCarts([
475 {
476 currency_code: "eur",
477 },
478 ])
479
480 const items = await service.addLineItems(createdCart.id, [
481 {
482 quantity: 1,
483 unit_price: 100,
484 title: "test",
485 },
486 ])
487
488 const cart = await service.retrieveCart(createdCart.id, {
489 relations: ["items"],
490 })
491
492 expect(items[0]).toEqual(
493 expect.objectContaining({
494 title: "test",
495 quantity: 1,
496 unit_price: 100,
497 })
498 )
499 expect(cart.items?.length).toBe(1)
500 })
501
502 it("should add multiple line items to cart succesfully", async () => {
503 const [createdCart] = await service.createCarts([
504 {
505 currency_code: "eur",
506 },
507 ])
508
509 await service.addLineItems([
510 {
511 quantity: 1,
512 unit_price: 100,
513 title: "test",
514 cart_id: createdCart.id,
515 },
516 {
517 quantity: 2,
518 unit_price: 200,
519 title: "test-2",
520 cart_id: createdCart.id,
521 },
522 ])
523
524 const cart = await service.retrieveCart(createdCart.id, {
525 relations: ["items"],
526 select: [],
527 })
528
529 expect(cart.items).toEqual(
530 expect.arrayContaining([
531 expect.objectContaining({
532 title: "test",
533 quantity: 1,
534 unit_price: 100,
535 }),
536 expect.objectContaining({
537 title: "test-2",
538 quantity: 2,
539 unit_price: 200,
540 }),
541 ])
542 )
543
544 expect(cart.items?.length).toBe(2)
545 })
546
547 it("should add multiple line items to multiple carts succesfully", async () => {
548 let [eurCart] = await service.createCarts([
549 {
550 currency_code: "eur",
551 },
552 ])
553
554 let [usdCart] = await service.createCarts([
555 {
556 currency_code: "usd",
557 },
558 ])
559
560 const items = await service.addLineItems([
561 {
562 cart_id: eurCart.id,
563 quantity: 1,
564 unit_price: 100,
565 title: "test",
566 },
567 {
568 cart_id: usdCart.id,
569 quantity: 1,
570 unit_price: 100,
571 title: "test",
572 },
573 ])
574
575 const carts = await service.listCarts(
576 { id: [eurCart.id, usdCart.id] },
577 { relations: ["items"] }
578 )
579
580 eurCart = carts.find((c) => c.currency_code === "eur")!
581 usdCart = carts.find((c) => c.currency_code === "usd")!
582
583 const eurItems = items.filter((i) => i.cart_id === eurCart.id)
584 const usdItems = items.filter((i) => i.cart_id === usdCart.id)
585
586 expect(eurCart.items![0].id).toBe(eurItems[0].id)
587 expect(usdCart.items![0].id).toBe(usdItems[0].id)
588
589 expect(eurCart.items?.length).toBe(1)
590 expect(usdCart.items?.length).toBe(1)
591 })
592
593 it("should throw if cart does not exist", async () => {
594 const error = await service
595 .addLineItems("foo", [
596 {
597 quantity: 1,
598 unit_price: 100,
599 title: "test",
600 tax_lines: [],
601 },
602 ])
603 .catch((e) => e)
604
605 expect(error.message).toContain("Cart with id: foo was not found")
606 })
607
608 it("should throw an error when required params are not passed adding to a single cart", async () => {
609 const [createdCart] = await service.createCarts([
610 {
611 currency_code: "eur",
612 },
613 ])
614
615 const error = await service
616 .addLineItems(createdCart.id, [
617 {
618 unit_price: 10,
619 title: "test",
620 },
621 ] as any)
622 .catch((e) => e)
623
624 expect(error.message).toContain(
625 "Value for LineItem.quantity is required, 'undefined' found"
626 )
627 })
628
629 it("should throw a generic error when required params are not passed using bulk add method", async () => {
630 const [createdCart] = await service.createCarts([
631 {
632 currency_code: "eur",
633 },
634 ])
635
636 const error = await service
637 .addLineItems([
638 {
639 cart_id: createdCart.id,
640 unit_price: 10,
641 title: "test",
642 },
643 ] as any)
644 .catch((e) => e)
645
646 expect(error.message).toContain(
647 "Value for LineItem.quantity is required, 'undefined' found"
648 )
649 })
650 })
651
652 describe("updateLineItems", () => {
653 it("should update a line item in cart succesfully with selector approach", async () => {
654 const [createdCart] = await service.createCarts([
655 {
656 currency_code: "eur",
657 },
658 ])
659
660 const [item] = await service.addLineItems(createdCart.id, [
661 {
662 quantity: 1,
663 unit_price: 100,
664 title: "test",
665 tax_lines: [],
666 },
667 ])
668
669 expect(item.title).toBe("test")
670
671 const [updatedItem] = await service.updateLineItems(
672 { cart_id: createdCart.id },
673 {
674 title: "test2",
675 }
676 )
677
678 expect(updatedItem.title).toBe("test2")
679 })
680
681 it("should update a line item in cart succesfully with data only approach", async () => {
682 const [createdCart] = await service.createCarts([
683 {
684 currency_code: "eur",
685 },
686 ])
687
688 const [item] = await service.addLineItems(createdCart.id, [
689 {
690 quantity: 1,
691 unit_price: 100,
692 title: "test",
693 tax_lines: [],
694 },
695 ])
696
697 expect(item.title).toBe("test")
698
699 const [updatedItem] = await service.updateLineItems([
700 {
701 id: item.id,
702 title: "test2",
703 },
704 ])
705
706 expect(updatedItem.title).toBe("test2")
707 })
708
709 it("should update a line item in cart succesfully with id approach", async () => {
710 const [createdCart] = await service.createCarts([
711 {
712 currency_code: "eur",
713 },
714 ])
715
716 const [item] = await service.addLineItems(createdCart.id, [
717 {
718 quantity: 1,
719 unit_price: 100,
720 title: "test",
721 tax_lines: [],
722 },
723 ])
724
725 expect(item.title).toBe("test")
726
727 const updatedItem = await service.updateLineItems(item.id, {
728 title: "test2",
729 })
730
731 expect(updatedItem.title).toBe("test2")
732 })
733
734 it("should update line items in carts succesfully with multi-selector approach", async () => {
735 const [createdCart] = await service.createCarts([
736 {
737 currency_code: "eur",
738 },
739 ])
740
741 const items = await service.addLineItems(createdCart.id, [
742 {
743 quantity: 1,
744 unit_price: 100,
745 title: "test",
746 },
747 {
748 quantity: 2,
749 unit_price: 200,
750 title: "other-test",
751 },
752 ])
753
754 expect(items).toEqual(
755 expect.arrayContaining([
756 expect.objectContaining({
757 title: "test",
758 quantity: 1,
759 unit_price: 100,
760 }),
761 expect.objectContaining({
762 title: "other-test",
763 quantity: 2,
764 unit_price: 200,
765 }),
766 ])
767 )
768
769 const itemOne = items.find((i) => i.title === "test")
770 const itemTwo = items.find((i) => i.title === "other-test")
771
772 const updatedItems = await service.updateLineItems([
773 {
774 selector: { cart_id: createdCart.id },
775 data: {
776 title: "changed-test",
777 },
778 },
779 {
780 selector: { id: itemTwo!.id },
781 data: {
782 title: "changed-other-test",
783 },
784 },
785 ])
786
787 expect(updatedItems).toEqual(
788 expect.arrayContaining([
789 expect.objectContaining({
790 title: "changed-test",
791 quantity: 1,
792 unit_price: 100,
793 }),
794 expect.objectContaining({
795 title: "changed-other-test",
796 quantity: 2,
797 unit_price: 200,
798 }),
799 ])
800 )
801 })
802 })
803
804 describe("removeLineItems", () => {
805 it("should remove a line item succesfully", async () => {
806 const [createdCart] = await service.createCarts([
807 {
808 currency_code: "eur",
809 },
810 ])
811
812 const [item] = await service.addLineItems(createdCart.id, [
813 {
814 quantity: 1,
815 unit_price: 100,
816 title: "test",
817 tax_lines: [],
818 },
819 ])
820
821 expect(item.title).toBe("test")
822
823 await service.softDeleteLineItems([item.id])
824
825 const cart = await service.retrieveCart(createdCart.id, {
826 relations: ["items"],
827 })
828
829 expect(cart.items?.length).toBe(0)
830 })
831
832 it("should remove multiple line items succesfully", async () => {
833 const [createdCart] = await service.createCarts([
834 {
835 currency_code: "eur",
836 },
837 ])
838
839 const [item, item2] = await service.addLineItems(createdCart.id, [
840 {
841 quantity: 1,
842 unit_price: 100,
843 title: "test",
844 },
845 {
846 quantity: 1,
847 unit_price: 100,
848 title: "test-2",
849 },
850 ])
851
852 await service.softDeleteLineItems([item.id, item2.id])
853
854 const cart = await service.retrieveCart(createdCart.id, {
855 relations: ["items"],
856 })
857
858 expect(cart.items?.length).toBe(0)
859 })
860 })
861
862 describe("addShippingMethods", () => {
863 it("should add a shipping method to cart succesfully", async () => {
864 const [createdCart] = await service.createCarts([
865 {
866 currency_code: "eur",
867 },
868 ])
869
870 const [method] = await service.addShippingMethods(createdCart.id, [
871 {
872 amount: 100,
873 name: "Test",
874 },
875 ])
876
877 const cart = await service.retrieveCart(createdCart.id, {
878 relations: ["shipping_methods"],
879 })
880
881 expect(method.id).toBe(cart.shipping_methods![0].id)
882 })
883
884 it("should throw when amount is negative", async () => {
885 const [createdCart] = await service.createCarts([
886 {
887 currency_code: "eur",
888 },
889 ])
890
891 const error = await service
892 .addShippingMethods(createdCart.id, [
893 {
894 amount: -100,
895 name: "Test",
896 },
897 ])
898 .catch((e) => e)
899
900 expect(error.name).toBe(CheckConstraintViolationException.name)
901 })
902
903 it("should add multiple shipping methods to multiple carts succesfully", async () => {
904 let [eurCart] = await service.createCarts([
905 {
906 currency_code: "eur",
907 },
908 ])
909
910 let [usdCart] = await service.createCarts([
911 {
912 currency_code: "usd",
913 },
914 ])
915
916 const methods = await service.addShippingMethods([
917 {
918 cart_id: eurCart.id,
919 amount: 100,
920 name: "Test One",
921 },
922 {
923 cart_id: usdCart.id,
924 amount: 100,
925 name: "Test One",
926 },
927 ])
928
929 const carts = await service.listCarts(
930 { id: [eurCart.id, usdCart.id] },
931 { relations: ["shipping_methods"] }
932 )
933
934 eurCart = carts.find((c) => c.currency_code === "eur")!
935 usdCart = carts.find((c) => c.currency_code === "usd")!
936
937 const eurMethods = methods.filter((m) => m.cart_id === eurCart.id)
938 const usdMethods = methods.filter((m) => m.cart_id === usdCart.id)
939
940 expect(eurCart.shipping_methods![0].id).toBe(eurMethods[0].id)
941 expect(usdCart.shipping_methods![0].id).toBe(usdMethods[0].id)
942
943 expect(eurCart.shipping_methods?.length).toBe(1)
944 expect(usdCart.shipping_methods?.length).toBe(1)
945 })
946 })
947
948 describe("removeShippingMethods", () => {
949 it("should remove a line item succesfully", async () => {
950 const [createdCart] = await service.createCarts([
951 {
952 currency_code: "eur",
953 },
954 ])
955
956 const [method] = await service.addShippingMethods(createdCart.id, [
957 {
958 amount: 100,
959 name: "test",
960 },
961 ])
962
963 expect(method.id).not.toBe(null)
964
965 await service.softDeleteShippingMethods([method.id])
966
967 const cart = await service.retrieveCart(createdCart.id, {
968 relations: ["shipping_methods"],
969 })
970
971 expect(cart.shipping_methods?.length).toBe(0)
972 })
973 })
974
975 describe("setLineItemAdjustments", () => {
976 it("should set line item adjustments for a cart", async () => {
977 const [createdCart] = await service.createCarts([
978 {
979 currency_code: "eur",
980 },
981 ])
982
983 const [itemOne] = await service.addLineItems(createdCart.id, [
984 {
985 quantity: 1,
986 unit_price: 100,
987 title: "test",
988 },
989 ])
990
991 const [itemTwo] = await service.addLineItems(createdCart.id, [
992 {
993 quantity: 2,
994 unit_price: 200,
995 title: "test-2",
996 },
997 ])
998
999 const adjustments = await service.setLineItemAdjustments(
1000 createdCart.id,
1001 [
1002 {
1003 item_id: itemOne.id,
1004 amount: 100,
1005 code: "FREE",
1006 },
1007 {
1008 item_id: itemTwo.id,
1009 amount: 200,
1010 code: "FREE-2",
1011 },
1012 ]
1013 )
1014
1015 expect(adjustments).toEqual(
1016 expect.arrayContaining([
1017 expect.objectContaining({
1018 item_id: itemOne.id,
1019 amount: 100,
1020 code: "FREE",
1021 }),
1022 expect.objectContaining({
1023 item_id: itemTwo.id,
1024 amount: 200,
1025 code: "FREE-2",
1026 }),
1027 ])
1028 )
1029 })
1030
1031 it("should replace line item adjustments for a cart", async () => {
1032 const [createdCart] = await service.createCarts([
1033 {
1034 currency_code: "eur",
1035 },
1036 ])
1037
1038 const [itemOne] = await service.addLineItems(createdCart.id, [
1039 {
1040 quantity: 1,
1041 unit_price: 100,
1042 title: "test",
1043 },
1044 ])
1045
1046 const adjustments = await service.setLineItemAdjustments(
1047 createdCart.id,
1048 [
1049 {
1050 item_id: itemOne.id,
1051 amount: 100,
1052 code: "FREE",
1053 },
1054 ]
1055 )
1056
1057 expect(adjustments).toEqual(
1058 expect.arrayContaining([
1059 expect.objectContaining({
1060 item_id: itemOne.id,
1061 amount: 100,
1062 code: "FREE",
1063 }),
1064 ])
1065 )
1066
1067 await service.setLineItemAdjustments(createdCart.id, [
1068 {
1069 item_id: itemOne.id,
1070 amount: 50,
1071 code: "50%",
1072 },
1073 ])
1074
1075 const cart = await service.retrieveCart(createdCart.id, {
1076 relations: ["items.adjustments"],
1077 select: [],
1078 })
1079
1080 expect(cart.items).toEqual(
1081 expect.arrayContaining([
1082 expect.objectContaining({
1083 id: itemOne.id,
1084 adjustments: expect.arrayContaining([
1085 expect.objectContaining({
1086 item_id: itemOne.id,
1087 amount: 50,
1088 code: "50%",
1089 }),
1090 ]),
1091 }),
1092 ])
1093 )
1094
1095 expect(cart.items?.length).toBe(1)
1096 expect(cart.items?.[0].adjustments?.length).toBe(1)
1097 })
1098
1099 it("should remove all line item adjustments for a cart", async () => {
1100 const [createdCart] = await service.createCarts([
1101 {
1102 currency_code: "eur",
1103 },
1104 ])
1105
1106 const [itemOne] = await service.addLineItems(createdCart.id, [
1107 {
1108 quantity: 1,
1109 unit_price: 100,
1110 title: "test",
1111 },
1112 ])
1113
1114 const adjustments = await service.setLineItemAdjustments(
1115 createdCart.id,
1116 [
1117 {
1118 item_id: itemOne.id,
1119 amount: 100,
1120 code: "FREE",
1121 },
1122 ]
1123 )
1124
1125 expect(adjustments).toEqual(
1126 expect.arrayContaining([
1127 expect.objectContaining({
1128 item_id: itemOne.id,
1129 amount: 100,
1130 code: "FREE",
1131 }),
1132 ])
1133 )
1134
1135 await service.setLineItemAdjustments(createdCart.id, [])
1136
1137 const cart = await service.retrieveCart(createdCart.id, {
1138 relations: ["items.adjustments"],
1139 })
1140
1141 expect(cart.items).toEqual(
1142 expect.arrayContaining([
1143 expect.objectContaining({
1144 id: itemOne.id,
1145 adjustments: [],
1146 }),
1147 ])
1148 )
1149
1150 expect(cart.items?.length).toBe(1)
1151 expect(cart.items?.[0].adjustments?.length).toBe(0)
1152 })
1153
1154 it("should update line item adjustments for a cart", async () => {
1155 const [createdCart] = await service.createCarts([
1156 {
1157 currency_code: "eur",
1158 },
1159 ])
1160
1161 const [itemOne] = await service.addLineItems(createdCart.id, [
1162 {
1163 quantity: 1,
1164 unit_price: 100,
1165 title: "test",
1166 },
1167 ])
1168
1169 const adjustments = await service.setLineItemAdjustments(
1170 createdCart.id,
1171 [
1172 {
1173 item_id: itemOne.id,
1174 amount: 100,
1175 code: "FREE",
1176 },
1177 ]
1178 )
1179
1180 expect(adjustments).toEqual(
1181 expect.arrayContaining([
1182 expect.objectContaining({
1183 item_id: itemOne.id,
1184 amount: 100,
1185 code: "FREE",
1186 }),
1187 ])
1188 )
1189
1190 await service.setLineItemAdjustments(createdCart.id, [
1191 {
1192 id: adjustments[0].id,
1193 item_id: itemOne.id,
1194 amount: 50,
1195 code: "50%",
1196 },
1197 ])
1198
1199 const cart = await service.retrieveCart(createdCart.id, {
1200 relations: ["items.adjustments"],
1201 select: [],
1202 })
1203
1204 expect(cart.items).toEqual(
1205 expect.arrayContaining([
1206 expect.objectContaining({
1207 id: itemOne.id,
1208 adjustments: [
1209 expect.objectContaining({
1210 id: adjustments[0].id,
1211 item_id: itemOne.id,
1212 amount: 50,
1213 code: "50%",
1214 }),
1215 ],
1216 }),
1217 ])
1218 )
1219
1220 expect(cart.items?.length).toBe(1)
1221 expect(cart.items?.[0].adjustments?.length).toBe(1)
1222 })
1223 })
1224
1225 describe("addLineItemAdjustments", () => {
1226 it("should add line item adjustments for items in a cart", async () => {
1227 const [createdCart] = await service.createCarts([
1228 {
1229 currency_code: "eur",
1230 },
1231 ])
1232
1233 const [itemOne] = await service.addLineItems(createdCart.id, [
1234 {
1235 quantity: 1,
1236 unit_price: 100,
1237 title: "test",
1238 },
1239 ])
1240
1241 const adjustments = await service.addLineItemAdjustments(
1242 createdCart.id,
1243 [
1244 {
1245 item_id: itemOne.id,
1246 amount: 100,
1247 code: "FREE",
1248 },
1249 ]
1250 )
1251
1252 expect(adjustments).toEqual(
1253 expect.arrayContaining([
1254 expect.objectContaining({
1255 item_id: itemOne.id,
1256 amount: 100,
1257 code: "FREE",
1258 }),
1259 ])
1260 )
1261 })
1262
1263 it("should add multiple line item adjustments for multiple line items", async () => {
1264 const [createdCart] = await service.createCarts([
1265 {
1266 currency_code: "eur",
1267 },
1268 ])
1269
1270 const [itemOne] = await service.addLineItems(createdCart.id, [
1271 {
1272 quantity: 1,
1273 unit_price: 100,
1274 title: "test",
1275 },
1276 ])
1277 const [itemTwo] = await service.addLineItems(createdCart.id, [
1278 {
1279 quantity: 2,
1280 unit_price: 200,
1281 title: "test-2",
1282 },
1283 ])
1284
1285 const adjustments = await service.addLineItemAdjustments(
1286 createdCart.id,
1287 [
1288 {
1289 item_id: itemOne.id,
1290 amount: 100,
1291 code: "FREE",
1292 },
1293 {
1294 item_id: itemTwo.id,
1295 amount: 150,
1296 code: "CODE-2",
1297 },
1298 ]
1299 )
1300
1301 expect(adjustments).toEqual(
1302 expect.arrayContaining([
1303 expect.objectContaining({
1304 item_id: itemOne.id,
1305 amount: 100,
1306 code: "FREE",
1307 }),
1308 expect.objectContaining({
1309 item_id: itemTwo.id,
1310 amount: 150,
1311 code: "CODE-2",
1312 }),
1313 ])
1314 )
1315 })
1316
1317 it("should add line item adjustments for line items on multiple carts", async () => {
1318 const [cartOne] = await service.createCarts([
1319 {
1320 currency_code: "eur",
1321 },
1322 ])
1323 const [cartTwo] = await service.createCarts([
1324 {
1325 currency_code: "usd",
1326 },
1327 ])
1328
1329 const [itemOne] = await service.addLineItems(cartOne.id, [
1330 {
1331 quantity: 1,
1332 unit_price: 100,
1333 title: "test",
1334 },
1335 ])
1336 const [itemTwo] = await service.addLineItems(cartTwo.id, [
1337 {
1338 quantity: 2,
1339 unit_price: 200,
1340 title: "test-2",
1341 },
1342 ])
1343
1344 await service.addLineItemAdjustments([
1345 // item from cart one
1346 {
1347 item_id: itemOne.id,
1348 amount: 100,
1349 code: "FREE",
1350 },
1351 // item from cart two
1352 {
1353 item_id: itemTwo.id,
1354 amount: 150,
1355 code: "CODE-2",
1356 },
1357 ])
1358
1359 const cartOneItems = await service.listLineItems(
1360 { cart_id: cartOne.id },
1361 { relations: ["adjustments"] }
1362 )
1363 const cartTwoItems = await service.listLineItems(
1364 { cart_id: cartTwo.id },
1365 { relations: ["adjustments"] }
1366 )
1367
1368 expect(cartOneItems).toEqual(
1369 expect.arrayContaining([
1370 expect.objectContaining({
1371 adjustments: expect.arrayContaining([
1372 expect.objectContaining({
1373 item_id: itemOne.id,
1374 amount: 100,
1375 code: "FREE",
1376 }),
1377 ]),
1378 }),
1379 ])
1380 )
1381 expect(cartTwoItems).toEqual(
1382 expect.arrayContaining([
1383 expect.objectContaining({
1384 adjustments: expect.arrayContaining([
1385 expect.objectContaining({
1386 item_id: itemTwo.id,
1387 amount: 150,
1388 code: "CODE-2",
1389 }),
1390 ]),
1391 }),
1392 ])
1393 )
1394 })
1395 })
1396
1397 describe("removeLineItemAdjustments", () => {
1398 it("should remove a line item succesfully", async () => {
1399 const [createdCart] = await service.createCarts([
1400 {
1401 currency_code: "eur",
1402 },
1403 ])
1404
1405 const [item] = await service.addLineItems(createdCart.id, [
1406 {
1407 quantity: 1,
1408 unit_price: 100,
1409 title: "test",
1410 },
1411 ])
1412
1413 const [adjustment] = await service.addLineItemAdjustments(
1414 createdCart.id,
1415 [
1416 {
1417 code: "50",
1418 item_id: item.id,
1419 amount: 50,
1420 },
1421 ]
1422 )
1423
1424 expect(adjustment.item_id).toBe(item.id)
1425
1426 await service.softDeleteLineItemAdjustments([adjustment.id])
1427
1428 const adjustments = await service.listLineItemAdjustments({
1429 item_id: item.id,
1430 })
1431
1432 expect(adjustments?.length).toBe(0)
1433 })
1434 })
1435
1436 describe("setShippingMethodAdjustments", () => {
1437 it("should set shipping method adjustments for a cart", async () => {
1438 const [createdCart] = await service.createCarts([
1439 {
1440 currency_code: "eur",
1441 },
1442 ])
1443
1444 const [shippingMethodOne] = await service.addShippingMethods(
1445 createdCart.id,
1446 [
1447 {
1448 amount: 100,
1449 name: "test",
1450 },
1451 ]
1452 )
1453
1454 const [shippingMethodTwo] = await service.addShippingMethods(
1455 createdCart.id,
1456 [
1457 {
1458 amount: 200,
1459 name: "test-2",
1460 },
1461 ]
1462 )
1463
1464 const adjustments = await service.setShippingMethodAdjustments(
1465 createdCart.id,
1466 [
1467 {
1468 shipping_method_id: shippingMethodOne.id,
1469 amount: 100,
1470 code: "FREE",
1471 },
1472 {
1473 shipping_method_id: shippingMethodTwo.id,
1474 amount: 200,
1475 code: "FREE-2",
1476 },
1477 ]
1478 )
1479
1480 expect(adjustments).toEqual(
1481 expect.arrayContaining([
1482 expect.objectContaining({
1483 shipping_method_id: shippingMethodOne.id,
1484 amount: 100,
1485 code: "FREE",
1486 }),
1487 expect.objectContaining({
1488 shipping_method_id: shippingMethodTwo.id,
1489 amount: 200,
1490 code: "FREE-2",
1491 }),
1492 ])
1493 )
1494 })
1495
1496 it("should replace shipping method adjustments for a cart", async () => {
1497 const [createdCart] = await service.createCarts([
1498 {
1499 currency_code: "eur",
1500 },
1501 ])
1502
1503 const [shippingMethodOne] = await service.addShippingMethods(
1504 createdCart.id,
1505 [
1506 {
1507 amount: 100,
1508 name: "test",
1509 },
1510 ]
1511 )
1512
1513 const adjustments = await service.setShippingMethodAdjustments(
1514 createdCart.id,
1515 [
1516 {
1517 shipping_method_id: shippingMethodOne.id,
1518 amount: 100,
1519 code: "FREE",
1520 },
1521 ]
1522 )
1523
1524 expect(adjustments).toEqual(
1525 expect.arrayContaining([
1526 expect.objectContaining({
1527 shipping_method_id: shippingMethodOne.id,
1528 amount: 100,
1529 code: "FREE",
1530 }),
1531 ])
1532 )
1533
1534 await service.setShippingMethodAdjustments(createdCart.id, [
1535 {
1536 shipping_method_id: shippingMethodOne.id,
1537 amount: 50,
1538 code: "50%",
1539 },
1540 ])
1541
1542 const cart = await service.retrieveCart(createdCart.id, {
1543 relations: ["shipping_methods.adjustments"],
1544 select: [],
1545 })
1546
1547 expect(cart.shipping_methods).toEqual(
1548 expect.arrayContaining([
1549 expect.objectContaining({
1550 id: shippingMethodOne.id,
1551 cart_id: createdCart.id,
1552 adjustments: expect.arrayContaining([
1553 expect.objectContaining({
1554 shipping_method_id: shippingMethodOne.id,
1555 amount: 50,
1556 code: "50%",
1557 }),
1558 ]),
1559 }),
1560 ])
1561 )
1562
1563 expect(cart.shipping_methods?.length).toBe(1)
1564 expect(cart.shipping_methods?.[0].adjustments?.length).toBe(1)
1565 })
1566
1567 it("should remove all shipping method adjustments for a cart", async () => {
1568 const [createdCart] = await service.createCarts([
1569 {
1570 currency_code: "eur",
1571 },
1572 ])
1573
1574 const [shippingMethodOne] = await service.addShippingMethods(
1575 createdCart.id,
1576 [
1577 {
1578 amount: 100,
1579 name: "test",
1580 },
1581 ]
1582 )
1583
1584 const adjustments = await service.setShippingMethodAdjustments(
1585 createdCart.id,
1586 [
1587 {
1588 shipping_method_id: shippingMethodOne.id,
1589 amount: 100,
1590 code: "FREE",
1591 },
1592 ]
1593 )
1594
1595 expect(adjustments).toEqual(
1596 expect.arrayContaining([
1597 expect.objectContaining({
1598 shipping_method_id: shippingMethodOne.id,
1599 amount: 100,
1600 code: "FREE",
1601 }),
1602 ])
1603 )
1604
1605 await service.setShippingMethodAdjustments(createdCart.id, [])
1606
1607 const cart = await service.retrieveCart(createdCart.id, {
1608 relations: ["shipping_methods.adjustments"],
1609 })
1610
1611 expect(cart.shipping_methods).toEqual(
1612 expect.arrayContaining([
1613 expect.objectContaining({
1614 id: shippingMethodOne.id,
1615 adjustments: [],
1616 }),
1617 ])
1618 )
1619
1620 expect(cart.shipping_methods?.length).toBe(1)
1621 expect(cart.shipping_methods?.[0].adjustments?.length).toBe(0)
1622 })
1623
1624 it("should update shipping method adjustments for a cart", async () => {
1625 const [createdCart] = await service.createCarts([
1626 {
1627 currency_code: "eur",
1628 },
1629 ])
1630
1631 const [shippingMethodOne] = await service.addShippingMethods(
1632 createdCart.id,
1633 [
1634 {
1635 amount: 100,
1636 name: "test",
1637 },
1638 ]
1639 )
1640
1641 const adjustments = await service.setShippingMethodAdjustments(
1642 createdCart.id,
1643 [
1644 {
1645 shipping_method_id: shippingMethodOne.id,
1646 amount: 100,
1647 code: "FREE",
1648 },
1649 ]
1650 )
1651
1652 expect(adjustments).toEqual(
1653 expect.arrayContaining([
1654 expect.objectContaining({
1655 shipping_method_id: shippingMethodOne.id,
1656 amount: 100,
1657 code: "FREE",
1658 }),
1659 ])
1660 )
1661
1662 await service.setShippingMethodAdjustments(createdCart.id, [
1663 {
1664 id: adjustments[0].id,
1665 amount: 50,
1666 code: "50%",
1667 },
1668 ])
1669
1670 const cart = await service.retrieveCart(createdCart.id, {
1671 relations: ["shipping_methods.adjustments"],
1672 select: [],
1673 })
1674
1675 expect(cart.shipping_methods).toEqual(
1676 expect.arrayContaining([
1677 expect.objectContaining({
1678 id: shippingMethodOne.id,
1679 adjustments: [
1680 expect.objectContaining({
1681 id: adjustments[0].id,
1682 shipping_method_id: shippingMethodOne.id,
1683 amount: 50,
1684 code: "50%",
1685 }),
1686 ],
1687 }),
1688 ])
1689 )
1690
1691 expect(cart.shipping_methods?.length).toBe(1)
1692 expect(cart.shipping_methods?.[0].adjustments?.length).toBe(1)
1693 })
1694 })
1695
1696 describe("addShippingMethodAdjustments", () => {
1697 it("should add shipping method adjustments in a cart", async () => {
1698 const [createdCart] = await service.createCarts([
1699 {
1700 currency_code: "eur",
1701 },
1702 ])
1703
1704 const [shippingMethodOne] = await service.addShippingMethods(
1705 createdCart.id,
1706 [
1707 {
1708 amount: 100,
1709 name: "test",
1710 },
1711 ]
1712 )
1713
1714 const adjustments = await service.addShippingMethodAdjustments(
1715 createdCart.id,
1716 [
1717 {
1718 shipping_method_id: shippingMethodOne.id,
1719 amount: 100,
1720 code: "FREE",
1721 },
1722 ]
1723 )
1724
1725 expect(adjustments).toEqual(
1726 expect.arrayContaining([
1727 expect.objectContaining({
1728 shipping_method_id: shippingMethodOne.id,
1729 amount: 100,
1730 code: "FREE",
1731 }),
1732 ])
1733 )
1734 })
1735
1736 it("should add multiple shipping method adjustments for multiple shipping methods", async () => {
1737 const [createdCart] = await service.createCarts([
1738 {
1739 currency_code: "eur",
1740 },
1741 ])
1742
1743 const [shippingMethodOne] = await service.addShippingMethods(
1744 createdCart.id,
1745 [
1746 {
1747 amount: 100,
1748 name: "test",
1749 },
1750 ]
1751 )
1752 const [shippingMethodTwo] = await service.addShippingMethods(
1753 createdCart.id,
1754 [
1755 {
1756 amount: 200,
1757 name: "test-2",
1758 },
1759 ]
1760 )
1761
1762 const adjustments = await service.addShippingMethodAdjustments(
1763 createdCart.id,
1764 [
1765 {
1766 shipping_method_id: shippingMethodOne.id,
1767 amount: 100,
1768 code: "FREE",
1769 },
1770 {
1771 shipping_method_id: shippingMethodTwo.id,
1772 amount: 150,
1773 code: "CODE-2",
1774 },
1775 ]
1776 )
1777
1778 expect(adjustments).toEqual(
1779 expect.arrayContaining([
1780 expect.objectContaining({
1781 shipping_method_id: shippingMethodOne.id,
1782 amount: 100,
1783 code: "FREE",
1784 }),
1785 expect.objectContaining({
1786 shipping_method_id: shippingMethodTwo.id,
1787 amount: 150,
1788 code: "CODE-2",
1789 }),
1790 ])
1791 )
1792 })
1793
1794 it("should add shipping method adjustments for shipping methods on multiple carts", async () => {
1795 const [cartOne] = await service.createCarts([
1796 {
1797 currency_code: "eur",
1798 },
1799 ])
1800 const [cartTwo] = await service.createCarts([
1801 {
1802 currency_code: "usd",
1803 },
1804 ])
1805
1806 const [shippingMethodOne] = await service.addShippingMethods(
1807 cartOne.id,
1808 [
1809 {
1810 amount: 100,
1811 name: "test",
1812 },
1813 ]
1814 )
1815 const [shippingMethodTwo] = await service.addShippingMethods(
1816 cartTwo.id,
1817 [
1818 {
1819 amount: 200,
1820 name: "test-2",
1821 },
1822 ]
1823 )
1824
1825 await service.addShippingMethodAdjustments([
1826 // item from cart one
1827 {
1828 shipping_method_id: shippingMethodOne.id,
1829 amount: 100,
1830 code: "FREE",
1831 },
1832 // item from cart two
1833 {
1834 shipping_method_id: shippingMethodTwo.id,
1835 amount: 150,
1836 code: "CODE-2",
1837 },
1838 ])
1839
1840 const cartOneMethods = await service.listShippingMethods(
1841 { cart_id: cartOne.id },
1842 { relations: ["adjustments"] }
1843 )
1844
1845 const cartTwoMethods = await service.listShippingMethods(
1846 { cart_id: cartTwo.id },
1847 { relations: ["adjustments"] }
1848 )
1849
1850 expect(cartOneMethods).toEqual(
1851 expect.arrayContaining([
1852 expect.objectContaining({
1853 adjustments: expect.arrayContaining([
1854 expect.objectContaining({
1855 shipping_method_id: shippingMethodOne.id,
1856 amount: 100,
1857 code: "FREE",
1858 }),
1859 ]),
1860 }),
1861 ])
1862 )
1863 expect(cartTwoMethods).toEqual(
1864 expect.arrayContaining([
1865 expect.objectContaining({
1866 adjustments: expect.arrayContaining([
1867 expect.objectContaining({
1868 shipping_method_id: shippingMethodTwo.id,
1869 amount: 150,
1870 code: "CODE-2",
1871 }),
1872 ]),
1873 }),
1874 ])
1875 )
1876 })
1877
1878 it("should throw if shipping method is not associated with cart", async () => {
1879 const [cartOne] = await service.createCarts([
1880 {
1881 currency_code: "eur",
1882 },
1883 ])
1884
1885 const [cartTwo] = await service.createCarts([
1886 {
1887 currency_code: "eur",
1888 },
1889 ])
1890
1891 const [shippingMethodOne] = await service.addShippingMethods(
1892 cartOne.id,
1893 [
1894 {
1895 amount: 100,
1896 name: "test",
1897 },
1898 ]
1899 )
1900
1901 const error = await service
1902 .addShippingMethodAdjustments(cartTwo.id, [
1903 {
1904 shipping_method_id: shippingMethodOne.id,
1905 amount: 100,
1906 code: "FREE",
1907 },
1908 ])
1909 .catch((e) => e)
1910
1911 expect(error.message).toBe(
1912 `Shipping method with id ${shippingMethodOne.id} does not exist on cart with id ${cartTwo.id}`
1913 )
1914 })
1915 })
1916
1917 describe("removeShippingMethodAdjustments", () => {
1918 it("should remove a shipping method succesfully", async () => {
1919 const [createdCart] = await service.createCarts([
1920 {
1921 currency_code: "eur",
1922 },
1923 ])
1924
1925 const [method] = await service.addShippingMethods(createdCart.id, [
1926 {
1927 amount: 100,
1928 name: "test",
1929 },
1930 ])
1931
1932 const [adjustment] = await service.addShippingMethodAdjustments(
1933 createdCart.id,
1934 [
1935 {
1936 shipping_method_id: method.id,
1937 amount: 50,
1938 code: "50%",
1939 },
1940 ]
1941 )
1942
1943 expect(adjustment.shipping_method_id).toBe(method.id)
1944
1945 await service.softDeleteShippingMethodAdjustments([adjustment.id])
1946
1947 const adjustments = await service.listShippingMethodAdjustments({
1948 shipping_method_id: method.id,
1949 })
1950
1951 expect(adjustments?.length).toBe(0)
1952 })
1953 })
1954
1955 describe("setLineItemTaxLines", () => {
1956 it("should set line item tax lines for a cart", async () => {
1957 const [createdCart] = await service.createCarts([
1958 {
1959 currency_code: "eur",
1960 },
1961 ])
1962
1963 const [itemOne] = await service.addLineItems(createdCart.id, [
1964 {
1965 quantity: 1,
1966 unit_price: 100,
1967 title: "test",
1968 },
1969 ])
1970
1971 const [itemTwo] = await service.addLineItems(createdCart.id, [
1972 {
1973 quantity: 2,
1974 unit_price: 200,
1975 title: "test-2",
1976 },
1977 ])
1978
1979 const taxLines = await service.setLineItemTaxLines(createdCart.id, [
1980 {
1981 item_id: itemOne.id,
1982 rate: 20,
1983 code: "TX",
1984 },
1985 {
1986 item_id: itemTwo.id,
1987 rate: 20,
1988 code: "TX",
1989 },
1990 ])
1991
1992 expect(taxLines).toEqual(
1993 expect.arrayContaining([
1994 expect.objectContaining({
1995 item_id: itemOne.id,
1996 rate: 20,
1997 code: "TX",
1998 }),
1999 expect.objectContaining({
2000 item_id: itemTwo.id,
2001 rate: 20,
2002 code: "TX",
2003 }),
2004 ])
2005 )
2006 })
2007
2008 it("should replace line item tax lines for a cart", async () => {
2009 const [createdCart] = await service.createCarts([
2010 {
2011 currency_code: "eur",
2012 },
2013 ])
2014
2015 const [itemOne] = await service.addLineItems(createdCart.id, [
2016 {
2017 quantity: 1,
2018 unit_price: 100,
2019 title: "test",
2020 },
2021 ])
2022
2023 const taxLines = await service.setLineItemTaxLines(createdCart.id, [
2024 {
2025 item_id: itemOne.id,
2026 rate: 20.753,
2027 code: "TX",
2028 },
2029 ])
2030
2031 expect(taxLines).toEqual(
2032 expect.arrayContaining([
2033 expect.objectContaining({
2034 item_id: itemOne.id,
2035 rate: 20.753,
2036 code: "TX",
2037 }),
2038 ])
2039 )
2040
2041 await service.setLineItemTaxLines(createdCart.id, [
2042 {
2043 item_id: itemOne.id,
2044 rate: 25.14789,
2045 code: "TX-2",
2046 },
2047 ])
2048
2049 const cart = await service.retrieveCart(createdCart.id, {
2050 relations: ["items.tax_lines"],
2051 })
2052
2053 expect(cart.items).toEqual(
2054 expect.arrayContaining([
2055 expect.objectContaining({
2056 id: itemOne.id,
2057 tax_lines: expect.arrayContaining([
2058 expect.objectContaining({
2059 item_id: itemOne.id,
2060 rate: 25.14789,
2061 code: "TX-2",
2062 }),
2063 ]),
2064 }),
2065 ])
2066 )
2067
2068 expect(cart.items?.length).toBe(1)
2069 expect(cart.items?.[0].tax_lines?.length).toBe(1)
2070 })
2071
2072 it("should remove all line item tax lines for a cart", async () => {
2073 const [createdCart] = await service.createCarts([
2074 {
2075 currency_code: "eur",
2076 },
2077 ])
2078
2079 const [itemOne] = await service.addLineItems(createdCart.id, [
2080 {
2081 quantity: 1,
2082 unit_price: 100,
2083 title: "test",
2084 },
2085 ])
2086
2087 const taxLines = await service.setLineItemTaxLines(createdCart.id, [
2088 {
2089 item_id: itemOne.id,
2090 rate: 20,
2091 code: "TX",
2092 },
2093 ])
2094
2095 expect(taxLines).toEqual(
2096 expect.arrayContaining([
2097 expect.objectContaining({
2098 item_id: itemOne.id,
2099 rate: 20,
2100 code: "TX",
2101 }),
2102 ])
2103 )
2104
2105 await service.setLineItemTaxLines(createdCart.id, [])
2106
2107 const cart = await service.retrieveCart(createdCart.id, {
2108 relations: ["items.tax_lines"],
2109 })
2110
2111 expect(cart.items).toEqual(
2112 expect.arrayContaining([
2113 expect.objectContaining({
2114 id: itemOne.id,
2115 tax_lines: [],
2116 }),
2117 ])
2118 )
2119
2120 expect(cart.items?.length).toBe(1)
2121 expect(cart.items?.[0].tax_lines?.length).toBe(0)
2122 })
2123
2124 it("should update line item tax lines for a cart", async () => {
2125 const [createdCart] = await service.createCarts([
2126 {
2127 currency_code: "eur",
2128 },
2129 ])
2130
2131 const [itemOne] = await service.addLineItems(createdCart.id, [
2132 {
2133 quantity: 1,
2134 unit_price: 100,
2135 title: "test",
2136 },
2137 ])
2138
2139 const taxLines = await service.setLineItemTaxLines(createdCart.id, [
2140 {
2141 item_id: itemOne.id,
2142 rate: 20,
2143 code: "TX",
2144 },
2145 ])
2146
2147 expect(taxLines).toEqual(
2148 expect.arrayContaining([
2149 expect.objectContaining({
2150 item_id: itemOne.id,
2151 rate: 20,
2152 code: "TX",
2153 }),
2154 ])
2155 )
2156
2157 await service.setLineItemTaxLines(createdCart.id, [
2158 {
2159 id: taxLines[0].id,
2160 item_id: itemOne.id,
2161 rate: 25,
2162 code: "TX",
2163 },
2164 ])
2165
2166 const cart = await service.retrieveCart(createdCart.id, {
2167 relations: ["items.tax_lines"],
2168 })
2169
2170 expect(cart.items).toEqual(
2171 expect.arrayContaining([
2172 expect.objectContaining({
2173 id: itemOne.id,
2174 tax_lines: [
2175 expect.objectContaining({
2176 id: taxLines[0].id,
2177 item_id: itemOne.id,
2178 rate: 25,
2179 code: "TX",
2180 }),
2181 ],
2182 }),
2183 ])
2184 )
2185
2186 expect(cart.items?.length).toBe(1)
2187 expect(cart.items?.[0].tax_lines?.length).toBe(1)
2188 })
2189
2190 it("should remove, update, and create line item tax lines for a cart", async () => {
2191 const [createdCart] = await service.createCarts([
2192 {
2193 currency_code: "eur",
2194 },
2195 ])
2196
2197 const [itemOne] = await service.addLineItems(createdCart.id, [
2198 {
2199 quantity: 1,
2200 unit_price: 100,
2201 title: "test",
2202 },
2203 ])
2204
2205 const taxLines = await service.setLineItemTaxLines(createdCart.id, [
2206 {
2207 item_id: itemOne.id,
2208 rate: 20,
2209 code: "TX",
2210 },
2211 {
2212 item_id: itemOne.id,
2213 rate: 25,
2214 code: "TX",
2215 },
2216 ])
2217
2218 expect(taxLines).toEqual(
2219 expect.arrayContaining([
2220 expect.objectContaining({
2221 item_id: itemOne.id,
2222 rate: 20,
2223 code: "TX",
2224 }),
2225 expect.objectContaining({
2226 item_id: itemOne.id,
2227 rate: 25,
2228 code: "TX",
2229 }),
2230 ])
2231 )
2232
2233 const taxLine = taxLines.find((tx) => tx.item_id === itemOne.id)
2234
2235 await service.setLineItemTaxLines(createdCart.id, [
2236 // update
2237 {
2238 id: taxLine.id,
2239 rate: 40,
2240 code: "TX",
2241 },
2242 // create
2243 {
2244 item_id: itemOne.id,
2245 rate: 25,
2246 code: "TX-2",
2247 },
2248 // remove: should remove the initial tax line for itemOne
2249 ])
2250
2251 const cart = await service.retrieveCart(createdCart.id, {
2252 relations: ["items.tax_lines"],
2253 })
2254
2255 expect(cart.items).toEqual(
2256 expect.arrayContaining([
2257 expect.objectContaining({
2258 id: itemOne.id,
2259 tax_lines: [
2260 expect.objectContaining({
2261 id: taxLine!.id,
2262 item_id: itemOne.id,
2263 rate: 40,
2264 code: "TX",
2265 }),
2266 expect.objectContaining({
2267 item_id: itemOne.id,
2268 rate: 25,
2269 code: "TX-2",
2270 }),
2271 ],
2272 }),
2273 ])
2274 )
2275
2276 expect(cart.items?.length).toBe(1)
2277 expect(cart.items?.[0].tax_lines?.length).toBe(2)
2278 })
2279
2280 it("should persist metadata and data on line item tax lines", async () => {
2281 const [createdCart] = await service.createCarts([
2282 {
2283 currency_code: "eur",
2284 },
2285 ])
2286
2287 const [item] = await service.addLineItems(createdCart.id, [
2288 {
2289 quantity: 1,
2290 unit_price: 100,
2291 title: "test",
2292 },
2293 ])
2294
2295 const taxLines = await service.setLineItemTaxLines(createdCart.id, [
2296 {
2297 item_id: item.id,
2298 rate: 8.9,
2299 code: "US-GA",
2300 metadata: { custom_key: "custom_value" },
2301 data: {
2302 state_rate: 4.0,
2303 county_rate: 3.0,
2304 city_rate: 1.9,
2305 },
2306 },
2307 ])
2308
2309 expect(taxLines).toEqual(
2310 expect.arrayContaining([
2311 expect.objectContaining({
2312 item_id: item.id,
2313 rate: 8.9,
2314 code: "US-GA",
2315 metadata: { custom_key: "custom_value" },
2316 data: {
2317 state_rate: 4.0,
2318 county_rate: 3.0,
2319 city_rate: 1.9,
2320 },
2321 }),
2322 ])
2323 )
2324 })
2325 })
2326
2327 describe("setShippingMethodTaxLines", () => {
2328 it("should set shipping item tax lines for a cart", async () => {
2329 const [createdCart] = await service.createCarts([
2330 {
2331 currency_code: "eur",
2332 },
2333 ])
2334
2335 const [itemOne] = await service.addShippingMethods(createdCart.id, [
2336 {
2337 name: "test",
2338 amount: 100,
2339 },
2340 ])
2341
2342 const [itemTwo] = await service.addShippingMethods(createdCart.id, [
2343 {
2344 name: "test-2",
2345 amount: 200,
2346 },
2347 ])
2348
2349 const taxLines = await service.setShippingMethodTaxLines(
2350 createdCart.id,
2351 [
2352 {
2353 shipping_method_id: itemOne.id,
2354 rate: 20,
2355 code: "TX",
2356 },
2357 {
2358 shipping_method_id: itemTwo.id,
2359 rate: 20,
2360 code: "TX",
2361 },
2362 ]
2363 )
2364
2365 expect(taxLines).toEqual(
2366 expect.arrayContaining([
2367 expect.objectContaining({
2368 shipping_method_id: itemOne.id,
2369 rate: 20,
2370 code: "TX",
2371 }),
2372 expect.objectContaining({
2373 shipping_method_id: itemTwo.id,
2374 rate: 20,
2375 code: "TX",
2376 }),
2377 ])
2378 )
2379 })
2380
2381 it("should replace shipping item tax lines for a cart", async () => {
2382 const [createdCart] = await service.createCarts([
2383 {
2384 currency_code: "eur",
2385 },
2386 ])
2387
2388 const [itemOne] = await service.addShippingMethods(createdCart.id, [
2389 {
2390 name: "test",
2391 amount: 100,
2392 },
2393 ])
2394
2395 const taxLines = await service.setShippingMethodTaxLines(
2396 createdCart.id,
2397 [
2398 {
2399 shipping_method_id: itemOne.id,
2400 rate: 20.753,
2401 code: "TX",
2402 },
2403 ]
2404 )
2405
2406 expect(taxLines).toEqual(
2407 expect.arrayContaining([
2408 expect.objectContaining({
2409 shipping_method_id: itemOne.id,
2410 rate: 20.753,
2411 code: "TX",
2412 }),
2413 ])
2414 )
2415
2416 await service.setShippingMethodTaxLines(createdCart.id, [
2417 {
2418 shipping_method_id: itemOne.id,
2419 rate: 25.14789,
2420 code: "TX-2",
2421 },
2422 ])
2423
2424 const cart = await service.retrieveCart(createdCart.id, {
2425 relations: ["shipping_methods.tax_lines"],
2426 })
2427
2428 expect(cart.shipping_methods).toEqual(
2429 expect.arrayContaining([
2430 expect.objectContaining({
2431 id: itemOne.id,
2432 tax_lines: expect.arrayContaining([
2433 expect.objectContaining({
2434 shipping_method_id: itemOne.id,
2435 rate: 25.14789,
2436 code: "TX-2",
2437 }),
2438 ]),
2439 }),
2440 ])
2441 )
2442
2443 expect(cart.shipping_methods?.length).toBe(1)
2444 expect(cart.shipping_methods?.[0].tax_lines?.length).toBe(1)
2445 })
2446
2447 it("should remove all shipping item tax lines for a cart", async () => {
2448 const [createdCart] = await service.createCarts([
2449 {
2450 currency_code: "eur",
2451 },
2452 ])
2453
2454 const [itemOne] = await service.addShippingMethods(createdCart.id, [
2455 {
2456 name: "test",
2457 amount: 100,
2458 },
2459 ])
2460
2461 const taxLines = await service.setShippingMethodTaxLines(
2462 createdCart.id,
2463 [
2464 {
2465 shipping_method_id: itemOne.id,
2466 rate: 20,
2467 code: "TX",
2468 },
2469 ]
2470 )
2471
2472 expect(taxLines).toEqual(
2473 expect.arrayContaining([
2474 expect.objectContaining({
2475 shipping_method_id: itemOne.id,
2476 rate: 20,
2477 code: "TX",
2478 }),
2479 ])
2480 )
2481
2482 await service.setShippingMethodTaxLines(createdCart.id, [])
2483
2484 const cart = await service.retrieveCart(createdCart.id, {
2485 relations: ["shipping_methods.tax_lines"],
2486 })
2487
2488 expect(cart.shipping_methods).toEqual(
2489 expect.arrayContaining([
2490 expect.objectContaining({
2491 id: itemOne.id,
2492 tax_lines: [],
2493 }),
2494 ])
2495 )
2496
2497 expect(cart.shipping_methods?.length).toBe(1)
2498 expect(cart.shipping_methods?.[0].tax_lines?.length).toBe(0)
2499 })
2500
2501 it("should update shipping item tax lines for a cart", async () => {
2502 const [createdCart] = await service.createCarts([
2503 {
2504 currency_code: "eur",
2505 },
2506 ])
2507
2508 const [itemOne] = await service.addShippingMethods(createdCart.id, [
2509 {
2510 name: "test",
2511 amount: 100,
2512 },
2513 ])
2514
2515 const taxLines = await service.setShippingMethodTaxLines(
2516 createdCart.id,
2517 [
2518 {
2519 shipping_method_id: itemOne.id,
2520 rate: 20,
2521 code: "TX",
2522 },
2523 ]
2524 )
2525
2526 expect(taxLines).toEqual(
2527 expect.arrayContaining([
2528 expect.objectContaining({
2529 shipping_method_id: itemOne.id,
2530 rate: 20,
2531 code: "TX",
2532 }),
2533 ])
2534 )
2535
2536 await service.setShippingMethodTaxLines(createdCart.id, [
2537 {
2538 id: taxLines[0].id,
2539 shipping_method_id: itemOne.id,
2540 rate: 25,
2541 code: "TX",
2542 },
2543 ])
2544
2545 const cart = await service.retrieveCart(createdCart.id, {
2546 relations: ["shipping_methods.tax_lines"],
2547 })
2548
2549 expect(cart.shipping_methods).toEqual(
2550 expect.arrayContaining([
2551 expect.objectContaining({
2552 id: itemOne.id,
2553 tax_lines: [
2554 expect.objectContaining({
2555 id: taxLines[0].id,
2556 shipping_method_id: itemOne.id,
2557 rate: 25,
2558 code: "TX",
2559 }),
2560 ],
2561 }),
2562 ])
2563 )
2564
2565 expect(cart.shipping_methods?.length).toBe(1)
2566 expect(cart.shipping_methods?.[0].tax_lines?.length).toBe(1)
2567 })
2568
2569 it("should remove, update, and create shipping item tax lines for a cart", async () => {
2570 const [createdCart] = await service.createCarts([
2571 {
2572 currency_code: "eur",
2573 },
2574 ])
2575
2576 const [itemOne] = await service.addShippingMethods(createdCart.id, [
2577 {
2578 name: "test",
2579 amount: 100,
2580 },
2581 ])
2582
2583 const taxLines = await service.setShippingMethodTaxLines(
2584 createdCart.id,
2585 [
2586 {
2587 shipping_method_id: itemOne.id,
2588 rate: 20,
2589 code: "TX",
2590 },
2591 {
2592 shipping_method_id: itemOne.id,
2593 rate: 25,
2594 code: "TX",
2595 },
2596 ]
2597 )
2598
2599 expect(taxLines).toEqual(
2600 expect.arrayContaining([
2601 expect.objectContaining({
2602 shipping_method_id: itemOne.id,
2603 rate: 20,
2604 code: "TX",
2605 }),
2606 expect.objectContaining({
2607 shipping_method_id: itemOne.id,
2608 rate: 25,
2609 code: "TX",
2610 }),
2611 ])
2612 )
2613
2614 const taxLine = taxLines.find(
2615 (tx) => tx.shipping_method_id === itemOne.id
2616 )
2617
2618 await service.setShippingMethodTaxLines(createdCart.id, [
2619 // update
2620 {
2621 id: taxLine.id,
2622 rate: 40,
2623 code: "TX",
2624 },
2625 // create
2626 {
2627 shipping_method_id: itemOne.id,
2628 rate: 25,
2629 code: "TX-2",
2630 },
2631 // remove: should remove the initial tax line for itemOne
2632 ])
2633
2634 const cart = await service.retrieveCart(createdCart.id, {
2635 relations: ["shipping_methods.tax_lines"],
2636 })
2637
2638 expect(cart.shipping_methods).toEqual(
2639 expect.arrayContaining([
2640 expect.objectContaining({
2641 id: itemOne.id,
2642 tax_lines: [
2643 expect.objectContaining({
2644 id: taxLine!.id,
2645 shipping_method_id: itemOne.id,
2646 rate: 40,
2647 code: "TX",
2648 }),
2649 expect.objectContaining({
2650 shipping_method_id: itemOne.id,
2651 rate: 25,
2652 code: "TX-2",
2653 }),
2654 ],
2655 }),
2656 ])
2657 )
2658
2659 expect(cart.shipping_methods?.length).toBe(1)
2660 expect(cart.shipping_methods?.[0].tax_lines?.length).toBe(2)
2661 })
2662
2663 it("should persist metadata and data on shipping method tax lines", async () => {
2664 const [createdCart] = await service.createCarts([
2665 {
2666 currency_code: "eur",
2667 },
2668 ])
2669
2670 const [shippingMethod] = await service.addShippingMethods(
2671 createdCart.id,
2672 [
2673 {
2674 amount: 10,
2675 name: "test",
2676 },
2677 ]
2678 )
2679
2680 const taxLines = await service.setShippingMethodTaxLines(
2681 createdCart.id,
2682 [
2683 {
2684 shipping_method_id: shippingMethod.id,
2685 rate: 8.9,
2686 code: "US-GA",
2687 metadata: { custom_key: "custom_value" },
2688 data: {
2689 state_rate: 4.0,
2690 county_rate: 3.0,
2691 city_rate: 1.9,
2692 },
2693 },
2694 ]
2695 )
2696
2697 expect(taxLines).toEqual(
2698 expect.arrayContaining([
2699 expect.objectContaining({
2700 shipping_method_id: shippingMethod.id,
2701 rate: 8.9,
2702 code: "US-GA",
2703 metadata: { custom_key: "custom_value" },
2704 data: {
2705 state_rate: 4.0,
2706 county_rate: 3.0,
2707 city_rate: 1.9,
2708 },
2709 }),
2710 ])
2711 )
2712 })
2713 })
2714
2715 describe("addLineItemAdjustments", () => {
2716 it("should add line item tax lines for items in a cart", async () => {
2717 const [createdCart] = await service.createCarts([
2718 {
2719 currency_code: "eur",
2720 },
2721 ])
2722
2723 const [itemOne] = await service.addLineItems(createdCart.id, [
2724 {
2725 quantity: 1,
2726 unit_price: 100,
2727 title: "test",
2728 },
2729 ])
2730
2731 const taxLines = await service.addLineItemTaxLines(createdCart.id, [
2732 {
2733 item_id: itemOne.id,
2734 rate: 20,
2735 code: "TX",
2736 },
2737 ])
2738
2739 expect(taxLines).toEqual(
2740 expect.arrayContaining([
2741 expect.objectContaining({
2742 item_id: itemOne.id,
2743 rate: 20,
2744 code: "TX",
2745 }),
2746 ])
2747 )
2748 })
2749
2750 it("should add multiple line item tax lines for multiple line items", async () => {
2751 const [createdCart] = await service.createCarts([
2752 {
2753 currency_code: "eur",
2754 },
2755 ])
2756
2757 const [itemOne] = await service.addLineItems(createdCart.id, [
2758 {
2759 quantity: 1,
2760 unit_price: 100,
2761 title: "test",
2762 },
2763 ])
2764 const [itemTwo] = await service.addLineItems(createdCart.id, [
2765 {
2766 quantity: 2,
2767 unit_price: 200,
2768 title: "test-2",
2769 },
2770 ])
2771
2772 const taxLines = await service.addLineItemTaxLines(createdCart.id, [
2773 {
2774 item_id: itemOne.id,
2775 rate: 20,
2776 code: "TX",
2777 },
2778 {
2779 item_id: itemTwo.id,
2780 rate: 20,
2781 code: "TX",
2782 },
2783 ])
2784
2785 expect(taxLines).toEqual(
2786 expect.arrayContaining([
2787 expect.objectContaining({
2788 item_id: itemOne.id,
2789 rate: 20,
2790 code: "TX",
2791 }),
2792 expect.objectContaining({
2793 item_id: itemTwo.id,
2794 rate: 20,
2795 code: "TX",
2796 }),
2797 ])
2798 )
2799 })
2800
2801 it("should add line item tax lines for line items on multiple carts", async () => {
2802 const [cartOne] = await service.createCarts([
2803 {
2804 currency_code: "eur",
2805 },
2806 ])
2807 const [cartTwo] = await service.createCarts([
2808 {
2809 currency_code: "usd",
2810 },
2811 ])
2812
2813 const [itemOne] = await service.addLineItems(cartOne.id, [
2814 {
2815 quantity: 1,
2816 unit_price: 100,
2817 title: "test",
2818 },
2819 ])
2820 const [itemTwo] = await service.addLineItems(cartTwo.id, [
2821 {
2822 quantity: 2,
2823 unit_price: 200,
2824 title: "test-2",
2825 },
2826 ])
2827
2828 await service.addLineItemTaxLines([
2829 // item from cart one
2830 {
2831 item_id: itemOne.id,
2832 rate: 20,
2833 code: "TX",
2834 },
2835 // item from cart two
2836 {
2837 item_id: itemTwo.id,
2838 rate: 25,
2839 code: "TX-2",
2840 },
2841 ])
2842
2843 const cartOneItems = await service.listLineItems(
2844 { cart_id: cartOne.id },
2845 { relations: ["tax_lines"] }
2846 )
2847 const cartTwoItems = await service.listLineItems(
2848 { cart_id: cartTwo.id },
2849 { relations: ["tax_lines"] }
2850 )
2851
2852 expect(cartOneItems).toEqual(
2853 expect.arrayContaining([
2854 expect.objectContaining({
2855 tax_lines: expect.arrayContaining([
2856 expect.objectContaining({
2857 item_id: itemOne.id,
2858 rate: 20,
2859 code: "TX",
2860 }),
2861 ]),
2862 }),
2863 ])
2864 )
2865 expect(cartTwoItems).toEqual(
2866 expect.arrayContaining([
2867 expect.objectContaining({
2868 tax_lines: expect.arrayContaining([
2869 expect.objectContaining({
2870 item_id: itemTwo.id,
2871 rate: 25,
2872 code: "TX-2",
2873 }),
2874 ]),
2875 }),
2876 ])
2877 )
2878 })
2879 })
2880
2881 describe("removeLineItemAdjustments", () => {
2882 it("should remove line item tax line succesfully", async () => {
2883 const [createdCart] = await service.createCarts([
2884 {
2885 currency_code: "eur",
2886 },
2887 ])
2888
2889 const [item] = await service.addLineItems(createdCart.id, [
2890 {
2891 quantity: 1,
2892 unit_price: 100,
2893 title: "test",
2894 },
2895 ])
2896
2897 const [taxLine] = await service.addLineItemTaxLines(createdCart.id, [
2898 {
2899 item_id: item.id,
2900 rate: 20,
2901 code: "TX",
2902 },
2903 ])
2904
2905 expect(taxLine.item_id).toBe(item.id)
2906
2907 await service.softDeleteLineItemTaxLines([taxLine.id])
2908
2909 const taxLines = await service.listLineItemTaxLines({
2910 item_id: item.id,
2911 })
2912
2913 expect(taxLines?.length).toBe(0)
2914 })
2915 })
2916 })
2917
2918 it("should calculate totals of a cart", async () => {
2919 const [createdCart] = await service.createCarts([
2920 {
2921 currency_code: "eur",
2922 },
2923 ])
2924
2925 const [itemOne] = await service.addLineItems(createdCart.id, [
2926 {
2927 quantity: 1,
2928 unit_price: 100,
2929 title: "test",
2930 },
2931 ])
2932
2933 const [itemTwo] = await service.addLineItems(createdCart.id, [
2934 {
2935 quantity: 2,
2936 unit_price: 200,
2937 title: "test-2",
2938 },
2939 ])
2940
2941 await service.setLineItemAdjustments(createdCart.id, [
2942 {
2943 item_id: itemOne.id,
2944 amount: 100,
2945 code: "FREE",
2946 },
2947 {
2948 item_id: itemTwo.id,
2949 amount: 200,
2950 code: "FREE-2",
2951 },
2952 ])
2953
2954 await service.addShippingMethods(createdCart.id, [
2955 {
2956 amount: 10,
2957 name: "Test",
2958 },
2959 ])
2960
2961 const cart = await service.retrieveCart(createdCart.id, {
2962 select: ["total"],
2963 })
2964 expect(cart.total).toBeInstanceOf(BigNumber)
2965
2966 const asJson = JSON.parse(JSON.stringify(cart))
2967 expect(asJson).toEqual({
2968 id: createdCart.id,
2969 items: [
2970 {
2971 id: itemOne.id,
2972 cart_id: createdCart.id,
2973 title: "test",
2974 subtitle: null,
2975 thumbnail: null,
2976 quantity: 1,
2977 variant_id: null,
2978 product_id: null,
2979 product_title: null,
2980 product_description: null,
2981 product_subtitle: null,
2982 product_type: null,
2983 product_type_id: null,
2984 product_collection: null,
2985 product_handle: null,
2986 variant_sku: null,
2987 variant_barcode: null,
2988 variant_title: null,
2989 variant_option_values: null,
2990 requires_shipping: true,
2991 is_discountable: true,
2992 is_giftcard: false,
2993 is_tax_inclusive: false,
2994 is_custom_price: false,
2995 raw_compare_at_unit_price: null,
2996 raw_unit_price: {
2997 value: "100",
2998 precision: 20,
2999 },
3000 metadata: null,
3001 created_at: expect.any(String),
3002 updated_at: expect.any(String),
3003 deleted_at: null,
3004 tax_lines: [],
3005 adjustments: [
3006 {
3007 id: expect.any(String),
3008 description: null,
3009 code: "FREE",
3010 raw_amount: {
3011 value: "100",
3012 precision: 20,
3013 },
3014 provider_id: null,
3015 metadata: null,
3016 created_at: expect.any(String),
3017 updated_at: expect.any(String),
3018 item_id: expect.any(String),
3019 is_tax_inclusive: false,
3020 promotion_id: null,
3021 deleted_at: null,
3022 amount: 100,
3023 subtotal: 100,
3024 total: 100,
3025 raw_subtotal: {
3026 value: "100",
3027 precision: 20,
3028 },
3029 raw_total: {
3030 value: "100",
3031 precision: 20,
3032 },
3033 },
3034 ],
3035 compare_at_unit_price: null,
3036 unit_price: 100,
3037 subtotal: 100,
3038 total: 0,
3039 original_total: 100,
3040 original_subtotal: 100,
3041 discount_total: 100,
3042 discount_subtotal: 100,
3043 discount_tax_total: 0,
3044 tax_total: 0,
3045 original_tax_total: 0,
3046 raw_subtotal: {
3047 value: "100",
3048 precision: 20,
3049 },
3050 raw_total: {
3051 value: "0",
3052 precision: 20,
3053 },
3054 raw_original_total: {
3055 value: "100",
3056 precision: 20,
3057 },
3058 raw_original_subtotal: {
3059 value: "100",
3060 precision: 20,
3061 },
3062 raw_discount_total: {
3063 value: "100",
3064 precision: 20,
3065 },
3066 raw_discount_subtotal: {
3067 value: "100",
3068 precision: 20,
3069 },
3070 raw_discount_tax_total: {
3071 value: "0",
3072 precision: 20,
3073 },
3074 raw_tax_total: {
3075 value: "0",
3076 precision: 20,
3077 },
3078 raw_original_tax_total: {
3079 value: "0",
3080 precision: 20,
3081 },
3082 },
3083 {
3084 id: itemTwo.id,
3085 cart_id: createdCart.id,
3086 title: "test-2",
3087 subtitle: null,
3088 thumbnail: null,
3089 quantity: 2,
3090 variant_id: null,
3091 product_id: null,
3092 product_title: null,
3093 product_description: null,
3094 product_subtitle: null,
3095 product_type: null,
3096 product_type_id: null,
3097 product_collection: null,
3098 product_handle: null,
3099 variant_sku: null,
3100 variant_barcode: null,
3101 variant_title: null,
3102 variant_option_values: null,
3103 requires_shipping: true,
3104 is_discountable: true,
3105 is_giftcard: false,
3106 is_tax_inclusive: false,
3107 is_custom_price: false,
3108 raw_compare_at_unit_price: null,
3109 raw_unit_price: {
3110 value: "200",
3111 precision: 20,
3112 },
3113 metadata: null,
3114 created_at: expect.any(String),
3115 updated_at: expect.any(String),
3116 deleted_at: null,
3117 tax_lines: [],
3118 adjustments: [
3119 {
3120 id: expect.any(String),
3121 description: null,
3122 code: "FREE-2",
3123 raw_amount: {
3124 value: "200",
3125 precision: 20,
3126 },
3127 provider_id: null,
3128 metadata: null,
3129 created_at: expect.any(String),
3130 updated_at: expect.any(String),
3131 item_id: expect.any(String),
3132 is_tax_inclusive: false,
3133 promotion_id: null,
3134 deleted_at: null,
3135 amount: 200,
3136 subtotal: 200,
3137 total: 200,
3138 raw_subtotal: {
3139 value: "200",
3140 precision: 20,
3141 },
3142 raw_total: {
3143 value: "200",
3144 precision: 20,
3145 },
3146 },
3147 ],
3148 compare_at_unit_price: null,
3149 unit_price: 200,
3150 subtotal: 400,
3151 total: 200,
3152 original_total: 400,
3153 original_subtotal: 400,
3154 discount_total: 200,
3155 discount_subtotal: 200,
3156 discount_tax_total: 0,
3157 tax_total: 0,
3158 original_tax_total: 0,
3159 raw_subtotal: {
3160 value: "400",
3161 precision: 20,
3162 },
3163 raw_total: {
3164 value: "200",
3165 precision: 20,
3166 },
3167 raw_original_total: {
3168 value: "400",
3169 precision: 20,
3170 },
3171 raw_original_subtotal: {
3172 value: "400",
3173 precision: 20,
3174 },
3175 raw_discount_total: {
3176 value: "200",
3177 precision: 20,
3178 },
3179 raw_discount_subtotal: {
3180 value: "200",
3181 precision: 20,
3182 },
3183 raw_discount_tax_total: {
3184 value: "0",
3185 precision: 20,
3186 },
3187 raw_tax_total: {
3188 value: "0",
3189 precision: 20,
3190 },
3191 raw_original_tax_total: {
3192 value: "0",
3193 precision: 20,
3194 },
3195 },
3196 ],
3197 shipping_methods: [
3198 {
3199 id: expect.any(String),
3200 cart_id: expect.any(String),
3201 name: "Test",
3202 description: null,
3203 raw_amount: {
3204 value: "10",
3205 precision: 20,
3206 },
3207 is_tax_inclusive: false,
3208 shipping_option_id: null,
3209 data: null,
3210 metadata: null,
3211 created_at: expect.any(String),
3212 updated_at: expect.any(String),
3213 deleted_at: null,
3214 tax_lines: [],
3215 adjustments: [],
3216 amount: 10,
3217 subtotal: 10,
3218 total: 10,
3219 original_total: 10,
3220 original_subtotal: 10,
3221 discount_total: 0,
3222 discount_subtotal: 0,
3223 discount_tax_total: 0,
3224 tax_total: 0,
3225 original_tax_total: 0,
3226 raw_subtotal: {
3227 value: "10",
3228 precision: 20,
3229 },
3230 raw_total: {
3231 value: "10",
3232 precision: 20,
3233 },
3234 raw_original_total: {
3235 value: "10",
3236 precision: 20,
3237 },
3238 raw_original_subtotal: {
3239 value: "10",
3240 precision: 20,
3241 },
3242 raw_discount_total: {
3243 value: "0",
3244 precision: 20,
3245 },
3246 raw_discount_subtotal: {
3247 value: "0",
3248 precision: 20,
3249 },
3250 raw_discount_tax_total: {
3251 value: "0",
3252 precision: 20,
3253 },
3254 raw_tax_total: {
3255 value: "0",
3256 precision: 20,
3257 },
3258 raw_original_tax_total: {
3259 value: "0",
3260 precision: 20,
3261 },
3262 },
3263 ],
3264 credit_lines: [],
3265 credit_line_subtotal: 0,
3266 credit_line_tax_total: 0,
3267 credit_line_total: 0,
3268 raw_credit_line_subtotal: {
3269 precision: 20,
3270 value: "0",
3271 },
3272 raw_credit_line_tax_total: {
3273 precision: 20,
3274 value: "0",
3275 },
3276 raw_credit_line_total: {
3277 precision: 20,
3278 value: "0",
3279 },
3280 total: 210,
3281 subtotal: 510,
3282 tax_total: 0,
3283 discount_total: 300,
3284 discount_subtotal: 300,
3285 discount_tax_total: 0,
3286 original_total: 510,
3287 original_subtotal: 510,
3288 original_tax_total: 0,
3289 item_total: 200,
3290 item_subtotal: 500,
3291 item_tax_total: 0,
3292 item_discount_total: 300,
3293 original_item_total: 500,
3294 original_item_subtotal: 500,
3295 original_item_tax_total: 0,
3296 shipping_total: 10,
3297 shipping_subtotal: 10,
3298 shipping_tax_total: 0,
3299 shipping_discount_total: 0,
3300 original_shipping_tax_total: 0,
3301 original_shipping_subtotal: 10,
3302 original_shipping_total: 10,
3303 raw_total: {
3304 value: "210",
3305 precision: 20,
3306 },
3307 raw_subtotal: {
3308 value: "510",
3309 precision: 20,
3310 },
3311 raw_tax_total: {
3312 value: "0",
3313 precision: 20,
3314 },
3315 raw_discount_total: {
3316 value: "300",
3317 precision: 20,
3318 },
3319 raw_item_discount_total: {
3320 value: "300",
3321 precision: 20,
3322 },
3323 raw_shipping_discount_total: {
3324 value: "0",
3325 precision: 20,
3326 },
3327 raw_discount_subtotal: {
3328 value: "300",
3329 precision: 20,
3330 },
3331 raw_discount_tax_total: {
3332 value: "0",
3333 precision: 20,
3334 },
3335 raw_original_total: {
3336 value: "510",
3337 precision: 20,
3338 },
3339 raw_original_subtotal: {
3340 value: "510",
3341 precision: 20,
3342 },
3343 raw_original_tax_total: {
3344 value: "0",
3345 precision: 20,
3346 },
3347 raw_item_total: {
3348 value: "200",
3349 precision: 20,
3350 },
3351 raw_item_subtotal: {
3352 value: "500",
3353 precision: 20,
3354 },
3355 raw_item_tax_total: {
3356 value: "0",
3357 precision: 20,
3358 },
3359 raw_original_item_total: {
3360 value: "500",
3361 precision: 20,
3362 },
3363 raw_original_item_subtotal: {
3364 value: "500",
3365 precision: 20,
3366 },
3367 raw_original_item_tax_total: {
3368 value: "0",
3369 precision: 20,
3370 },
3371 raw_shipping_total: {
3372 value: "10",
3373 precision: 20,
3374 },
3375 raw_shipping_subtotal: {
3376 value: "10",
3377 precision: 20,
3378 },
3379 raw_shipping_tax_total: {
3380 value: "0",
3381 precision: 20,
3382 },
3383 raw_original_shipping_tax_total: {
3384 value: "0",
3385 precision: 20,
3386 },
3387 raw_original_shipping_subtotal: {
3388 value: "10",
3389 precision: 20,
3390 },
3391 raw_original_shipping_total: {
3392 value: "10",
3393 precision: 20,
3394 },
3395 })
3396 })
3397 },
3398 })