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

Prmdr 452: Back button functionality on the search screens #150

Merged
merged 7 commits into from
Nov 21, 2023
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
65 changes: 64 additions & 1 deletion app/src/components/generic/backButton/BackButton.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { render, screen, waitFor } from '@testing-library/react';
import * as ReactRouter from 'react-router';
import userEvent from '@testing-library/user-event';
import BackButton from './BackButton';
import { routes } from '../../../types/generic/routes';
import { endpoints } from '../../../types/generic/endpoints';
import { useBaseAPIUrl } from '../../../providers/configProvider/ConfigProvider';

describe('BackButton', () => {
it('navigates to previous page when clicking the back button', async () => {
it('navigates to previous page when clicking the back buttonand not on the search pages', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo

const history = createMemoryHistory({
thisusernameisnowtaken marked this conversation as resolved.
Show resolved Hide resolved
initialEntries: ['/', '/example'],
initialIndex: 1,
Expand All @@ -23,4 +26,64 @@ describe('BackButton', () => {
expect(history.location.pathname).toBe('/');
});
});

it('calls the login handler when clicking the back button on the upload search page', async () => {
const test_location_prefix = useBaseAPIUrl();
thisusernameisnowtaken marked this conversation as resolved.
Show resolved Hide resolved

const history = createMemoryHistory({
initialEntries: ['/', '/example'],
initialIndex: 1,
});

//Override the default window.location property as it is not configurable so can't be mocked by jest
Object.defineProperty(window, 'location', {
configurable: true,
enumerable: true,
value: new URL(window.location.href),
});

window.location.replace = jest.fn();

render(
<ReactRouter.Router navigator={history} location={routes.UPLOAD_SEARCH}>
<BackButton />
</ReactRouter.Router>,
);

userEvent.click(screen.getByText('Back'));

await waitFor(() => {
expect(window.location.replace).toBeCalledWith(test_location_prefix + endpoints.LOGIN);
});
});

it('calls the login handler when clicking the back button on the download search page', async () => {
const test_location_prefix = useBaseAPIUrl();

thisusernameisnowtaken marked this conversation as resolved.
Show resolved Hide resolved
const history = createMemoryHistory({
initialEntries: ['/', '/example'],
initialIndex: 1,
});

//Override the default window.location property as it is not configurable so can't be mocked by jest
Object.defineProperty(window, 'location', {
configurable: true,
enumerable: true,
value: new URL(window.location.href),
});

window.location.replace = jest.fn();

render(
<ReactRouter.Router navigator={history} location={routes.DOWNLOAD_SEARCH}>
<BackButton />
</ReactRouter.Router>,
);

userEvent.click(screen.getByText('Back'));

await waitFor(() => {
expect(window.location.replace).toBeCalledWith(test_location_prefix + endpoints.LOGIN);
});
});
});
16 changes: 14 additions & 2 deletions app/src/components/generic/backButton/BackButton.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import { BackLink } from 'nhsuk-react-components';
import React from 'react';
import type { MouseEvent } from 'react';
import { useNavigate } from 'react-router';
import { useNavigate, useLocation } from 'react-router';
import { endpoints } from '../../../types/generic/endpoints';
import { useBaseAPIUrl } from '../../../providers/configProvider/ConfigProvider';
import { routes } from '../../../types/generic/routes';

const BackButton = () => {
const navigate = useNavigate();
const location = useLocation();
const baseAPIUrl = useBaseAPIUrl();

const onBack = (e: MouseEvent<HTMLAnchorElement>) => {
e.preventDefault();
navigate(-1);
if (
location.pathname === routes.UPLOAD_SEARCH ||
location.pathname === routes.DOWNLOAD_SEARCH
) {
window.location.replace(`${baseAPIUrl}${endpoints.LOGIN}`);
} else {
navigate(-1);
}
};

return (
Expand Down
2 changes: 1 addition & 1 deletion app/src/pages/patientSearchPage/PatientSearchPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('PatientSearchPage', () => {
},
);
it.each(authorisedRoles)(
"rdisplays a loading spinner when the patients details are being requested when user role is '%s'",
"displays a loading spinner when the patients details are being requested when user role is '%s'",
async (role) => {
mockedUseRole.mockReturnValue(role);

Expand Down
Loading