generated from NHSDigital/nhs-notify-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpage.test.tsx
More file actions
164 lines (141 loc) · 4.12 KB
/
page.test.tsx
File metadata and controls
164 lines (141 loc) · 4.12 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* @jest-environment node
*/
import RequestProofPage, {
generateMetadata,
} from '@app/request-proof-of-template/[templateId]/page';
import { RequestProof } from '@forms/RequestProof/RequestProof';
import { redirect } from 'next/navigation';
import { getTemplate } from '@utils/form-actions';
import { TemplateDto } from 'nhs-notify-backend-client';
import {
EMAIL_TEMPLATE,
LETTER_TEMPLATE,
NHS_APP_TEMPLATE,
SMS_TEMPLATE,
} from '../../helpers';
import { serverIsFeatureEnabled } from '@utils/server-features';
import content from '@content/content';
const { pageTitle } = content.components.requestProof;
jest.mock('@utils/form-actions');
jest.mock('next/navigation');
jest.mock('@forms/RequestProof/RequestProof');
jest.mock('@utils/server-features');
const getTemplateMock = jest.mocked(getTemplate);
const redirectMock = jest.mocked(redirect);
const serverIsFeatureEnabledMock = jest.mocked(serverIsFeatureEnabled);
describe('RequestProofPage', () => {
beforeEach(() => {
jest.resetAllMocks();
serverIsFeatureEnabledMock.mockResolvedValueOnce(true);
});
test('should load page', async () => {
const state = {
id: 'template-id',
templateType: 'LETTER',
templateStatus: 'NOT_YET_SUBMITTED',
name: 'template-name',
letterType: 'x0',
language: 'en',
proofingEnabled: true,
files: {
pdfTemplate: {
virusScanStatus: 'PASSED',
currentVersion: 'a',
fileName: 'a.pdf',
},
},
} satisfies Partial<TemplateDto>;
getTemplateMock.mockResolvedValue({
...state,
createdAt: 'today',
updatedAt: 'today',
});
const page = await RequestProofPage({
params: Promise.resolve({
templateId: 'template-id',
}),
});
expect(await generateMetadata()).toEqual({
title: pageTitle,
});
expect(page).toEqual(
<RequestProof
templateName={state.name}
templateId={state.id}
channel='LETTER'
/>
);
});
test('should handle invalid template', async () => {
getTemplateMock.mockResolvedValue(undefined);
await RequestProofPage({
params: Promise.resolve({
templateId: 'template-id',
}),
});
expect(redirectMock).toHaveBeenCalledWith('/invalid-template', 'replace');
});
test.each([
SMS_TEMPLATE,
NHS_APP_TEMPLATE,
EMAIL_TEMPLATE,
{
...LETTER_TEMPLATE,
name: undefined as unknown as string,
},
])(
'should redirect to invalid-template when template is $templateType and name is $name',
async (value) => {
getTemplateMock.mockResolvedValueOnce(value);
await RequestProofPage({
params: Promise.resolve({
templateId: 'template-id',
}),
});
expect(redirectMock).toHaveBeenCalledWith('/invalid-template', 'replace');
}
);
test('should forbid user from requesting a proof when client does not have feature enabled', async () => {
serverIsFeatureEnabledMock.mockReset();
serverIsFeatureEnabledMock.mockResolvedValueOnce(false);
await RequestProofPage({
params: Promise.resolve({
templateId: 'template-id',
}),
});
expect(redirectMock).toHaveBeenCalledWith('/invalid-template', 'replace');
});
test('should forbid user from requesting a proof when proofingEnabled is false', async () => {
const state = {
id: 'template-id',
templateType: 'LETTER',
templateStatus: 'NOT_YET_SUBMITTED',
name: 'template-name',
letterType: 'x0',
language: 'ar',
proofingEnabled: false,
files: {
pdfTemplate: {
virusScanStatus: 'PASSED',
currentVersion: 'a',
fileName: 'a.pdf',
},
},
} satisfies Partial<TemplateDto>;
getTemplateMock.mockResolvedValue({
...state,
createdAt: 'today',
updatedAt: 'today',
});
await RequestProofPage({
params: Promise.resolve({
templateId: 'template-id',
}),
});
expect(await generateMetadata()).toEqual({
title: pageTitle,
});
expect(redirectMock).toHaveBeenCalledWith('/invalid-template', 'replace');
});
});