-
Notifications
You must be signed in to change notification settings - Fork 1
[PRMP-1493] Implement restriction post and complete page #1198
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
Draft
adamwhitingnhs
wants to merge
1
commit into
PRMP-1481
Choose a base branch
from
PRMP-1493
base: PRMP-1481
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+330
−101
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
99 changes: 91 additions & 8 deletions
99
...ns/userPatientRestrictionsAddConfirmStage/UserPatientRestrictionsAddConfirmStage.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,106 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import UserPatientRestrictionsAddConfirmStage from './UserPatientRestrictionsAddConfirmStage'; | ||
| import { Mock } from 'vitest'; | ||
| import usePatient from '../../../../helpers/hooks/usePatient'; | ||
| import postUserPatientRestriction from '../../../../helpers/requests/userPatientRestrictions/createUserPatientRestriction'; | ||
| import { buildPatientDetails } from '../../../../helpers/test/testBuilders'; | ||
| import { userEvent } from '@testing-library/user-event'; | ||
| import { routeChildren, routes } from '../../../../types/generic/routes'; | ||
|
|
||
| vi.mock('react-router-dom'); | ||
| vi.mock('react-router-dom', async () => { | ||
| const actual = await vi.importActual('react-router-dom'); | ||
| return { | ||
| ...actual, | ||
| useNavigate: (): Mock => mockNavigate, | ||
| Link: ({ children, to }: { children: React.ReactNode; to: string }): React.JSX.Element => ( | ||
| <a href={to}>{children}</a> | ||
| ), | ||
| }; | ||
| }); | ||
| vi.mock('../../../../helpers/hooks/usePatient'); | ||
| vi.mock('../../../../helpers/hooks/useBaseAPIUrl'); | ||
| vi.mock('../../../../helpers/hooks/useBaseAPIHeaders'); | ||
| vi.mock('../../../../helpers/requests/userPatientRestrictions/createUserPatientRestriction'); | ||
|
|
||
| const mockNavigate = vi.fn(); | ||
| const mockUsePatient = usePatient as Mock; | ||
| const mockPostUserPatientRestriction = postUserPatientRestriction as Mock; | ||
| const mockPatient = buildPatientDetails(); | ||
| const userInformation = { | ||
| name: 'John Doe', | ||
| smartcardId: '123456789012', | ||
| firstName: 'John', | ||
| lastName: 'Doe', | ||
| }; | ||
|
|
||
| describe('UserPatientRestrictionsAddConfirmStage', () => { | ||
| it('renders correctly', () => { | ||
| const userInformation = { | ||
| name: 'John Doe', | ||
| smartcardId: '123456789012', | ||
| firstName: 'John', | ||
| lastName: 'Doe', | ||
| }; | ||
| beforeEach(() => { | ||
| vi.resetAllMocks(); | ||
| mockUsePatient.mockReturnValue(mockPatient); | ||
| mockPostUserPatientRestriction.mockResolvedValue({}); | ||
| }); | ||
|
|
||
| it('renders correctly', () => { | ||
| render(<UserPatientRestrictionsAddConfirmStage userInformation={userInformation} />); | ||
|
|
||
| expect(screen.getByTestId('smartcard-id')).toHaveTextContent(userInformation.smartcardId); | ||
| expect(screen.getByTestId('staff-member')).toHaveTextContent( | ||
| `${userInformation.firstName} ${userInformation.lastName}`, | ||
| ); | ||
| }); | ||
|
|
||
| it('submits new patient restriction and navigates to success page on button click', async () => { | ||
| render(<UserPatientRestrictionsAddConfirmStage userInformation={userInformation} />); | ||
|
|
||
| const button = screen.getByTestId('continue-button'); | ||
| await userEvent.click(button); | ||
|
|
||
| expect(mockPostUserPatientRestriction).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| nhsNumber: mockPatient.nhsNumber, | ||
| smartcardId: userInformation.smartcardId, | ||
| }), | ||
| ); | ||
|
|
||
| expect(mockNavigate).toHaveBeenCalledWith( | ||
| routeChildren.USER_PATIENT_RESTRICTIONS_ACTION_COMPLETE, | ||
| ); | ||
| }); | ||
|
|
||
| it('shows loading state when submitting restriction', async () => { | ||
| render(<UserPatientRestrictionsAddConfirmStage userInformation={userInformation} />); | ||
|
|
||
| mockPostUserPatientRestriction.mockReturnValue(new Promise(() => {})); | ||
|
|
||
| const button = screen.getByTestId('continue-button'); | ||
| await userEvent.click(button); | ||
|
|
||
| expect(screen.getByText('Processing...')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('navigates to error page on 500', async () => { | ||
| render(<UserPatientRestrictionsAddConfirmStage userInformation={userInformation} />); | ||
|
|
||
| mockPostUserPatientRestriction.mockRejectedValue({ | ||
| response: { status: 500 }, | ||
| }); | ||
|
|
||
| const button = screen.getByTestId('continue-button'); | ||
| await userEvent.click(button); | ||
|
|
||
| expect(mockNavigate).toHaveBeenCalledWith(expect.stringContaining(routes.SERVER_ERROR)); | ||
| }); | ||
|
|
||
| it('navigates to session expired page on 403', async () => { | ||
| render(<UserPatientRestrictionsAddConfirmStage userInformation={userInformation} />); | ||
|
|
||
| mockPostUserPatientRestriction.mockRejectedValue({ | ||
| response: { status: 403 }, | ||
| }); | ||
|
|
||
| const button = screen.getByTestId('continue-button'); | ||
| await userEvent.click(button); | ||
|
|
||
| expect(mockNavigate).toHaveBeenCalledWith(routes.SESSION_EXPIRED); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
app/src/helpers/requests/userPatientRestrictions/createUserPatientRestriction.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import axios from 'axios'; | ||
| import { Mocked } from 'vitest'; | ||
| import postUserPatientRestriction from './createUserPatientRestriction'; | ||
| import { endpoints } from '../../../types/generic/endpoints'; | ||
|
|
||
| vi.mock('axios'); | ||
| const mockedAxios = axios as Mocked<typeof axios>; | ||
|
|
||
| describe('createUserPatientRestriction', () => { | ||
| it('should make expected API call', async () => { | ||
| const mockPost = vi.fn(); | ||
| mockedAxios.post.mockImplementation(mockPost); | ||
|
|
||
| const args = { | ||
| smartcardId: '123', | ||
| nhsNumber: '9876543210', | ||
| baseAPIUrl: 'http://example.com/api', | ||
| baseAPIHeaders: { Authorization: 'Bearer token', 'Content-Type': 'application/json' }, | ||
| }; | ||
|
|
||
| await postUserPatientRestriction(args); | ||
|
|
||
| expect(mockPost).toHaveBeenCalledWith( | ||
| `${args.baseAPIUrl}${endpoints.USER_PATIENT_RESTRICTIONS}`, | ||
| { | ||
| smartcardId: args.smartcardId, | ||
| nhsNumber: args.nhsNumber, | ||
| }, | ||
| { | ||
| headers: { | ||
| ...args.baseAPIHeaders, | ||
| }, | ||
| params: { | ||
| patientId: args.nhsNumber, | ||
| }, | ||
| }, | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import axios, { AxiosError } from 'axios'; | ||
| import { AuthHeaders } from '../../../types/blocks/authHeaders'; | ||
| import { endpoints } from '../../../types/generic/endpoints'; | ||
|
|
||
| type CreateUserPatientRestrictionArgs = { | ||
| smartcardId: string; | ||
| nhsNumber: string; | ||
| baseAPIUrl: string; | ||
| baseAPIHeaders: AuthHeaders; | ||
| }; | ||
|
|
||
| const postUserPatientRestriction = async ({ | ||
| smartcardId, | ||
| nhsNumber, | ||
| baseAPIUrl, | ||
| baseAPIHeaders, | ||
| }: CreateUserPatientRestrictionArgs): Promise<void> => { | ||
| try { | ||
| const url = baseAPIUrl + endpoints.USER_PATIENT_RESTRICTIONS; | ||
| await axios.post( | ||
| url, | ||
| { | ||
| smartcardId, | ||
| nhsNumber, | ||
| }, | ||
| { | ||
| headers: { | ||
| ...baseAPIHeaders, | ||
| }, | ||
| params: { | ||
| patientId: nhsNumber, | ||
| }, | ||
| }, | ||
| ); | ||
| } catch (e) { | ||
| const error = e as AxiosError; | ||
| throw error; | ||
| } | ||
| }; | ||
|
|
||
| export default postUserPatientRestriction; |
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.