@merchi/cart 1.4.79 → 1.4.80
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/CartProvider.js +31 -22
- package/esm/CartProvider.load.test.js +603 -548
- package/lib/CartProvider.js +31 -22
- package/package.json +1 -1
- package/src/CartProvider.load.test.tsx +17 -1
- package/src/CartProvider.tsx +8 -0
package/lib/CartProvider.js
CHANGED
|
@@ -608,7 +608,7 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
608
608
|
// so a later refetch does not put that cart back after the shopper replaced it.
|
|
609
609
|
function _getCart() {
|
|
610
610
|
_getCart = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(cartIdAndToken) {
|
|
611
|
-
var id, _cart2, cartJson, isTest, jobSource, saved, _t6;
|
|
611
|
+
var id, _cartJson$invoice, _cart2, cartJson, isTest, _cartJson$client2, jobSource, saved, _t6;
|
|
612
612
|
return _regenerator().w(function (_context6) {
|
|
613
613
|
while (1) switch (_context6.p = _context6.n) {
|
|
614
614
|
case 0:
|
|
@@ -636,63 +636,72 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
636
636
|
case 4:
|
|
637
637
|
return _context6.a(2, _context6.v);
|
|
638
638
|
case 5:
|
|
639
|
+
if (!(((_cartJson$invoice = cartJson.invoice) === null || _cartJson$invoice === void 0 ? void 0 : _cartJson$invoice.unpaid) === false)) {
|
|
640
|
+
_context6.n = 6;
|
|
641
|
+
break;
|
|
642
|
+
}
|
|
643
|
+
setCartClient((_cartJson$client2 = cartJson.client) !== null && _cartJson$client2 !== void 0 && _cartJson$client2.id ? cartJson.client : null);
|
|
644
|
+
setCart(_objectSpread({}, cartJson));
|
|
645
|
+
dismissInitializationError();
|
|
646
|
+
return _context6.a(2, cartJson);
|
|
647
|
+
case 6:
|
|
639
648
|
jobSource = (0, _job_source.jobSourceFieldsForApi)((0, _job_source.captureAndReadJobSource)());
|
|
640
649
|
if (!(0, _job_source.cartNeedsSourceUpdate)(cartJson, jobSource)) {
|
|
641
|
-
_context6.n =
|
|
650
|
+
_context6.n = 8;
|
|
642
651
|
break;
|
|
643
652
|
}
|
|
644
|
-
_context6.n =
|
|
653
|
+
_context6.n = 7;
|
|
645
654
|
return merchi.authenticatedFetch("/carts/".concat(_cart2.id, "/"), {
|
|
646
655
|
method: 'PATCH',
|
|
647
656
|
body: (0, _job_source.appendJobSourceToFormData)(_cart2.toFormData(), jobSource),
|
|
648
657
|
query: [['embed', JSON.stringify(_helpers.cartEmbed)], ['skip_rights', 'y']]
|
|
649
658
|
});
|
|
650
|
-
case
|
|
659
|
+
case 7:
|
|
651
660
|
saved = _context6.v;
|
|
652
661
|
_cart2.fromJson(saved.cart);
|
|
653
662
|
cartJson = _cart2.toJson();
|
|
654
|
-
case
|
|
663
|
+
case 8:
|
|
655
664
|
if (!((0, _cart3.stripeIsValidAndActive)(cartJson) || isTest)) {
|
|
656
|
-
_context6.n =
|
|
665
|
+
_context6.n = 10;
|
|
657
666
|
break;
|
|
658
667
|
}
|
|
659
|
-
_context6.n =
|
|
668
|
+
_context6.n = 9;
|
|
660
669
|
return attachSavedClientIfNeeded(cartJson);
|
|
661
|
-
case
|
|
670
|
+
case 9:
|
|
662
671
|
cartJson = _context6.v;
|
|
663
672
|
setCart(_objectSpread({}, cartJson));
|
|
664
673
|
dismissInitializationError();
|
|
665
674
|
return _context6.a(2, cartJson);
|
|
666
|
-
case
|
|
675
|
+
case 10:
|
|
667
676
|
console.error("MErhci cart error: Stripe payment " + "options have not been correctly set up. " + "Check the company profile payment options tab " + "to set and edit stripe payment options.");
|
|
668
677
|
setCartSettingsInvalid(true);
|
|
669
|
-
case 10:
|
|
670
|
-
_context6.n = 14;
|
|
671
|
-
break;
|
|
672
678
|
case 11:
|
|
673
|
-
_context6.
|
|
679
|
+
_context6.n = 15;
|
|
680
|
+
break;
|
|
681
|
+
case 12:
|
|
682
|
+
_context6.p = 12;
|
|
674
683
|
_t6 = _context6.v;
|
|
675
684
|
if (!(0, _cartLoadFailure.canReplaceMissingCart)(_t6)) {
|
|
676
|
-
_context6.n =
|
|
685
|
+
_context6.n = 14;
|
|
677
686
|
break;
|
|
678
687
|
}
|
|
679
|
-
_context6.n =
|
|
688
|
+
_context6.n = 13;
|
|
680
689
|
return createCartAndCookie(undefined, true);
|
|
681
|
-
case 12:
|
|
682
|
-
return _context6.a(2, _context6.v);
|
|
683
690
|
case 13:
|
|
691
|
+
return _context6.a(2, _context6.v);
|
|
692
|
+
case 14:
|
|
684
693
|
// A network/server/auth failure does not mean the saved cart is gone.
|
|
685
694
|
// Preserve its cookie and contents so the shopper can retry the read.
|
|
686
695
|
(0, _reportFailure.reportCartFailure)(_t6, _reportFailure.CART_FAILURE_SOURCES.load);
|
|
687
696
|
showCartError((_t6 === null || _t6 === void 0 ? void 0 : _t6.errorMessage) || (_t6 === null || _t6 === void 0 ? void 0 : _t6.message) || 'Unable to load cart. Please try again.', true);
|
|
688
|
-
case 14:
|
|
689
|
-
_context6.p = 14;
|
|
690
|
-
setFetchingCart(false);
|
|
691
|
-
return _context6.f(14);
|
|
692
697
|
case 15:
|
|
698
|
+
_context6.p = 15;
|
|
699
|
+
setFetchingCart(false);
|
|
700
|
+
return _context6.f(15);
|
|
701
|
+
case 16:
|
|
693
702
|
return _context6.a(2, undefined);
|
|
694
703
|
}
|
|
695
|
-
}, _callee6, null, [[1,
|
|
704
|
+
}, _callee6, null, [[1, 12, 15, 16]]);
|
|
696
705
|
}));
|
|
697
706
|
return _getCart.apply(this, arguments);
|
|
698
707
|
}
|
package/package.json
CHANGED
|
@@ -4,6 +4,7 @@ import { act } from 'react-dom/test-utils';
|
|
|
4
4
|
|
|
5
5
|
import CartProvider, { useCartContext } from './CartProvider';
|
|
6
6
|
import { getCartCookie, setCartCookie } from './utilities/cookie';
|
|
7
|
+
import { cartNeedsSourceUpdate } from './utilities/job_source';
|
|
7
8
|
import { reportCartFailure, CART_FAILURE_SOURCES } from './utilities/reportFailure';
|
|
8
9
|
import { tabIdClearCart, tabIdItems } from './utilities/tabs';
|
|
9
10
|
|
|
@@ -39,7 +40,7 @@ jest.mock('./utilities/local_storage', () => ({ getSavedCheckoutCustomer: () =>
|
|
|
39
40
|
jest.mock('./utilities/job_source', () => ({
|
|
40
41
|
captureAndReadJobSource: () => null,
|
|
41
42
|
jobSourceFieldsForApi: () => ({}),
|
|
42
|
-
cartNeedsSourceUpdate: () => false,
|
|
43
|
+
cartNeedsSourceUpdate: jest.fn(() => false),
|
|
43
44
|
appendJobSourceToFormData: (form: FormData) => form,
|
|
44
45
|
}));
|
|
45
46
|
jest.mock('./utilities/reportFailure', () => ({
|
|
@@ -70,6 +71,7 @@ describe('cart load and replacement failures', () => {
|
|
|
70
71
|
beforeEach(() => {
|
|
71
72
|
for (const name of bindingNames) delete (window as any)[name];
|
|
72
73
|
jest.clearAllMocks();
|
|
74
|
+
(cartNeedsSourceUpdate as jest.Mock).mockReturnValue(false);
|
|
73
75
|
mockGet.mockReset();
|
|
74
76
|
mockPost.mockReset();
|
|
75
77
|
mockCreateItem.mockReset();
|
|
@@ -109,6 +111,20 @@ describe('cart load and replacement failures', () => {
|
|
|
109
111
|
});
|
|
110
112
|
await act(async () => context.setActiveTabIndex(tabIdClearCart));
|
|
111
113
|
}
|
|
114
|
+
it('restores a paid cart without writing new attribution to the closed order', async () => {
|
|
115
|
+
const paidCart = { id: 42, isTest: false, invoice: { unpaid: false }, cartItems: [{ id: 7 }] };
|
|
116
|
+
mockGet.mockResolvedValue({ id: 42, toJson: () => paidCart, toFormData: () => new FormData() });
|
|
117
|
+
(cartNeedsSourceUpdate as jest.Mock).mockReturnValue(true);
|
|
118
|
+
mockPost.mockRejectedValue({ statusCode: 401, message: 'Authentication required' });
|
|
119
|
+
|
|
120
|
+
await act(async () => {
|
|
121
|
+
root.render(<CartProvider domainId={5} createCartAndCookie={() => {}}><Probe /></CartProvider>);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
expect(context.cart).toEqual(paidCart);
|
|
125
|
+
expect(mockPost).not.toHaveBeenCalled();
|
|
126
|
+
expect(reportCartFailure).not.toHaveBeenCalled();
|
|
127
|
+
});
|
|
112
128
|
it('does not expose cart operations until a suspended provider commits', async () => {
|
|
113
129
|
setUserAgent(headlessUserAgent);
|
|
114
130
|
(getCartCookie as jest.Mock).mockResolvedValue(undefined);
|
package/src/CartProvider.tsx
CHANGED
|
@@ -694,6 +694,14 @@ const CartProvider = ({
|
|
|
694
694
|
if (Boolean(cartJson.isTest) !== isTest) {
|
|
695
695
|
return await createCartAndCookie(undefined, true);
|
|
696
696
|
}
|
|
697
|
+
// A paid cart can be read but cannot be edited by the shopper.
|
|
698
|
+
// Restore it before attribution or saved customer writes.
|
|
699
|
+
if (cartJson.invoice?.unpaid === false) {
|
|
700
|
+
setCartClient(cartJson.client?.id ? cartJson.client : null);
|
|
701
|
+
setCart({ ...cartJson });
|
|
702
|
+
dismissInitializationError();
|
|
703
|
+
return cartJson;
|
|
704
|
+
}
|
|
697
705
|
const jobSource = jobSourceFieldsForApi(captureAndReadJobSource());
|
|
698
706
|
if (cartNeedsSourceUpdate(cartJson, jobSource)) {
|
|
699
707
|
const saved = await merchi.authenticatedFetch(`/carts/${cart.id}/`, {
|