@merchi/cart 1.4.64 → 1.4.65
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 +42 -23
- package/esm/CartProvider.load.test.js +774 -387
- package/lib/CartProvider.js +41 -22
- package/package.json +1 -1
- package/src/CartProvider.load.test.tsx +103 -0
- package/src/CartProvider.tsx +23 -12
package/lib/CartProvider.js
CHANGED
|
@@ -44,6 +44,7 @@ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r)
|
|
|
44
44
|
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
45
45
|
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
46
46
|
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
47
|
+
var useCartBindingsEffect = typeof window === 'undefined' ? _react.useEffect : _react.useLayoutEffect;
|
|
47
48
|
var CartContext = /*#__PURE__*/(0, _react.createContext)({
|
|
48
49
|
cart: {},
|
|
49
50
|
cartClient: {},
|
|
@@ -909,11 +910,11 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
909
910
|
}
|
|
910
911
|
function _getMerchiCartValues() {
|
|
911
912
|
_getMerchiCartValues = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee1() {
|
|
912
|
-
var
|
|
913
|
+
var _ref3, cartItems, currency, subtotalCost, taxAmount, totalCost, cartItemsCount;
|
|
913
914
|
return _regenerator().w(function (_context1) {
|
|
914
915
|
while (1) switch (_context1.n) {
|
|
915
916
|
case 0:
|
|
916
|
-
|
|
917
|
+
_ref3 = cart, cartItems = _ref3.cartItems, currency = _ref3.currency, subtotalCost = _ref3.subtotalCost, taxAmount = _ref3.taxAmount, totalCost = _ref3.totalCost;
|
|
917
918
|
cartItemsCount = cartItems ? cartItems.length : 0;
|
|
918
919
|
return _context1.a(2, {
|
|
919
920
|
cart: cart,
|
|
@@ -1130,27 +1131,45 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
1130
1131
|
}
|
|
1131
1132
|
};
|
|
1132
1133
|
|
|
1133
|
-
//
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1134
|
+
// Publish only committed providers. Suspense can discard render-time state,
|
|
1135
|
+
// so exposing these closures during render can strand a successful cart POST.
|
|
1136
|
+
useCartBindingsEffect(function () {
|
|
1137
|
+
var bindings = {
|
|
1138
|
+
addCartItem: addCartItem,
|
|
1139
|
+
toggleCartOpen: toggleCartModal,
|
|
1140
|
+
getCart: function () {
|
|
1141
|
+
var _getCart2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
|
|
1142
|
+
return _regenerator().w(function (_context2) {
|
|
1143
|
+
while (1) switch (_context2.n) {
|
|
1144
|
+
case 0:
|
|
1145
|
+
_context2.n = 1;
|
|
1146
|
+
return actionGetMerchiCart();
|
|
1147
|
+
case 1:
|
|
1148
|
+
return _context2.a(2);
|
|
1149
|
+
}
|
|
1150
|
+
}, _callee2);
|
|
1151
|
+
}));
|
|
1152
|
+
function getCart() {
|
|
1153
|
+
return _getCart2.apply(this, arguments);
|
|
1145
1154
|
}
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1155
|
+
return getCart;
|
|
1156
|
+
}(),
|
|
1157
|
+
getMerchiCartValues: getMerchiCartValues,
|
|
1158
|
+
isMerchiCartFetching: isMerchiCartFetching,
|
|
1159
|
+
setCartComplete: setCartComplete,
|
|
1160
|
+
refetchCart: refetchCart,
|
|
1161
|
+
createCartAndCookie: requestNewCart
|
|
1162
|
+
};
|
|
1163
|
+
Object.assign(window, bindings);
|
|
1164
|
+
return function () {
|
|
1165
|
+
for (var _i = 0, _Object$entries = Object.entries(bindings); _i < _Object$entries.length; _i++) {
|
|
1166
|
+
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
|
|
1167
|
+
name = _Object$entries$_i[0],
|
|
1168
|
+
binding = _Object$entries$_i[1];
|
|
1169
|
+
if (window[name] === binding) delete window[name];
|
|
1170
|
+
}
|
|
1171
|
+
};
|
|
1172
|
+
});
|
|
1154
1173
|
(0, _react.useEffect)(function () {
|
|
1155
1174
|
(0, _test_checkout.persistTestCheckoutFlag)();
|
|
1156
1175
|
if (domainId) actionGetMerchiCart(!(0, _automatedClient.isAutomatedClient)());
|
package/package.json
CHANGED
|
@@ -54,6 +54,11 @@ function Probe() {
|
|
|
54
54
|
return null;
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
const bindingNames = [
|
|
58
|
+
'addCartItem', 'toggleCartOpen', 'getCart', 'getMerchiCartValues',
|
|
59
|
+
'isMerchiCartFetching', 'setCartComplete', 'refetchCart', 'createCartAndCookie',
|
|
60
|
+
];
|
|
61
|
+
|
|
57
62
|
describe('cart load and replacement failures', () => {
|
|
58
63
|
let root: Root;
|
|
59
64
|
let container: HTMLDivElement;
|
|
@@ -63,6 +68,7 @@ describe('cart load and replacement failures', () => {
|
|
|
63
68
|
Object.defineProperty(navigator, 'userAgent', { configurable: true, value });
|
|
64
69
|
}
|
|
65
70
|
beforeEach(() => {
|
|
71
|
+
for (const name of bindingNames) delete (window as any)[name];
|
|
66
72
|
jest.clearAllMocks();
|
|
67
73
|
mockGet.mockReset();
|
|
68
74
|
mockPost.mockReset();
|
|
@@ -103,6 +109,103 @@ describe('cart load and replacement failures', () => {
|
|
|
103
109
|
});
|
|
104
110
|
await act(async () => context.setActiveTabIndex(tabIdClearCart));
|
|
105
111
|
}
|
|
112
|
+
it('does not expose cart operations until a suspended provider commits', async () => {
|
|
113
|
+
setUserAgent(headlessUserAgent);
|
|
114
|
+
(getCartCookie as jest.Mock).mockResolvedValue(undefined);
|
|
115
|
+
let ready = false;
|
|
116
|
+
let release!: () => void;
|
|
117
|
+
const gate = new Promise<void>(resolve => { release = resolve; });
|
|
118
|
+
function Suspender() { if (!ready) throw gate; return null; }
|
|
119
|
+
await act(async () => root.render(
|
|
120
|
+
<React.Suspense fallback={<span>Loading</span>}>
|
|
121
|
+
<CartProvider domainId={5} createCartAndCookie={() => {}}>
|
|
122
|
+
<Suspender /><Probe />
|
|
123
|
+
</CartProvider>
|
|
124
|
+
</React.Suspense>,
|
|
125
|
+
));
|
|
126
|
+
for (const name of bindingNames) expect((window as any)[name]).toBeUndefined();
|
|
127
|
+
expect(mockPost).not.toHaveBeenCalled();
|
|
128
|
+
await act(async () => { ready = true; release(); });
|
|
129
|
+
for (const name of bindingNames) expect((window as any)[name]).toBeInstanceOf(Function);
|
|
130
|
+
await act(async () => { await (window as any).getCart(); });
|
|
131
|
+
expect(mockPost).toHaveBeenCalledTimes(1);
|
|
132
|
+
expect((await (window as any).getMerchiCartValues()).cart.id).toBe(99);
|
|
133
|
+
expect(context.cart.id).toBe(99);
|
|
134
|
+
});
|
|
135
|
+
it('preserves committed operations when another provider suspends and is abandoned', async () => {
|
|
136
|
+
setUserAgent(headlessUserAgent);
|
|
137
|
+
(getCartCookie as jest.Mock).mockResolvedValue(undefined);
|
|
138
|
+
await act(async () => root.render(
|
|
139
|
+
<CartProvider domainId={5} createCartAndCookie={() => {}}><Probe /></CartProvider>,
|
|
140
|
+
));
|
|
141
|
+
const committed = bindingNames.map(name => (window as any)[name]);
|
|
142
|
+
const otherRoot = createRoot(document.createElement('div'));
|
|
143
|
+
const gate = new Promise<void>(() => {});
|
|
144
|
+
function Suspender(): never { throw gate; }
|
|
145
|
+
try {
|
|
146
|
+
await act(async () => otherRoot.render(
|
|
147
|
+
<React.Suspense fallback={null}>
|
|
148
|
+
<CartProvider domainId={6} createCartAndCookie={() => {}}><Suspender /></CartProvider>
|
|
149
|
+
</React.Suspense>,
|
|
150
|
+
));
|
|
151
|
+
bindingNames.forEach((name, index) => expect((window as any)[name]).toBe(committed[index]));
|
|
152
|
+
await act(async () => { await (window as any).getCart(); });
|
|
153
|
+
expect(context.cart.id).toBe(99);
|
|
154
|
+
expect(mockPost).toHaveBeenCalledTimes(1);
|
|
155
|
+
} finally { await act(async () => otherRoot.unmount()); }
|
|
156
|
+
expect((await (window as any).getMerchiCartValues()).cart.id).toBe(99);
|
|
157
|
+
});
|
|
158
|
+
it('removes its own global operations on unmount', async () => {
|
|
159
|
+
await mountSavedCart();
|
|
160
|
+
await act(async () => root.render(null));
|
|
161
|
+
for (const name of bindingNames) expect((window as any)[name]).toBeUndefined();
|
|
162
|
+
});
|
|
163
|
+
it('keeps the committed domain bindings during a suspended transition', async () => {
|
|
164
|
+
setUserAgent(headlessUserAgent);
|
|
165
|
+
mockGet.mockResolvedValue({ toJson: () => ({ id: 42, cartItems: [] }) });
|
|
166
|
+
const gate = new Promise<void>(() => {});
|
|
167
|
+
function Suspender(): never { throw gate; }
|
|
168
|
+
const tree = (domainId: number, suspend: boolean) => (
|
|
169
|
+
<React.Suspense fallback={null}>
|
|
170
|
+
<CartProvider domainId={domainId} createCartAndCookie={() => {}}>
|
|
171
|
+
{suspend ? <Suspender /> : <Probe />}
|
|
172
|
+
</CartProvider>
|
|
173
|
+
</React.Suspense>
|
|
174
|
+
);
|
|
175
|
+
await act(async () => root.render(tree(5, false)));
|
|
176
|
+
const committed = bindingNames.map(name => (window as any)[name]);
|
|
177
|
+
await act(async () => { React.startTransition(() => root.render(tree(6, true))); });
|
|
178
|
+
bindingNames.forEach((name, index) => expect((window as any)[name]).toBe(committed[index]));
|
|
179
|
+
expect((await (window as any).getMerchiCartValues()).cart.id).toBe(42);
|
|
180
|
+
expect(mockGet).toHaveBeenCalledTimes(1);
|
|
181
|
+
expect(mockPost).not.toHaveBeenCalled();
|
|
182
|
+
});
|
|
183
|
+
it('does not remove global operations owned by another committed provider', async () => {
|
|
184
|
+
await mountSavedCart();
|
|
185
|
+
const otherRoot = createRoot(document.createElement('div'));
|
|
186
|
+
try {
|
|
187
|
+
await act(async () => otherRoot.render(
|
|
188
|
+
<CartProvider domainId={6} createCartAndCookie={() => {}}><Probe /></CartProvider>,
|
|
189
|
+
));
|
|
190
|
+
const otherBindings = bindingNames.map(name => (window as any)[name]);
|
|
191
|
+
await act(async () => root.render(null));
|
|
192
|
+
bindingNames.forEach((name, index) => expect((window as any)[name]).toBe(otherBindings[index]));
|
|
193
|
+
expect((await (window as any).getMerchiCartValues()).cart.id).toBe(42);
|
|
194
|
+
} finally { await act(async () => otherRoot.unmount()); }
|
|
195
|
+
for (const name of bindingNames) expect((window as any)[name]).toBeUndefined();
|
|
196
|
+
});
|
|
197
|
+
it('keeps globals usable after StrictMode setup and cleanup replay', async () => {
|
|
198
|
+
setUserAgent(headlessUserAgent);
|
|
199
|
+
(getCartCookie as jest.Mock).mockResolvedValue(undefined);
|
|
200
|
+
await act(async () => root.render(
|
|
201
|
+
<React.StrictMode>
|
|
202
|
+
<CartProvider domainId={5} createCartAndCookie={() => {}}><Probe /></CartProvider>
|
|
203
|
+
</React.StrictMode>,
|
|
204
|
+
));
|
|
205
|
+
await act(async () => { await (window as any).getCart(); });
|
|
206
|
+
expect(mockPost).toHaveBeenCalledTimes(1);
|
|
207
|
+
expect((await (window as any).getMerchiCartValues()).cart.id).toBe(99);
|
|
208
|
+
});
|
|
106
209
|
it.each([
|
|
107
210
|
'Mozilla/5.0 (X11; Linux x86_64) HeadlessChrome/153.0.8010.0 Safari/537.36',
|
|
108
211
|
'Mozilla/5.0 (compatible; Baiduspider-render/2.0)',
|
package/src/CartProvider.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import { createContext, ReactNode, useContext, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { createContext, ReactNode, useContext, useEffect, useLayoutEffect, useRef, useState } from 'react';
|
|
3
3
|
import { faCheckCircle, faExclamationTriangle, faInfoCircle } from '@fortawesome/free-solid-svg-icons';
|
|
4
4
|
import { Alert, Tab } from './types';
|
|
5
5
|
import {
|
|
@@ -31,6 +31,8 @@ import {
|
|
|
31
31
|
jobSourceFieldsForApi,
|
|
32
32
|
} from './utilities/job_source';
|
|
33
33
|
|
|
34
|
+
const useCartBindingsEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;
|
|
35
|
+
|
|
34
36
|
export interface PropsCart {
|
|
35
37
|
cart: any;
|
|
36
38
|
cartClient?: any;
|
|
@@ -923,17 +925,26 @@ const CartProvider = ({
|
|
|
923
925
|
}
|
|
924
926
|
};
|
|
925
927
|
|
|
926
|
-
//
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
928
|
+
// Publish only committed providers. Suspense can discard render-time state,
|
|
929
|
+
// so exposing these closures during render can strand a successful cart POST.
|
|
930
|
+
useCartBindingsEffect(() => {
|
|
931
|
+
const bindings = {
|
|
932
|
+
addCartItem,
|
|
933
|
+
toggleCartOpen: toggleCartModal,
|
|
934
|
+
getCart: async () => { await actionGetMerchiCart(); },
|
|
935
|
+
getMerchiCartValues,
|
|
936
|
+
isMerchiCartFetching,
|
|
937
|
+
setCartComplete,
|
|
938
|
+
refetchCart,
|
|
939
|
+
createCartAndCookie: requestNewCart,
|
|
940
|
+
};
|
|
941
|
+
Object.assign(window, bindings);
|
|
942
|
+
return () => {
|
|
943
|
+
for (const [name, binding] of Object.entries(bindings)) {
|
|
944
|
+
if ((window as any)[name] === binding) delete (window as any)[name];
|
|
945
|
+
}
|
|
946
|
+
};
|
|
947
|
+
});
|
|
937
948
|
|
|
938
949
|
useEffect(() => {
|
|
939
950
|
persistTestCheckoutFlag();
|