@merchi/cart 1.4.57 → 1.4.59
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 +10 -5
- package/esm/CartProvider.load.test.js +158 -12
- package/esm/utilities/reportFailure.js +13 -4
- package/esm/utilities/reportFailure.test.js +73 -4
- package/lib/CartProvider.js +10 -5
- package/lib/utilities/reportFailure.js +13 -4
- package/package.json +1 -1
- package/src/CartProvider.load.test.tsx +48 -1
- package/src/CartProvider.tsx +6 -4
- package/src/utilities/reportFailure.test.ts +52 -1
- package/src/utilities/reportFailure.ts +13 -2
package/esm/CartProvider.js
CHANGED
|
@@ -544,7 +544,7 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
544
544
|
_context2.p = 5;
|
|
545
545
|
_t3 = _context2.v;
|
|
546
546
|
reportCartFailure(_t3, CART_FAILURE_SOURCES.create);
|
|
547
|
-
alertError(_t3.errorMessage || _t3.message || 'Unable to fetch cart.');
|
|
547
|
+
alertError((_t3 === null || _t3 === void 0 ? void 0 : _t3.errorMessage) || (_t3 === null || _t3 === void 0 ? void 0 : _t3.message) || 'Unable to fetch cart.');
|
|
548
548
|
case 6:
|
|
549
549
|
_context2.p = 6;
|
|
550
550
|
setFetchingCart(false);
|
|
@@ -737,11 +737,16 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
737
737
|
while (1) switch (_context6.n) {
|
|
738
738
|
case 0:
|
|
739
739
|
_context6.n = 1;
|
|
740
|
-
return createCartAndCookie()
|
|
740
|
+
return createCartAndCookie(function () {
|
|
741
|
+
setAlert(function (previous) {
|
|
742
|
+
return _objectSpread(_objectSpread({}, previous), {}, {
|
|
743
|
+
show: false
|
|
744
|
+
});
|
|
745
|
+
});
|
|
746
|
+
if (callback) callback();
|
|
747
|
+
closeClearCart();
|
|
748
|
+
});
|
|
741
749
|
case 1:
|
|
742
|
-
if (callback) callback();
|
|
743
|
-
closeClearCart();
|
|
744
|
-
case 2:
|
|
745
750
|
return _context6.a(2);
|
|
746
751
|
}
|
|
747
752
|
}, _callee6);
|
|
@@ -8,6 +8,7 @@ import { act } from 'react-dom/test-utils';
|
|
|
8
8
|
import CartProvider, { useCartContext } from './CartProvider';
|
|
9
9
|
import { getCartCookie, setCartCookie } from './utilities/cookie';
|
|
10
10
|
import { reportCartFailure, CART_FAILURE_SOURCES } from './utilities/reportFailure';
|
|
11
|
+
import { tabIdClearCart, tabIdItems } from './utilities/tabs';
|
|
11
12
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
12
13
|
var mockGet = jest.fn();
|
|
13
14
|
var mockPost = jest.fn();
|
|
@@ -99,7 +100,7 @@ function Probe() {
|
|
|
99
100
|
context = useCartContext();
|
|
100
101
|
return null;
|
|
101
102
|
}
|
|
102
|
-
describe('
|
|
103
|
+
describe('cart load and replacement failures', function () {
|
|
103
104
|
var root;
|
|
104
105
|
var container;
|
|
105
106
|
beforeEach(function () {
|
|
@@ -139,15 +140,15 @@ describe('existing-cart load failures', function () {
|
|
|
139
140
|
return _mount.apply(this, arguments);
|
|
140
141
|
}
|
|
141
142
|
function _mount() {
|
|
142
|
-
_mount = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function
|
|
143
|
-
return _regenerator().w(function (
|
|
144
|
-
while (1) switch (
|
|
143
|
+
_mount = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee11(error) {
|
|
144
|
+
return _regenerator().w(function (_context11) {
|
|
145
|
+
while (1) switch (_context11.n) {
|
|
145
146
|
case 0:
|
|
146
147
|
mockGet.mockRejectedValue(error);
|
|
147
|
-
|
|
148
|
-
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function
|
|
149
|
-
return _regenerator().w(function (
|
|
150
|
-
while (1) switch (
|
|
148
|
+
_context11.n = 1;
|
|
149
|
+
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee10() {
|
|
150
|
+
return _regenerator().w(function (_context10) {
|
|
151
|
+
while (1) switch (_context10.n) {
|
|
151
152
|
case 0:
|
|
152
153
|
root.render(/*#__PURE__*/_jsx(CartProvider, {
|
|
153
154
|
domainId: 5,
|
|
@@ -161,17 +162,68 @@ describe('existing-cart load failures', function () {
|
|
|
161
162
|
children: /*#__PURE__*/_jsx(Probe, {})
|
|
162
163
|
}));
|
|
163
164
|
case 1:
|
|
164
|
-
return
|
|
165
|
+
return _context10.a(2);
|
|
165
166
|
}
|
|
166
|
-
},
|
|
167
|
+
}, _callee10);
|
|
167
168
|
})));
|
|
168
169
|
case 1:
|
|
169
|
-
return
|
|
170
|
+
return _context11.a(2);
|
|
170
171
|
}
|
|
171
|
-
},
|
|
172
|
+
}, _callee11);
|
|
172
173
|
}));
|
|
173
174
|
return _mount.apply(this, arguments);
|
|
174
175
|
}
|
|
176
|
+
function mountSavedCart() {
|
|
177
|
+
return _mountSavedCart.apply(this, arguments);
|
|
178
|
+
}
|
|
179
|
+
function _mountSavedCart() {
|
|
180
|
+
_mountSavedCart = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee14() {
|
|
181
|
+
return _regenerator().w(function (_context14) {
|
|
182
|
+
while (1) switch (_context14.n) {
|
|
183
|
+
case 0:
|
|
184
|
+
mockGet.mockResolvedValue({
|
|
185
|
+
toJson: function toJson() {
|
|
186
|
+
return {
|
|
187
|
+
id: 42,
|
|
188
|
+
isTest: false,
|
|
189
|
+
cartItems: [{
|
|
190
|
+
id: 7
|
|
191
|
+
}]
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
_context14.n = 1;
|
|
196
|
+
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee12() {
|
|
197
|
+
return _regenerator().w(function (_context12) {
|
|
198
|
+
while (1) switch (_context12.n) {
|
|
199
|
+
case 0:
|
|
200
|
+
root.render(/*#__PURE__*/_jsx(CartProvider, {
|
|
201
|
+
domainId: 5,
|
|
202
|
+
createCartAndCookie: function createCartAndCookie() {},
|
|
203
|
+
children: /*#__PURE__*/_jsx(Probe, {})
|
|
204
|
+
}));
|
|
205
|
+
case 1:
|
|
206
|
+
return _context12.a(2);
|
|
207
|
+
}
|
|
208
|
+
}, _callee12);
|
|
209
|
+
})));
|
|
210
|
+
case 1:
|
|
211
|
+
_context14.n = 2;
|
|
212
|
+
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee13() {
|
|
213
|
+
return _regenerator().w(function (_context13) {
|
|
214
|
+
while (1) switch (_context13.n) {
|
|
215
|
+
case 0:
|
|
216
|
+
return _context13.a(2, context.setActiveTabIndex(tabIdClearCart));
|
|
217
|
+
}
|
|
218
|
+
}, _callee13);
|
|
219
|
+
})));
|
|
220
|
+
case 2:
|
|
221
|
+
return _context14.a(2);
|
|
222
|
+
}
|
|
223
|
+
}, _callee14);
|
|
224
|
+
}));
|
|
225
|
+
return _mountSavedCart.apply(this, arguments);
|
|
226
|
+
}
|
|
175
227
|
it.each([new TypeError('Load failed'), {
|
|
176
228
|
statusCode: 502,
|
|
177
229
|
message: 'Gateway unavailable'
|
|
@@ -270,4 +322,98 @@ describe('existing-cart load failures', function () {
|
|
|
270
322
|
}
|
|
271
323
|
}, _callee6);
|
|
272
324
|
})));
|
|
325
|
+
it.each([new TypeError('Failed to fetch'), {
|
|
326
|
+
statusCode: 503,
|
|
327
|
+
message: 'Service unavailable'
|
|
328
|
+
}, null, undefined])('keeps the clear-cart panel and saved cart when replacement fails with %s', /*#__PURE__*/function () {
|
|
329
|
+
var _ref7 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(error) {
|
|
330
|
+
return _regenerator().w(function (_context8) {
|
|
331
|
+
while (1) switch (_context8.n) {
|
|
332
|
+
case 0:
|
|
333
|
+
_context8.n = 1;
|
|
334
|
+
return mountSavedCart();
|
|
335
|
+
case 1:
|
|
336
|
+
mockPost.mockRejectedValueOnce(error);
|
|
337
|
+
_context8.n = 2;
|
|
338
|
+
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7() {
|
|
339
|
+
return _regenerator().w(function (_context7) {
|
|
340
|
+
while (1) switch (_context7.n) {
|
|
341
|
+
case 0:
|
|
342
|
+
_context7.n = 1;
|
|
343
|
+
return context.clearCart();
|
|
344
|
+
case 1:
|
|
345
|
+
return _context7.a(2);
|
|
346
|
+
}
|
|
347
|
+
}, _callee7);
|
|
348
|
+
})));
|
|
349
|
+
case 2:
|
|
350
|
+
expect(context.activeTabIndex).toBe(tabIdClearCart);
|
|
351
|
+
expect(context.cart).toEqual({
|
|
352
|
+
id: 42,
|
|
353
|
+
isTest: false,
|
|
354
|
+
cartItems: [{
|
|
355
|
+
id: 7
|
|
356
|
+
}]
|
|
357
|
+
});
|
|
358
|
+
expect(setCartCookie).not.toHaveBeenCalled();
|
|
359
|
+
expect(mockPost).toHaveBeenCalledTimes(1);
|
|
360
|
+
expect(reportCartFailure).toHaveBeenCalledWith(error, CART_FAILURE_SOURCES.create);
|
|
361
|
+
expect(context.alert.show).toBe(true);
|
|
362
|
+
expect(context.alert.message).toBe((error === null || error === void 0 ? void 0 : error.message) || 'Unable to fetch cart.');
|
|
363
|
+
expect(context.fetchingCart).toBe(false);
|
|
364
|
+
case 3:
|
|
365
|
+
return _context8.a(2);
|
|
366
|
+
}
|
|
367
|
+
}, _callee8);
|
|
368
|
+
}));
|
|
369
|
+
return function (_x4) {
|
|
370
|
+
return _ref7.apply(this, arguments);
|
|
371
|
+
};
|
|
372
|
+
}());
|
|
373
|
+
it('finishes clearing only after an explicit retry successfully creates the replacement', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee1() {
|
|
374
|
+
return _regenerator().w(function (_context1) {
|
|
375
|
+
while (1) switch (_context1.n) {
|
|
376
|
+
case 0:
|
|
377
|
+
_context1.n = 1;
|
|
378
|
+
return mountSavedCart();
|
|
379
|
+
case 1:
|
|
380
|
+
mockPost.mockRejectedValueOnce(new TypeError('Failed to fetch'));
|
|
381
|
+
_context1.n = 2;
|
|
382
|
+
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9() {
|
|
383
|
+
return _regenerator().w(function (_context9) {
|
|
384
|
+
while (1) switch (_context9.n) {
|
|
385
|
+
case 0:
|
|
386
|
+
_context9.n = 1;
|
|
387
|
+
return context.clearCart();
|
|
388
|
+
case 1:
|
|
389
|
+
return _context9.a(2);
|
|
390
|
+
}
|
|
391
|
+
}, _callee9);
|
|
392
|
+
})));
|
|
393
|
+
case 2:
|
|
394
|
+
expect(context.activeTabIndex).toBe(tabIdClearCart);
|
|
395
|
+
_context1.n = 3;
|
|
396
|
+
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee0() {
|
|
397
|
+
return _regenerator().w(function (_context0) {
|
|
398
|
+
while (1) switch (_context0.n) {
|
|
399
|
+
case 0:
|
|
400
|
+
_context0.n = 1;
|
|
401
|
+
return context.clearCart();
|
|
402
|
+
case 1:
|
|
403
|
+
return _context0.a(2);
|
|
404
|
+
}
|
|
405
|
+
}, _callee0);
|
|
406
|
+
})));
|
|
407
|
+
case 3:
|
|
408
|
+
expect(mockPost).toHaveBeenCalledTimes(2);
|
|
409
|
+
expect(context.cart.id).toBe(99);
|
|
410
|
+
expect(setCartCookie).toHaveBeenCalledTimes(1);
|
|
411
|
+
expect(context.activeTabIndex).toBe(tabIdItems);
|
|
412
|
+
expect(context.alert.show).toBe(false);
|
|
413
|
+
expect(context.fetchingCart).toBe(false);
|
|
414
|
+
case 4:
|
|
415
|
+
return _context1.a(2);
|
|
416
|
+
}
|
|
417
|
+
}, _callee1);
|
|
418
|
+
})));
|
|
273
419
|
});
|
|
@@ -1,9 +1,9 @@
|
|
|
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
1
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
3
2
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
4
3
|
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
5
4
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
6
5
|
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
6
|
+
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); }
|
|
7
7
|
import { captureUnexpected } from '@merchi/error-catch';
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -41,17 +41,26 @@ var OPERATIONS = {
|
|
|
41
41
|
export function reportCartFailure(error, source) {
|
|
42
42
|
var handledValidation = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
43
43
|
try {
|
|
44
|
+
// A UI callback returning only confirms display, not a validation failure.
|
|
45
|
+
// Keep this predicate aligned with error-catch safeOperationSuppression.
|
|
46
|
+
var failure = error;
|
|
47
|
+
var validation = false;
|
|
48
|
+
try {
|
|
49
|
+
validation = handledValidation && failure !== null && _typeof(failure) === 'object' && [400, 409, 422].includes(failure.statusCode) && [3, 5, 6, 7].includes(failure.errorCode);
|
|
50
|
+
} catch (_unused) {
|
|
51
|
+
// Classification must not prevent the reporter from handling this value.
|
|
52
|
+
}
|
|
44
53
|
captureUnexpected(error, {
|
|
45
54
|
source: 'manual',
|
|
46
55
|
digest: source,
|
|
47
56
|
operation: _objectSpread({
|
|
48
57
|
name: OPERATIONS[source],
|
|
49
|
-
outcome:
|
|
50
|
-
},
|
|
58
|
+
outcome: validation ? 'handled' : 'failed'
|
|
59
|
+
}, validation ? {
|
|
51
60
|
reason: 'validation'
|
|
52
61
|
} : {})
|
|
53
62
|
});
|
|
54
|
-
} catch (
|
|
63
|
+
} catch (_unused2) {
|
|
55
64
|
// Reporting must not replace the original failure.
|
|
56
65
|
}
|
|
57
66
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { configureFrontendErrors } from '@merchi/error-catch';
|
|
2
|
-
import { reportAddCartItemFailure } from './reportFailure';
|
|
2
|
+
import { reportAddCartItemFailure, reportCartFailure } from './reportFailure';
|
|
3
3
|
function apiError(statusCode, message) {
|
|
4
4
|
var error = new Error(message);
|
|
5
5
|
error.name = 'ApiError';
|
|
@@ -36,11 +36,40 @@ describe('reportAddCartItemFailure', function () {
|
|
|
36
36
|
source: 'manual',
|
|
37
37
|
operation: {
|
|
38
38
|
name: 'cart.add_item',
|
|
39
|
-
outcome: '
|
|
40
|
-
reason: 'validation'
|
|
39
|
+
outcome: 'failed'
|
|
41
40
|
}
|
|
42
41
|
});
|
|
43
42
|
});
|
|
43
|
+
it.each(['Load failed', 'Failed to fetch'])('does not label %s as handled validation', function (message) {
|
|
44
|
+
reportAddCartItemFailure(new TypeError(message), jest.fn());
|
|
45
|
+
expect(events).toHaveLength(1);
|
|
46
|
+
expect(events[0].operation).toEqual({
|
|
47
|
+
name: 'cart.add_item',
|
|
48
|
+
outcome: 'failed'
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
it.each([400, 409, 422])('preserves handled known-input suppression for status %s', function (status) {
|
|
52
|
+
for (var _i = 0, _arr = [3, 5, 6, 7]; _i < _arr.length; _i++) {
|
|
53
|
+
var code = _arr[_i];
|
|
54
|
+
var error = Object.assign(apiError(status, 'input error'), {
|
|
55
|
+
errorCode: code
|
|
56
|
+
});
|
|
57
|
+
reportAddCartItemFailure(error, jest.fn());
|
|
58
|
+
}
|
|
59
|
+
expect(events).toEqual([]);
|
|
60
|
+
});
|
|
61
|
+
it.each([[400, 2], [400, 0], [400, 1], [401, 3], [403, 3], [404, 3], [429, 3], [500, 3], [undefined, 3], [422, undefined], ['422', 3], [422, '3']])('retains unresolved API failure (%s, %s) without a validation label', function (statusCode, errorCode) {
|
|
62
|
+
var error = Object.assign(new Error('request failed'), {
|
|
63
|
+
statusCode: statusCode,
|
|
64
|
+
errorCode: errorCode
|
|
65
|
+
});
|
|
66
|
+
reportAddCartItemFailure(error, jest.fn());
|
|
67
|
+
expect(events).toHaveLength(1);
|
|
68
|
+
expect(events[0].operation).toEqual({
|
|
69
|
+
name: 'cart.add_item',
|
|
70
|
+
outcome: 'failed'
|
|
71
|
+
});
|
|
72
|
+
});
|
|
44
73
|
it('normalises a non-Error rejection and forwards the original value', function () {
|
|
45
74
|
var onError = jest.fn();
|
|
46
75
|
reportAddCartItemFailure('stock check failed', onError);
|
|
@@ -48,7 +77,11 @@ describe('reportAddCartItemFailure', function () {
|
|
|
48
77
|
expect(events[0]).toMatchObject({
|
|
49
78
|
digest: 'cart.addItem',
|
|
50
79
|
errorType: 'NonErrorException',
|
|
51
|
-
message: 'stock check failed'
|
|
80
|
+
message: 'stock check failed',
|
|
81
|
+
operation: {
|
|
82
|
+
name: 'cart.add_item',
|
|
83
|
+
outcome: 'failed'
|
|
84
|
+
}
|
|
52
85
|
});
|
|
53
86
|
});
|
|
54
87
|
it('skips an expected client error and still calls onError', function () {
|
|
@@ -58,6 +91,14 @@ describe('reportAddCartItemFailure', function () {
|
|
|
58
91
|
expect(onError).toHaveBeenCalledWith(error);
|
|
59
92
|
expect(events).toEqual([]);
|
|
60
93
|
});
|
|
94
|
+
it('retains validation-shaped failures without confirmed UI handling', function () {
|
|
95
|
+
reportCartFailure(apiError(422, 'input error'), 'cart.addItem');
|
|
96
|
+
expect(events).toHaveLength(1);
|
|
97
|
+
expect(events[0].operation).toEqual({
|
|
98
|
+
name: 'cart.add_item',
|
|
99
|
+
outcome: 'failed'
|
|
100
|
+
});
|
|
101
|
+
});
|
|
61
102
|
it('reports a server error', function () {
|
|
62
103
|
var error = apiError(500, 'cart service down');
|
|
63
104
|
var onError = jest.fn();
|
|
@@ -99,4 +140,32 @@ describe('reportCartFailure before error-catch is configured', function () {
|
|
|
99
140
|
expect(errorCatch.getDefaultFrontendErrorReporter()).toBeUndefined();
|
|
100
141
|
});
|
|
101
142
|
});
|
|
143
|
+
});
|
|
144
|
+
describe('classification failures', function () {
|
|
145
|
+
it('still calls the reporter when a diagnostic getter throws', function () {
|
|
146
|
+
jest.isolateModules(function () {
|
|
147
|
+
var capture = jest.fn();
|
|
148
|
+
jest.doMock('@merchi/error-catch', function () {
|
|
149
|
+
return {
|
|
150
|
+
captureUnexpected: capture
|
|
151
|
+
};
|
|
152
|
+
});
|
|
153
|
+
var reporting = require('./reportFailure');
|
|
154
|
+
var error = Object.defineProperty(new TypeError('Load failed'), 'statusCode', {
|
|
155
|
+
get: function get() {
|
|
156
|
+
throw new Error('unreadable status');
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
var callback = jest.fn();
|
|
160
|
+
reporting.reportAddCartItemFailure(error, callback);
|
|
161
|
+
expect(callback).toHaveBeenCalledWith(error);
|
|
162
|
+
expect(capture).toHaveBeenCalledWith(error, expect.objectContaining({
|
|
163
|
+
operation: {
|
|
164
|
+
name: 'cart.add_item',
|
|
165
|
+
outcome: 'failed'
|
|
166
|
+
}
|
|
167
|
+
}));
|
|
168
|
+
jest.dontMock('@merchi/error-catch');
|
|
169
|
+
});
|
|
170
|
+
});
|
|
102
171
|
});
|
package/lib/CartProvider.js
CHANGED
|
@@ -550,7 +550,7 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
550
550
|
_context2.p = 5;
|
|
551
551
|
_t3 = _context2.v;
|
|
552
552
|
(0, _reportFailure.reportCartFailure)(_t3, _reportFailure.CART_FAILURE_SOURCES.create);
|
|
553
|
-
alertError(_t3.errorMessage || _t3.message || 'Unable to fetch cart.');
|
|
553
|
+
alertError((_t3 === null || _t3 === void 0 ? void 0 : _t3.errorMessage) || (_t3 === null || _t3 === void 0 ? void 0 : _t3.message) || 'Unable to fetch cart.');
|
|
554
554
|
case 6:
|
|
555
555
|
_context2.p = 6;
|
|
556
556
|
setFetchingCart(false);
|
|
@@ -743,11 +743,16 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
743
743
|
while (1) switch (_context6.n) {
|
|
744
744
|
case 0:
|
|
745
745
|
_context6.n = 1;
|
|
746
|
-
return createCartAndCookie()
|
|
746
|
+
return createCartAndCookie(function () {
|
|
747
|
+
setAlert(function (previous) {
|
|
748
|
+
return _objectSpread(_objectSpread({}, previous), {}, {
|
|
749
|
+
show: false
|
|
750
|
+
});
|
|
751
|
+
});
|
|
752
|
+
if (callback) callback();
|
|
753
|
+
closeClearCart();
|
|
754
|
+
});
|
|
747
755
|
case 1:
|
|
748
|
-
if (callback) callback();
|
|
749
|
-
closeClearCart();
|
|
750
|
-
case 2:
|
|
751
756
|
return _context6.a(2);
|
|
752
757
|
}
|
|
753
758
|
}, _callee6);
|
|
@@ -7,12 +7,12 @@ exports.CART_FAILURE_SOURCES = void 0;
|
|
|
7
7
|
exports.reportAddCartItemFailure = reportAddCartItemFailure;
|
|
8
8
|
exports.reportCartFailure = reportCartFailure;
|
|
9
9
|
var _errorCatch = require("@merchi/error-catch");
|
|
10
|
-
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); }
|
|
11
10
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
12
11
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
13
12
|
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
14
13
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
15
14
|
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
15
|
+
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); }
|
|
16
16
|
/**
|
|
17
17
|
* Call-site tags recorded on the error-catch event.
|
|
18
18
|
*
|
|
@@ -48,17 +48,26 @@ var OPERATIONS = {
|
|
|
48
48
|
function reportCartFailure(error, source) {
|
|
49
49
|
var handledValidation = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
50
50
|
try {
|
|
51
|
+
// A UI callback returning only confirms display, not a validation failure.
|
|
52
|
+
// Keep this predicate aligned with error-catch safeOperationSuppression.
|
|
53
|
+
var failure = error;
|
|
54
|
+
var validation = false;
|
|
55
|
+
try {
|
|
56
|
+
validation = handledValidation && failure !== null && _typeof(failure) === 'object' && [400, 409, 422].includes(failure.statusCode) && [3, 5, 6, 7].includes(failure.errorCode);
|
|
57
|
+
} catch (_unused) {
|
|
58
|
+
// Classification must not prevent the reporter from handling this value.
|
|
59
|
+
}
|
|
51
60
|
(0, _errorCatch.captureUnexpected)(error, {
|
|
52
61
|
source: 'manual',
|
|
53
62
|
digest: source,
|
|
54
63
|
operation: _objectSpread({
|
|
55
64
|
name: OPERATIONS[source],
|
|
56
|
-
outcome:
|
|
57
|
-
},
|
|
65
|
+
outcome: validation ? 'handled' : 'failed'
|
|
66
|
+
}, validation ? {
|
|
58
67
|
reason: 'validation'
|
|
59
68
|
} : {})
|
|
60
69
|
});
|
|
61
|
-
} catch (
|
|
70
|
+
} catch (_unused2) {
|
|
62
71
|
// Reporting must not replace the original failure.
|
|
63
72
|
}
|
|
64
73
|
}
|
package/package.json
CHANGED
|
@@ -5,6 +5,7 @@ import { act } from 'react-dom/test-utils';
|
|
|
5
5
|
import CartProvider, { useCartContext } from './CartProvider';
|
|
6
6
|
import { getCartCookie, setCartCookie } from './utilities/cookie';
|
|
7
7
|
import { reportCartFailure, CART_FAILURE_SOURCES } from './utilities/reportFailure';
|
|
8
|
+
import { tabIdClearCart, tabIdItems } from './utilities/tabs';
|
|
8
9
|
|
|
9
10
|
const mockGet = jest.fn();
|
|
10
11
|
const mockPost = jest.fn();
|
|
@@ -48,7 +49,7 @@ function Probe() {
|
|
|
48
49
|
return null;
|
|
49
50
|
}
|
|
50
51
|
|
|
51
|
-
describe('
|
|
52
|
+
describe('cart load and replacement failures', () => {
|
|
52
53
|
let root: Root;
|
|
53
54
|
let container: HTMLDivElement;
|
|
54
55
|
beforeEach(() => {
|
|
@@ -71,6 +72,17 @@ describe('existing-cart load failures', () => {
|
|
|
71
72
|
</CartProvider>);
|
|
72
73
|
});
|
|
73
74
|
}
|
|
75
|
+
async function mountSavedCart() {
|
|
76
|
+
mockGet.mockResolvedValue({
|
|
77
|
+
toJson: () => ({ id: 42, isTest: false, cartItems: [{ id: 7 }] }),
|
|
78
|
+
});
|
|
79
|
+
await act(async () => {
|
|
80
|
+
root.render(<CartProvider domainId={5} createCartAndCookie={() => {}}>
|
|
81
|
+
<Probe />
|
|
82
|
+
</CartProvider>);
|
|
83
|
+
});
|
|
84
|
+
await act(async () => context.setActiveTabIndex(tabIdClearCart));
|
|
85
|
+
}
|
|
74
86
|
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
87
|
await mount(error);
|
|
76
88
|
expect(getCartCookie).toHaveBeenCalledWith(5);
|
|
@@ -95,4 +107,39 @@ describe('existing-cart load failures', () => {
|
|
|
95
107
|
expect(context.cart.cartItems).toHaveLength(1);
|
|
96
108
|
expect(mockPost).not.toHaveBeenCalled();
|
|
97
109
|
});
|
|
110
|
+
it.each([
|
|
111
|
+
new TypeError('Failed to fetch'),
|
|
112
|
+
{ statusCode: 503, message: 'Service unavailable' },
|
|
113
|
+
null,
|
|
114
|
+
undefined,
|
|
115
|
+
])('keeps the clear-cart panel and saved cart when replacement fails with %s', async error => {
|
|
116
|
+
await mountSavedCart();
|
|
117
|
+
mockPost.mockRejectedValueOnce(error);
|
|
118
|
+
|
|
119
|
+
await act(async () => { await context.clearCart(); });
|
|
120
|
+
|
|
121
|
+
expect(context.activeTabIndex).toBe(tabIdClearCart);
|
|
122
|
+
expect(context.cart).toEqual({ id: 42, isTest: false, cartItems: [{ id: 7 }] });
|
|
123
|
+
expect(setCartCookie).not.toHaveBeenCalled();
|
|
124
|
+
expect(mockPost).toHaveBeenCalledTimes(1);
|
|
125
|
+
expect(reportCartFailure).toHaveBeenCalledWith(error, CART_FAILURE_SOURCES.create);
|
|
126
|
+
expect(context.alert.show).toBe(true);
|
|
127
|
+
expect(context.alert.message).toBe(error?.message || 'Unable to fetch cart.');
|
|
128
|
+
expect(context.fetchingCart).toBe(false);
|
|
129
|
+
});
|
|
130
|
+
it('finishes clearing only after an explicit retry successfully creates the replacement', async () => {
|
|
131
|
+
await mountSavedCart();
|
|
132
|
+
mockPost.mockRejectedValueOnce(new TypeError('Failed to fetch'));
|
|
133
|
+
await act(async () => { await context.clearCart(); });
|
|
134
|
+
expect(context.activeTabIndex).toBe(tabIdClearCart);
|
|
135
|
+
|
|
136
|
+
await act(async () => { await context.clearCart(); });
|
|
137
|
+
|
|
138
|
+
expect(mockPost).toHaveBeenCalledTimes(2);
|
|
139
|
+
expect(context.cart.id).toBe(99);
|
|
140
|
+
expect(setCartCookie).toHaveBeenCalledTimes(1);
|
|
141
|
+
expect(context.activeTabIndex).toBe(tabIdItems);
|
|
142
|
+
expect(context.alert.show).toBe(false);
|
|
143
|
+
expect(context.fetchingCart).toBe(false);
|
|
144
|
+
});
|
|
98
145
|
});
|
package/src/CartProvider.tsx
CHANGED
|
@@ -651,7 +651,7 @@ const CartProvider = ({
|
|
|
651
651
|
if (callback) callback();
|
|
652
652
|
} catch (e: any) {
|
|
653
653
|
reportCartFailure(e, CART_FAILURE_SOURCES.create);
|
|
654
|
-
alertError(e
|
|
654
|
+
alertError(e?.errorMessage || e?.message || 'Unable to fetch cart.');
|
|
655
655
|
} finally {
|
|
656
656
|
setFetchingCart(false);
|
|
657
657
|
}
|
|
@@ -733,9 +733,11 @@ const CartProvider = ({
|
|
|
733
733
|
}
|
|
734
734
|
|
|
735
735
|
async function clearCart(callback?: () => void) {
|
|
736
|
-
await createCartAndCookie()
|
|
737
|
-
|
|
738
|
-
|
|
736
|
+
await createCartAndCookie(() => {
|
|
737
|
+
setAlert(previous => ({ ...previous, show: false }));
|
|
738
|
+
if (callback) callback();
|
|
739
|
+
closeClearCart();
|
|
740
|
+
});
|
|
739
741
|
}
|
|
740
742
|
|
|
741
743
|
const [fetchingShipmentGroups, setFetchingShipmentGroups] = useState(false);
|
|
@@ -43,10 +43,34 @@ describe('reportAddCartItemFailure', () => {
|
|
|
43
43
|
errorType: 'TypeError',
|
|
44
44
|
message: 'cart item create failed',
|
|
45
45
|
source: 'manual',
|
|
46
|
-
operation: { name: 'cart.add_item', outcome: '
|
|
46
|
+
operation: { name: 'cart.add_item', outcome: 'failed' },
|
|
47
47
|
});
|
|
48
48
|
});
|
|
49
49
|
|
|
50
|
+
it.each(['Load failed', 'Failed to fetch'])('does not label %s as handled validation', (message) => {
|
|
51
|
+
reportAddCartItemFailure(new TypeError(message), jest.fn());
|
|
52
|
+
expect(events).toHaveLength(1);
|
|
53
|
+
expect(events[0].operation).toEqual({ name: 'cart.add_item', outcome: 'failed' });
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it.each([400, 409, 422])('preserves handled known-input suppression for status %s', (status) => {
|
|
57
|
+
for (const code of [3, 5, 6, 7]) {
|
|
58
|
+
const error = Object.assign(apiError(status, 'input error'), { errorCode: code });
|
|
59
|
+
reportAddCartItemFailure(error, jest.fn());
|
|
60
|
+
}
|
|
61
|
+
expect(events).toEqual([]);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it.each([
|
|
65
|
+
[400, 2], [400, 0], [400, 1], [401, 3], [403, 3], [404, 3],
|
|
66
|
+
[429, 3], [500, 3], [undefined, 3], [422, undefined], ['422', 3], [422, '3'],
|
|
67
|
+
])('retains unresolved API failure (%s, %s) without a validation label', (statusCode, errorCode) => {
|
|
68
|
+
const error = Object.assign(new Error('request failed'), { statusCode, errorCode });
|
|
69
|
+
reportAddCartItemFailure(error, jest.fn());
|
|
70
|
+
expect(events).toHaveLength(1);
|
|
71
|
+
expect(events[0].operation).toEqual({ name: 'cart.add_item', outcome: 'failed' });
|
|
72
|
+
});
|
|
73
|
+
|
|
50
74
|
it('normalises a non-Error rejection and forwards the original value', () => {
|
|
51
75
|
const onError = jest.fn();
|
|
52
76
|
|
|
@@ -57,6 +81,7 @@ describe('reportAddCartItemFailure', () => {
|
|
|
57
81
|
digest: 'cart.addItem',
|
|
58
82
|
errorType: 'NonErrorException',
|
|
59
83
|
message: 'stock check failed',
|
|
84
|
+
operation: { name: 'cart.add_item', outcome: 'failed' },
|
|
60
85
|
});
|
|
61
86
|
});
|
|
62
87
|
|
|
@@ -70,6 +95,12 @@ describe('reportAddCartItemFailure', () => {
|
|
|
70
95
|
expect(events).toEqual([]);
|
|
71
96
|
});
|
|
72
97
|
|
|
98
|
+
it('retains validation-shaped failures without confirmed UI handling', () => {
|
|
99
|
+
reportCartFailure(apiError(422, 'input error'), 'cart.addItem');
|
|
100
|
+
expect(events).toHaveLength(1);
|
|
101
|
+
expect(events[0].operation).toEqual({ name: 'cart.add_item', outcome: 'failed' });
|
|
102
|
+
});
|
|
103
|
+
|
|
73
104
|
it('reports a server error', () => {
|
|
74
105
|
const error = apiError(500, 'cart service down');
|
|
75
106
|
const onError = jest.fn();
|
|
@@ -109,3 +140,23 @@ describe('reportCartFailure before error-catch is configured', () => {
|
|
|
109
140
|
});
|
|
110
141
|
});
|
|
111
142
|
});
|
|
143
|
+
|
|
144
|
+
describe('classification failures', () => {
|
|
145
|
+
it('still calls the reporter when a diagnostic getter throws', () => {
|
|
146
|
+
jest.isolateModules(() => {
|
|
147
|
+
const capture = jest.fn();
|
|
148
|
+
jest.doMock('@merchi/error-catch', () => ({ captureUnexpected: capture }));
|
|
149
|
+
const reporting = require('./reportFailure');
|
|
150
|
+
const error = Object.defineProperty(new TypeError('Load failed'), 'statusCode', {
|
|
151
|
+
get() { throw new Error('unreadable status'); },
|
|
152
|
+
});
|
|
153
|
+
const callback = jest.fn();
|
|
154
|
+
reporting.reportAddCartItemFailure(error, callback);
|
|
155
|
+
expect(callback).toHaveBeenCalledWith(error);
|
|
156
|
+
expect(capture).toHaveBeenCalledWith(error, expect.objectContaining({
|
|
157
|
+
operation: { name: 'cart.add_item', outcome: 'failed' },
|
|
158
|
+
}));
|
|
159
|
+
jest.dontMock('@merchi/error-catch');
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
});
|
|
@@ -35,13 +35,24 @@ export type CartFailureSource =
|
|
|
35
35
|
*/
|
|
36
36
|
export function reportCartFailure(error: unknown, source: CartFailureSource, handledValidation = false): void {
|
|
37
37
|
try {
|
|
38
|
+
// A UI callback returning only confirms display, not a validation failure.
|
|
39
|
+
// Keep this predicate aligned with error-catch safeOperationSuppression.
|
|
40
|
+
const failure = error as { statusCode?: unknown; errorCode?: unknown } | null;
|
|
41
|
+
let validation = false;
|
|
42
|
+
try {
|
|
43
|
+
validation = handledValidation && failure !== null && typeof failure === 'object' &&
|
|
44
|
+
[400, 409, 422].includes(failure.statusCode as number) &&
|
|
45
|
+
[3, 5, 6, 7].includes(failure.errorCode as number);
|
|
46
|
+
} catch {
|
|
47
|
+
// Classification must not prevent the reporter from handling this value.
|
|
48
|
+
}
|
|
38
49
|
captureUnexpected(error, {
|
|
39
50
|
source: 'manual',
|
|
40
51
|
digest: source,
|
|
41
52
|
operation: {
|
|
42
53
|
name: OPERATIONS[source],
|
|
43
|
-
outcome:
|
|
44
|
-
...(
|
|
54
|
+
outcome: validation ? 'handled' : 'failed',
|
|
55
|
+
...(validation ? { reason: 'validation' as const } : {}),
|
|
45
56
|
},
|
|
46
57
|
});
|
|
47
58
|
} catch {
|