Skip to content
This repository was archived by the owner on Nov 22, 2022. It is now read-only.

Commit cbc1bf4

Browse files
Extend and improve structure of html.Form component tests
1 parent 3746dd4 commit cbc1bf4

File tree

1 file changed

+44
-21
lines changed

1 file changed

+44
-21
lines changed

library/test/html.js

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -219,41 +219,55 @@ describe('HTML-based components', () => {
219219
})
220220
})
221221

222-
it('reacts to form submission', () => {
222+
const exampleForm = '' +
223+
'<form>' +
224+
' <input type="text" name="text_input" value="text_input_contents">' +
225+
' <button name="button" type="submit" value="value">Submit</button>' +
226+
'</form>'
227+
228+
it('catches form submission', () => {
223229
// In this test, we are using an actual
224230
// document node because the virtual
225231
// elements used above do not deal correctly
226232
// with the click event used below.
227233
f.options.el = null
228-
f.options.content = '' +
229-
'<form>' +
230-
' <input type="text" name="text_input" value="text_input_contents">' +
231-
' <button name="button" type="submit" value="value">Submit</button>' +
232-
'</form>'
234+
f.options.content = exampleForm
233235

234-
const callback = sinon.spy()
235-
f.on('end', callback)
236-
237-
const p = f.waitFor('end').then(() => {
238-
// Tests
239-
assert.ok(callback.calledOnce)
240-
assert.equal(f.data.text_input, 'text_input_contents')
241-
assert.equal(f.data.button, 'value')
242-
243-
// Remove HTML content from the page
244-
// when we're done.
245-
f.options.el.innerHTML = ''
246-
})
236+
const spy = sinon.spy(f, 'submit')
247237

248238
// Submit the form
249239
// (note that direct submission via form.submit()
250240
// overrides the event handlers and reloads
251241
// the page)
252-
f.run().then(() => {
242+
return f.run().then(() => {
253243
f.options.el.querySelector('button').click()
244+
assert.ok(spy.calledOnce)
245+
246+
// Clean up content
247+
f.options.el.innerHTML = ''
248+
})
249+
})
250+
251+
it('ends after successful submission', () => {
252+
f.options.content = exampleForm
253+
254+
const spy = sinon.spy(f, 'end')
255+
256+
return f.run().then(() => {
257+
f.submit()
258+
assert.equal(f.status, 3)
259+
assert.ok(spy.calledOnce)
254260
})
261+
})
262+
263+
it('saves form data to store', () => {
264+
f.options.content = exampleForm
255265

256-
return p
266+
return f.run().then(() => {
267+
f.submit()
268+
assert.equal(f.data.text_input, 'text_input_contents')
269+
assert.equal(f.data.button, 'value')
270+
})
257271
})
258272

259273
// Validation --------------------------------------------------------------
@@ -288,6 +302,15 @@ describe('HTML-based components', () => {
288302
assert.ok(f.validate())
289303
})
290304

305+
it('doesn\'t end on submission if the form data is invalid', () => {
306+
f.options.content = minimalInvalidForm
307+
308+
return f.run().then(() => {
309+
f.submit()
310+
assert.equal(f.status, 2) // Still running
311+
})
312+
})
313+
291314
it('adds data-labjs-validated attribute after failed validation', () => {
292315
f.options.content = minimalInvalidForm
293316

0 commit comments

Comments
 (0)