Skip to content

Commit e12e4fe

Browse files
authored
Pwa 3339 (#4314)
* PWA-3339:Header location should set to correct endpoint in case of 301|302 * PWA-3339: Header location should set to correct endpoint in case of 301|302 * PWA:3339- runing lint
1 parent 329dfc5 commit e12e4fe

File tree

3 files changed

+64
-21
lines changed

3 files changed

+64
-21
lines changed

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

+33-11
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,19 @@ const givenQueryResult = response => {
3939
useLazyQuery.mockReset();
4040
runQuery.mockReset();
4141
useLazyQuery.mockImplementation(() => {
42-
return [runQuery, response];
42+
// Create a promise that resolves to the mocked response
43+
const queryPromise = new Promise(resolve => {
44+
// Immediately resolve with the response
45+
resolve(response);
46+
});
47+
48+
// Return a tuple where the first item is an async function (runQuery) and the second is the response object
49+
return [
50+
async () => {
51+
return await queryPromise; // Return the response when the promise resolves
52+
},
53+
response
54+
];
4355
});
4456
};
4557

@@ -243,7 +255,8 @@ describe('returns NOT_FOUND when queries come back empty', () => {
243255
});
244256

245257
describe('returns REDIRECT after receiving a redirect code', () => {
246-
test('redirect code 301', async () => {
258+
test('redirect code 301', () => {
259+
// Arrange: Set up the query result
247260
givenQueryResult({
248261
data: {
249262
route: {
@@ -256,15 +269,24 @@ describe('returns REDIRECT after receiving a redirect code', () => {
256269
loading: false
257270
});
258271

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'
272+
// Create a new Promise to handle `act`
273+
return new Promise(resolve => {
274+
// Use `act` to render the component
275+
act(() => {
276+
// Render the component and resolve the promise
277+
create(<Component />);
278+
resolve();
279+
});
280+
}).then(() => {
281+
// Assert that `replace` was called once
282+
expect(replace).toHaveBeenCalledTimes(1);
283+
// Assert that `log` was called once
284+
expect(log).toHaveBeenCalledTimes(1);
285+
// Assert that `log` was called with the expected arguments
286+
expect(log).toHaveBeenNthCalledWith(1, {
287+
isRedirect: true,
288+
relativeUrl: '/foo.html'
289+
});
268290
});
269291
});
270292

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

+30-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useEffect, useRef } from 'react';
1+
import { useCallback, useEffect, useRef, useState } from 'react';
22
import { useHistory, useLocation } from 'react-router-dom';
33
import { useLazyQuery } from '@apollo/client';
44
import { useRootComponents } from '../../context/rootComponents';
@@ -44,9 +44,11 @@ export const useMagentoRoute = (props = {}) => {
4444
const component = componentMap.get(pathname);
4545

4646
const [runQuery, queryResult] = useLazyQuery(resolveUrlQuery);
47+
4748
// destructure the query result
4849
const { data, error, loading } = queryResult;
49-
const { route } = data || {};
50+
const [getRouteData, setRouteData] = useState(data);
51+
const { route } = getRouteData || {};
5052

5153
// redirect to external url
5254
useEffect(() => {
@@ -59,15 +61,34 @@ export const useMagentoRoute = (props = {}) => {
5961
}, [route]);
6062

6163
useEffect(() => {
64+
let isMounted = true; // Track whether the component is still mounted
65+
const runInitialQuery = async () => {
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+
}
82+
};
6283
if (initialized.current || !getInlinedPageData()) {
63-
runQuery({
64-
fetchPolicy: 'cache-and-network',
65-
nextFetchPolicy: 'cache-first',
66-
variables: { url: pathname }
67-
});
68-
fetchedPathname.current = pathname;
84+
runInitialQuery();
6985
}
70-
}, [initialized, pathname]); // eslint-disable-line react-hooks/exhaustive-deps
86+
// Cleanup function
87+
return () => {
88+
isMounted = false; // Mark as unmounted
89+
};
90+
}, [initialized, pathname, runQuery]);
91+
// eslint-disable-line react-hooks/exhaustive-deps
7192

7293
useEffect(() => {
7394
if (component) {

packages/venia-ui/upward.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ veniaResponse:
2121
pattern: '(js|json|png|jpg|gif|svg|ico|css|txt)'
2222
use: veniaStatic
2323
- matches: urlResolver.redirect_code
24-
pattern: '301'
24+
pattern: '(301|302)'
2525
use: dynamicRedirect
2626
default: veniaAppShell
2727

0 commit comments

Comments
 (0)