@tapcart/mobile-components 0.15.2 → 0.16.1
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/dist/components/core/button.d.ts +37 -0
- package/dist/components/core/button.d.ts.map +1 -0
- package/dist/components/core/button.js +44 -0
- package/dist/components/core/html.d.ts +15 -0
- package/dist/components/core/html.d.ts.map +1 -0
- package/dist/components/core/html.js +45 -0
- package/dist/components/core/text.d.ts +29 -0
- package/dist/components/core/text.d.ts.map +1 -0
- package/dist/components/core/text.js +88 -0
- package/dist/components/hooks/use-nosto-recommendation.d.ts +4 -0
- package/dist/components/hooks/use-nosto-recommendation.d.ts.map +1 -1
- package/dist/components/hooks/use-nosto-recommendation.js +116 -23
- package/dist/components/hooks/use-nosto-recommendation.queries.d.ts +0 -1
- package/dist/components/hooks/use-nosto-recommendation.queries.d.ts.map +1 -1
- package/dist/components/hooks/use-nosto-recommendation.queries.js +0 -3
- package/dist/components/hooks/use-nosto-recommendation.test.js +168 -171
- package/dist/components/hooks/use-products.d.ts.map +1 -1
- package/dist/components/hooks/use-products.js +24 -4
- package/dist/components/hooks/use-products.test.d.ts +2 -0
- package/dist/components/hooks/use-products.test.d.ts.map +1 -0
- package/dist/components/hooks/use-products.test.js +129 -0
- package/dist/components/hooks/use-search-analytics-open-product.d.ts +3 -2
- package/dist/components/hooks/use-search-analytics-open-product.d.ts.map +1 -1
- package/dist/components/libs/cache/ProductsLocalStorage.d.ts.map +1 -1
- package/dist/components/libs/cache/ProductsLocalStorage.js +32 -0
- package/dist/components/libs/cache/ProductsLocalStorage.test.d.ts +2 -0
- package/dist/components/libs/cache/ProductsLocalStorage.test.d.ts.map +1 -0
- package/dist/components/libs/cache/ProductsLocalStorage.test.js +110 -0
- package/dist/core/index.d.ts +99 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +98 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/lib/get-data.d.ts +27 -0
- package/dist/lib/get-data.d.ts.map +1 -0
- package/dist/lib/get-data.js +65 -0
- package/dist/lib/get-data.test.d.ts +2 -0
- package/dist/lib/get-data.test.d.ts.map +1 -0
- package/dist/lib/get-data.test.js +74 -0
- package/dist/lib/to-html.d.ts +9 -0
- package/dist/lib/to-html.d.ts.map +1 -0
- package/dist/lib/to-html.js +102 -0
- package/dist/styles.css +6 -0
- package/package.json +1 -1
|
@@ -14,13 +14,13 @@ import { useProducts } from "./use-products";
|
|
|
14
14
|
jest.mock("./use-products", () => ({
|
|
15
15
|
useProducts: jest.fn(),
|
|
16
16
|
}));
|
|
17
|
-
// Mock fetch for GraphQL queries
|
|
17
|
+
// Mock fetch for GraphQL queries. `newSession` is intentionally NOT handled:
|
|
18
|
+
// after TICKET-5763 nothing may ever mint a session, so a newSession request
|
|
19
|
+
// would be a bug — fail loudly if one is ever issued.
|
|
18
20
|
global.fetch = jest.fn((url, options) => {
|
|
19
21
|
const body = JSON.parse(options === null || options === void 0 ? void 0 : options.body);
|
|
20
22
|
if (body.query.includes("newSession")) {
|
|
21
|
-
return Promise.
|
|
22
|
-
json: () => Promise.resolve({ data: { newSession: "mockSessionId" } }),
|
|
23
|
-
});
|
|
23
|
+
return Promise.reject(new Error("newSession must never be called (TICKET-5763)"));
|
|
24
24
|
}
|
|
25
25
|
if (body.query.includes("GetFrontPageRecommendations")) {
|
|
26
26
|
return Promise.resolve({
|
|
@@ -93,6 +93,31 @@ global.fetch = jest.fn((url, options) => {
|
|
|
93
93
|
}
|
|
94
94
|
return Promise.reject(new Error("Unknown query"));
|
|
95
95
|
});
|
|
96
|
+
// Asserts the invariant that must hold for EVERY test: no request ever asks
|
|
97
|
+
// Nosto to mint a new session.
|
|
98
|
+
const expectNoNewSessionCall = () => {
|
|
99
|
+
const calls = global.fetch.mock.calls;
|
|
100
|
+
const mintedSession = calls.some(([, options]) => {
|
|
101
|
+
const raw = options === null || options === void 0 ? void 0 : options.body;
|
|
102
|
+
return typeof raw === "string" && raw.includes("newSession");
|
|
103
|
+
});
|
|
104
|
+
expect(mintedSession).toBe(false);
|
|
105
|
+
};
|
|
106
|
+
const setCidCookie = (value) => {
|
|
107
|
+
document.cookie = `2c.cId=${value}`;
|
|
108
|
+
};
|
|
109
|
+
const clearCookies = () => {
|
|
110
|
+
document.cookie = "2c.cId=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
|
|
111
|
+
};
|
|
112
|
+
const recentSession = (sessionId) => ({
|
|
113
|
+
sessionId,
|
|
114
|
+
lastSessionEventTimestamp: Date.now(),
|
|
115
|
+
});
|
|
116
|
+
const flush = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
117
|
+
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
118
|
+
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
119
|
+
}));
|
|
120
|
+
});
|
|
96
121
|
describe("useNostoRecommendations", () => {
|
|
97
122
|
const mockIntegrations = [
|
|
98
123
|
{
|
|
@@ -104,233 +129,218 @@ describe("useNostoRecommendations", () => {
|
|
|
104
129
|
const mockBaseURL = "http://mockbaseurl.com";
|
|
105
130
|
beforeEach(() => {
|
|
106
131
|
jest.clearAllMocks();
|
|
132
|
+
clearCookies();
|
|
107
133
|
useProducts.mockReturnValue({
|
|
108
134
|
products: [{ id: "1", name: "Product 1" }],
|
|
109
135
|
isLoading: false,
|
|
110
136
|
error: null,
|
|
111
137
|
});
|
|
112
138
|
});
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
139
|
+
afterEach(() => {
|
|
140
|
+
// Invariant across every path: never mint a Nosto session.
|
|
141
|
+
expectNoNewSessionCall();
|
|
142
|
+
clearCookies();
|
|
143
|
+
});
|
|
144
|
+
it("uses the passed-in session for front page recommendations", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
145
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "mockSlotId", "US", undefined, undefined, undefined, undefined, undefined, recentSession("passed-session")));
|
|
117
146
|
expect(result.current.isLoading).toBe(true);
|
|
118
|
-
|
|
119
|
-
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
120
|
-
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
121
|
-
}));
|
|
122
|
-
// Check final state
|
|
147
|
+
yield flush();
|
|
123
148
|
expect(result.current.isLoading).toBe(false);
|
|
124
149
|
expect(result.current.error).toBe(null);
|
|
150
|
+
// Native session was used -> personalization surfaced as "session".
|
|
151
|
+
expect(result.current.personalization).toBe("session");
|
|
125
152
|
expect(useProducts).toHaveBeenCalledWith({
|
|
126
153
|
productIds: ["front-page-id-1"],
|
|
127
154
|
baseURL: mockBaseURL,
|
|
128
155
|
productHandles: [],
|
|
129
|
-
queryVariables: {
|
|
130
|
-
appId: undefined,
|
|
131
|
-
country: "US",
|
|
132
|
-
},
|
|
156
|
+
queryVariables: { appId: undefined, country: "US" },
|
|
133
157
|
mock: false,
|
|
134
158
|
});
|
|
135
159
|
}));
|
|
136
|
-
it("
|
|
137
|
-
|
|
138
|
-
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL,
|
|
139
|
-
// Initial state check
|
|
160
|
+
it("falls back to the 2c.cId cookie when no session is passed (front page)", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
161
|
+
setCidCookie("cookie-cid");
|
|
162
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "mockSlotId", "US"));
|
|
140
163
|
expect(result.current.isLoading).toBe(true);
|
|
141
|
-
|
|
142
|
-
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
143
|
-
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
144
|
-
}));
|
|
145
|
-
// Check final state
|
|
164
|
+
yield flush();
|
|
146
165
|
expect(result.current.isLoading).toBe(false);
|
|
147
|
-
|
|
166
|
+
// Cookie-derived session resolves, so the personalized front page query runs.
|
|
167
|
+
expect(result.current.personalization).toBe("cookie");
|
|
148
168
|
expect(useProducts).toHaveBeenCalledWith({
|
|
149
|
-
productIds: ["
|
|
169
|
+
productIds: ["front-page-id-1"],
|
|
150
170
|
baseURL: mockBaseURL,
|
|
151
171
|
productHandles: [],
|
|
152
|
-
queryVariables: {
|
|
153
|
-
appId: undefined,
|
|
154
|
-
country: "US",
|
|
155
|
-
},
|
|
172
|
+
queryVariables: { appId: undefined, country: "US" },
|
|
156
173
|
mock: false,
|
|
157
174
|
});
|
|
158
175
|
}));
|
|
159
|
-
it("
|
|
160
|
-
const
|
|
161
|
-
const productIds = ["mockProductId"];
|
|
162
|
-
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, slotId, "US", NostoRecommendationsType.BEST_SELLERS, productIds));
|
|
163
|
-
// Initial state check
|
|
176
|
+
it("falls back to bestSellers on the front page when there is no session and no cookie", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
177
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "mockSlotId", "US"));
|
|
164
178
|
expect(result.current.isLoading).toBe(true);
|
|
165
|
-
|
|
166
|
-
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
167
|
-
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
168
|
-
}));
|
|
169
|
-
// Check final state
|
|
179
|
+
yield flush();
|
|
170
180
|
expect(result.current.isLoading).toBe(false);
|
|
171
|
-
|
|
181
|
+
// No identity available -> personalization "none".
|
|
182
|
+
expect(result.current.personalization).toBe("none");
|
|
183
|
+
// No session-based query fired; bestSellers fallback engaged.
|
|
172
184
|
expect(useProducts).toHaveBeenCalledWith({
|
|
173
185
|
productIds: ["best-seller-1"],
|
|
174
186
|
baseURL: mockBaseURL,
|
|
175
187
|
productHandles: [],
|
|
176
|
-
queryVariables: {
|
|
177
|
-
appId: undefined,
|
|
178
|
-
country: "US",
|
|
179
|
-
},
|
|
188
|
+
queryVariables: { appId: undefined, country: "US" },
|
|
180
189
|
mock: false,
|
|
181
190
|
});
|
|
182
191
|
}));
|
|
183
|
-
it("
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
//
|
|
192
|
+
it("upgrades from bestSellers to personalized once a late 2c.cId cookie appears", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
193
|
+
// The WebView Nosto SDK mints the cookie asynchronously, so a carousel can
|
|
194
|
+
// mount before it exists. The bounded poll must pick up the late cookie and
|
|
195
|
+
// re-resolve from the bestSellers fallback to the personalized front page.
|
|
196
|
+
jest.useFakeTimers();
|
|
197
|
+
try {
|
|
198
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "mockSlotId", "US"));
|
|
199
|
+
// No cookie at mount -> "none" + bestSellers fallback.
|
|
200
|
+
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
201
|
+
yield jest.advanceTimersByTimeAsync(0);
|
|
202
|
+
}));
|
|
203
|
+
expect(result.current.personalization).toBe("none");
|
|
204
|
+
expect(useProducts).toHaveBeenLastCalledWith(expect.objectContaining({ productIds: ["best-seller-1"] }));
|
|
205
|
+
// Cookie minted after mount; the 250ms poll picks it up and refetches.
|
|
206
|
+
setCidCookie("late-cookie-cid");
|
|
207
|
+
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
208
|
+
yield jest.advanceTimersByTimeAsync(250);
|
|
209
|
+
}));
|
|
210
|
+
expect(result.current.personalization).toBe("cookie");
|
|
211
|
+
expect(useProducts).toHaveBeenLastCalledWith(expect.objectContaining({ productIds: ["front-page-id-1"] }));
|
|
212
|
+
}
|
|
213
|
+
finally {
|
|
214
|
+
jest.useRealTimers();
|
|
215
|
+
}
|
|
216
|
+
}));
|
|
217
|
+
it("returns best sellers when productIds is empty and slotId is not provided", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
218
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "", "US"));
|
|
187
219
|
expect(result.current.isLoading).toBe(true);
|
|
188
|
-
|
|
189
|
-
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
190
|
-
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
191
|
-
}));
|
|
192
|
-
// Check final state
|
|
220
|
+
yield flush();
|
|
193
221
|
expect(result.current.isLoading).toBe(false);
|
|
194
222
|
expect(result.current.error).toBe(null);
|
|
195
223
|
expect(useProducts).toHaveBeenCalledWith({
|
|
196
|
-
productIds: ["
|
|
224
|
+
productIds: ["best-seller-1"],
|
|
197
225
|
baseURL: mockBaseURL,
|
|
198
226
|
productHandles: [],
|
|
199
|
-
queryVariables: {
|
|
200
|
-
appId: undefined,
|
|
201
|
-
country: "US",
|
|
202
|
-
},
|
|
227
|
+
queryVariables: { appId: undefined, country: "US" },
|
|
203
228
|
mock: false,
|
|
204
229
|
});
|
|
205
230
|
}));
|
|
206
|
-
it("
|
|
207
|
-
|
|
208
|
-
|
|
231
|
+
it("returns best sellers when BEST_SELLERS type and productIds are provided but no slotId", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
232
|
+
// Stable reference so the hook's effect deps don't change across renders.
|
|
233
|
+
const productIds = ["mockProductId"];
|
|
234
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "", "US", NostoRecommendationsType.BEST_SELLERS, productIds));
|
|
209
235
|
expect(result.current.isLoading).toBe(true);
|
|
210
|
-
|
|
211
|
-
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
212
|
-
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
213
|
-
}));
|
|
214
|
-
// Check final state
|
|
236
|
+
yield flush();
|
|
215
237
|
expect(result.current.isLoading).toBe(false);
|
|
216
|
-
expect(result.current.error).toBe(null);
|
|
217
238
|
expect(useProducts).toHaveBeenCalledWith({
|
|
218
239
|
productIds: ["best-seller-1"],
|
|
219
240
|
baseURL: mockBaseURL,
|
|
220
241
|
productHandles: [],
|
|
221
|
-
queryVariables: {
|
|
222
|
-
appId: undefined,
|
|
223
|
-
country: "US",
|
|
224
|
-
},
|
|
242
|
+
queryVariables: { appId: undefined, country: "US" },
|
|
225
243
|
mock: false,
|
|
226
244
|
});
|
|
227
245
|
}));
|
|
228
|
-
it("
|
|
246
|
+
it("returns product page recommendations when a session, productIds and slotId are provided", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
229
247
|
const productIds = ["mockProductId"];
|
|
230
|
-
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "", "US", NostoRecommendationsType.VIEWED_TOGETHER, productIds, undefined));
|
|
231
|
-
// Initial state check
|
|
248
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "mockSlotId", "US", NostoRecommendationsType.VIEWED_TOGETHER, productIds, undefined, undefined, undefined, recentSession("passed-session")));
|
|
232
249
|
expect(result.current.isLoading).toBe(true);
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
250
|
+
yield flush();
|
|
251
|
+
expect(result.current.isLoading).toBe(false);
|
|
252
|
+
expect(useProducts).toHaveBeenCalledWith({
|
|
253
|
+
productIds: ["product-page-id-1"],
|
|
254
|
+
baseURL: mockBaseURL,
|
|
255
|
+
productHandles: [],
|
|
256
|
+
queryVariables: { appId: undefined, country: "US" },
|
|
257
|
+
mock: false,
|
|
258
|
+
});
|
|
259
|
+
}));
|
|
260
|
+
it("returns the related products PDP fallback (no slotId, non-category type)", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
261
|
+
const productIds = ["mockProductId"];
|
|
262
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "", "US", NostoRecommendationsType.VIEWED_TOGETHER, productIds));
|
|
263
|
+
expect(result.current.isLoading).toBe(true);
|
|
264
|
+
yield flush();
|
|
238
265
|
expect(result.current.isLoading).toBe(false);
|
|
239
|
-
expect(result.current.error).toBe(null);
|
|
240
266
|
expect(useProducts).toHaveBeenCalledWith({
|
|
241
267
|
productIds: ["related-1"],
|
|
242
268
|
baseURL: mockBaseURL,
|
|
243
269
|
productHandles: [],
|
|
244
|
-
queryVariables: {
|
|
245
|
-
appId: undefined,
|
|
246
|
-
country: "US",
|
|
247
|
-
},
|
|
270
|
+
queryVariables: { appId: undefined, country: "US" },
|
|
248
271
|
mock: false,
|
|
249
272
|
});
|
|
250
273
|
}));
|
|
251
|
-
it("
|
|
252
|
-
const slotId = "mockSlotId";
|
|
274
|
+
it("returns the category PDP fallback when a session is available and type is VIEWED_CATEGORY", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
253
275
|
const productIds = ["mockProductId"];
|
|
254
|
-
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL,
|
|
255
|
-
// Initial state check
|
|
276
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "", "US", NostoRecommendationsType.VIEWED_CATEGORY, productIds, undefined, undefined, undefined, recentSession("passed-session")));
|
|
256
277
|
expect(result.current.isLoading).toBe(true);
|
|
257
|
-
|
|
258
|
-
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
259
|
-
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
260
|
-
}));
|
|
261
|
-
// Check final state
|
|
278
|
+
yield flush();
|
|
262
279
|
expect(result.current.isLoading).toBe(false);
|
|
263
|
-
expect(result.current.error).toBe(null);
|
|
264
280
|
expect(useProducts).toHaveBeenCalledWith({
|
|
265
|
-
productIds: ["
|
|
281
|
+
productIds: ["category-page-id-1"],
|
|
266
282
|
baseURL: mockBaseURL,
|
|
267
283
|
productHandles: [],
|
|
268
|
-
queryVariables: {
|
|
269
|
-
appId: undefined,
|
|
270
|
-
country: "US",
|
|
271
|
-
},
|
|
284
|
+
queryVariables: { appId: undefined, country: "US" },
|
|
272
285
|
mock: false,
|
|
273
286
|
});
|
|
274
287
|
}));
|
|
275
|
-
it("
|
|
288
|
+
it("falls back to bestSellers for a VIEWED_CATEGORY PDP when there is no session", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
276
289
|
const productIds = ["mockProductId"];
|
|
277
|
-
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "", "US", NostoRecommendationsType.
|
|
278
|
-
// Initial state check
|
|
290
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "", "US", NostoRecommendationsType.VIEWED_CATEGORY, productIds));
|
|
279
291
|
expect(result.current.isLoading).toBe(true);
|
|
280
|
-
|
|
281
|
-
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
282
|
-
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
283
|
-
}));
|
|
284
|
-
// Check final state
|
|
292
|
+
yield flush();
|
|
285
293
|
expect(result.current.isLoading).toBe(false);
|
|
286
|
-
expect(result.current.error).toBe(null);
|
|
287
294
|
expect(useProducts).toHaveBeenCalledWith({
|
|
288
|
-
productIds: ["
|
|
295
|
+
productIds: ["best-seller-1"],
|
|
289
296
|
baseURL: mockBaseURL,
|
|
290
297
|
productHandles: [],
|
|
291
|
-
queryVariables: {
|
|
292
|
-
appId: undefined,
|
|
293
|
-
country: "US",
|
|
294
|
-
},
|
|
298
|
+
queryVariables: { appId: undefined, country: "US" },
|
|
295
299
|
mock: false,
|
|
296
300
|
});
|
|
297
301
|
}));
|
|
298
|
-
it("
|
|
302
|
+
it("serves bestSellers (not an error) for a VIEWED_CATEGORY PDP when the cId cookie is malformed", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
303
|
+
// A bad percent-encoding makes decodeURIComponent throw; the shared reader
|
|
304
|
+
// must return null (never the raw value) so this routes into the designed
|
|
305
|
+
// bestSellers fallback instead of escaping into the hook's error state.
|
|
306
|
+
document.cookie = "2c.cId=%";
|
|
299
307
|
const productIds = ["mockProductId"];
|
|
300
|
-
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "", "US", NostoRecommendationsType.VIEWED_CATEGORY, productIds
|
|
301
|
-
// Initial state check
|
|
308
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "", "US", NostoRecommendationsType.VIEWED_CATEGORY, productIds));
|
|
302
309
|
expect(result.current.isLoading).toBe(true);
|
|
303
|
-
|
|
304
|
-
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
305
|
-
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
306
|
-
}));
|
|
307
|
-
// Check final state
|
|
310
|
+
yield flush();
|
|
308
311
|
expect(result.current.isLoading).toBe(false);
|
|
309
312
|
expect(result.current.error).toBe(null);
|
|
313
|
+
expect(result.current.personalization).toBe("none");
|
|
310
314
|
expect(useProducts).toHaveBeenCalledWith({
|
|
311
|
-
productIds: ["
|
|
315
|
+
productIds: ["best-seller-1"],
|
|
312
316
|
baseURL: mockBaseURL,
|
|
313
317
|
productHandles: [],
|
|
314
|
-
queryVariables: {
|
|
315
|
-
appId: undefined,
|
|
316
|
-
country: "US",
|
|
317
|
-
},
|
|
318
|
+
queryVariables: { appId: undefined, country: "US" },
|
|
318
319
|
mock: false,
|
|
319
320
|
});
|
|
320
321
|
}));
|
|
321
|
-
it("
|
|
322
|
+
it("defaults to best sellers if productIds are empty but a non-BestSellers type is used", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
323
|
+
const productIds = [];
|
|
324
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "", "US", NostoRecommendationsType.VIEWED_TOGETHER, productIds));
|
|
325
|
+
expect(result.current.isLoading).toBe(true);
|
|
326
|
+
yield flush();
|
|
327
|
+
expect(result.current.isLoading).toBe(false);
|
|
328
|
+
expect(useProducts).toHaveBeenCalledWith({
|
|
329
|
+
productIds: ["best-seller-1"],
|
|
330
|
+
baseURL: mockBaseURL,
|
|
331
|
+
productHandles: [],
|
|
332
|
+
queryVariables: { appId: undefined, country: "US" },
|
|
333
|
+
mock: false,
|
|
334
|
+
});
|
|
335
|
+
}));
|
|
336
|
+
it("handles loading state", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
322
337
|
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "", "US"));
|
|
323
|
-
// Check initial loading state
|
|
324
338
|
expect(result.current.isLoading).toBe(true);
|
|
325
|
-
|
|
326
|
-
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
327
|
-
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
328
|
-
}));
|
|
329
|
-
// Verify loading has finished
|
|
339
|
+
yield flush();
|
|
330
340
|
expect(result.current.isLoading).toBe(false);
|
|
331
341
|
expect(result.current.error).toBe(null);
|
|
332
342
|
}));
|
|
333
|
-
it("
|
|
343
|
+
it("handles error state", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
334
344
|
const mockError = () => "Failed to fetch products";
|
|
335
345
|
useProducts.mockReturnValue({
|
|
336
346
|
products: [],
|
|
@@ -338,58 +348,45 @@ describe("useNostoRecommendations", () => {
|
|
|
338
348
|
error: mockError,
|
|
339
349
|
});
|
|
340
350
|
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL));
|
|
341
|
-
// Initial state check
|
|
342
351
|
expect(result.current.isLoading).toBe(true);
|
|
343
|
-
|
|
344
|
-
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
345
|
-
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
346
|
-
}));
|
|
347
|
-
// Verify final error state
|
|
352
|
+
yield flush();
|
|
348
353
|
expect(result.current.isLoading).toBe(false);
|
|
349
354
|
expect(result.current.error).toBe(mockError);
|
|
350
355
|
expect(result.current.recommendations).toEqual([]);
|
|
351
356
|
}));
|
|
352
|
-
it("
|
|
353
|
-
const
|
|
354
|
-
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "", "US", NostoRecommendationsType.VIEWED_TOGETHER, productIds, undefined));
|
|
355
|
-
// Initial state check
|
|
357
|
+
it("returns an empty array if skip is true", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
358
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "", "US", NostoRecommendationsType.BEST_SELLERS, undefined, true));
|
|
356
359
|
expect(result.current.isLoading).toBe(true);
|
|
357
|
-
|
|
358
|
-
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
359
|
-
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
360
|
-
}));
|
|
361
|
-
// Check final state
|
|
360
|
+
yield flush();
|
|
362
361
|
expect(result.current.isLoading).toBe(false);
|
|
363
|
-
expect(result.current.error).toBe(null);
|
|
364
362
|
expect(useProducts).toHaveBeenCalledWith({
|
|
365
|
-
productIds: [
|
|
363
|
+
productIds: [],
|
|
366
364
|
baseURL: mockBaseURL,
|
|
367
365
|
productHandles: [],
|
|
368
|
-
queryVariables: {
|
|
369
|
-
appId: undefined,
|
|
370
|
-
country: "US",
|
|
371
|
-
},
|
|
366
|
+
queryVariables: { appId: undefined, country: "US" },
|
|
372
367
|
mock: false,
|
|
373
368
|
});
|
|
369
|
+
// skip must short-circuit before any Nosto request and before any
|
|
370
|
+
// bestSellers fallback resolves — the empty-productIds call above always
|
|
371
|
+
// matches on first render, so these guard against a broken skip.
|
|
372
|
+
expect(global.fetch).not.toHaveBeenCalled();
|
|
373
|
+
expect(useProducts).not.toHaveBeenCalledWith(expect.objectContaining({ productIds: ["best-seller-1"] }));
|
|
374
374
|
}));
|
|
375
|
-
it("
|
|
376
|
-
const
|
|
377
|
-
|
|
375
|
+
it("ignores an expired session and falls back without minting a new one", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
376
|
+
const staleSession = {
|
|
377
|
+
sessionId: "stale-session",
|
|
378
|
+
lastSessionEventTimestamp: Date.now() - 60 * 60 * 1000, // 1h ago
|
|
379
|
+
};
|
|
380
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "mockSlotId", "US", undefined, undefined, undefined, undefined, undefined, staleSession));
|
|
378
381
|
expect(result.current.isLoading).toBe(true);
|
|
379
|
-
|
|
380
|
-
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
381
|
-
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
382
|
-
}));
|
|
383
|
-
// Check final state
|
|
382
|
+
yield flush();
|
|
384
383
|
expect(result.current.isLoading).toBe(false);
|
|
384
|
+
// Expired session is dropped by the hook -> null sessionId -> bestSellers.
|
|
385
385
|
expect(useProducts).toHaveBeenCalledWith({
|
|
386
|
-
productIds: [],
|
|
386
|
+
productIds: ["best-seller-1"],
|
|
387
387
|
baseURL: mockBaseURL,
|
|
388
388
|
productHandles: [],
|
|
389
|
-
queryVariables: {
|
|
390
|
-
appId: undefined,
|
|
391
|
-
country: "US",
|
|
392
|
-
},
|
|
389
|
+
queryVariables: { appId: undefined, country: "US" },
|
|
393
390
|
mock: false,
|
|
394
391
|
});
|
|
395
392
|
}));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-products.d.ts","sourceRoot":"","sources":["../../../components/hooks/use-products.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAK1C,KAAK,GAAG,GAAG,MAAM,CAAA;AACjB,KAAK,cAAc,GAAG;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAAA;AACxD,KAAK,gBAAgB,GAAG;IACtB,UAAU,CAAC,EAAE,MAAM,EAAE,CAAA;IACrB,UAAU,CAAC,EAAE,MAAM,EAAE,CAAA;IACrB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAA;IACzB,OAAO,EAAE,GAAG,CAAA;IACZ,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IACpC,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI,KAAK,OAAO,CAAC,GAAG,CAAC,CAAA;IAC1E,UAAU,CAAC,EAAE,cAAc,EAAE,CAAA;IAC7B,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,qBAAqB,CAAC,EAAE,OAAO,CAAA;CAChC,CAAA;AACD,KAAK,iBAAiB,GAAG;IACvB,QAAQ,EAAE,OAAO,EAAE,CAAA;IACnB,KAAK,EAAE,GAAG,CAAA;IACV,SAAS,EAAE,OAAO,CAAA;IAClB,YAAY,EAAE,OAAO,CAAA;IACrB,YAAY,EAAE,CAAC,OAAO,EAAE,OAAO,KAC3B;QACE,EAAE,EAAE,MAAM,CAAA;QACV,MAAM,EAAE,MAAM,CAAA;KACf,GACD,SAAS,CAAA;CACd,CAAA;AAqDD,wBAAgB,WAAW,CAAC,KAAK,EAAE,gBAAgB,GAAG,IAAI,GAAG,iBAAiB,
|
|
1
|
+
{"version":3,"file":"use-products.d.ts","sourceRoot":"","sources":["../../../components/hooks/use-products.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAK1C,KAAK,GAAG,GAAG,MAAM,CAAA;AACjB,KAAK,cAAc,GAAG;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAAA;AACxD,KAAK,gBAAgB,GAAG;IACtB,UAAU,CAAC,EAAE,MAAM,EAAE,CAAA;IACrB,UAAU,CAAC,EAAE,MAAM,EAAE,CAAA;IACrB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAA;IACzB,OAAO,EAAE,GAAG,CAAA;IACZ,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IACpC,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI,KAAK,OAAO,CAAC,GAAG,CAAC,CAAA;IAC1E,UAAU,CAAC,EAAE,cAAc,EAAE,CAAA;IAC7B,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,qBAAqB,CAAC,EAAE,OAAO,CAAA;CAChC,CAAA;AACD,KAAK,iBAAiB,GAAG;IACvB,QAAQ,EAAE,OAAO,EAAE,CAAA;IACnB,KAAK,EAAE,GAAG,CAAA;IACV,SAAS,EAAE,OAAO,CAAA;IAClB,YAAY,EAAE,OAAO,CAAA;IACrB,YAAY,EAAE,CAAC,OAAO,EAAE,OAAO,KAC3B;QACE,EAAE,EAAE,MAAM,CAAA;QACV,MAAM,EAAE,MAAM,CAAA;KACf,GACD,SAAS,CAAA;CACd,CAAA;AAqDD,wBAAgB,WAAW,CAAC,KAAK,EAAE,gBAAgB,GAAG,IAAI,GAAG,iBAAiB,CAqL7E"}
|
|
@@ -108,16 +108,36 @@ export function useProducts(props) {
|
|
|
108
108
|
const productsArray = Array.isArray(data) ? data : data.products || [];
|
|
109
109
|
const cachedProducts = productsArray
|
|
110
110
|
.filter((product) => !!product)
|
|
111
|
-
.map((product) =>
|
|
112
|
-
productsLocalStorage.setCacheItem(product);
|
|
113
|
-
return product;
|
|
114
|
-
});
|
|
111
|
+
.map((product) => productsLocalStorage.setCacheItem(product) || product);
|
|
115
112
|
// Return either just the products array or an object with spread contents and overridden products
|
|
116
113
|
return Array.isArray(data)
|
|
117
114
|
? cachedProducts
|
|
118
115
|
: Object.assign(Object.assign({}, data), { products: cachedProducts });
|
|
119
116
|
}));
|
|
120
117
|
const fetcher = (props === null || props === void 0 ? void 0 : props.fetcher) || defaultFetcher;
|
|
118
|
+
// Reset local product state when the requested product identity changes so
|
|
119
|
+
// stale or partial state from a previously viewed product cannot carry over.
|
|
120
|
+
const requestedProductKey = React.useMemo(() => {
|
|
121
|
+
var _a, _b, _c;
|
|
122
|
+
return JSON.stringify({
|
|
123
|
+
productIds: (_a = props === null || props === void 0 ? void 0 : props.productIds) !== null && _a !== void 0 ? _a : [],
|
|
124
|
+
productHandles: (_b = props === null || props === void 0 ? void 0 : props.productHandles) !== null && _b !== void 0 ? _b : [],
|
|
125
|
+
variantIds: (_c = props === null || props === void 0 ? void 0 : props.variantIds) !== null && _c !== void 0 ? _c : [],
|
|
126
|
+
});
|
|
127
|
+
}, [props === null || props === void 0 ? void 0 : props.productIds, props === null || props === void 0 ? void 0 : props.productHandles, props === null || props === void 0 ? void 0 : props.variantIds]);
|
|
128
|
+
const hasMountedRef = React.useRef(false);
|
|
129
|
+
React.useEffect(() => {
|
|
130
|
+
if (!hasMountedRef.current) {
|
|
131
|
+
hasMountedRef.current = true;
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
setProductResponse(productsLocalStorage.getCacheItems({
|
|
135
|
+
productIds: props === null || props === void 0 ? void 0 : props.productIds,
|
|
136
|
+
productHandles: props === null || props === void 0 ? void 0 : props.productHandles,
|
|
137
|
+
}));
|
|
138
|
+
setIsProductResponseReady(false);
|
|
139
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
140
|
+
}, [requestedProductKey]);
|
|
121
141
|
const { data, error, isLoading } = useSWR(url ? [url, body] : null, ([url, body]) => fetcher(url, body));
|
|
122
142
|
const cacheProduct = (product) => {
|
|
123
143
|
const cachedProduct = productsLocalStorage.setCacheItem(product);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-products.test.d.ts","sourceRoot":"","sources":["../../../components/hooks/use-products.test.tsx"],"names":[],"mappings":""}
|