generated from NHSDigital/nhs-notify-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCopyTemplate.test.tsx
More file actions
107 lines (92 loc) · 2.88 KB
/
CopyTemplate.test.tsx
File metadata and controls
107 lines (92 loc) · 2.88 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use client';
import { useActionState } from 'react';
import { mockDeep } from 'jest-mock-extended';
import { render, screen, fireEvent } from '@testing-library/react';
import { CopyTemplate, ValidCopyType } from '@forms/CopyTemplate/CopyTemplate';
import { TemplateFormState } from 'nhs-notify-web-template-management-utils';
import { ValidatedTemplateDto } from 'nhs-notify-backend-client';
jest.mock('@utils/amplify-utils');
jest.mock('react', () => {
const originalModule = jest.requireActual('react');
return {
...originalModule,
useActionState: jest
.fn()
.mockImplementation(
(
_: (
formState: TemplateFormState,
formData: FormData
) => Promise<TemplateFormState>,
initialState: TemplateFormState
) => [initialState, '/action']
),
};
});
describe('Choose template page', () => {
it('selects one radio button at a time', () => {
const container = render(
<CopyTemplate
template={mockDeep<
ValidatedTemplateDto & { templateType: ValidCopyType }
>()}
/>
);
expect(container.asFragment()).toMatchSnapshot();
const radioButtons = [
screen.getByTestId('EMAIL-radio'),
screen.getByTestId('NHS_APP-radio'),
screen.getByTestId('SMS-radio'),
];
const submitButton = screen.getByTestId('submit-button');
for (const radioButton of radioButtons) {
expect(radioButton).toBeInTheDocument();
expect(radioButton).not.toBeChecked();
}
expect(submitButton).toBeInTheDocument();
for (const [, radioButton] of radioButtons.entries()) {
// select an option
fireEvent(radioButton, new MouseEvent('click'));
expect(radioButton).toBeChecked();
const notCheckedRadioButtons = radioButtons.filter(
(r) => r !== radioButton
);
for (const button of notCheckedRadioButtons)
expect(button).not.toBeChecked();
}
});
it('renders error component', () => {
const mockUseActionState = jest.fn().mockReturnValue([
{
errorState: {
formErrors: [],
fieldErrors: {
page: ['Component error message'],
},
},
},
'/action',
]);
jest.mocked(useActionState).mockImplementation(mockUseActionState);
const container = render(
<CopyTemplate
template={mockDeep<
ValidatedTemplateDto & { templateType: ValidCopyType }
>()}
/>
);
expect(container.asFragment()).toMatchSnapshot();
});
test('Client-side validation triggers', () => {
const container = render(
<CopyTemplate
template={mockDeep<
ValidatedTemplateDto & { templateType: ValidCopyType }
>()}
/>
);
const submitButton = screen.getByTestId('submit-button');
fireEvent.click(submitButton);
expect(container.asFragment()).toMatchSnapshot();
});
});