@merchi/cart 1.4.58 → 1.4.60

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.
@@ -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('existing-cart load failures', function () {
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 _callee8(error) {
143
- return _regenerator().w(function (_context8) {
144
- while (1) switch (_context8.n) {
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
- _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) {
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 _context7.a(2);
165
+ return _context10.a(2);
165
166
  }
166
- }, _callee7);
167
+ }, _callee10);
167
168
  })));
168
169
  case 1:
169
- return _context8.a(2);
170
+ return _context11.a(2);
170
171
  }
171
- }, _callee8);
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
  });
@@ -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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@merchi/cart",
3
- "version": "1.4.58",
3
+ "version": "1.4.60",
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.9",
97
97
  "@merchi/invoice": "^1.3.14",
98
- "@merchi/product-form": "^1.19.53",
99
- "@merchi/product-form-sdk": "^1.0.34",
100
- "@merchi/sdk": "^1.42.23",
98
+ "@merchi/product-form": "^1.19.54",
99
+ "@merchi/product-form-sdk": "^1.0.35",
100
+ "@merchi/sdk": "^1.42.24",
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",
@@ -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('existing-cart load failures', () => {
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
  });
@@ -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.errorMessage || e.message || 'Unable to fetch cart.');
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
- if (callback) callback();
738
- closeClearCart();
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);