@@ -219,41 +219,55 @@ describe('HTML-based components', () => {
219
219
} )
220
220
} )
221
221
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' , ( ) => {
223
229
// In this test, we are using an actual
224
230
// document node because the virtual
225
231
// elements used above do not deal correctly
226
232
// with the click event used below.
227
233
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
233
235
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' )
247
237
248
238
// Submit the form
249
239
// (note that direct submission via form.submit()
250
240
// overrides the event handlers and reloads
251
241
// the page)
252
- f . run ( ) . then ( ( ) => {
242
+ return f . run ( ) . then ( ( ) => {
253
243
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 )
254
260
} )
261
+ } )
262
+
263
+ it ( 'saves form data to store' , ( ) => {
264
+ f . options . content = exampleForm
255
265
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
+ } )
257
271
} )
258
272
259
273
// Validation --------------------------------------------------------------
@@ -288,6 +302,15 @@ describe('HTML-based components', () => {
288
302
assert . ok ( f . validate ( ) )
289
303
} )
290
304
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
+
291
314
it ( 'adds data-labjs-validated attribute after failed validation' , ( ) => {
292
315
f . options . content = minimalInvalidForm
293
316
0 commit comments