Skip to content

Commit 02cc29a

Browse files
chore(driver): move cy.focused and cy.root into their own files (#20054)
1 parent 4b50f9e commit 02cc29a

File tree

7 files changed

+450
-417
lines changed

7 files changed

+450
-417
lines changed
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
const { assertLogLength } = require('../../../support/utils')
2+
3+
const { _ } = Cypress
4+
5+
describe('src/cy/commands/querying', () => {
6+
beforeEach(() => {
7+
cy.visit('/fixtures/dom.html')
8+
})
9+
10+
context('#focused', () => {
11+
it('returns the activeElement', () => {
12+
const $button = cy.$$('#button')
13+
14+
$button.get(0).focus()
15+
16+
expect(cy.state('document').activeElement).to.eq($button.get(0))
17+
18+
cy.focused().then(($focused) => {
19+
expect($focused.get(0)).to.eq($button.get(0))
20+
})
21+
})
22+
23+
it('returns null if no activeElement', () => {
24+
const $button = cy.$$('#button')
25+
26+
$button.get(0).focus()
27+
$button.get(0).blur()
28+
29+
cy.focused().should('not.exist').then(($focused) => {
30+
expect($focused).to.be.null
31+
})
32+
})
33+
34+
describe('assertion verification', () => {
35+
beforeEach(function () {
36+
cy.on('log:added', (attrs, log) => {
37+
if (log.get('name') === 'assert') {
38+
this.lastLog = log
39+
}
40+
})
41+
42+
return null
43+
})
44+
45+
it('eventually passes the assertion', () => {
46+
cy.on('command:retry', _.after(2, () => {
47+
cy.$$(':text:first').addClass('focused').focus()
48+
}))
49+
50+
cy.focused().should('have.class', 'focused').then(function () {
51+
const { lastLog } = this
52+
53+
expect(lastLog.get('name')).to.eq('assert')
54+
expect(lastLog.get('state')).to.eq('passed')
55+
56+
expect(lastLog.get('ended')).to.be.true
57+
})
58+
})
59+
60+
// https://github.com/cypress-io/cypress/issues/409
61+
it('retries on an elements value', () => {
62+
const $input = cy.$$('input:first')
63+
64+
cy.on('command:retry', _.after(2, () => {
65+
$input.val('1234')
66+
67+
$input.get(0).focus()
68+
}))
69+
70+
cy.focused().should('have.value', '1234').then(function () {
71+
const { lastLog } = this
72+
73+
expect(lastLog.get('name')).to.eq('assert')
74+
expect(lastLog.get('state')).to.eq('passed')
75+
76+
expect(lastLog.get('ended')).to.be.true
77+
})
78+
})
79+
})
80+
81+
describe('.log', () => {
82+
beforeEach(function () {
83+
cy.$$('input:first').get(0).focus()
84+
85+
cy.on('log:added', (attrs, log) => {
86+
if (log.get('name') === 'focused') {
87+
this.lastLog = log
88+
}
89+
})
90+
91+
return null
92+
})
93+
94+
it('is a parent command', () => {
95+
cy.get('body').focused().then(function () {
96+
const { lastLog } = this
97+
98+
expect(lastLog.get('type')).to.eq('parent')
99+
})
100+
})
101+
102+
it('ends immediately', () => {
103+
cy.focused().then(function () {
104+
const { lastLog } = this
105+
106+
expect(lastLog.get('ended')).to.be.true
107+
108+
expect(lastLog.get('state')).to.eq('passed')
109+
})
110+
})
111+
112+
it('snapshots immediately', () => {
113+
cy.focused().then(function () {
114+
const { lastLog } = this
115+
116+
expect(lastLog.get('snapshots').length).to.eq(1)
117+
118+
expect(lastLog.get('snapshots')[0]).to.be.an('object')
119+
})
120+
})
121+
122+
it('passes in $el', () => {
123+
cy.get('input:first').focused().then(function ($input) {
124+
const { lastLog } = this
125+
126+
expect(lastLog.get('$el')).to.eq($input)
127+
})
128+
})
129+
130+
it('#consoleProps', () => {
131+
cy.get('input:first').focused().then(function ($input) {
132+
expect(this.lastLog.invoke('consoleProps')).to.deep.eq({
133+
Command: 'focused',
134+
Yielded: $input.get(0),
135+
Elements: 1,
136+
})
137+
})
138+
})
139+
140+
it('#consoleProps with null element', () => {
141+
const button = cy.$$('#button')
142+
143+
button.get(0).focus()
144+
button.get(0).blur()
145+
146+
cy.focused().should('not.exist').then(function () {
147+
expect(this.lastLog.invoke('consoleProps')).to.deep.eq({
148+
Command: 'focused',
149+
Yielded: '--nothing--',
150+
Elements: 0,
151+
})
152+
})
153+
})
154+
})
155+
156+
describe('errors', {
157+
defaultCommandTimeout: 100,
158+
}, () => {
159+
beforeEach(function () {
160+
this.logs = []
161+
162+
cy.on('log:added', (attrs, log) => {
163+
this.lastLog = log
164+
165+
this.logs.push(log)
166+
})
167+
168+
return null
169+
})
170+
171+
it('fails waiting for the element to exist', (done) => {
172+
const button = cy.$$('#button')
173+
174+
button.get(0).focus()
175+
button.get(0).blur()
176+
177+
cy.on('fail', (err) => {
178+
expect(err.message).to.include('Expected to find element: `focused`, but never found it.')
179+
180+
done()
181+
})
182+
183+
cy.focused()
184+
})
185+
186+
it('fails waiting for the focused element not to exist', (done) => {
187+
cy.$$('input:first').focus()
188+
189+
cy.on('fail', (err) => {
190+
expect(err.message).to.include('Expected <input#input> not to exist in the DOM, but it was continuously found.')
191+
192+
done()
193+
})
194+
195+
cy.focused().should('not.exist')
196+
})
197+
198+
it('eventually fails the assertion', function (done) {
199+
cy.$$('input:first').focus()
200+
201+
cy.on('fail', (err) => {
202+
const { lastLog } = this
203+
204+
expect(err.message).to.include(lastLog.get('error').message)
205+
expect(err.message).not.to.include('undefined')
206+
expect(lastLog.get('name')).to.eq('assert')
207+
expect(lastLog.get('state')).to.eq('failed')
208+
expect(lastLog.get('error')).to.be.an.instanceof(chai.AssertionError)
209+
210+
done()
211+
})
212+
213+
cy.focused().should('have.class', 'focused')
214+
})
215+
216+
it('does not log an additional log on failure', function (done) {
217+
cy.on('fail', () => {
218+
assertLogLength(this.logs, 2)
219+
220+
done()
221+
})
222+
223+
cy.focused().should('have.class', 'focused')
224+
})
225+
})
226+
})
227+
})

0 commit comments

Comments
 (0)