Skip to content

Commit

Permalink
Pwa 3339 (#4314)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
glo11372 authored Aug 28, 2024
1 parent 329dfc5 commit e12e4fe
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,19 @@ const givenQueryResult = response => {
useLazyQuery.mockReset();
runQuery.mockReset();
useLazyQuery.mockImplementation(() => {
return [runQuery, response];
// Create a promise that resolves to the mocked response
const queryPromise = new Promise(resolve => {
// Immediately resolve with the response
resolve(response);
});

// Return a tuple where the first item is an async function (runQuery) and the second is the response object
return [
async () => {
return await queryPromise; // Return the response when the promise resolves
},
response
];
});
};

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

describe('returns REDIRECT after receiving a redirect code', () => {
test('redirect code 301', async () => {
test('redirect code 301', () => {
// Arrange: Set up the query result
givenQueryResult({
data: {
route: {
Expand All @@ -256,15 +269,24 @@ describe('returns REDIRECT after receiving a redirect code', () => {
loading: false
});

await act(() => {
create(<Component />);
});

expect(replace).toHaveBeenCalledTimes(1);
expect(log).toHaveBeenCalledTimes(1);
expect(log).toHaveBeenNthCalledWith(1, {
isRedirect: true,
relativeUrl: '/foo.html'
// Create a new Promise to handle `act`
return new Promise(resolve => {
// Use `act` to render the component
act(() => {
// Render the component and resolve the promise
create(<Component />);
resolve();
});
}).then(() => {
// Assert that `replace` was called once
expect(replace).toHaveBeenCalledTimes(1);
// Assert that `log` was called once
expect(log).toHaveBeenCalledTimes(1);
// Assert that `log` was called with the expected arguments
expect(log).toHaveBeenNthCalledWith(1, {
isRedirect: true,
relativeUrl: '/foo.html'
});
});
});

Expand Down
39 changes: 30 additions & 9 deletions packages/peregrine/lib/talons/MagentoRoute/useMagentoRoute.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useRef } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';
import { useHistory, useLocation } from 'react-router-dom';
import { useLazyQuery } from '@apollo/client';
import { useRootComponents } from '../../context/rootComponents';
Expand Down Expand Up @@ -44,9 +44,11 @@ export const useMagentoRoute = (props = {}) => {
const component = componentMap.get(pathname);

const [runQuery, queryResult] = useLazyQuery(resolveUrlQuery);

// destructure the query result
const { data, error, loading } = queryResult;
const { route } = data || {};
const [getRouteData, setRouteData] = useState(data);
const { route } = getRouteData || {};

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

useEffect(() => {
let isMounted = true; // Track whether the component is still mounted
const runInitialQuery = async () => {
try {
const { data } = await runQuery({
fetchPolicy: 'cache-and-network',
nextFetchPolicy: 'cache-first',
variables: { url: pathname }
});

if (isMounted) {
setRouteData(data);
fetchedPathname.current = pathname;
}
} catch (error) {
if (isMounted) {
console.error('Error running initial query:', error);
}
}
};
if (initialized.current || !getInlinedPageData()) {
runQuery({
fetchPolicy: 'cache-and-network',
nextFetchPolicy: 'cache-first',
variables: { url: pathname }
});
fetchedPathname.current = pathname;
runInitialQuery();
}
}, [initialized, pathname]); // eslint-disable-line react-hooks/exhaustive-deps
// Cleanup function
return () => {
isMounted = false; // Mark as unmounted
};
}, [initialized, pathname, runQuery]);
// eslint-disable-line react-hooks/exhaustive-deps

useEffect(() => {
if (component) {
Expand Down
2 changes: 1 addition & 1 deletion packages/venia-ui/upward.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ veniaResponse:
pattern: '(js|json|png|jpg|gif|svg|ico|css|txt)'
use: veniaStatic
- matches: urlResolver.redirect_code
pattern: '301'
pattern: '(301|302)'
use: dynamicRedirect
default: veniaAppShell

Expand Down

0 comments on commit e12e4fe

Please sign in to comment.