-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSummaryPageWithConfirmationEmailController.test.ts
More file actions
227 lines (204 loc) · 7.27 KB
/
SummaryPageWithConfirmationEmailController.test.ts
File metadata and controls
227 lines (204 loc) · 7.27 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { type CacheService } from '@defra/forms-engine-plugin/cache-service.js'
import { type QuestionPageController } from '@defra/forms-engine-plugin/controllers/QuestionPageController.js'
import { FormModel } from '@defra/forms-engine-plugin/engine/models/FormModel.js'
import { buildFormRequest } from '@defra/forms-engine-plugin/engine/pageControllers/__stubs__/request.js'
import { type FormSubmissionState } from '@defra/forms-engine-plugin/engine/types.js'
import {
type FormRequest,
type FormRequestPayload,
type FormResponseToolkit
} from '@defra/forms-engine-plugin/types'
import {
ControllerType,
type PageSummaryWithConfirmationEmail
} from '@defra/forms-model'
import {
SummaryPageWithConfirmationEmailController,
getUserConfirmationEmailAddress
} from '~/src/server/plugins/SummaryPageWithConfirmationEmailController.js'
import definition from '~/test/form/definitions/basic-with-payment.js'
describe('SummaryPageWithConfirmationEmailController', () => {
let model: FormModel
let controller: SummaryPageWithConfirmationEmailController
let requestPage: FormRequest
const response = {
code: jest.fn().mockImplementation(() => response)
}
const h = {
redirect: jest.fn().mockReturnValue(response),
view: jest.fn(),
continue: Symbol('continue')
} as unknown as FormResponseToolkit
beforeEach(() => {
model = new FormModel(definition, {
basePath: 'test'
})
// Create a mock page for SummaryPageWithConfirmationEmailController
const mockPage = {
...definition.pages[0],
controller: ControllerType.SummaryWithConfirmationEmail
} as unknown as PageSummaryWithConfirmationEmail
controller = new SummaryPageWithConfirmationEmailController(model, mockPage)
requestPage = buildFormRequest({
method: 'get',
url: new URL('http://example.com/test/summary'),
path: '/test/summary',
params: {
path: 'summary',
slug: 'test'
},
query: {},
app: { model }
} as FormRequest)
})
describe('handle errors', () => {
it('should display errors including summary', async () => {
const state: FormSubmissionState = {
$$__referenceNumber: 'foobar',
licenceLength: 365,
fullName: 'John Smith'
}
const request = {
...requestPage,
method: 'post',
payload: { invalid: '123', action: 'send' }
} as unknown as FormRequestPayload
const context = model.getFormContext(request, state)
jest
.spyOn(controller as unknown as QuestionPageController, 'getState')
.mockResolvedValue({})
jest
.spyOn(controller as unknown as QuestionPageController, 'setState')
.mockResolvedValue(state)
const postHandler = controller.makePostRouteHandler()
await postHandler(request, context, h)
const viewModel = controller.getSummaryViewModel(request, context)
expect(h.view).toHaveBeenCalledWith(
'summary-with-confirmation',
expect.anything()
)
expect(viewModel.errors).toHaveLength(1)
const errorText = viewModel.errors ? viewModel.errors[0].text : ''
expect(errorText).toBe('"invalid" is not allowed')
})
})
describe('handleSaveAndExit', () => {
it('should invoke saveAndExit plugin option', async () => {
const saveAndExitMock = jest.fn(() => ({}))
const state: FormSubmissionState = {
$$__referenceNumber: 'foobar',
licenceLength: 365,
fullName: 'John Smith'
}
const request = {
...requestPage,
server: {
plugins: {
'forms-engine-plugin': {
saveAndExit: saveAndExitMock,
cacheService: {
clearState: jest.fn()
} as unknown as CacheService
}
}
},
method: 'post',
payload: { fullName: 'John Smith', action: 'save-and-exit' }
} as unknown as FormRequestPayload
const context = model.getFormContext(request, state)
const postHandler = controller.makePostRouteHandler()
await postHandler(request, context, h)
expect(saveAndExitMock).toHaveBeenCalledWith(request, h, context)
})
})
describe('getUserConfirmationEmailAddress', () => {
test('should get confirmation email', () => {
const field = getUserConfirmationEmailAddress()
expect(field.name).toBe('userConfirmationEmailAddress')
expect(field.value).toBeUndefined()
})
test('should get confirmation email with retained value', () => {
const field = getUserConfirmationEmailAddress({
userConfirmationEmailAddress: 'emailval'
})
expect(field.name).toBe('userConfirmationEmailAddress')
expect(field.value).toBe('emailval')
})
})
describe('getSummaryViewModel', () => {
test('should prepopulate user confirmation email', () => {
const state: FormSubmissionState = {
$$__referenceNumber: 'foobar',
licenceLength: 365,
fullName: 'John Smith',
paymentField: {
payerEmail: 'payer-email@test.com',
paymentId: 'payment-id',
reference: 'payment-ref',
amount: 150,
description: 'payment description',
uuid: 'ee501106-4ce1-4947-91a7-7cc1a335ccd8',
formId: 'form-id',
isLivePayment: false
} as unknown as string // Force the type to satisy the linting
}
const request = {
...requestPage,
server: {
plugins: {
'forms-engine-plugin': {
cacheService: {
clearState: jest.fn()
} as unknown as CacheService
}
}
},
method: 'post',
payload: { fullName: 'John Smith', action: 'send' }
} as unknown as FormRequestPayload
const context = model.getFormContext(request, state)
const viewModel = controller.getSummaryViewModel(request, context)
// @ts-expect-error - dynamic field name
expect(viewModel.userConfirmationEmailField.value).toBe(
'payer-email@test.com'
)
})
test('should prepopulate user confirmation email even if empty payload', () => {
const state: FormSubmissionState = {
$$__referenceNumber: 'foobar',
licenceLength: 365,
fullName: 'John Smith',
paymentField: {
payerEmail: 'payer-email@test.com',
paymentId: 'payment-id',
reference: 'payment-ref',
amount: 150,
description: 'payment description',
uuid: 'ee501106-4ce1-4947-91a7-7cc1a335ccd8',
formId: 'form-id',
isLivePayment: false
} as unknown as string // Force the type to satisy the linting
}
const request = {
...requestPage,
server: {
plugins: {
'forms-engine-plugin': {
cacheService: {
clearState: jest.fn()
} as unknown as CacheService
}
}
},
method: 'post',
payload: undefined
} as unknown as FormRequestPayload
const context = model.getFormContext(request, state)
const viewModel = controller.getSummaryViewModel(request, context)
// @ts-expect-error - dynamic field name
expect(viewModel.userConfirmationEmailField.value).toBe(
'payer-email@test.com'
)
})
})
})