@defra/forms-engine-plugin 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.public/assets/fonts/bold-affa96571d-v2.woff +0 -0
- package/.public/assets/fonts/bold-b542beb274-v2.woff2 +0 -0
- package/.public/assets/fonts/light-94a07e06a1-v2.woff2 +0 -0
- package/.public/assets/fonts/light-f591b13f7d-v2.woff +0 -0
- package/.public/assets/images/favicon.ico +0 -0
- package/.public/assets/images/favicon.svg +1 -0
- package/.public/assets/images/govuk-crest.svg +1 -0
- package/.public/assets/images/govuk-icon-180.png +0 -0
- package/.public/assets/images/govuk-icon-192.png +0 -0
- package/.public/assets/images/govuk-icon-512.png +0 -0
- package/.public/assets/images/govuk-icon-mask.svg +1 -0
- package/.public/assets/images/govuk-opengraph-image.png +0 -0
- package/.public/assets/manifest.json +39 -0
- package/.public/assets-manifest.json +24 -0
- package/.public/javascripts/application.0fd8c18.min.js +3 -0
- package/.public/javascripts/application.0fd8c18.min.js.LICENSE.txt +58 -0
- package/.public/javascripts/application.0fd8c18.min.js.map +1 -0
- package/.public/javascripts/file-upload.b2f18f5.min.js +2 -0
- package/.public/javascripts/file-upload.b2f18f5.min.js.map +1 -0
- package/.public/javascripts/vendor/accessible-autocomplete.275d332.min.js +2 -0
- package/.public/javascripts/vendor/accessible-autocomplete.275d332.min.js.map +1 -0
- package/.public/stylesheets/application.e340021.min.css +14 -0
- package/.public/stylesheets/application.e340021.min.css.map +1 -0
- package/package.json +7 -2
- package/.browserslistrc +0 -16
- package/.editorconfig +0 -9
- package/.eslintrc.cjs +0 -266
- package/.github/dependabot.yml +0 -85
- package/.github/workflows/check-pull-request.yml +0 -209
- package/.github/workflows/pr-notifier.yml +0 -98
- package/.github/workflows/publish.yml +0 -111
- package/.husky/pre-commit +0 -1
- package/.lintstagedrc.js +0 -4
- package/.nvmrc +0 -1
- package/.prettierignore +0 -8
- package/.prettierrc.cjs +0 -26
- package/Dockerfile +0 -61
- package/Procfile +0 -1
- package/babel.config.cjs +0 -55
- package/compose/aws.env +0 -4
- package/compose/start-localstack.sh +0 -26
- package/docker-compose.yaml +0 -86
- package/globals.d.ts +0 -1
- package/jest.config.cjs +0 -54
- package/jest.environment.js +0 -4
- package/jest.setup.cjs +0 -14
- package/postcss.config.js +0 -26
- package/sonar-project.properties +0 -17
- package/stylelint.config.js +0 -10
- package/test/client/javascripts/file-upload.test.js +0 -1197
- package/test/condition/checkboxes.test.js +0 -112
- package/test/condition/radios.test.js +0 -112
- package/test/condition/text.test.js +0 -103
- package/test/fixtures/assets-manifest.json +0 -4
- package/test/fixtures/form.js +0 -86
- package/test/fixtures/index.js +0 -2
- package/test/fixtures/list.js +0 -92
- package/test/form/cookies.test.js +0 -338
- package/test/form/csrf.test.js +0 -87
- package/test/form/definitions/basic.js +0 -101
- package/test/form/definitions/blank.js +0 -10
- package/test/form/definitions/checkboxes.json +0 -88
- package/test/form/definitions/components.json +0 -452
- package/test/form/definitions/conditional-reveal.js +0 -140
- package/test/form/definitions/conditions-basic.js +0 -187
- package/test/form/definitions/conditions-complex.js +0 -338
- package/test/form/definitions/conditions-dates.js +0 -78
- package/test/form/definitions/conditions-escaping.js +0 -143
- package/test/form/definitions/demo-cph-number.js +0 -3099
- package/test/form/definitions/feedback.json +0 -45
- package/test/form/definitions/fields-optional.js +0 -402
- package/test/form/definitions/fields-required.js +0 -402
- package/test/form/definitions/file-upload-basic.js +0 -44
- package/test/form/definitions/file-upload.js +0 -66
- package/test/form/definitions/minimal.js +0 -39
- package/test/form/definitions/phase-alpha.json +0 -33
- package/test/form/definitions/phase-default.json +0 -26
- package/test/form/definitions/radios.json +0 -88
- package/test/form/definitions/repeat-mixed.js +0 -54
- package/test/form/definitions/repeat.js +0 -70
- package/test/form/definitions/status.json +0 -126
- package/test/form/definitions/templates.js +0 -183
- package/test/form/definitions/test.json +0 -581
- package/test/form/definitions/text.json +0 -75
- package/test/form/definitions/titles.json +0 -170
- package/test/form/definitions.test.js +0 -47
- package/test/form/exit-page.test.js +0 -210
- package/test/form/feedback.test.js +0 -68
- package/test/form/fields-optional.test.js +0 -237
- package/test/form/fields-required.test.js +0 -294
- package/test/form/file-upload.test.js +0 -313
- package/test/form/govuk-notify.test.js +0 -449
- package/test/form/journey-basic.test.js +0 -444
- package/test/form/persist-files.test.js +0 -227
- package/test/form/phase-banner.test.js +0 -71
- package/test/form/repeat.test.js +0 -628
- package/test/form/summary-submission-email.test.js +0 -95
- package/test/form/template.test.js +0 -288
- package/test/form/titles.test.js +0 -204
- package/test/helpers/component-helpers.js +0 -74
- package/test/utils/get-cookie.js +0 -42
- package/test/utils/get-form-definitions.js +0 -18
- package/tmp.pdf +0 -1
- package/tsconfig.json +0 -28
- package/webpack.config.js +0 -208
|
@@ -1,1197 +0,0 @@
|
|
|
1
|
-
import { initFileUpload } from '~/src/client/javascripts/file-upload.js'
|
|
2
|
-
|
|
3
|
-
describe('File Upload Client JS', () => {
|
|
4
|
-
beforeEach(() => {
|
|
5
|
-
document.body.innerHTML = `
|
|
6
|
-
<div class="govuk-error-summary-container"></div>
|
|
7
|
-
<form>
|
|
8
|
-
<input type="file" id="file-upload">
|
|
9
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
10
|
-
</form>
|
|
11
|
-
<form>
|
|
12
|
-
<div id="uploadedFilesContainer">
|
|
13
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
14
|
-
<p class="govuk-body">0 files uploaded</p>
|
|
15
|
-
</div>
|
|
16
|
-
<button class="govuk-button">Continue</button>
|
|
17
|
-
</form>
|
|
18
|
-
`
|
|
19
|
-
|
|
20
|
-
jest.useFakeTimers()
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
afterEach(() => {
|
|
24
|
-
document.body.innerHTML = ''
|
|
25
|
-
jest.restoreAllMocks()
|
|
26
|
-
jest.useRealTimers()
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
test('initFileUpload initializes without errors when DOM elements are present', () => {
|
|
30
|
-
expect(() => initFileUpload()).not.toThrow()
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
test('initFileUpload does nothing when required DOM elements are missing', () => {
|
|
34
|
-
document.body.innerHTML = '<div></div>'
|
|
35
|
-
expect(() => initFileUpload()).not.toThrow()
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
function setupTestableComponent(runInit = true) {
|
|
39
|
-
const fileInput = document.querySelector('input[type="file"]')
|
|
40
|
-
const uploadButton = document.querySelector('.upload-file-button')
|
|
41
|
-
|
|
42
|
-
/** @type {(() => void) | null} */
|
|
43
|
-
let changeHandler = null
|
|
44
|
-
/** @type {((event: any) => void) | null} */
|
|
45
|
-
let clickHandler = null
|
|
46
|
-
|
|
47
|
-
if (fileInput) {
|
|
48
|
-
const originalFileInputAddEventListener =
|
|
49
|
-
fileInput.addEventListener.bind(fileInput)
|
|
50
|
-
fileInput.addEventListener = jest.fn(
|
|
51
|
-
/**
|
|
52
|
-
* @param {string} [type]
|
|
53
|
-
* @param {EventListenerOrEventListenerObject} [handler]
|
|
54
|
-
*/
|
|
55
|
-
(type, handler) => {
|
|
56
|
-
if (type === 'change') {
|
|
57
|
-
changeHandler = /** @type {() => void} */ (handler)
|
|
58
|
-
}
|
|
59
|
-
if (type && handler) {
|
|
60
|
-
originalFileInputAddEventListener(type, handler)
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (uploadButton) {
|
|
67
|
-
const originalButtonAddEventListener =
|
|
68
|
-
uploadButton.addEventListener.bind(uploadButton)
|
|
69
|
-
uploadButton.addEventListener = jest.fn(
|
|
70
|
-
/**
|
|
71
|
-
* @param {string} [type]
|
|
72
|
-
* @param {EventListenerOrEventListenerObject} [handler]
|
|
73
|
-
*/
|
|
74
|
-
(type, handler) => {
|
|
75
|
-
if (type === 'click') {
|
|
76
|
-
clickHandler = /** @type {(event: any) => void} */ (handler)
|
|
77
|
-
}
|
|
78
|
-
if (type && handler) {
|
|
79
|
-
originalButtonAddEventListener(type, handler)
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
)
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (runInit) {
|
|
86
|
-
initFileUpload()
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return {
|
|
90
|
-
fileInput,
|
|
91
|
-
uploadButton,
|
|
92
|
-
triggerChange: () => {
|
|
93
|
-
if (changeHandler) changeHandler()
|
|
94
|
-
},
|
|
95
|
-
triggerClick: (event = {}) => {
|
|
96
|
-
if (clickHandler) clickHandler(event)
|
|
97
|
-
},
|
|
98
|
-
loadFile: (filename = 'some-file.pdf') => {
|
|
99
|
-
if (!fileInput) return
|
|
100
|
-
const file = new File(['some file'], filename, {
|
|
101
|
-
type: 'application/pdf'
|
|
102
|
-
})
|
|
103
|
-
Object.defineProperty(fileInput, 'files', {
|
|
104
|
-
value: [file],
|
|
105
|
-
writable: true
|
|
106
|
-
})
|
|
107
|
-
},
|
|
108
|
-
runInit: () => {
|
|
109
|
-
initFileUpload()
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
test('shows error when upload button is clicked without selecting a file', () => {
|
|
115
|
-
const event = { preventDefault: jest.fn() }
|
|
116
|
-
const { triggerClick, fileInput } = setupTestableComponent()
|
|
117
|
-
|
|
118
|
-
triggerClick(event)
|
|
119
|
-
|
|
120
|
-
expect(event.preventDefault).toHaveBeenCalled()
|
|
121
|
-
|
|
122
|
-
const errorSummary = document.querySelector('.govuk-error-summary')
|
|
123
|
-
expect(errorSummary).not.toBeNull()
|
|
124
|
-
expect(errorSummary?.textContent).toContain('Select a file')
|
|
125
|
-
|
|
126
|
-
const errorSummaryTitle = document.getElementById('error-summary-title')
|
|
127
|
-
expect(errorSummaryTitle).not.toBeNull()
|
|
128
|
-
expect(fileInput?.getAttribute('aria-describedby')).toBe(
|
|
129
|
-
'error-summary-title'
|
|
130
|
-
)
|
|
131
|
-
})
|
|
132
|
-
|
|
133
|
-
test('clears error when file is selected', () => {
|
|
134
|
-
const errorContainer = document.querySelector(
|
|
135
|
-
'.govuk-error-summary-container'
|
|
136
|
-
)
|
|
137
|
-
if (!errorContainer) return
|
|
138
|
-
|
|
139
|
-
errorContainer.innerHTML = `
|
|
140
|
-
<div class="govuk-error-summary" data-module="govuk-error-summary">
|
|
141
|
-
<div role="alert">
|
|
142
|
-
<h2 class="govuk-error-summary__title">There is a problem</h2>
|
|
143
|
-
<div class="govuk-error-summary__body">
|
|
144
|
-
<ul class="govuk-list govuk-error-summary__list">
|
|
145
|
-
<li><a href="#file-upload">Select a file</a></li>
|
|
146
|
-
</ul>
|
|
147
|
-
</div>
|
|
148
|
-
</div>
|
|
149
|
-
</div>
|
|
150
|
-
`
|
|
151
|
-
|
|
152
|
-
expect(document.querySelector('.govuk-error-summary')).not.toBeNull()
|
|
153
|
-
|
|
154
|
-
const { loadFile, triggerChange } = setupTestableComponent()
|
|
155
|
-
|
|
156
|
-
loadFile()
|
|
157
|
-
triggerChange()
|
|
158
|
-
|
|
159
|
-
expect(errorContainer.innerHTML).toBe('')
|
|
160
|
-
})
|
|
161
|
-
|
|
162
|
-
test('renderSummary creates new summary list when it does not exist', () => {
|
|
163
|
-
document.body.innerHTML = `
|
|
164
|
-
<div class="govuk-error-summary-container"></div>
|
|
165
|
-
<form>
|
|
166
|
-
<input type="file" id="file-upload">
|
|
167
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
168
|
-
</form>
|
|
169
|
-
<form>
|
|
170
|
-
<div id="uploadedFilesContainer">
|
|
171
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
172
|
-
<p class="govuk-body">0 files uploaded</p>
|
|
173
|
-
</div>
|
|
174
|
-
<button class="govuk-button">Continue</button>
|
|
175
|
-
</form>
|
|
176
|
-
`
|
|
177
|
-
|
|
178
|
-
const { loadFile, triggerChange, triggerClick } = setupTestableComponent()
|
|
179
|
-
|
|
180
|
-
loadFile('some-file.pdf')
|
|
181
|
-
triggerChange()
|
|
182
|
-
triggerClick({})
|
|
183
|
-
|
|
184
|
-
const summaryList = document.querySelector('dl.govuk-summary-list')
|
|
185
|
-
expect(summaryList).not.toBeNull()
|
|
186
|
-
expect(summaryList?.className).toContain('govuk-summary-list--long-key')
|
|
187
|
-
expect(summaryList?.parentElement?.tagName).toBe('FORM')
|
|
188
|
-
})
|
|
189
|
-
|
|
190
|
-
test('renderSummary uses existing summary list when it already exists', () => {
|
|
191
|
-
document.body.innerHTML = `
|
|
192
|
-
<div class="govuk-error-summary-container"></div>
|
|
193
|
-
<form>
|
|
194
|
-
<input type="file" id="file-upload">
|
|
195
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
196
|
-
</form>
|
|
197
|
-
<form>
|
|
198
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
199
|
-
<p class="govuk-body">0 files uploaded</p>
|
|
200
|
-
<dl class="govuk-summary-list govuk-summary-list--long-key"></dl>
|
|
201
|
-
<button class="govuk-button">Continue</button>
|
|
202
|
-
</form>
|
|
203
|
-
`
|
|
204
|
-
|
|
205
|
-
const { loadFile, triggerChange, triggerClick } = setupTestableComponent()
|
|
206
|
-
const originalSummaryList = document.querySelector('dl.govuk-summary-list')
|
|
207
|
-
|
|
208
|
-
loadFile('some-file.pdf')
|
|
209
|
-
triggerChange()
|
|
210
|
-
triggerClick({})
|
|
211
|
-
|
|
212
|
-
const currentSummaryList = document.querySelector('dl.govuk-summary-list')
|
|
213
|
-
expect(currentSummaryList).toBe(originalSummaryList)
|
|
214
|
-
expect(document.querySelectorAll('dl.govuk-summary-list')).toHaveLength(1)
|
|
215
|
-
})
|
|
216
|
-
|
|
217
|
-
test('renderSummary handles existing rows with same filename', () => {
|
|
218
|
-
document.body.innerHTML = `
|
|
219
|
-
<div class="govuk-error-summary-container"></div>
|
|
220
|
-
<form>
|
|
221
|
-
<input type="file" id="file-upload">
|
|
222
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
223
|
-
</form>
|
|
224
|
-
<form>
|
|
225
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
226
|
-
<p class="govuk-body">0 files uploaded</p>
|
|
227
|
-
<dl class="govuk-summary-list govuk-summary-list--long-key">
|
|
228
|
-
<div class="govuk-summary-list__row" data-filename="test.pdf">
|
|
229
|
-
<dt class="govuk-summary-list__key">test.pdf</dt>
|
|
230
|
-
<dd class="govuk-summary-list__value">
|
|
231
|
-
<strong class="govuk-tag govuk-tag--yellow">Uploading…</strong>
|
|
232
|
-
</dd>
|
|
233
|
-
<dd class="govuk-summary-list__actions"></dd>
|
|
234
|
-
</div>
|
|
235
|
-
</dl>
|
|
236
|
-
<button class="govuk-button">Continue</button>
|
|
237
|
-
</form>
|
|
238
|
-
`
|
|
239
|
-
|
|
240
|
-
const { loadFile, triggerChange, triggerClick } = setupTestableComponent()
|
|
241
|
-
|
|
242
|
-
loadFile('some-file.pdf')
|
|
243
|
-
triggerChange()
|
|
244
|
-
triggerClick({})
|
|
245
|
-
|
|
246
|
-
expect(
|
|
247
|
-
document.querySelectorAll('[data-filename="test.pdf"]')
|
|
248
|
-
).toHaveLength(1)
|
|
249
|
-
expect(
|
|
250
|
-
document.querySelector('[data-filename="test.pdf"]')?.textContent
|
|
251
|
-
).toContain('Uploading…')
|
|
252
|
-
})
|
|
253
|
-
|
|
254
|
-
test('renderSummary does nothing when selectedFile is null', () => {
|
|
255
|
-
document.body.innerHTML = `
|
|
256
|
-
<div class="govuk-error-summary-container"></div>
|
|
257
|
-
<form>
|
|
258
|
-
<input type="file" id="file-upload">
|
|
259
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
260
|
-
</form>
|
|
261
|
-
<form>
|
|
262
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
263
|
-
<p class="govuk-body">0 files uploaded</p>
|
|
264
|
-
<button class="govuk-button">Continue</button>
|
|
265
|
-
</form>
|
|
266
|
-
`
|
|
267
|
-
|
|
268
|
-
const { triggerClick } = setupTestableComponent()
|
|
269
|
-
|
|
270
|
-
triggerClick({ preventDefault: jest.fn() })
|
|
271
|
-
|
|
272
|
-
const summaryList = document.querySelector('dl.govuk-summary-list')
|
|
273
|
-
expect(summaryList).toBeNull()
|
|
274
|
-
})
|
|
275
|
-
|
|
276
|
-
test('renderSummary does nothing when second form is missing', () => {
|
|
277
|
-
document.body.innerHTML = `
|
|
278
|
-
<div class="govuk-error-summary-container"></div>
|
|
279
|
-
<form>
|
|
280
|
-
<input type="file" id="file-upload">
|
|
281
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
282
|
-
</form>
|
|
283
|
-
`
|
|
284
|
-
|
|
285
|
-
const { loadFile, triggerChange, triggerClick } = setupTestableComponent()
|
|
286
|
-
|
|
287
|
-
loadFile('some-file.pdf')
|
|
288
|
-
triggerChange()
|
|
289
|
-
triggerClick({})
|
|
290
|
-
|
|
291
|
-
const summaryList = document.querySelector('dl.govuk-summary-list')
|
|
292
|
-
expect(summaryList).toBeNull()
|
|
293
|
-
})
|
|
294
|
-
|
|
295
|
-
test('renderSummary does nothing when file count paragraph is missing', () => {
|
|
296
|
-
document.body.innerHTML = `
|
|
297
|
-
<div class="govuk-error-summary-container"></div>
|
|
298
|
-
<form>
|
|
299
|
-
<input type="file" id="file-upload">
|
|
300
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
301
|
-
</form>
|
|
302
|
-
<form>
|
|
303
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
304
|
-
<button class="govuk-button">Continue</button>
|
|
305
|
-
</form>
|
|
306
|
-
`
|
|
307
|
-
|
|
308
|
-
const { loadFile, triggerChange, triggerClick } = setupTestableComponent()
|
|
309
|
-
|
|
310
|
-
loadFile('some-file.pdf')
|
|
311
|
-
triggerChange()
|
|
312
|
-
triggerClick({})
|
|
313
|
-
|
|
314
|
-
const summaryList = document.querySelector('dl.govuk-summary-list')
|
|
315
|
-
expect(summaryList).toBeNull()
|
|
316
|
-
})
|
|
317
|
-
|
|
318
|
-
test('status announcer is created or attached properly', () => {
|
|
319
|
-
document.body.innerHTML = `
|
|
320
|
-
<div class="govuk-error-summary-container"></div>
|
|
321
|
-
<form>
|
|
322
|
-
<input type="file" id="file-upload">
|
|
323
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
324
|
-
</form>
|
|
325
|
-
<form>
|
|
326
|
-
<div id="uploadedFilesContainer">
|
|
327
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
328
|
-
<p class="govuk-body">0 files uploaded</p>
|
|
329
|
-
</div>
|
|
330
|
-
<button class="govuk-button">Continue</button>
|
|
331
|
-
</form>
|
|
332
|
-
`
|
|
333
|
-
|
|
334
|
-
const { loadFile, triggerChange, triggerClick } = setupTestableComponent()
|
|
335
|
-
|
|
336
|
-
loadFile('some-file.pdf')
|
|
337
|
-
triggerChange()
|
|
338
|
-
triggerClick({})
|
|
339
|
-
|
|
340
|
-
const statusAnnouncer = document.getElementById('statusInformation')
|
|
341
|
-
expect(statusAnnouncer).not.toBeNull()
|
|
342
|
-
expect(document.contains(statusAnnouncer)).toBe(true)
|
|
343
|
-
})
|
|
344
|
-
|
|
345
|
-
test('disables form controls after submission', () => {
|
|
346
|
-
const { fileInput, loadFile, triggerChange, triggerClick } =
|
|
347
|
-
setupTestableComponent()
|
|
348
|
-
|
|
349
|
-
/** @type {HTMLInputElement | null} */
|
|
350
|
-
const input = /** @type {HTMLInputElement} */ (fileInput)
|
|
351
|
-
expect(input.disabled).toBeFalsy()
|
|
352
|
-
/** @type {HTMLButtonElement | null} */
|
|
353
|
-
const button = document.querySelector('.upload-file-button')
|
|
354
|
-
expect(button?.disabled).toBeFalsy()
|
|
355
|
-
/** @type {HTMLButtonElement | null} */
|
|
356
|
-
const continueButton = document.querySelector(
|
|
357
|
-
'button.govuk-button:not(.govuk-button--secondary)'
|
|
358
|
-
)
|
|
359
|
-
expect(continueButton?.disabled).toBeFalsy()
|
|
360
|
-
|
|
361
|
-
loadFile()
|
|
362
|
-
triggerChange()
|
|
363
|
-
triggerClick({})
|
|
364
|
-
|
|
365
|
-
jest.advanceTimersByTime(150)
|
|
366
|
-
|
|
367
|
-
expect(input.disabled).toBeTruthy()
|
|
368
|
-
expect(button?.disabled).toBeTruthy()
|
|
369
|
-
expect(continueButton?.disabled).toBeTruthy()
|
|
370
|
-
})
|
|
371
|
-
|
|
372
|
-
test('sets focus on file input after upload begins', () => {
|
|
373
|
-
document.body.innerHTML = `
|
|
374
|
-
<div class="govuk-error-summary-container"></div>
|
|
375
|
-
<form>
|
|
376
|
-
<input type="file" id="file-upload">
|
|
377
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
378
|
-
</form>
|
|
379
|
-
`
|
|
380
|
-
|
|
381
|
-
const fileInput = /** @type {HTMLInputElement} */ (
|
|
382
|
-
document.querySelector('input[type="file"]')
|
|
383
|
-
)
|
|
384
|
-
|
|
385
|
-
const focusSpy = jest.spyOn(fileInput, 'focus')
|
|
386
|
-
|
|
387
|
-
const { loadFile, triggerChange, triggerClick } =
|
|
388
|
-
setupTestableComponent(true)
|
|
389
|
-
|
|
390
|
-
loadFile('test.pdf')
|
|
391
|
-
triggerChange()
|
|
392
|
-
triggerClick({})
|
|
393
|
-
|
|
394
|
-
expect(focusSpy).toHaveBeenCalled()
|
|
395
|
-
})
|
|
396
|
-
|
|
397
|
-
test('prevents multiple submissions', () => {
|
|
398
|
-
const { loadFile, triggerChange, triggerClick } = setupTestableComponent()
|
|
399
|
-
|
|
400
|
-
loadFile('some-file.pdf')
|
|
401
|
-
triggerChange()
|
|
402
|
-
|
|
403
|
-
if (document.querySelector('.govuk-error-summary-container')) {
|
|
404
|
-
const container = /** @type {HTMLElement} */ (
|
|
405
|
-
document.querySelector('.govuk-error-summary-container')
|
|
406
|
-
)
|
|
407
|
-
container.innerHTML = ''
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
const event1 = { preventDefault: jest.fn() }
|
|
411
|
-
triggerClick(event1)
|
|
412
|
-
expect(event1.preventDefault).not.toHaveBeenCalled()
|
|
413
|
-
|
|
414
|
-
const event2 = { preventDefault: jest.fn() }
|
|
415
|
-
triggerClick(event2)
|
|
416
|
-
expect(event2.preventDefault).toHaveBeenCalled()
|
|
417
|
-
})
|
|
418
|
-
|
|
419
|
-
test('renderSummary handles the case where next element is not a form', () => {
|
|
420
|
-
document.body.innerHTML = `
|
|
421
|
-
<div class="govuk-error-summary-container"></div>
|
|
422
|
-
<form>
|
|
423
|
-
<input type="file" id="file-upload">
|
|
424
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
425
|
-
</form>
|
|
426
|
-
<div>Not a form element</div>
|
|
427
|
-
`
|
|
428
|
-
|
|
429
|
-
const { loadFile, triggerChange, triggerClick } = setupTestableComponent()
|
|
430
|
-
|
|
431
|
-
loadFile('some-file.pdf')
|
|
432
|
-
triggerChange()
|
|
433
|
-
triggerClick({})
|
|
434
|
-
|
|
435
|
-
expect(document.querySelector('dl.govuk-summary-list')).toBeNull()
|
|
436
|
-
})
|
|
437
|
-
|
|
438
|
-
test('renderSummary inserts summaryList before continue button when no fileCountP.nextSibling exists', () => {
|
|
439
|
-
document.body.innerHTML = `
|
|
440
|
-
<div class="govuk-error-summary-container"></div>
|
|
441
|
-
<form>
|
|
442
|
-
<input type="file" id="file-upload">
|
|
443
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
444
|
-
</form>
|
|
445
|
-
<form>
|
|
446
|
-
<div id="uploadedFilesContainer">
|
|
447
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
448
|
-
<p class="govuk-body">0 files uploaded</p>
|
|
449
|
-
<!-- No next sibling after p.govuk-body -->
|
|
450
|
-
</div>
|
|
451
|
-
<button class="govuk-button">Continue</button>
|
|
452
|
-
</form>
|
|
453
|
-
`
|
|
454
|
-
|
|
455
|
-
const { loadFile, triggerChange, triggerClick } = setupTestableComponent()
|
|
456
|
-
|
|
457
|
-
loadFile('some-file.pdf')
|
|
458
|
-
triggerChange()
|
|
459
|
-
triggerClick({})
|
|
460
|
-
|
|
461
|
-
const summaryList = document.querySelector('dl.govuk-summary-list')
|
|
462
|
-
|
|
463
|
-
expect(summaryList).not.toBeNull()
|
|
464
|
-
expect(summaryList?.parentElement).toBe(
|
|
465
|
-
document.querySelectorAll('form')[1]
|
|
466
|
-
)
|
|
467
|
-
|
|
468
|
-
const fileRow = document.querySelector('[data-filename="some-file.pdf"]')
|
|
469
|
-
expect(fileRow).not.toBeNull()
|
|
470
|
-
expect(fileRow?.textContent).toContain('Uploading…')
|
|
471
|
-
|
|
472
|
-
expect(summaryList?.tagName).toBe('DL')
|
|
473
|
-
expect(summaryList?.classList.contains('govuk-summary-list')).toBe(true)
|
|
474
|
-
})
|
|
475
|
-
|
|
476
|
-
test('renderSummary properly removes existing row with same filename', () => {
|
|
477
|
-
document.body.innerHTML = `
|
|
478
|
-
<div class="govuk-error-summary-container"></div>
|
|
479
|
-
<form>
|
|
480
|
-
<input type="file" id="file-upload">
|
|
481
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
482
|
-
</form>
|
|
483
|
-
<form>
|
|
484
|
-
<div id="uploadedFilesContainer">
|
|
485
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
486
|
-
<p class="govuk-body">0 files uploaded</p>
|
|
487
|
-
</div>
|
|
488
|
-
<dl class="govuk-summary-list govuk-summary-list--long-key">
|
|
489
|
-
<div class="govuk-summary-list__row" data-filename="some-file.pdf">
|
|
490
|
-
<dt class="govuk-summary-list__key">some-file.pdf</dt>
|
|
491
|
-
<dd class="govuk-summary-list__value">
|
|
492
|
-
<strong class="govuk-tag govuk-tag--yellow">Previous status</strong>
|
|
493
|
-
</dd>
|
|
494
|
-
<dd class="govuk-summary-list__actions"></dd>
|
|
495
|
-
</div>
|
|
496
|
-
</dl>
|
|
497
|
-
<button class="govuk-button">Continue</button>
|
|
498
|
-
</form>
|
|
499
|
-
`
|
|
500
|
-
|
|
501
|
-
expect(
|
|
502
|
-
document.querySelectorAll('[data-filename="some-file.pdf"]')
|
|
503
|
-
).toHaveLength(1)
|
|
504
|
-
expect(
|
|
505
|
-
document.querySelector('[data-filename="some-file.pdf"]')?.textContent
|
|
506
|
-
).toContain('Previous status')
|
|
507
|
-
|
|
508
|
-
const { loadFile, triggerChange, triggerClick } = setupTestableComponent()
|
|
509
|
-
|
|
510
|
-
loadFile('some-file.pdf')
|
|
511
|
-
triggerChange()
|
|
512
|
-
triggerClick({})
|
|
513
|
-
|
|
514
|
-
expect(
|
|
515
|
-
document.querySelectorAll('[data-filename="some-file.pdf"]')
|
|
516
|
-
).toHaveLength(1)
|
|
517
|
-
expect(
|
|
518
|
-
document.querySelector('[data-filename="some-file.pdf"]')?.textContent
|
|
519
|
-
).toContain('Uploading…')
|
|
520
|
-
expect(
|
|
521
|
-
document.querySelector('[data-filename="some-file.pdf"]')?.textContent
|
|
522
|
-
).not.toContain('Previous status')
|
|
523
|
-
})
|
|
524
|
-
|
|
525
|
-
test('renderSummary uses fileCountP.nextSibling when no continue button exists', () => {
|
|
526
|
-
document.body.innerHTML = `
|
|
527
|
-
<div class="govuk-error-summary-container"></div>
|
|
528
|
-
<form>
|
|
529
|
-
<input type="file" id="file-upload">
|
|
530
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
531
|
-
</form>
|
|
532
|
-
<form id="uploadedFilesContainer">
|
|
533
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
534
|
-
<p class="govuk-body">0 files uploaded</p>
|
|
535
|
-
<div class="next-element">Some other element</div>
|
|
536
|
-
<!-- No continue button -->
|
|
537
|
-
</form>
|
|
538
|
-
`
|
|
539
|
-
|
|
540
|
-
const { loadFile, triggerChange, triggerClick } = setupTestableComponent()
|
|
541
|
-
|
|
542
|
-
loadFile('some-file.pdf')
|
|
543
|
-
triggerChange()
|
|
544
|
-
triggerClick({})
|
|
545
|
-
|
|
546
|
-
const summaryList = document.querySelector('dl.govuk-summary-list')
|
|
547
|
-
|
|
548
|
-
expect(summaryList).not.toBeNull()
|
|
549
|
-
|
|
550
|
-
const secondForm = document.querySelectorAll('form')[1]
|
|
551
|
-
expect(summaryList?.parentElement).toBe(secondForm)
|
|
552
|
-
|
|
553
|
-
const fileRow = document.querySelector('[data-filename="some-file.pdf"]')
|
|
554
|
-
expect(fileRow).not.toBeNull()
|
|
555
|
-
|
|
556
|
-
const statusAnnouncer = document.getElementById('statusInformation')
|
|
557
|
-
expect(statusAnnouncer).not.toBeNull()
|
|
558
|
-
})
|
|
559
|
-
|
|
560
|
-
test('sets aria-describedby on file input to connect with status announcer when uploading', () => {
|
|
561
|
-
document.body.innerHTML = `
|
|
562
|
-
<div class="govuk-error-summary-container"></div>
|
|
563
|
-
<form>
|
|
564
|
-
<input type="file" id="file-upload">
|
|
565
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
566
|
-
</form>
|
|
567
|
-
<form>
|
|
568
|
-
<div id="uploadedFilesContainer">
|
|
569
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
570
|
-
<p class="govuk-body">0 files uploaded</p>
|
|
571
|
-
</div>
|
|
572
|
-
<button class="govuk-button">Continue</button>
|
|
573
|
-
</form>
|
|
574
|
-
`
|
|
575
|
-
|
|
576
|
-
const { fileInput, loadFile, triggerChange, triggerClick } =
|
|
577
|
-
setupTestableComponent()
|
|
578
|
-
|
|
579
|
-
loadFile('test.pdf')
|
|
580
|
-
triggerChange()
|
|
581
|
-
triggerClick({})
|
|
582
|
-
|
|
583
|
-
const statusAnnouncer = document.getElementById('statusInformation')
|
|
584
|
-
|
|
585
|
-
expect(statusAnnouncer).not.toBeNull()
|
|
586
|
-
expect(fileInput?.getAttribute('aria-describedby')).toBe(
|
|
587
|
-
'statusInformation'
|
|
588
|
-
)
|
|
589
|
-
expect(statusAnnouncer?.textContent).toContain('test.pdf')
|
|
590
|
-
expect(statusAnnouncer?.textContent).toContain('Uploading')
|
|
591
|
-
})
|
|
592
|
-
|
|
593
|
-
test('disables controls after timeout when uploading', () => {
|
|
594
|
-
document.body.innerHTML = `
|
|
595
|
-
<div class="govuk-error-summary-container"></div>
|
|
596
|
-
<form>
|
|
597
|
-
<input type="file" id="file-upload">
|
|
598
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
599
|
-
</form>
|
|
600
|
-
<form>
|
|
601
|
-
<div id="uploadedFilesContainer">
|
|
602
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
603
|
-
<p class="govuk-body">0 files uploaded</p>
|
|
604
|
-
</div>
|
|
605
|
-
<button class="govuk-button">Continue</button>
|
|
606
|
-
</form>
|
|
607
|
-
`
|
|
608
|
-
|
|
609
|
-
const { fileInput, uploadButton, loadFile, triggerChange, triggerClick } =
|
|
610
|
-
setupTestableComponent()
|
|
611
|
-
|
|
612
|
-
loadFile('test.pdf')
|
|
613
|
-
triggerChange()
|
|
614
|
-
triggerClick({})
|
|
615
|
-
|
|
616
|
-
expect(/** @type {HTMLInputElement} */ (fileInput).disabled).toBe(false)
|
|
617
|
-
expect(/** @type {HTMLButtonElement} */ (uploadButton).disabled).toBe(false)
|
|
618
|
-
|
|
619
|
-
jest.advanceTimersByTime(101)
|
|
620
|
-
|
|
621
|
-
expect(/** @type {HTMLInputElement} */ (fileInput).disabled).toBe(true)
|
|
622
|
-
expect(/** @type {HTMLButtonElement} */ (uploadButton).disabled).toBe(true)
|
|
623
|
-
})
|
|
624
|
-
|
|
625
|
-
test('handles null form gracefully', () => {
|
|
626
|
-
document.body.innerHTML =
|
|
627
|
-
'<div class="govuk-error-summary-container"></div>'
|
|
628
|
-
expect(() => initFileUpload()).not.toThrow()
|
|
629
|
-
})
|
|
630
|
-
|
|
631
|
-
test('renderSummary handles existing row correctly', () => {
|
|
632
|
-
document.body.innerHTML = `
|
|
633
|
-
<div class="govuk-error-summary-container"></div>
|
|
634
|
-
<form>
|
|
635
|
-
<input type="file" id="file-upload">
|
|
636
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
637
|
-
</form>
|
|
638
|
-
<form>
|
|
639
|
-
<div id="uploadedFilesContainer">
|
|
640
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
641
|
-
<p class="govuk-body">0 files uploaded</p>
|
|
642
|
-
</div>
|
|
643
|
-
<button class="govuk-button">Continue</button>
|
|
644
|
-
</form>
|
|
645
|
-
<div class="govuk-summary-list__row" data-filename="some-file.pdf">
|
|
646
|
-
<dt class="govuk-summary-list__key">some-file.pdf</dt>
|
|
647
|
-
<dd class="govuk-summary-list__value">
|
|
648
|
-
<strong class="govuk-tag govuk-tag--yellow">Old status</strong>
|
|
649
|
-
</dd>
|
|
650
|
-
</div>
|
|
651
|
-
`
|
|
652
|
-
|
|
653
|
-
const { loadFile, triggerChange, triggerClick } = setupTestableComponent()
|
|
654
|
-
|
|
655
|
-
loadFile('some-file.pdf')
|
|
656
|
-
triggerChange()
|
|
657
|
-
triggerClick({})
|
|
658
|
-
|
|
659
|
-
expect(
|
|
660
|
-
document.querySelectorAll('[data-filename="some-file.pdf"]')
|
|
661
|
-
).toHaveLength(1)
|
|
662
|
-
|
|
663
|
-
const summaryList = document.querySelector('dl.govuk-summary-list')
|
|
664
|
-
const fileRow = document.querySelector('[data-filename="some-file.pdf"]')
|
|
665
|
-
expect(fileRow).not.toBeNull()
|
|
666
|
-
expect(fileRow?.parentNode).toBe(summaryList)
|
|
667
|
-
expect(fileRow?.textContent).toContain('Uploading…')
|
|
668
|
-
})
|
|
669
|
-
|
|
670
|
-
test('handles missing file count paragraph', () => {
|
|
671
|
-
document.body.innerHTML = `
|
|
672
|
-
<div class="govuk-error-summary-container"></div>
|
|
673
|
-
<form>
|
|
674
|
-
<input type="file" id="file-upload">
|
|
675
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
676
|
-
</form>
|
|
677
|
-
<form id="uploadedFilesContainer">
|
|
678
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
679
|
-
<!-- No p.govuk-body element -->
|
|
680
|
-
<button class="govuk-button">Continue</button>
|
|
681
|
-
</form>
|
|
682
|
-
`
|
|
683
|
-
|
|
684
|
-
const { loadFile, triggerChange, triggerClick } = setupTestableComponent()
|
|
685
|
-
|
|
686
|
-
loadFile('some-file.pdf')
|
|
687
|
-
triggerChange()
|
|
688
|
-
triggerClick({})
|
|
689
|
-
|
|
690
|
-
const summaryList = document.querySelector('dl.govuk-summary-list')
|
|
691
|
-
expect(summaryList).toBeNull()
|
|
692
|
-
})
|
|
693
|
-
|
|
694
|
-
test('handles missing file input gracefully', () => {
|
|
695
|
-
document.body.innerHTML = `
|
|
696
|
-
<div class="govuk-error-summary-container"></div>
|
|
697
|
-
<form>
|
|
698
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
699
|
-
</form>
|
|
700
|
-
<form id="uploadedFilesContainer">
|
|
701
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
702
|
-
<p class="govuk-body">0 files uploaded</p>
|
|
703
|
-
<button class="govuk-button">Continue</button>
|
|
704
|
-
</form>
|
|
705
|
-
`
|
|
706
|
-
|
|
707
|
-
expect(() => initFileUpload()).not.toThrow()
|
|
708
|
-
})
|
|
709
|
-
|
|
710
|
-
test('handles existing row correctly in dom', () => {
|
|
711
|
-
document.body.innerHTML = `
|
|
712
|
-
<div class="govuk-error-summary-container"></div>
|
|
713
|
-
<form>
|
|
714
|
-
<input type="file" id="file-upload">
|
|
715
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
716
|
-
</form>
|
|
717
|
-
<form id="uploadedFilesContainer">
|
|
718
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
719
|
-
<p class="govuk-body">0 files uploaded</p>
|
|
720
|
-
<div class="govuk-summary-list__row" data-filename="some-file.pdf">
|
|
721
|
-
<dt>Original row that should be removed</dt>
|
|
722
|
-
</div>
|
|
723
|
-
<button class="govuk-button">Continue</button>
|
|
724
|
-
</form>
|
|
725
|
-
`
|
|
726
|
-
|
|
727
|
-
const { loadFile, triggerChange, triggerClick } = setupTestableComponent()
|
|
728
|
-
|
|
729
|
-
loadFile('some-file.pdf')
|
|
730
|
-
triggerChange()
|
|
731
|
-
triggerClick({})
|
|
732
|
-
|
|
733
|
-
expect(
|
|
734
|
-
document.querySelectorAll('[data-filename="some-file.pdf"]')
|
|
735
|
-
).toHaveLength(1)
|
|
736
|
-
|
|
737
|
-
const row = document.querySelector('[data-filename="some-file.pdf"]')
|
|
738
|
-
expect(row?.textContent).toContain('Uploading…')
|
|
739
|
-
expect(row?.textContent).not.toContain(
|
|
740
|
-
'Original row that should be removed'
|
|
741
|
-
)
|
|
742
|
-
})
|
|
743
|
-
|
|
744
|
-
test('file upload handles null form gracefully when creating status announcer', () => {
|
|
745
|
-
document.body.innerHTML = `
|
|
746
|
-
<div class="govuk-error-summary-container"></div>
|
|
747
|
-
<form id="uploadForm">
|
|
748
|
-
<input type="file" id="file-upload">
|
|
749
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
750
|
-
</form>
|
|
751
|
-
<form>
|
|
752
|
-
<div id="uploadedFilesContainer">
|
|
753
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
754
|
-
<p class="govuk-body">0 files uploaded</p>
|
|
755
|
-
</div>
|
|
756
|
-
<button class="govuk-button">Continue</button>
|
|
757
|
-
</form>
|
|
758
|
-
`
|
|
759
|
-
|
|
760
|
-
const { loadFile, triggerChange } = setupTestableComponent()
|
|
761
|
-
|
|
762
|
-
loadFile('some-file.pdf')
|
|
763
|
-
triggerChange()
|
|
764
|
-
|
|
765
|
-
const form = document.getElementById('uploadForm')
|
|
766
|
-
if (form) {
|
|
767
|
-
form.parentNode?.removeChild(form)
|
|
768
|
-
}
|
|
769
|
-
|
|
770
|
-
const uploadButton = document.querySelector('.upload-file-button')
|
|
771
|
-
|
|
772
|
-
const clickEvent = new MouseEvent('click', {
|
|
773
|
-
bubbles: true,
|
|
774
|
-
cancelable: true
|
|
775
|
-
})
|
|
776
|
-
|
|
777
|
-
expect(() => {
|
|
778
|
-
uploadButton?.dispatchEvent(clickEvent)
|
|
779
|
-
}).not.toThrow()
|
|
780
|
-
|
|
781
|
-
expect(document.querySelector('[data-filename="some-file.pdf"]')).toBeNull()
|
|
782
|
-
expect(document.getElementById('statusInformation')).toBeNull()
|
|
783
|
-
})
|
|
784
|
-
|
|
785
|
-
test('renderSummary explicitly handles null selectedFile', () => {
|
|
786
|
-
document.body.innerHTML = `
|
|
787
|
-
<div class="govuk-error-summary-container"></div>
|
|
788
|
-
<form>
|
|
789
|
-
<input type="file" id="file-upload">
|
|
790
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
791
|
-
</form>
|
|
792
|
-
<form>
|
|
793
|
-
<div id="uploadedFilesContainer">
|
|
794
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
795
|
-
<p class="govuk-body">0 files uploaded</p>
|
|
796
|
-
</div>
|
|
797
|
-
<button class="govuk-button">Continue</button>
|
|
798
|
-
</form>
|
|
799
|
-
`
|
|
800
|
-
|
|
801
|
-
const { triggerClick } = setupTestableComponent()
|
|
802
|
-
|
|
803
|
-
const containerObserver = new MutationObserver(() => {
|
|
804
|
-
/* intentionally empty - we just want to collect mutations */
|
|
805
|
-
})
|
|
806
|
-
const summaryListContainer = document.querySelector('form:nth-child(2)')
|
|
807
|
-
|
|
808
|
-
if (summaryListContainer) {
|
|
809
|
-
containerObserver.observe(summaryListContainer, {
|
|
810
|
-
childList: true,
|
|
811
|
-
subtree: true
|
|
812
|
-
})
|
|
813
|
-
}
|
|
814
|
-
|
|
815
|
-
containerObserver.takeRecords()
|
|
816
|
-
|
|
817
|
-
triggerClick({ preventDefault: jest.fn() })
|
|
818
|
-
|
|
819
|
-
const mutations = containerObserver.takeRecords()
|
|
820
|
-
const summaryListAdded = mutations.some((mutation) =>
|
|
821
|
-
Array.from(mutation.addedNodes).some(
|
|
822
|
-
(node) =>
|
|
823
|
-
node.nodeType === Node.ELEMENT_NODE &&
|
|
824
|
-
node instanceof HTMLElement &&
|
|
825
|
-
node.classList.contains('govuk-summary-list')
|
|
826
|
-
)
|
|
827
|
-
)
|
|
828
|
-
|
|
829
|
-
containerObserver.disconnect()
|
|
830
|
-
|
|
831
|
-
expect(summaryListAdded).toBe(false)
|
|
832
|
-
expect(document.querySelector('dl.govuk-summary-list')).toBeNull()
|
|
833
|
-
})
|
|
834
|
-
|
|
835
|
-
test('status announcer falls back to document.body when form.appendChild fails', () => {
|
|
836
|
-
document.body.innerHTML = `
|
|
837
|
-
<div class="govuk-error-summary-container"></div>
|
|
838
|
-
<form id="upload-form">
|
|
839
|
-
<input type="file" id="file-upload">
|
|
840
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
841
|
-
</form>
|
|
842
|
-
<form id="second-form">
|
|
843
|
-
<div id="uploadedFilesContainer">
|
|
844
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
845
|
-
<p class="govuk-body">0 files uploaded</p>
|
|
846
|
-
</div>
|
|
847
|
-
<button class="govuk-button">Continue</button>
|
|
848
|
-
</form>
|
|
849
|
-
`
|
|
850
|
-
|
|
851
|
-
const containerDiv = document.getElementById('uploadedFilesContainer')
|
|
852
|
-
if (!containerDiv) {
|
|
853
|
-
throw new Error()
|
|
854
|
-
}
|
|
855
|
-
const originalContainerAppend = containerDiv.appendChild.bind(containerDiv)
|
|
856
|
-
containerDiv.appendChild = jest.fn(() => {
|
|
857
|
-
throw new Error()
|
|
858
|
-
})
|
|
859
|
-
|
|
860
|
-
const form = document.getElementById('second-form')
|
|
861
|
-
if (!form) {
|
|
862
|
-
throw new Error()
|
|
863
|
-
}
|
|
864
|
-
const originalFormAppend = form.appendChild.bind(form)
|
|
865
|
-
form.appendChild = jest.fn(() => {
|
|
866
|
-
throw new Error()
|
|
867
|
-
})
|
|
868
|
-
|
|
869
|
-
const bodyAppendChildSpy = jest.spyOn(document.body, 'appendChild')
|
|
870
|
-
|
|
871
|
-
const { loadFile, triggerChange, triggerClick } = setupTestableComponent()
|
|
872
|
-
loadFile('test.pdf')
|
|
873
|
-
triggerChange()
|
|
874
|
-
triggerClick({})
|
|
875
|
-
|
|
876
|
-
expect(bodyAppendChildSpy).toHaveBeenCalled()
|
|
877
|
-
const statusAnnouncerAppended = bodyAppendChildSpy.mock.calls.some(
|
|
878
|
-
(call) =>
|
|
879
|
-
call[0] instanceof HTMLElement && call[0].id === 'statusInformation'
|
|
880
|
-
)
|
|
881
|
-
expect(statusAnnouncerAppended).toBe(true)
|
|
882
|
-
|
|
883
|
-
const statusAnnouncer = document.getElementById('statusInformation')
|
|
884
|
-
expect(statusAnnouncer).not.toBeNull()
|
|
885
|
-
expect(statusAnnouncer?.parentNode).toBe(document.body)
|
|
886
|
-
|
|
887
|
-
containerDiv.appendChild = originalContainerAppend
|
|
888
|
-
form.appendChild = originalFormAppend
|
|
889
|
-
bodyAppendChildSpy.mockRestore()
|
|
890
|
-
})
|
|
891
|
-
|
|
892
|
-
test('uses form action when proxyUrl is undefined and uses proxyUrl when defined', () => {
|
|
893
|
-
const originalFetch = global.fetch
|
|
894
|
-
const fetchMock = jest.fn(() =>
|
|
895
|
-
Promise.resolve(new Response(JSON.stringify({}), { status: 200 }))
|
|
896
|
-
)
|
|
897
|
-
global.fetch = fetchMock
|
|
898
|
-
|
|
899
|
-
document.body.innerHTML = `
|
|
900
|
-
<div class="govuk-error-summary-container"></div>
|
|
901
|
-
<form action="http://some-url.com/upload" enctype="multipart/form-data" data-upload-id="test-id">
|
|
902
|
-
<input type="file" id="file-upload">
|
|
903
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
904
|
-
</form>
|
|
905
|
-
`
|
|
906
|
-
setupTestableComponent()
|
|
907
|
-
|
|
908
|
-
const form = document.querySelector('form')
|
|
909
|
-
const uploadUrl1 = form?.dataset.proxyUrl ?? form?.action
|
|
910
|
-
|
|
911
|
-
expect(uploadUrl1).toBe('http://some-url.com/upload')
|
|
912
|
-
|
|
913
|
-
form?.setAttribute('data-proxy-url', 'http://some-proxy-url.com/upload')
|
|
914
|
-
|
|
915
|
-
const uploadUrl2 = form?.dataset.proxyUrl ?? form?.action
|
|
916
|
-
|
|
917
|
-
expect(uploadUrl2).toBe('http://some-proxy-url.com/upload')
|
|
918
|
-
|
|
919
|
-
global.fetch = originalFetch
|
|
920
|
-
})
|
|
921
|
-
|
|
922
|
-
test('form submission depends on formElement having action attribute and uploadId', () => {
|
|
923
|
-
const originalFormData = global.FormData
|
|
924
|
-
global.FormData = /** @type {any} */ (
|
|
925
|
-
jest.fn(() => ({
|
|
926
|
-
append: jest.fn()
|
|
927
|
-
}))
|
|
928
|
-
)
|
|
929
|
-
|
|
930
|
-
document.body.innerHTML = `
|
|
931
|
-
<div class="govuk-error-summary-container"></div>
|
|
932
|
-
<form action="/upload-endpoint" data-upload-id="test-id">
|
|
933
|
-
<input type="file" id="file-upload">
|
|
934
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
935
|
-
</form>
|
|
936
|
-
`
|
|
937
|
-
|
|
938
|
-
const form1 = document.querySelector('form')
|
|
939
|
-
expect(form1?.hasAttribute('action')).toBe(true)
|
|
940
|
-
expect(form1?.getAttribute('action')).toBe('/upload-endpoint')
|
|
941
|
-
|
|
942
|
-
document.body.innerHTML = `
|
|
943
|
-
<div class="govuk-error-summary-container"></div>
|
|
944
|
-
<form data-upload-id="test-id">
|
|
945
|
-
<input type="file" id="file-upload">
|
|
946
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
947
|
-
</form>
|
|
948
|
-
`
|
|
949
|
-
|
|
950
|
-
const form2 = document.querySelector('form')
|
|
951
|
-
expect(form2?.hasAttribute('action')).toBe(false)
|
|
952
|
-
|
|
953
|
-
document.body.innerHTML = `
|
|
954
|
-
<div class="govuk-error-summary-container"></div>
|
|
955
|
-
<form action="/upload-endpoint">
|
|
956
|
-
<input type="file" id="file-upload">
|
|
957
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
958
|
-
</form>
|
|
959
|
-
`
|
|
960
|
-
|
|
961
|
-
const form3 = document.querySelector('form')
|
|
962
|
-
expect(form3?.hasAttribute('action')).toBe(true)
|
|
963
|
-
expect(form3?.hasAttribute('data-upload-id')).toBe(false)
|
|
964
|
-
|
|
965
|
-
global.FormData = originalFormData
|
|
966
|
-
})
|
|
967
|
-
|
|
968
|
-
test('upload URL is correctly determined by formElement attributes', () => {
|
|
969
|
-
document.body.innerHTML = `
|
|
970
|
-
<form action="/upload-endpoint" data-upload-id="test-id">
|
|
971
|
-
<input type="file" id="file-upload">
|
|
972
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
973
|
-
</form>
|
|
974
|
-
`
|
|
975
|
-
|
|
976
|
-
const form1 = document.querySelector('form')
|
|
977
|
-
const uploadUrl1 = form1?.dataset.proxyUrl ?? form1?.getAttribute('action')
|
|
978
|
-
expect(uploadUrl1).toBe('/upload-endpoint')
|
|
979
|
-
|
|
980
|
-
document.body.innerHTML = `
|
|
981
|
-
<form action="/upload-endpoint" data-upload-id="test-id" data-proxy-url="/proxy-endpoint">
|
|
982
|
-
<input type="file" id="file-upload">
|
|
983
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
984
|
-
</form>
|
|
985
|
-
`
|
|
986
|
-
|
|
987
|
-
const form2 = document.querySelector('form')
|
|
988
|
-
const uploadUrl2 = form2?.dataset.proxyUrl ?? form2?.getAttribute('action')
|
|
989
|
-
expect(uploadUrl2).toBe('/proxy-endpoint')
|
|
990
|
-
|
|
991
|
-
document.body.innerHTML = `
|
|
992
|
-
<form data-upload-id="test-id" data-proxy-url="/proxy-endpoint">
|
|
993
|
-
<input type="file" id="file-upload">
|
|
994
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
995
|
-
</form>
|
|
996
|
-
`
|
|
997
|
-
|
|
998
|
-
const form3 = document.querySelector('form')
|
|
999
|
-
const uploadUrl3 = form3?.dataset.proxyUrl ?? form3?.getAttribute('action')
|
|
1000
|
-
expect(uploadUrl3).toBe('/proxy-endpoint')
|
|
1001
|
-
|
|
1002
|
-
document.body.innerHTML = `
|
|
1003
|
-
<form data-upload-id="test-id">
|
|
1004
|
-
<input type="file" id="file-upload">
|
|
1005
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
1006
|
-
</form>
|
|
1007
|
-
`
|
|
1008
|
-
|
|
1009
|
-
const form4 = document.querySelector('form')
|
|
1010
|
-
const uploadUrl4 = form4?.dataset.proxyUrl ?? form4?.getAttribute('action')
|
|
1011
|
-
expect(uploadUrl4).toBeNull()
|
|
1012
|
-
})
|
|
1013
|
-
|
|
1014
|
-
test('handles AJAX form submission when form has action and uploadId', () => {
|
|
1015
|
-
const originalFetch = global.fetch
|
|
1016
|
-
const fetchMock = jest.fn(() =>
|
|
1017
|
-
Promise.resolve(new Response(JSON.stringify({}), { status: 200 }))
|
|
1018
|
-
)
|
|
1019
|
-
global.fetch = fetchMock
|
|
1020
|
-
|
|
1021
|
-
document.body.innerHTML = `
|
|
1022
|
-
<div class="govuk-error-summary-container"></div>
|
|
1023
|
-
<form action="http://some-url.com/upload" enctype="multipart/form-data" data-upload-id="test-id">
|
|
1024
|
-
<input type="file" id="file-upload">
|
|
1025
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
1026
|
-
</form>
|
|
1027
|
-
<form>
|
|
1028
|
-
<div id="uploadedFilesContainer">
|
|
1029
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
1030
|
-
<p class="govuk-body">0 files uploaded</p>
|
|
1031
|
-
</div>
|
|
1032
|
-
<button class="govuk-button">Continue</button>
|
|
1033
|
-
</form>
|
|
1034
|
-
`
|
|
1035
|
-
|
|
1036
|
-
const originalFormData = global.FormData
|
|
1037
|
-
const formDataMock = jest.fn(function () {
|
|
1038
|
-
return {
|
|
1039
|
-
append: jest.fn()
|
|
1040
|
-
}
|
|
1041
|
-
})
|
|
1042
|
-
global.FormData = /** @type {any} */ (formDataMock)
|
|
1043
|
-
|
|
1044
|
-
const tempGlobal = /** @type {any} */ (global)
|
|
1045
|
-
// using bracket notation with ESLint disabled to safely add property to global
|
|
1046
|
-
// eslint-disable-next-line @typescript-eslint/dot-notation, @typescript-eslint/no-unsafe-member-access
|
|
1047
|
-
tempGlobal['pollUploadStatus'] = jest.fn()
|
|
1048
|
-
|
|
1049
|
-
const { loadFile, triggerChange, triggerClick } = setupTestableComponent()
|
|
1050
|
-
|
|
1051
|
-
loadFile('test-file.pdf')
|
|
1052
|
-
triggerChange()
|
|
1053
|
-
|
|
1054
|
-
const preventDefaultMock = jest.fn()
|
|
1055
|
-
triggerClick({ preventDefault: preventDefaultMock })
|
|
1056
|
-
|
|
1057
|
-
expect(preventDefaultMock).toHaveBeenCalled()
|
|
1058
|
-
expect(formDataMock).toHaveBeenCalled()
|
|
1059
|
-
expect(fetchMock).toHaveBeenCalledWith(
|
|
1060
|
-
'http://some-url.com/upload',
|
|
1061
|
-
expect.objectContaining({
|
|
1062
|
-
method: 'POST',
|
|
1063
|
-
redirect: 'manual'
|
|
1064
|
-
})
|
|
1065
|
-
)
|
|
1066
|
-
|
|
1067
|
-
global.fetch = originalFetch
|
|
1068
|
-
global.FormData = originalFormData
|
|
1069
|
-
})
|
|
1070
|
-
|
|
1071
|
-
test('handles AJAX form submission with proxy URL when provided', () => {
|
|
1072
|
-
const originalFetch = global.fetch
|
|
1073
|
-
const fetchMock = jest.fn(() =>
|
|
1074
|
-
Promise.resolve(new Response(JSON.stringify({}), { status: 200 }))
|
|
1075
|
-
)
|
|
1076
|
-
global.fetch = fetchMock
|
|
1077
|
-
|
|
1078
|
-
const originalFormData = global.FormData
|
|
1079
|
-
const formDataMock = jest.fn(function () {
|
|
1080
|
-
return {
|
|
1081
|
-
append: jest.fn()
|
|
1082
|
-
}
|
|
1083
|
-
})
|
|
1084
|
-
global.FormData = /** @type {any} */ (formDataMock)
|
|
1085
|
-
|
|
1086
|
-
document.body.innerHTML = `
|
|
1087
|
-
<div class="govuk-error-summary-container"></div>
|
|
1088
|
-
<form action="/upload-endpoint" data-proxy-url="/proxy-endpoint" data-upload-id="test-id" enctype="multipart/form-data">
|
|
1089
|
-
<input type="file" id="file-upload">
|
|
1090
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
1091
|
-
</form>
|
|
1092
|
-
<form>
|
|
1093
|
-
<div id="uploadedFilesContainer">
|
|
1094
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
1095
|
-
<p class="govuk-body">0 files uploaded</p>
|
|
1096
|
-
</div>
|
|
1097
|
-
<button class="govuk-button">Continue</button>
|
|
1098
|
-
</form>
|
|
1099
|
-
`
|
|
1100
|
-
|
|
1101
|
-
const tempGlobal = /** @type {any} */ (global)
|
|
1102
|
-
// eslint-disable-next-line @typescript-eslint/dot-notation, @typescript-eslint/no-unsafe-member-access
|
|
1103
|
-
tempGlobal['pollUploadStatus'] = jest.fn()
|
|
1104
|
-
|
|
1105
|
-
const { loadFile, triggerChange, triggerClick } = setupTestableComponent()
|
|
1106
|
-
|
|
1107
|
-
loadFile('some-file.pdf')
|
|
1108
|
-
triggerChange()
|
|
1109
|
-
|
|
1110
|
-
const preventDefaultMock = jest.fn()
|
|
1111
|
-
triggerClick({ preventDefault: preventDefaultMock })
|
|
1112
|
-
|
|
1113
|
-
expect(preventDefaultMock).toHaveBeenCalled()
|
|
1114
|
-
expect(formDataMock).toHaveBeenCalled()
|
|
1115
|
-
|
|
1116
|
-
expect(fetchMock).toHaveBeenCalledWith(
|
|
1117
|
-
'/proxy-endpoint',
|
|
1118
|
-
expect.objectContaining({
|
|
1119
|
-
method: 'POST',
|
|
1120
|
-
redirect: 'follow',
|
|
1121
|
-
mode: 'no-cors'
|
|
1122
|
-
})
|
|
1123
|
-
)
|
|
1124
|
-
|
|
1125
|
-
// it was NOT called with 'manual' redirect
|
|
1126
|
-
expect(fetchMock).not.toHaveBeenCalledWith(
|
|
1127
|
-
'/proxy-endpoint',
|
|
1128
|
-
expect.objectContaining({
|
|
1129
|
-
redirect: 'manual'
|
|
1130
|
-
})
|
|
1131
|
-
)
|
|
1132
|
-
|
|
1133
|
-
global.fetch = originalFetch
|
|
1134
|
-
global.FormData = originalFormData
|
|
1135
|
-
})
|
|
1136
|
-
|
|
1137
|
-
test('handles AJAX form submission without proxy URL (production)', () => {
|
|
1138
|
-
const originalFetch = global.fetch
|
|
1139
|
-
const fetchMock = jest.fn(() =>
|
|
1140
|
-
Promise.resolve(new Response(JSON.stringify({}), { status: 200 }))
|
|
1141
|
-
)
|
|
1142
|
-
global.fetch = fetchMock
|
|
1143
|
-
|
|
1144
|
-
const originalFormData = global.FormData
|
|
1145
|
-
const formDataMock = jest.fn(function () {
|
|
1146
|
-
return {
|
|
1147
|
-
append: jest.fn()
|
|
1148
|
-
}
|
|
1149
|
-
})
|
|
1150
|
-
global.FormData = /** @type {any} */ (formDataMock)
|
|
1151
|
-
|
|
1152
|
-
document.body.innerHTML = `
|
|
1153
|
-
<div class="govuk-error-summary-container"></div>
|
|
1154
|
-
<form action="/upload-endpoint" data-upload-id="test-id" enctype="multipart/form-data">
|
|
1155
|
-
<input type="file" id="file-upload">
|
|
1156
|
-
<button class="govuk-button govuk-button--secondary upload-file-button">Upload file</button>
|
|
1157
|
-
</form>
|
|
1158
|
-
<form>
|
|
1159
|
-
<div id="uploadedFilesContainer">
|
|
1160
|
-
<h2 class="govuk-heading-m">Uploaded files</h2>
|
|
1161
|
-
<p class="govuk-body">0 files uploaded</p>
|
|
1162
|
-
</div>
|
|
1163
|
-
<button class="govuk-button">Continue</button>
|
|
1164
|
-
</form>
|
|
1165
|
-
`
|
|
1166
|
-
|
|
1167
|
-
const tempGlobal = /** @type {any} */ (global)
|
|
1168
|
-
// eslint-disable-next-line @typescript-eslint/dot-notation, @typescript-eslint/no-unsafe-member-access
|
|
1169
|
-
tempGlobal['pollUploadStatus'] = jest.fn()
|
|
1170
|
-
|
|
1171
|
-
const { loadFile, triggerChange, triggerClick } = setupTestableComponent()
|
|
1172
|
-
|
|
1173
|
-
loadFile('some-file.pdf')
|
|
1174
|
-
triggerChange()
|
|
1175
|
-
|
|
1176
|
-
const preventDefaultMock = jest.fn()
|
|
1177
|
-
triggerClick({ preventDefault: preventDefaultMock })
|
|
1178
|
-
|
|
1179
|
-
expect(preventDefaultMock).toHaveBeenCalled()
|
|
1180
|
-
expect(formDataMock).toHaveBeenCalled()
|
|
1181
|
-
|
|
1182
|
-
expect(fetchMock).toHaveBeenCalled()
|
|
1183
|
-
const [actualUrl, options] = /** @type {[string, RequestInit]} */ (
|
|
1184
|
-
/** @type {unknown} */ (fetchMock.mock.calls[0])
|
|
1185
|
-
)
|
|
1186
|
-
expect(actualUrl.endsWith('/upload-endpoint')).toBe(true)
|
|
1187
|
-
expect(options).toMatchObject({
|
|
1188
|
-
method: 'POST',
|
|
1189
|
-
redirect: 'manual'
|
|
1190
|
-
})
|
|
1191
|
-
|
|
1192
|
-
expect(options.mode).not.toBe('no-cors')
|
|
1193
|
-
|
|
1194
|
-
global.fetch = originalFetch
|
|
1195
|
-
global.FormData = originalFormData
|
|
1196
|
-
})
|
|
1197
|
-
})
|