forked from cypress-io/cypress
/
submit.ts
87 lines (73 loc) · 2.68 KB
/
submit.ts
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
import _ from 'lodash'
import Promise from 'bluebird'
import $dom from '../../../dom'
import $utils from '../../../cypress/utils'
import $errUtils from '../../../cypress/error_utils'
import $actionability from '../../actionability'
interface InternalSubmitOptions extends Partial<Cypress.Loggable & Cypress.Timeoutable>{
_log?: any
$el: JQuery<HTMLFormElement>
}
export default (Commands, Cypress, cy) => {
Commands.addAll({ prevSubject: 'element' }, {
submit (subject, userOptions: Partial<Cypress.Loggable & Cypress.Timeoutable> = {}) {
const options: InternalSubmitOptions = _.defaults({}, userOptions, {
log: true,
$el: subject,
})
// changing this to a promise .map() causes submit events
// to break when they need to be triggered synchronously
// like with type {enter}. either convert type to a promise
// to just create a synchronous submit function
const form = options.$el.get(0)
if (options.log) {
options._log = Cypress.log({
$el: options.$el,
timeout: options.timeout,
consoleProps () {
return {
'Applied To': $dom.getElements(options.$el),
Elements: options.$el.length,
}
},
})
options._log.snapshot('before', { next: 'after' })
}
if (!options.$el.is('form')) {
const node = $dom.stringify(options.$el)
const word = $utils.plural(options.$el, 'contains', 'is')
$errUtils.throwErrByPath('submit.not_on_form', {
onFail: options._log,
args: { node, word },
})
}
if (options.$el.length && options.$el.length > 1) {
$errUtils.throwErrByPath('submit.multiple_forms', {
onFail: options._log,
args: { num: options.$el.length },
})
}
// calling the native submit method will not actually trigger
// a submit event, so we need to dispatch this manually so
// native event listeners and jquery can bind to it
const submit = new Event('submit', { bubbles: true, cancelable: true })
const dispatched = form.dispatchEvent(submit)
// now we need to check to see if we should actually submit the form!
// dont submit the form if our dispatched event was canceled (false)
if (dispatched) {
form.submit()
}
cy.timeout($actionability.delay, true)
return Promise
.delay($actionability.delay, 'submit')
.then(() => {
const verifyAssertions = () => {
return cy.verifyUpcomingAssertions(options.$el, options, {
onRetry: verifyAssertions,
})
}
return verifyAssertions()
})
},
})
}