forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
focus.ts
182 lines (147 loc) · 4.87 KB
/
focus.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
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
import _ from 'lodash'
import $dom from '../../../dom'
import $utils from '../../../cypress/utils'
import $errUtils from '../../../cypress/error_utils'
import $elements from '../../../dom/elements'
export default (Commands, Cypress, cy) => {
return Commands.addAll({ prevSubject: ['element', 'window'] }, {
// TODO: any -> Partial<Cypress.Loggable & Cypress.Timeoutable>
focus (subject, options: any = {}) {
const userOptions = options
// we should throw errors by default!
// but allow them to be silenced
options = _.defaults({}, userOptions, {
$el: subject,
error: true,
log: true,
verify: true,
})
const isWin = $dom.isWindow(options.$el)
if (isWin) {
// get this into a jquery object
options.$el = $dom.wrap(options.$el)
}
if (options.log) {
options._log = Cypress.log({
$el: options.$el,
timeout: options.timeout,
consoleProps () {
return { 'Applied To': $dom.getElements(options.$el) }
},
})
}
const el = options.$el.get(0)
// the body is not really focusable, but it
// can have focus on initial page load.
// this is instead a noop.
// TODO: throw on body instead (breaking change)
const isBody = $dom.isJquery(options.$el) &&
$elements.isElement(options.$el.get(0)) &&
$elements.isBody(options.$el.get(0))
// http://www.w3.org/$R/html5/editing.html#specially-focusable
// ensure there is only 1 dom element in the subject
// make sure its allowed to be focusable
if (!(isWin || isBody || $dom.isFocusable(options.$el))) {
if (options.error === false) {
return
}
const node = $dom.stringify(options.$el)
$errUtils.throwErrByPath('focus.invalid_element', {
onFail: options._log,
args: { node },
})
}
if (options.$el.length && options.$el.length > 1) {
if (options.error === false) {
return
}
$errUtils.throwErrByPath('focus.multiple_elements', {
onFail: options._log,
args: { num: options.$el.length },
})
}
cy.fireFocus(el)
const verifyAssertions = () => {
return cy.verifyUpcomingAssertions(options.$el, options, {
onRetry: verifyAssertions,
})
}
return verifyAssertions()
},
// TODO: any -> Partial<Cypress.BlurOptions>
blur (subject, options: any = {}) {
const userOptions = options
// we should throw errors by default!
// but allow them to be silenced
options = _.defaults({}, userOptions, {
$el: subject,
$focused: cy.getFocused(),
error: true,
log: true,
verify: true,
force: false,
})
const { $focused } = options
const isWin = $dom.isWindow(options.$el)
if (isWin) {
// get this into a jquery object
options.$el = $dom.wrap(options.$el)
}
const isBody = options.$el.is('body')
if (options.log) {
// figure out the options which actually change the behavior of clicks
const deltaOptions = $utils.filterOutOptions(options)
options._log = Cypress.log({
$el: options.$el,
message: deltaOptions,
timeout: options.timeout,
consoleProps () {
return { 'Applied To': $dom.getElements(options.$el) }
},
})
}
if (options.$el.length && options.$el.length > 1) {
if (options.error === false) {
return
}
$errUtils.throwErrByPath('blur.multiple_elements', {
onFail: options._log,
args: { num: options.$el.length },
})
}
// if we haven't forced the blur, and we don't currently
// have a focused element OR we aren't the window or body then error
if (options.force !== true && !$focused && !isWin && !isBody) {
if (options.error === false) {
return
}
$errUtils.throwErrByPath('blur.no_focused_element', { onFail: options._log })
}
// if we're currently window dont check for the wrong
// focused element because window will not show up
// as $focused
if (options.force !== true &&
!isWin &&
!isBody &&
options.$el.get(0) !== $focused.get(0)
) {
if (options.error === false) {
return
}
const node = $dom.stringify($focused)
$errUtils.throwErrByPath('blur.wrong_focused_element', {
onFail: options._log,
args: { node },
})
}
const el = options.$el.get(0)
cy.fireBlur(el)
const verifyAssertions = () => {
return cy.verifyUpcomingAssertions(options.$el, options, {
onRetry: verifyAssertions,
})
}
return verifyAssertions()
},
})
}