Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redirect with query param regression #2226

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/app/content/hooks/receivePageNotFoundId.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import createTestServices from '../../../test/createTestServices';
import createTestStore from '../../../test/createTestStore';
import { replace } from '../../navigation/actions';
import { AnyMatch } from '../../navigation/types';
import { createRouterService } from '../../navigation/routerService';
import { MiddlewareAPI, Store } from '../../types';
import { assertWindow } from '../../utils';
import { receivePageNotFoundId } from '../actions';
import { processBrowserRedirect } from '../utils/processBrowserRedirect';
import * as errors from '../../errors';
import * as content from '../../content';

const mockFetch = (valueToReturn: any, error?: any) => () => new Promise((resolve, reject) => {
if (error) {
Expand All @@ -17,14 +20,21 @@ describe('receivePageNotFoundId hook', () => {
let store: Store;
let helpers: MiddlewareAPI & ReturnType<typeof createTestServices>;
let historyReplaceSpy: jest.SpyInstance;
let dispatch: jest.SpyInstance;
let fetchBackup: any;
let window: Window;

beforeEach(() => {
store = createTestStore();
window = assertWindow();
delete (window as any).location;

window.location = {
origin: 'openstax.org',
} as any as Window['location'];

helpers = {
...createTestServices(),
router: createRouterService([...Object.values(content.routes), ...Object.values(errors.routes)]),
dispatch: store.dispatch,
getState: store.getState,
};
Expand All @@ -33,8 +43,6 @@ describe('receivePageNotFoundId hook', () => {
pathname: '/books/physics/pages/1-introduction301',
} as any;

dispatch = jest.spyOn(helpers, 'dispatch');

historyReplaceSpy = jest.spyOn(helpers.history, 'replace')
.mockImplementation(jest.fn());

Expand Down Expand Up @@ -66,8 +74,16 @@ describe('receivePageNotFoundId hook', () => {
it('calls history.replace if redirect is found', async() => {
(globalThis as any).fetch = mockFetch([{ from: helpers.history.location.pathname, to: '/books/redirected' }]);

await hook(receivePageNotFoundId('asdf'));
await processBrowserRedirect(helpers);

expect(historyReplaceSpy).toHaveBeenCalledWith('/books/redirected');
});

it('updates window.location if target is not within rex', async() => {
(globalThis as any).fetch = mockFetch([{ from: helpers.history.location.pathname, to: '/redirected' }]);

await processBrowserRedirect(helpers);

expect(dispatch).toHaveBeenCalledWith(replace(helpers.router.findRoute('/books/redirected') as AnyMatch));
expect(window.location.href).toEqual('openstax.org/redirected');
});
});
18 changes: 9 additions & 9 deletions src/app/content/utils/processBrowserRedirect.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { History } from 'history';
import { Redirects } from '../../../../data/redirects/types';
import { RouterService } from '../../navigation/routerService';
import { Dispatch } from '../../types';
import { replace } from '../../navigation/actions';
import { AnyMatch } from '../../navigation/types';
import { assertWindow } from '../../utils';
import { matchForRoute } from '../../navigation/utils';
import { external } from '../../errors/routes';

export const processBrowserRedirect = async(services: {
router: RouterService,
history: History,
dispatch: Dispatch
}) => {
export const processBrowserRedirect = async(services: {router: RouterService, history: History}) => {
const window = assertWindow();
const redirects: Redirects = await fetch('/rex/redirects.json')
.then((res) => res.json())
.catch(() => []);

for (const {from, to} of redirects) {
if (from === services.history.location.pathname) {
services.dispatch(replace(services.router.findRoute(to) as AnyMatch));
if (matchForRoute(external, services.router.findRoute(to))) {
window.location.href = window.location.origin + to;
}
services.history.replace(to);
return true;
}
}
Expand Down
Loading