@merchi/cart 1.4.39 → 1.4.41
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 +29 -9
- package/esm/CartProvider.load.test.js +273 -0
- package/esm/utilities/cartLoadFailure.js +7 -0
- package/esm/utilities/cartLoadFailure.test.js +16 -0
- package/esm/utilities/reportFailure.js +2 -0
- package/lib/CartProvider.js +29 -9
- package/lib/utilities/cartLoadFailure.d.ts +2 -0
- package/lib/utilities/cartLoadFailure.js +13 -0
- package/lib/utilities/cartLoadFailure.test.d.ts +1 -0
- package/lib/utilities/reportFailure.d.ts +1 -0
- package/lib/utilities/reportFailure.js +2 -0
- package/package.json +4 -4
- package/src/CartProvider.load.test.tsx +98 -0
- package/src/CartProvider.tsx +11 -3
- package/src/utilities/cartLoadFailure.test.ts +13 -0
- package/src/utilities/cartLoadFailure.ts +6 -0
- package/src/utilities/reportFailure.ts +2 -0
package/esm/CartProvider.js
CHANGED
|
@@ -34,6 +34,7 @@ import { getSavedCheckoutCustomer, saveCheckoutAddress } from './utilities/local
|
|
|
34
34
|
import { makeUser } from './utilities/user';
|
|
35
35
|
import { persistTestCheckoutFlag, readTestCheckoutFlag } from './utilities/test_checkout';
|
|
36
36
|
import { reportAddCartItemFailure, reportCartFailure, CART_FAILURE_SOURCES } from './utilities/reportFailure';
|
|
37
|
+
import { canReplaceMissingCart } from './utilities/cartLoadFailure';
|
|
37
38
|
import { appendJobSourceToFormData, captureAndReadJobSource, cartNeedsSourceUpdate, jobSourceFieldsForApi } from './utilities/job_source';
|
|
38
39
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
39
40
|
var CartContext = /*#__PURE__*/createContext({
|
|
@@ -620,21 +621,33 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
620
621
|
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.");
|
|
621
622
|
setCartSettingsInvalid(true);
|
|
622
623
|
case 10:
|
|
623
|
-
_context3.n =
|
|
624
|
+
_context3.n = 14;
|
|
624
625
|
break;
|
|
625
626
|
case 11:
|
|
626
627
|
_context3.p = 11;
|
|
627
628
|
_t4 = _context3.v;
|
|
629
|
+
if (!canReplaceMissingCart(_t4)) {
|
|
630
|
+
_context3.n = 13;
|
|
631
|
+
break;
|
|
632
|
+
}
|
|
628
633
|
_context3.n = 12;
|
|
629
634
|
return createCartAndCookie();
|
|
630
635
|
case 12:
|
|
631
|
-
_context3.
|
|
632
|
-
|
|
633
|
-
return _context3.f(12);
|
|
636
|
+
_context3.n = 14;
|
|
637
|
+
break;
|
|
634
638
|
case 13:
|
|
639
|
+
// A network/server/auth failure does not mean the saved cart is gone.
|
|
640
|
+
// Preserve its cookie and contents so the shopper can retry the read.
|
|
641
|
+
reportCartFailure(_t4, CART_FAILURE_SOURCES.load);
|
|
642
|
+
alertError((_t4 === null || _t4 === void 0 ? void 0 : _t4.errorMessage) || (_t4 === null || _t4 === void 0 ? void 0 : _t4.message) || 'Unable to load cart. Please try again.');
|
|
643
|
+
case 14:
|
|
644
|
+
_context3.p = 14;
|
|
645
|
+
setFetchingCart(false);
|
|
646
|
+
return _context3.f(14);
|
|
647
|
+
case 15:
|
|
635
648
|
return _context3.a(2);
|
|
636
649
|
}
|
|
637
|
-
}, _callee3, null, [[1, 11,
|
|
650
|
+
}, _callee3, null, [[1, 11, 14, 15]]);
|
|
638
651
|
}));
|
|
639
652
|
return _getCart.apply(this, arguments);
|
|
640
653
|
}
|
|
@@ -696,12 +709,19 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
696
709
|
return getCartCookie(domainId);
|
|
697
710
|
case 2:
|
|
698
711
|
cartIdAndToken = _context5.v;
|
|
699
|
-
if (cartIdAndToken) {
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
createCartAndCookie();
|
|
712
|
+
if (!cartIdAndToken) {
|
|
713
|
+
_context5.n = 4;
|
|
714
|
+
break;
|
|
703
715
|
}
|
|
716
|
+
_context5.n = 3;
|
|
717
|
+
return getCart(cartIdAndToken);
|
|
704
718
|
case 3:
|
|
719
|
+
_context5.n = 5;
|
|
720
|
+
break;
|
|
721
|
+
case 4:
|
|
722
|
+
_context5.n = 5;
|
|
723
|
+
return createCartAndCookie();
|
|
724
|
+
case 5:
|
|
705
725
|
return _context5.a(2);
|
|
706
726
|
}
|
|
707
727
|
}, _callee5);
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
|
|
2
|
+
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); }
|
|
3
|
+
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
|
|
4
|
+
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
|
|
5
|
+
import * as React from 'react';
|
|
6
|
+
import { createRoot } from 'react-dom/client';
|
|
7
|
+
import { act } from 'react-dom/test-utils';
|
|
8
|
+
import CartProvider, { useCartContext } from './CartProvider';
|
|
9
|
+
import { getCartCookie, setCartCookie } from './utilities/cookie';
|
|
10
|
+
import { reportCartFailure, CART_FAILURE_SOURCES } from './utilities/reportFailure';
|
|
11
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
12
|
+
var mockGet = jest.fn();
|
|
13
|
+
var mockPost = jest.fn();
|
|
14
|
+
jest.mock('@merchi/sdk', function () {
|
|
15
|
+
return {
|
|
16
|
+
Merchi: jest.fn().mockImplementation(function () {
|
|
17
|
+
return {
|
|
18
|
+
Cart: {
|
|
19
|
+
get: function get() {
|
|
20
|
+
return mockGet.apply(void 0, arguments);
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
authenticatedFetch: function authenticatedFetch() {
|
|
24
|
+
return mockPost.apply(void 0, arguments);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
})
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
jest.mock('./utilities/product', function () {
|
|
31
|
+
return {
|
|
32
|
+
makeProduct: jest.fn()
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
jest.mock('./utilities/cart', function () {
|
|
36
|
+
return {
|
|
37
|
+
makeCart: function makeCart() {
|
|
38
|
+
return {
|
|
39
|
+
toFormData: function toFormData() {
|
|
40
|
+
return new FormData();
|
|
41
|
+
},
|
|
42
|
+
fromJson: jest.fn(),
|
|
43
|
+
toJson: function toJson() {
|
|
44
|
+
return {
|
|
45
|
+
id: 99,
|
|
46
|
+
token: 'new-token',
|
|
47
|
+
isTest: false
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
stripeIsValidAndActive: function stripeIsValidAndActive() {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
jest.mock('./utilities/cookie', function () {
|
|
58
|
+
return {
|
|
59
|
+
getCartCookie: jest.fn().mockResolvedValue(['42', 'saved-token']),
|
|
60
|
+
getCartCookieToken: jest.fn(),
|
|
61
|
+
setCartCookie: jest.fn(),
|
|
62
|
+
readHandoffCart: jest.fn()
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
jest.mock('./utilities/local_storage', function () {
|
|
66
|
+
return {
|
|
67
|
+
getSavedCheckoutCustomer: function getSavedCheckoutCustomer() {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
jest.mock('./utilities/job_source', function () {
|
|
73
|
+
return {
|
|
74
|
+
captureAndReadJobSource: function captureAndReadJobSource() {
|
|
75
|
+
return null;
|
|
76
|
+
},
|
|
77
|
+
jobSourceFieldsForApi: function jobSourceFieldsForApi() {
|
|
78
|
+
return {};
|
|
79
|
+
},
|
|
80
|
+
cartNeedsSourceUpdate: function cartNeedsSourceUpdate() {
|
|
81
|
+
return false;
|
|
82
|
+
},
|
|
83
|
+
appendJobSourceToFormData: function appendJobSourceToFormData(form) {
|
|
84
|
+
return form;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
});
|
|
88
|
+
jest.mock('./utilities/reportFailure', function () {
|
|
89
|
+
return {
|
|
90
|
+
reportCartFailure: jest.fn(),
|
|
91
|
+
CART_FAILURE_SOURCES: {
|
|
92
|
+
load: 'cart.load',
|
|
93
|
+
create: 'cart.create'
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
});
|
|
97
|
+
var context;
|
|
98
|
+
function Probe() {
|
|
99
|
+
context = useCartContext();
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
describe('existing-cart load failures', function () {
|
|
103
|
+
var root;
|
|
104
|
+
var container;
|
|
105
|
+
beforeEach(function () {
|
|
106
|
+
jest.clearAllMocks();
|
|
107
|
+
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
|
|
108
|
+
container = document.createElement('div');
|
|
109
|
+
document.body.appendChild(container);
|
|
110
|
+
root = createRoot(container);
|
|
111
|
+
mockPost.mockResolvedValue({
|
|
112
|
+
cart: {
|
|
113
|
+
id: 99,
|
|
114
|
+
token: 'new-token'
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
afterEach(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
|
|
119
|
+
return _regenerator().w(function (_context2) {
|
|
120
|
+
while (1) switch (_context2.n) {
|
|
121
|
+
case 0:
|
|
122
|
+
_context2.n = 1;
|
|
123
|
+
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
|
|
124
|
+
return _regenerator().w(function (_context) {
|
|
125
|
+
while (1) switch (_context.n) {
|
|
126
|
+
case 0:
|
|
127
|
+
return _context.a(2, root.unmount());
|
|
128
|
+
}
|
|
129
|
+
}, _callee);
|
|
130
|
+
})));
|
|
131
|
+
case 1:
|
|
132
|
+
container.remove();
|
|
133
|
+
case 2:
|
|
134
|
+
return _context2.a(2);
|
|
135
|
+
}
|
|
136
|
+
}, _callee2);
|
|
137
|
+
})));
|
|
138
|
+
function mount(_x) {
|
|
139
|
+
return _mount.apply(this, arguments);
|
|
140
|
+
}
|
|
141
|
+
function _mount() {
|
|
142
|
+
_mount = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(error) {
|
|
143
|
+
return _regenerator().w(function (_context8) {
|
|
144
|
+
while (1) switch (_context8.n) {
|
|
145
|
+
case 0:
|
|
146
|
+
mockGet.mockRejectedValue(error);
|
|
147
|
+
_context8.n = 1;
|
|
148
|
+
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7() {
|
|
149
|
+
return _regenerator().w(function (_context7) {
|
|
150
|
+
while (1) switch (_context7.n) {
|
|
151
|
+
case 0:
|
|
152
|
+
root.render(/*#__PURE__*/_jsx(CartProvider, {
|
|
153
|
+
domainId: 5,
|
|
154
|
+
createCartAndCookie: function createCartAndCookie() {},
|
|
155
|
+
cart: {
|
|
156
|
+
id: 42,
|
|
157
|
+
cartItems: [{
|
|
158
|
+
id: 7
|
|
159
|
+
}]
|
|
160
|
+
},
|
|
161
|
+
children: /*#__PURE__*/_jsx(Probe, {})
|
|
162
|
+
}));
|
|
163
|
+
case 1:
|
|
164
|
+
return _context7.a(2);
|
|
165
|
+
}
|
|
166
|
+
}, _callee7);
|
|
167
|
+
})));
|
|
168
|
+
case 1:
|
|
169
|
+
return _context8.a(2);
|
|
170
|
+
}
|
|
171
|
+
}, _callee8);
|
|
172
|
+
}));
|
|
173
|
+
return _mount.apply(this, arguments);
|
|
174
|
+
}
|
|
175
|
+
it.each([new TypeError('Load failed'), {
|
|
176
|
+
statusCode: 502,
|
|
177
|
+
message: 'Gateway unavailable'
|
|
178
|
+
}, {
|
|
179
|
+
statusCode: 403,
|
|
180
|
+
message: 'Forbidden'
|
|
181
|
+
}])('preserves the saved cart and never POSTs a replacement after %s', /*#__PURE__*/function () {
|
|
182
|
+
var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(error) {
|
|
183
|
+
return _regenerator().w(function (_context3) {
|
|
184
|
+
while (1) switch (_context3.n) {
|
|
185
|
+
case 0:
|
|
186
|
+
_context3.n = 1;
|
|
187
|
+
return mount(error);
|
|
188
|
+
case 1:
|
|
189
|
+
expect(getCartCookie).toHaveBeenCalledWith(5);
|
|
190
|
+
expect(mockPost).not.toHaveBeenCalled();
|
|
191
|
+
expect(setCartCookie).not.toHaveBeenCalled();
|
|
192
|
+
expect(context.cart).toEqual({
|
|
193
|
+
id: 42,
|
|
194
|
+
cartItems: [{
|
|
195
|
+
id: 7
|
|
196
|
+
}]
|
|
197
|
+
});
|
|
198
|
+
expect(context.alert.show).toBe(true);
|
|
199
|
+
expect(context.fetchingCart).toBe(false);
|
|
200
|
+
expect(reportCartFailure).toHaveBeenCalledWith(error, CART_FAILURE_SOURCES.load);
|
|
201
|
+
case 2:
|
|
202
|
+
return _context3.a(2);
|
|
203
|
+
}
|
|
204
|
+
}, _callee3);
|
|
205
|
+
}));
|
|
206
|
+
return function (_x2) {
|
|
207
|
+
return _ref3.apply(this, arguments);
|
|
208
|
+
};
|
|
209
|
+
}());
|
|
210
|
+
it.each([404, 410])('replaces a genuinely missing cart after HTTP %s', /*#__PURE__*/function () {
|
|
211
|
+
var _ref4 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(statusCode) {
|
|
212
|
+
return _regenerator().w(function (_context4) {
|
|
213
|
+
while (1) switch (_context4.n) {
|
|
214
|
+
case 0:
|
|
215
|
+
_context4.n = 1;
|
|
216
|
+
return mount({
|
|
217
|
+
statusCode: statusCode
|
|
218
|
+
});
|
|
219
|
+
case 1:
|
|
220
|
+
expect(mockPost).toHaveBeenCalledTimes(1);
|
|
221
|
+
expect(mockPost).toHaveBeenCalledWith('/carts/', expect.objectContaining({
|
|
222
|
+
method: 'POST'
|
|
223
|
+
}));
|
|
224
|
+
expect(setCartCookie).toHaveBeenCalledTimes(1);
|
|
225
|
+
case 2:
|
|
226
|
+
return _context4.a(2);
|
|
227
|
+
}
|
|
228
|
+
}, _callee4);
|
|
229
|
+
}));
|
|
230
|
+
return function (_x3) {
|
|
231
|
+
return _ref4.apply(this, arguments);
|
|
232
|
+
};
|
|
233
|
+
}());
|
|
234
|
+
it('allows an explicit retry to restore the same cart without creating another', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6() {
|
|
235
|
+
return _regenerator().w(function (_context6) {
|
|
236
|
+
while (1) switch (_context6.n) {
|
|
237
|
+
case 0:
|
|
238
|
+
_context6.n = 1;
|
|
239
|
+
return mount(new TypeError('Load failed'));
|
|
240
|
+
case 1:
|
|
241
|
+
mockGet.mockResolvedValue({
|
|
242
|
+
toJson: function toJson() {
|
|
243
|
+
return {
|
|
244
|
+
id: 42,
|
|
245
|
+
isTest: false,
|
|
246
|
+
cartItems: [{
|
|
247
|
+
id: 7
|
|
248
|
+
}]
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
_context6.n = 2;
|
|
253
|
+
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5() {
|
|
254
|
+
return _regenerator().w(function (_context5) {
|
|
255
|
+
while (1) switch (_context5.n) {
|
|
256
|
+
case 0:
|
|
257
|
+
_context5.n = 1;
|
|
258
|
+
return window.getCart();
|
|
259
|
+
case 1:
|
|
260
|
+
return _context5.a(2);
|
|
261
|
+
}
|
|
262
|
+
}, _callee5);
|
|
263
|
+
})));
|
|
264
|
+
case 2:
|
|
265
|
+
expect(context.cart.id).toBe(42);
|
|
266
|
+
expect(context.cart.cartItems).toHaveLength(1);
|
|
267
|
+
expect(mockPost).not.toHaveBeenCalled();
|
|
268
|
+
case 3:
|
|
269
|
+
return _context6.a(2);
|
|
270
|
+
}
|
|
271
|
+
}, _callee6);
|
|
272
|
+
})));
|
|
273
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
/** Only a confirmed missing/expired cart permits replacing the saved cart. */
|
|
3
|
+
export function canReplaceMissingCart(error) {
|
|
4
|
+
if (!error || _typeof(error) !== 'object') return false;
|
|
5
|
+
var status = error.statusCode;
|
|
6
|
+
return status === 404 || status === 410;
|
|
7
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { canReplaceMissingCart } from './cartLoadFailure';
|
|
2
|
+
describe('saved-cart replacement', function () {
|
|
3
|
+
it.each([404, 410])('replaces a cart only after a confirmed HTTP %s', function (statusCode) {
|
|
4
|
+
expect(canReplaceMissingCart({
|
|
5
|
+
statusCode: statusCode
|
|
6
|
+
})).toBe(true);
|
|
7
|
+
});
|
|
8
|
+
it.each([400, 401, 403, 408, 422, 429, 500, 502, 503])('preserves the existing cart after HTTP %s', function (statusCode) {
|
|
9
|
+
expect(canReplaceMissingCart({
|
|
10
|
+
statusCode: statusCode
|
|
11
|
+
})).toBe(false);
|
|
12
|
+
});
|
|
13
|
+
it.each([new TypeError('Load failed'), new TypeError('Failed to fetch'), null, undefined, 'offline'])('preserves the existing cart after a failure without an HTTP status', function (error) {
|
|
14
|
+
expect(canReplaceMissingCart(error)).toBe(false);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -15,6 +15,7 @@ import { captureUnexpected } from '@merchi/error-catch';
|
|
|
15
15
|
* the legacy `digest` as well as bounded operation context.
|
|
16
16
|
*/
|
|
17
17
|
export var CART_FAILURE_SOURCES = {
|
|
18
|
+
load: 'cart.load',
|
|
18
19
|
addItem: 'cart.addItem',
|
|
19
20
|
invoicePdf: 'cart.invoicePdf',
|
|
20
21
|
create: 'cart.create',
|
|
@@ -22,6 +23,7 @@ export var CART_FAILURE_SOURCES = {
|
|
|
22
23
|
shipping: 'cart.shipping'
|
|
23
24
|
};
|
|
24
25
|
var OPERATIONS = {
|
|
26
|
+
'cart.load': 'cart.load',
|
|
25
27
|
'cart.addItem': 'cart.add_item',
|
|
26
28
|
'cart.invoicePdf': 'cart.invoice_pdf',
|
|
27
29
|
'cart.create': 'cart.create',
|
package/lib/CartProvider.js
CHANGED
|
@@ -20,6 +20,7 @@ var _local_storage = require("./utilities/local_storage");
|
|
|
20
20
|
var _user = require("./utilities/user");
|
|
21
21
|
var _test_checkout = require("./utilities/test_checkout");
|
|
22
22
|
var _reportFailure = require("./utilities/reportFailure");
|
|
23
|
+
var _cartLoadFailure = require("./utilities/cartLoadFailure");
|
|
23
24
|
var _job_source = require("./utilities/job_source");
|
|
24
25
|
var _jsxRuntime = require("react/jsx-runtime");
|
|
25
26
|
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t9 in e) "default" !== _t9 && {}.hasOwnProperty.call(e, _t9) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t9)) && (i.get || i.set) ? o(f, _t9, i) : f[_t9] = e[_t9]); return f; })(e, t); }
|
|
@@ -626,21 +627,33 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
626
627
|
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.");
|
|
627
628
|
setCartSettingsInvalid(true);
|
|
628
629
|
case 10:
|
|
629
|
-
_context3.n =
|
|
630
|
+
_context3.n = 14;
|
|
630
631
|
break;
|
|
631
632
|
case 11:
|
|
632
633
|
_context3.p = 11;
|
|
633
634
|
_t4 = _context3.v;
|
|
635
|
+
if (!(0, _cartLoadFailure.canReplaceMissingCart)(_t4)) {
|
|
636
|
+
_context3.n = 13;
|
|
637
|
+
break;
|
|
638
|
+
}
|
|
634
639
|
_context3.n = 12;
|
|
635
640
|
return createCartAndCookie();
|
|
636
641
|
case 12:
|
|
637
|
-
_context3.
|
|
638
|
-
|
|
639
|
-
return _context3.f(12);
|
|
642
|
+
_context3.n = 14;
|
|
643
|
+
break;
|
|
640
644
|
case 13:
|
|
645
|
+
// A network/server/auth failure does not mean the saved cart is gone.
|
|
646
|
+
// Preserve its cookie and contents so the shopper can retry the read.
|
|
647
|
+
(0, _reportFailure.reportCartFailure)(_t4, _reportFailure.CART_FAILURE_SOURCES.load);
|
|
648
|
+
alertError((_t4 === null || _t4 === void 0 ? void 0 : _t4.errorMessage) || (_t4 === null || _t4 === void 0 ? void 0 : _t4.message) || 'Unable to load cart. Please try again.');
|
|
649
|
+
case 14:
|
|
650
|
+
_context3.p = 14;
|
|
651
|
+
setFetchingCart(false);
|
|
652
|
+
return _context3.f(14);
|
|
653
|
+
case 15:
|
|
641
654
|
return _context3.a(2);
|
|
642
655
|
}
|
|
643
|
-
}, _callee3, null, [[1, 11,
|
|
656
|
+
}, _callee3, null, [[1, 11, 14, 15]]);
|
|
644
657
|
}));
|
|
645
658
|
return _getCart.apply(this, arguments);
|
|
646
659
|
}
|
|
@@ -702,12 +715,19 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
702
715
|
return (0, _cookie.getCartCookie)(domainId);
|
|
703
716
|
case 2:
|
|
704
717
|
cartIdAndToken = _context5.v;
|
|
705
|
-
if (cartIdAndToken) {
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
createCartAndCookie();
|
|
718
|
+
if (!cartIdAndToken) {
|
|
719
|
+
_context5.n = 4;
|
|
720
|
+
break;
|
|
709
721
|
}
|
|
722
|
+
_context5.n = 3;
|
|
723
|
+
return getCart(cartIdAndToken);
|
|
710
724
|
case 3:
|
|
725
|
+
_context5.n = 5;
|
|
726
|
+
break;
|
|
727
|
+
case 4:
|
|
728
|
+
_context5.n = 5;
|
|
729
|
+
return createCartAndCookie();
|
|
730
|
+
case 5:
|
|
711
731
|
return _context5.a(2);
|
|
712
732
|
}
|
|
713
733
|
}, _callee5);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.canReplaceMissingCart = canReplaceMissingCart;
|
|
7
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
8
|
+
/** Only a confirmed missing/expired cart permits replacing the saved cart. */
|
|
9
|
+
function canReplaceMissingCart(error) {
|
|
10
|
+
if (!error || _typeof(error) !== 'object') return false;
|
|
11
|
+
var status = error.statusCode;
|
|
12
|
+
return status === 404 || status === 410;
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -22,6 +22,7 @@ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e
|
|
|
22
22
|
* the legacy `digest` as well as bounded operation context.
|
|
23
23
|
*/
|
|
24
24
|
var CART_FAILURE_SOURCES = exports.CART_FAILURE_SOURCES = {
|
|
25
|
+
load: 'cart.load',
|
|
25
26
|
addItem: 'cart.addItem',
|
|
26
27
|
invoicePdf: 'cart.invoicePdf',
|
|
27
28
|
create: 'cart.create',
|
|
@@ -29,6 +30,7 @@ var CART_FAILURE_SOURCES = exports.CART_FAILURE_SOURCES = {
|
|
|
29
30
|
shipping: 'cart.shipping'
|
|
30
31
|
};
|
|
31
32
|
var OPERATIONS = {
|
|
33
|
+
'cart.load': 'cart.load',
|
|
32
34
|
'cart.addItem': 'cart.add_item',
|
|
33
35
|
'cart.invoicePdf': 'cart.invoice_pdf',
|
|
34
36
|
'cart.create': 'cart.create',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@merchi/cart",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.41",
|
|
4
4
|
"description": "Merchi's cart",
|
|
5
5
|
"source": "src/index.ts",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -95,9 +95,9 @@
|
|
|
95
95
|
"@fortawesome/react-fontawesome": "^0.2.0",
|
|
96
96
|
"@merchi/error-catch": "^0.1.5",
|
|
97
97
|
"@merchi/invoice": "^1.3.9",
|
|
98
|
-
"@merchi/product-form": "^1.19.
|
|
99
|
-
"@merchi/product-form-sdk": "^1.0.
|
|
100
|
-
"@merchi/sdk": "^1.42.
|
|
98
|
+
"@merchi/product-form": "^1.19.42",
|
|
99
|
+
"@merchi/product-form-sdk": "^1.0.30",
|
|
100
|
+
"@merchi/sdk": "^1.42.19",
|
|
101
101
|
"@stripe/react-stripe-js": "^2.3.0",
|
|
102
102
|
"@stripe/stripe-js": "^2.1.6",
|
|
103
103
|
"browser-or-node": "^3.0.0-pre.0",
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { createRoot, Root } from 'react-dom/client';
|
|
3
|
+
import { act } from 'react-dom/test-utils';
|
|
4
|
+
|
|
5
|
+
import CartProvider, { useCartContext } from './CartProvider';
|
|
6
|
+
import { getCartCookie, setCartCookie } from './utilities/cookie';
|
|
7
|
+
import { reportCartFailure, CART_FAILURE_SOURCES } from './utilities/reportFailure';
|
|
8
|
+
|
|
9
|
+
const mockGet = jest.fn();
|
|
10
|
+
const mockPost = jest.fn();
|
|
11
|
+
|
|
12
|
+
jest.mock('@merchi/sdk', () => ({
|
|
13
|
+
Merchi: jest.fn().mockImplementation(() => ({
|
|
14
|
+
Cart: { get: (...args: unknown[]) => mockGet(...args) },
|
|
15
|
+
authenticatedFetch: (...args: unknown[]) => mockPost(...args),
|
|
16
|
+
})),
|
|
17
|
+
}));
|
|
18
|
+
jest.mock('./utilities/product', () => ({ makeProduct: jest.fn() }));
|
|
19
|
+
jest.mock('./utilities/cart', () => ({
|
|
20
|
+
makeCart: () => ({
|
|
21
|
+
toFormData: () => new FormData(),
|
|
22
|
+
fromJson: jest.fn(),
|
|
23
|
+
toJson: () => ({ id: 99, token: 'new-token', isTest: false }),
|
|
24
|
+
}),
|
|
25
|
+
stripeIsValidAndActive: () => true,
|
|
26
|
+
}));
|
|
27
|
+
jest.mock('./utilities/cookie', () => ({
|
|
28
|
+
getCartCookie: jest.fn().mockResolvedValue(['42', 'saved-token']),
|
|
29
|
+
getCartCookieToken: jest.fn(),
|
|
30
|
+
setCartCookie: jest.fn(),
|
|
31
|
+
readHandoffCart: jest.fn(),
|
|
32
|
+
}));
|
|
33
|
+
jest.mock('./utilities/local_storage', () => ({ getSavedCheckoutCustomer: () => null }));
|
|
34
|
+
jest.mock('./utilities/job_source', () => ({
|
|
35
|
+
captureAndReadJobSource: () => null,
|
|
36
|
+
jobSourceFieldsForApi: () => ({}),
|
|
37
|
+
cartNeedsSourceUpdate: () => false,
|
|
38
|
+
appendJobSourceToFormData: (form: FormData) => form,
|
|
39
|
+
}));
|
|
40
|
+
jest.mock('./utilities/reportFailure', () => ({
|
|
41
|
+
reportCartFailure: jest.fn(),
|
|
42
|
+
CART_FAILURE_SOURCES: { load: 'cart.load', create: 'cart.create' },
|
|
43
|
+
}));
|
|
44
|
+
|
|
45
|
+
let context: ReturnType<typeof useCartContext>;
|
|
46
|
+
function Probe() {
|
|
47
|
+
context = useCartContext();
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
describe('existing-cart load failures', () => {
|
|
52
|
+
let root: Root;
|
|
53
|
+
let container: HTMLDivElement;
|
|
54
|
+
beforeEach(() => {
|
|
55
|
+
jest.clearAllMocks();
|
|
56
|
+
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
|
|
57
|
+
container = document.createElement('div');
|
|
58
|
+
document.body.appendChild(container);
|
|
59
|
+
root = createRoot(container);
|
|
60
|
+
mockPost.mockResolvedValue({ cart: { id: 99, token: 'new-token' } });
|
|
61
|
+
});
|
|
62
|
+
afterEach(async () => {
|
|
63
|
+
await act(async () => root.unmount());
|
|
64
|
+
container.remove();
|
|
65
|
+
});
|
|
66
|
+
async function mount(error: unknown) {
|
|
67
|
+
mockGet.mockRejectedValue(error);
|
|
68
|
+
await act(async () => {
|
|
69
|
+
root.render(<CartProvider domainId={5} createCartAndCookie={() => {}} cart={{ id: 42, cartItems: [{ id: 7 }] }}>
|
|
70
|
+
<Probe />
|
|
71
|
+
</CartProvider>);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
it.each([new TypeError('Load failed'), { statusCode: 502, message: 'Gateway unavailable' }, { statusCode: 403, message: 'Forbidden' }])('preserves the saved cart and never POSTs a replacement after %s', async error => {
|
|
75
|
+
await mount(error);
|
|
76
|
+
expect(getCartCookie).toHaveBeenCalledWith(5);
|
|
77
|
+
expect(mockPost).not.toHaveBeenCalled();
|
|
78
|
+
expect(setCartCookie).not.toHaveBeenCalled();
|
|
79
|
+
expect(context.cart).toEqual({ id: 42, cartItems: [{ id: 7 }] });
|
|
80
|
+
expect(context.alert.show).toBe(true);
|
|
81
|
+
expect(context.fetchingCart).toBe(false);
|
|
82
|
+
expect(reportCartFailure).toHaveBeenCalledWith(error, CART_FAILURE_SOURCES.load);
|
|
83
|
+
});
|
|
84
|
+
it.each([404, 410])('replaces a genuinely missing cart after HTTP %s', async statusCode => {
|
|
85
|
+
await mount({ statusCode });
|
|
86
|
+
expect(mockPost).toHaveBeenCalledTimes(1);
|
|
87
|
+
expect(mockPost).toHaveBeenCalledWith('/carts/', expect.objectContaining({ method: 'POST' }));
|
|
88
|
+
expect(setCartCookie).toHaveBeenCalledTimes(1);
|
|
89
|
+
});
|
|
90
|
+
it('allows an explicit retry to restore the same cart without creating another', async () => {
|
|
91
|
+
await mount(new TypeError('Load failed'));
|
|
92
|
+
mockGet.mockResolvedValue({ toJson: () => ({ id: 42, isTest: false, cartItems: [{ id: 7 }] }) });
|
|
93
|
+
await act(async () => { await (window as any).getCart(); });
|
|
94
|
+
expect(context.cart.id).toBe(42);
|
|
95
|
+
expect(context.cart.cartItems).toHaveLength(1);
|
|
96
|
+
expect(mockPost).not.toHaveBeenCalled();
|
|
97
|
+
});
|
|
98
|
+
});
|
package/src/CartProvider.tsx
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
import { makeUser } from './utilities/user';
|
|
23
23
|
import { persistTestCheckoutFlag, readTestCheckoutFlag } from './utilities/test_checkout';
|
|
24
24
|
import { reportAddCartItemFailure, reportCartFailure, CART_FAILURE_SOURCES } from './utilities/reportFailure';
|
|
25
|
+
import { canReplaceMissingCart } from './utilities/cartLoadFailure';
|
|
25
26
|
import {
|
|
26
27
|
appendJobSourceToFormData,
|
|
27
28
|
captureAndReadJobSource,
|
|
@@ -696,7 +697,14 @@ const CartProvider = ({
|
|
|
696
697
|
setCartSettingsInvalid(true);
|
|
697
698
|
}
|
|
698
699
|
} catch(e: any){
|
|
699
|
-
|
|
700
|
+
if (canReplaceMissingCart(e)) {
|
|
701
|
+
await createCartAndCookie();
|
|
702
|
+
} else {
|
|
703
|
+
// A network/server/auth failure does not mean the saved cart is gone.
|
|
704
|
+
// Preserve its cookie and contents so the shopper can retry the read.
|
|
705
|
+
reportCartFailure(e, CART_FAILURE_SOURCES.load);
|
|
706
|
+
alertError(e?.errorMessage || e?.message || 'Unable to load cart. Please try again.');
|
|
707
|
+
}
|
|
700
708
|
} finally {
|
|
701
709
|
setFetchingCart(false);
|
|
702
710
|
}
|
|
@@ -718,9 +726,9 @@ const CartProvider = ({
|
|
|
718
726
|
await adoptHandoffCart();
|
|
719
727
|
const cartIdAndToken = await getCartCookie((domainId as number));
|
|
720
728
|
if (cartIdAndToken) {
|
|
721
|
-
getCart(cartIdAndToken);
|
|
729
|
+
await getCart(cartIdAndToken);
|
|
722
730
|
} else {
|
|
723
|
-
createCartAndCookie();
|
|
731
|
+
await createCartAndCookie();
|
|
724
732
|
}
|
|
725
733
|
}
|
|
726
734
|
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { canReplaceMissingCart } from './cartLoadFailure';
|
|
2
|
+
|
|
3
|
+
describe('saved-cart replacement', () => {
|
|
4
|
+
it.each([404, 410])('replaces a cart only after a confirmed HTTP %s', statusCode => {
|
|
5
|
+
expect(canReplaceMissingCart({ statusCode })).toBe(true);
|
|
6
|
+
});
|
|
7
|
+
it.each([400, 401, 403, 408, 422, 429, 500, 502, 503])('preserves the existing cart after HTTP %s', statusCode => {
|
|
8
|
+
expect(canReplaceMissingCart({ statusCode })).toBe(false);
|
|
9
|
+
});
|
|
10
|
+
it.each([new TypeError('Load failed'), new TypeError('Failed to fetch'), null, undefined, 'offline'])('preserves the existing cart after a failure without an HTTP status', error => {
|
|
11
|
+
expect(canReplaceMissingCart(error)).toBe(false);
|
|
12
|
+
});
|
|
13
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Only a confirmed missing/expired cart permits replacing the saved cart. */
|
|
2
|
+
export function canReplaceMissingCart(error: unknown): boolean {
|
|
3
|
+
if (!error || typeof error !== 'object') return false;
|
|
4
|
+
const status = (error as { statusCode?: unknown }).statusCode;
|
|
5
|
+
return status === 404 || status === 410;
|
|
6
|
+
}
|
|
@@ -9,6 +9,7 @@ import { captureUnexpected, type FrontendErrorOperation } from '@merchi/error-ca
|
|
|
9
9
|
* the legacy `digest` as well as bounded operation context.
|
|
10
10
|
*/
|
|
11
11
|
export const CART_FAILURE_SOURCES = {
|
|
12
|
+
load: 'cart.load',
|
|
12
13
|
addItem: 'cart.addItem',
|
|
13
14
|
invoicePdf: 'cart.invoicePdf',
|
|
14
15
|
create: 'cart.create',
|
|
@@ -16,6 +17,7 @@ export const CART_FAILURE_SOURCES = {
|
|
|
16
17
|
shipping: 'cart.shipping',
|
|
17
18
|
} as const;
|
|
18
19
|
const OPERATIONS: Record<CartFailureSource, FrontendErrorOperation> = {
|
|
20
|
+
'cart.load': 'cart.load',
|
|
19
21
|
'cart.addItem': 'cart.add_item', 'cart.invoicePdf': 'cart.invoice_pdf',
|
|
20
22
|
'cart.create': 'cart.create', 'cart.update': 'cart.update', 'cart.shipping': 'cart.shipping',
|
|
21
23
|
};
|