generated from NHSDigital/nhs-notify-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNHSNotifyFormWrapper.test.tsx
More file actions
66 lines (51 loc) · 1.91 KB
/
NHSNotifyFormWrapper.test.tsx
File metadata and controls
66 lines (51 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { mockDeep } from 'jest-mock-extended';
import {
NHSNotifyFormWrapper,
csrfServerAction,
} from '@molecules/NHSNotifyFormWrapper/NHSNotifyFormWrapper';
import { render } from '@testing-library/react';
import { redirect } from 'next/navigation';
import { verifyFormCsrfToken } from '@utils/csrf-utils';
jest.mock('next/navigation');
jest.mock('@utils/csrf-utils', () => ({
verifyFormCsrfToken: jest.fn(),
}));
test('Renders back button', () => {
const container = render(
<NHSNotifyFormWrapper formId='form-id' action='/action'>
<input id='input' value='4' readOnly />
</NHSNotifyFormWrapper>
);
expect(container.asFragment()).toMatchSnapshot();
});
describe('csrfServerAction', () => {
test('string action', async () => {
const action = csrfServerAction('/action');
expect(action).toEqual('/action');
});
test('server action with valid csrf check', async () => {
jest.mocked(verifyFormCsrfToken).mockResolvedValueOnce(true);
const mockAction = jest.fn();
const action = csrfServerAction(mockAction);
if (typeof action === 'string') {
throw new TypeError('Expected server action');
}
const mockFormData = mockDeep<FormData>();
await action(mockFormData);
expect(verifyFormCsrfToken).toHaveBeenCalledWith(mockFormData);
expect(mockAction).toHaveBeenCalledWith(mockFormData);
});
test('server action with failed csrf check', async () => {
jest.mocked(verifyFormCsrfToken).mockResolvedValueOnce(false);
const mockAction = jest.fn();
const action = csrfServerAction(mockAction);
if (typeof action === 'string') {
throw new TypeError('Expected server action');
}
const mockFormData = mockDeep<FormData>();
await action(mockFormData);
expect(verifyFormCsrfToken).toHaveBeenCalledWith(mockFormData);
expect(redirect).toHaveBeenCalledWith('/auth/signout');
expect(mockAction).not.toHaveBeenCalled();
});
});