Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e660597

Browse files
committedAug 22, 2024·
PWA-3339: Header location should set to correct endpoint in case of 301|302
1 parent d3130b8 commit e660597

File tree

2 files changed

+61
-22
lines changed

2 files changed

+61
-22
lines changed
 

‎packages/peregrine/lib/talons/MagentoRoute/__tests__/useMagentoRoute.spec.js

+35-11
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,22 @@ jest.mock('@apollo/client', () => {
3838
const givenQueryResult = response => {
3939
useLazyQuery.mockReset();
4040
runQuery.mockReset();
41+
const resolve = jest.fn().mockName('resolve');
42+
const reject = jest.fn().mockName('reject');
4143
useLazyQuery.mockImplementation(() => {
42-
return [runQuery, response];
44+
// Create a promise that resolves to the mocked response
45+
const queryPromise = new Promise((resolve, reject) => {
46+
// Immediately resolve with the response
47+
resolve(response);
48+
});
49+
50+
// Return a tuple where the first item is an async function (runQuery) and the second is the response object
51+
return [
52+
async () => {
53+
return await queryPromise; // Return the response when the promise resolves
54+
},
55+
response
56+
];
4357
});
4458
};
4559

@@ -243,7 +257,8 @@ describe('returns NOT_FOUND when queries come back empty', () => {
243257
});
244258

245259
describe('returns REDIRECT after receiving a redirect code', () => {
246-
test('redirect code 301', async () => {
260+
test('redirect code 301', () => {
261+
// Arrange: Set up the query result
247262
givenQueryResult({
248263
data: {
249264
route: {
@@ -256,15 +271,24 @@ describe('returns REDIRECT after receiving a redirect code', () => {
256271
loading: false
257272
});
258273

259-
await act(() => {
260-
create(<Component />);
261-
});
262-
263-
expect(replace).toHaveBeenCalledTimes(1);
264-
expect(log).toHaveBeenCalledTimes(1);
265-
expect(log).toHaveBeenNthCalledWith(1, {
266-
isRedirect: true,
267-
relativeUrl: '/foo.html'
274+
// Create a new Promise to handle `act`
275+
return new Promise(resolve => {
276+
// Use `act` to render the component
277+
act(() => {
278+
// Render the component and resolve the promise
279+
create(<Component />);
280+
resolve();
281+
});
282+
}).then(() => {
283+
// Assert that `replace` was called once
284+
expect(replace).toHaveBeenCalledTimes(1);
285+
// Assert that `log` was called once
286+
expect(log).toHaveBeenCalledTimes(1);
287+
// Assert that `log` was called with the expected arguments
288+
expect(log).toHaveBeenNthCalledWith(1, {
289+
isRedirect: true,
290+
relativeUrl: '/foo.html'
291+
});
268292
});
269293
});
270294

‎packages/peregrine/lib/talons/MagentoRoute/useMagentoRoute.js

+26-11
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ export const useMagentoRoute = (props = {}) => {
4444
const component = componentMap.get(pathname);
4545

4646
const [runQuery, queryResult] = useLazyQuery(resolveUrlQuery);
47-
const [getRouteData, setRouteData] = useState(null);
4847

4948
// destructure the query result
50-
const { error, loading } = queryResult;
49+
const { data, error, loading } = queryResult;
50+
const [getRouteData, setRouteData] = useState(data);
5151
const { route } = getRouteData || {};
5252

5353
// redirect to external url
@@ -61,20 +61,35 @@ export const useMagentoRoute = (props = {}) => {
6161
}, [route]);
6262

6363
useEffect(() => {
64+
let isMounted = true; // Track whether the component is still mounted
6465
const runInitialQuery = async () => {
65-
const { data } = await runQuery({
66-
fetchPolicy: 'cache-and-network',
67-
nextFetchPolicy: 'cache-first',
68-
variables: { url: pathname }
69-
});
70-
setRouteData(data);
71-
fetchedPathname.current = pathname;
66+
try {
67+
const { data } = await runQuery({
68+
fetchPolicy: 'cache-and-network',
69+
nextFetchPolicy: 'cache-first',
70+
variables: { url: pathname }
71+
});
72+
73+
if (isMounted) {
74+
setRouteData(data);
75+
fetchedPathname.current = pathname;
76+
}
77+
} catch (error) {
78+
if (isMounted) {
79+
console.error('Error running initial query:', error);
80+
}
81+
}
7282
};
7383
if (initialized.current || !getInlinedPageData()) {
7484
runInitialQuery();
7585
}
76-
}, [initialized, pathname, runQuery]); // eslint-disable-line react-hooks/exhaustive-deps
77-
86+
// Cleanup function
87+
return () => {
88+
isMounted = false; // Mark as unmounted
89+
};
90+
}, [initialized, pathname]);
91+
// eslint-disable-line react-hooks/exhaustive-deps
92+
7893
useEffect(() => {
7994
if (component) {
8095
return;

0 commit comments

Comments
 (0)