Skip to content

Commit ada7fbc

Browse files
CCM-5539: Duplicate template link (#225)
1 parent 9bcb32a commit ada7fbc

File tree

29 files changed

+1111
-53
lines changed

29 files changed

+1111
-53
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"react/require-default-props": "off",
7575
"no-useless-constructor": "off",
7676
"sonarjs/no-small-switch": "off",
77+
"sonarjs/no-unused-vars": "off",
7778
"react/jsx-no-bind": "off",
7879
"unicorn/no-null": "off",
7980
"prefer-regex-literals": "off",
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
import CopyTemplatePage from '@app/copy-template/[templateId]/page';
5+
import { CopyTemplate } from '@forms/CopyTemplate/CopyTemplate';
6+
import {
7+
EmailTemplate,
8+
TemplateType,
9+
TemplateStatus,
10+
} from 'nhs-notify-web-template-management-utils';
11+
import { redirect } from 'next/navigation';
12+
import { getTemplate } from '@utils/form-actions';
13+
14+
jest.mock('@utils/form-actions');
15+
jest.mock('next/navigation');
16+
jest.mock('@forms/ChooseTemplate/ChooseTemplate');
17+
18+
const redirectMock = jest.mocked(redirect);
19+
const getTemplateMock = jest.mocked(getTemplate);
20+
21+
describe('CopyTemplatePage', () => {
22+
beforeEach(jest.resetAllMocks);
23+
24+
it('should load page', async () => {
25+
const template: EmailTemplate = {
26+
id: 'template-id',
27+
version: 1,
28+
templateType: TemplateType.EMAIL,
29+
templateStatus: TemplateStatus.NOT_YET_SUBMITTED,
30+
name: 'template-name',
31+
subject: 'template-subject-line',
32+
message: 'template-message',
33+
};
34+
35+
getTemplateMock.mockResolvedValueOnce(template);
36+
37+
const page = await CopyTemplatePage({
38+
params: {
39+
templateId: 'template-id',
40+
},
41+
});
42+
43+
expect(page).toEqual(<CopyTemplate template={template} />);
44+
});
45+
46+
it('should redirect to invalid-template when no templateId is found', async () => {
47+
await CopyTemplatePage({
48+
params: {
49+
templateId: 'template-id',
50+
},
51+
});
52+
53+
expect(redirectMock).toHaveBeenCalledWith('/invalid-template', 'replace');
54+
});
55+
});
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use client';
2+
3+
import { mockDeep } from 'jest-mock-extended';
4+
import { useFormState } from 'react-dom';
5+
import { render, screen, fireEvent } from '@testing-library/react';
6+
import { CopyTemplate } from '@forms/CopyTemplate/CopyTemplate';
7+
import {
8+
Template,
9+
TemplateFormState,
10+
} from 'nhs-notify-web-template-management-utils';
11+
12+
jest.mock('@utils/amplify-utils', () => ({
13+
getAmplifyBackendClient: () => {},
14+
}));
15+
16+
jest.mock('react-dom', () => {
17+
const originalModule = jest.requireActual('react-dom');
18+
19+
return {
20+
...originalModule,
21+
useFormState: jest
22+
.fn()
23+
.mockImplementation(
24+
(
25+
_: (
26+
formState: TemplateFormState,
27+
formData: FormData
28+
) => Promise<TemplateFormState>,
29+
initialState: TemplateFormState
30+
) => [initialState, '/action']
31+
),
32+
};
33+
});
34+
35+
describe('Choose template page', () => {
36+
it('selects one radio button at a time', () => {
37+
const container = render(<CopyTemplate template={mockDeep<Template>()} />);
38+
expect(container.asFragment()).toMatchSnapshot();
39+
40+
const radioButtons = [
41+
screen.getByTestId('EMAIL-radio'),
42+
screen.getByTestId('NHS_APP-radio'),
43+
screen.getByTestId('SMS-radio'),
44+
];
45+
const submitButton = screen.getByTestId('submit-button');
46+
47+
for (const radioButton of radioButtons) {
48+
expect(radioButton).toBeInTheDocument();
49+
expect(radioButton).not.toBeChecked();
50+
}
51+
expect(submitButton).toBeInTheDocument();
52+
53+
for (const [, radioButton] of radioButtons.entries()) {
54+
// select an option
55+
fireEvent(radioButton, new MouseEvent('click'));
56+
57+
expect(radioButton).toBeChecked();
58+
59+
const notCheckedRadioButtons = radioButtons.filter(
60+
(r) => r !== radioButton
61+
);
62+
63+
for (const button of notCheckedRadioButtons)
64+
expect(button).not.toBeChecked();
65+
}
66+
});
67+
68+
it('renders error component', () => {
69+
const mockUseFormState = jest.fn().mockReturnValue([
70+
{
71+
validationError: {
72+
formErrors: [],
73+
fieldErrors: {
74+
page: ['Component error message'],
75+
},
76+
},
77+
},
78+
'/action',
79+
]);
80+
81+
jest.mocked(useFormState).mockImplementation(mockUseFormState);
82+
83+
const container = render(<CopyTemplate template={mockDeep<Template>()} />);
84+
expect(container.asFragment()).toMatchSnapshot();
85+
});
86+
});

0 commit comments

Comments
 (0)