-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
submit_spec.js
430 lines (331 loc) · 10.6 KB
/
submit_spec.js
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
const { assertLogLength } = require('../../../support/utils')
const { _, $, Promise } = Cypress
describe('src/cy/commands/actions/submit', () => {
before(() => {
cy
.visit('/fixtures/dom.html')
.then(function (win) {
this.body = win.document.body.outerHTML
})
})
beforeEach(function () {
const doc = cy.state('document')
$(doc.body).empty().html(this.body)
})
context('#submit', () => {
it('does not change the subject when default actions is prevented', () => {
const form = cy.$$('form:first').on('submit', () => {
return false
})
cy.get('form:first').submit().then(($form) => {
expect($form.get(0)).to.eq(form.get(0))
})
})
it('works with native event listeners', () => {
let submitted = false
cy.$$('form:first').get(0).addEventListener('submit', () => {
submitted = true
})
cy.get('form:first').submit().then(() => {
expect(submitted).to.be.true
})
})
it('bubbles up to the window', () => {
let onsubmitCalled = false
cy
.window().then((win) => {
win.onsubmit = () => {
onsubmitCalled = true
}
})
.get('form:first').submit().then(() => {
expect(onsubmitCalled).to.be.true
})
})
it('does not submit the form action is prevented default', (done) => {
cy.$$('form:first').parent().on('submit', (e) => {
e.preventDefault()
})
cy
.window()
.then((win) => {
const $win = $(win)
// if we reach beforeunload we know the form
// has been submitted
$win.on('beforeunload', () => {
done(new Error('submit event should not submit the form.'))
return undefined
})
cy.get('form:first').submit().then(() => {
$win.off('beforeunload')
done()
})
})
})
it('does not submit the form action returned false', (done) => {
cy.$$('form:first').parent().on('submit', (e) => {
return false
})
cy
.window()
.then((win) => {
const $win = $(win)
// if we reach beforeunload we know the form
// has been submitted
$win.on('beforeunload', () => {
done('submit event should not submit the form.')
return undefined
})
cy.get('form:first').submit().then(() => {
$win.off('beforeunload')
done()
})
})
})
it('actually submits the form.', () => {
let beforeunload = false
cy
.window().then((win) => {
// if we reach beforeunload we know the form
// has been submitted
$(win).on('beforeunload', () => {
beforeunload = true
return undefined
})
})
.get('form:first').submit().then(() => {
expect(beforeunload).to.be.true
})
})
// if we removed our submit handler this would fail.
it('fires \'form:submitted event\'', () => {
const $form = cy.$$('form:first')
// we must rely on isReady already being pending here
// because the submit method does not trigger beforeunload
// synchronously.
let submitted = false
cy.on('form:submitted', (e) => {
submitted = true
expect(e.target).to.eq($form.get(0))
})
cy.get('form:first').submit().then(() => {
expect(submitted).to.be.true
})
})
it('does not fire \'form:submitted\' if default action is prevented', () => {
let submitted = false
cy.on('form:submitted', (e) => {
submitted = true
})
cy.$$('form:first').on('submit', (e) => {
e.preventDefault()
})
cy
.get('form:first').submit().then(() => {
expect(submitted).to.be.false
})
})
it('delays 50ms before resolving', () => {
cy.$$('form:first').on('submit', (e) => {
cy.spy(Promise, 'delay')
})
cy.get('form:first').submit().then(() => {
expect(Promise.delay).to.be.calledWith(50, 'submit')
})
})
it('increases the timeout delta', () => {
cy.spy(cy, 'timeout')
cy.get('form:first').submit().then(() => {
expect(cy.timeout).to.be.calledWith(50, true)
})
})
describe('assertion verification', () => {
beforeEach(function () {
cy.on('log:added', (attrs, log) => {
if (log.get('name') === 'assert') {
this.lastLog = log
}
})
return null
})
it('eventually passes the assertion', () => {
cy.$$('form:first').submit(function () {
_.delay(() => {
$(this).addClass('submitted')
}, 100)
return false
})
cy.get('form:first').submit().should('have.class', 'submitted').then(function () {
const { lastLog } = this
expect(lastLog.get('name')).to.eq('assert')
expect(lastLog.get('state')).to.eq('passed')
expect(lastLog.get('ended')).to.be.true
})
})
})
describe('errors', {
defaultCommandTimeout: 100,
}, () => {
beforeEach(function () {
this.logs = []
cy.on('log:added', (attrs, log) => {
this.lastLog = log
this.logs.push(log)
})
return null
})
it('is a child command', (done) => {
cy.on('fail', () => {
done()
})
cy.submit()
})
it('throws when non dom subject', (done) => {
cy.on('fail', () => {
done()
})
cy.noop({}).submit()
})
it('throws when subject isnt a form', function (done) {
cy.on('fail', (err) => {
const { lastLog } = this
assertLogLength(this.logs, 2)
expect(lastLog.get('error')).to.eq(err)
expect(err.message).to.include('`cy.submit()` can only be called on a `<form>`. Your subject contains a: `<input id="input">`')
expect(err.docsUrl).to.eq('https://on.cypress.io/submit')
done()
})
cy.get('input').submit()
})
it('throws when subject is not in the document', (done) => {
let submitted = 0
const form = cy.$$('form:first').submit((e) => {
submitted += 1
form.remove()
return false
})
cy.on('fail', (err) => {
expect(submitted).to.eq(1)
expect(err.message).to.include('`cy.submit()` failed because this element')
done()
})
cy.get('form:first').submit().submit()
})
it('throws when subject is a collection of elements', (done) => {
const forms = cy.$$('form')
// make sure we have more than 1 form.
expect(forms.length).to.be.gt(1)
cy.on('fail', (err) => {
expect(err.message).to.include(`\`cy.submit()\` can only be called on a single \`form\`. Your subject contained ${forms.length} \`form\` elements.`)
expect(err.docsUrl).to.eq('https://on.cypress.io/submit')
done()
})
cy.get('form').submit()
})
it('logs once when not dom subject', function (done) {
cy.on('fail', (err) => {
const { lastLog } = this
assertLogLength(this.logs, 1)
expect(lastLog.get('error')).to.eq(err)
done()
})
return cy.submit()
})
it('eventually fails the assertion', function (done) {
cy.$$('form:first').submit(() => {
return false
})
cy.on('fail', (err) => {
const { lastLog } = this
expect(err.message).to.include(lastLog.get('error').message)
expect(err.message).not.to.include('undefined')
expect(lastLog.get('name')).to.eq('assert')
expect(lastLog.get('state')).to.eq('failed')
expect(lastLog.get('error')).to.be.an.instanceof(chai.AssertionError)
done()
})
cy.get('form:first').submit().should('have.class', 'submitted')
})
it('does not log an additional log on failure', function (done) {
cy.$$('form:first').submit(() => {
return false
})
cy.on('fail', () => {
assertLogLength(this.logs, 3)
done()
})
cy.get('form:first').submit().should('have.class', 'submitted')
})
})
describe('.log', () => {
beforeEach(function () {
cy.on('log:added', (attrs, log) => {
if (log.get('name') === 'submit') {
this.lastLog = log
}
})
return null
})
it('logs immediately before resolving', () => {
const $form = cy.$$('form:first')
cy.on('log:added', (attrs, log) => {
if (log.get('name') === 'submit') {
expect(log.get('state')).to.eq('pending')
expect(log.get('$el').get(0)).to.eq($form.get(0))
}
})
cy.get('form:first').submit()
})
it('provides $el', () => {
cy.$$('form:first').submit(() => {
return false
})
cy.get('form').first().submit().then(function ($form) {
const { lastLog } = this
expect(lastLog.get('name')).to.eq('submit')
expect(lastLog.get('$el')).to.match($form)
})
})
it('snapshots before submitted', function () {
let expected = false
cy.$$('form:first').submit(() => {
return false
})
cy.$$('form').first().submit(() => {
const { lastLog } = this
expected = true
expect(lastLog.get('snapshots').length).to.eq(1)
expect(lastLog.get('snapshots')[0].name).to.eq('before')
expect(lastLog.get('snapshots')[0].body).to.be.an('object')
})
cy.get('form').first().submit().then(() => {
expect(expected).to.be.true
})
})
it('snapshots after submitting', () => {
cy.$$('form:first').submit(() => {
return false
})
cy.get('form').first().submit().then(function ($form) {
const { lastLog } = this
expect(lastLog.get('snapshots').length).to.eq(2)
expect(lastLog.get('snapshots')[1].name).to.eq('after')
expect(lastLog.get('snapshots')[1].body).to.be.an('object')
})
})
it('#consoleProps', () => {
cy.$$('form:first').submit(() => {
return false
})
cy.get('form').first().submit().then(function ($form) {
const { lastLog } = this
expect(this.lastLog.invoke('consoleProps')).to.deep.eq({
Command: 'submit',
'Applied To': lastLog.get('$el').get(0),
Elements: 1,
})
})
})
})
})
})