-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcheckboxes.test.js
More file actions
112 lines (93 loc) · 2.77 KB
/
checkboxes.test.js
File metadata and controls
112 lines (93 loc) · 2.77 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
import { resolve } from 'node:path'
import { StatusCodes } from 'http-status-codes'
import { createServer } from '~/src/server/index.js'
import { getFormMetadata } from '~/src/server/plugins/engine/services/formsService.js'
import * as fixtures from '~/test/fixtures/index.js'
import { renderResponse } from '~/test/helpers/component-helpers.js'
const basePath = '/checkboxes'
const key = 'wqJmSf'
jest.mock('~/src/server/plugins/engine/services/formsService.js')
describe('Checkboxes based conditions', () => {
/** @type {Server} */
let server
// Create server before each test
beforeAll(async () => {
server = await createServer({
formFileName: 'checkboxes.json',
formFilePath: resolve(import.meta.dirname, '../form/definitions')
})
await server.initialize()
})
beforeEach(() => {
jest.mocked(getFormMetadata).mockResolvedValue(fixtures.form.metadata)
})
afterAll(async () => {
await server.stop()
})
test('Checkboxes are rendered', async () => {
const { container } = await renderResponse(server, {
url: `${basePath}/first-page`
})
for (const example of [
{
name: key,
id: key,
text: 'Shire',
value: 'shire'
},
{
name: key,
id: `${key}-2`,
text: 'Race',
value: 'race'
},
{
name: key,
id: `${key}-3`,
text: 'Pantomime',
value: 'pantomime'
},
{
name: key,
id: `${key}-4`,
text: 'Other',
value: 'other'
}
]) {
const $checkbox = container.getByRole('checkbox', {
name: example.text
})
expect($checkbox).toBeInTheDocument()
expect($checkbox).toHaveAttribute('id', expect.any(String)) // id is now a uuid
expect($checkbox).toHaveAttribute('name', example.name)
expect($checkbox).toHaveAttribute('value', example.value)
expect($checkbox).toHaveClass('govuk-checkboxes__input')
expect($checkbox).not.toBeChecked()
}
})
test('Testing POST /first-page with nothing checked redirects correctly', async () => {
const form = {}
const res = await server.inject({
url: `${basePath}/first-page`,
method: 'POST',
payload: form
})
expect(res.statusCode).toBe(StatusCodes.SEE_OTHER)
expect(res.headers.location).toBe(`${basePath}/second-page`)
})
test('Testing POST /first-page with "other" checked redirects correctly', async () => {
const form = {
[key]: 'other'
}
const res = await server.inject({
url: `${basePath}/first-page`,
method: 'POST',
payload: form
})
expect(res.statusCode).toBe(StatusCodes.SEE_OTHER)
expect(res.headers.location).toBe(`${basePath}/third-page`)
})
})
/**
* @import { Server } from '@hapi/hapi'
*/