/
within.cy.js
350 lines (275 loc) · 9.48 KB
/
within.cy.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
const { assertLogLength } = require('../../../support/utils')
const { _ } = Cypress
describe('src/cy/commands/querying/within', () => {
context('#within', () => {
beforeEach(() => {
cy.visit('/fixtures/dom.html')
})
it('invokes callback function with runnable.ctx', function () {
const ctx = this
cy.get('div:first').within(function () {
expect(ctx === this).to.be.true
})
})
it('scopes additional GET finders to the subject', () => {
const input = cy.$$('#by-name input:first')
cy.get('#by-name').within(() => {
cy.get('input:first').then(($input) => {
expect($input.get(0)).to.eq(input.get(0))
})
})
})
it('scopes additional CONTAINS finders to the subject', () => {
const span = cy.$$('#nested-div span:contains(foo)')
cy.contains('foo').then(($span) => {
expect($span.get(0)).not.to.eq(span.get(0))
})
cy.get('#nested-div').within(() => {
cy.contains('foo').then(($span) => {
expect($span.get(0)).to.eq(span.get(0))
})
})
})
it('does not change the subject', () => {
const form = cy.$$('#by-name')
cy.get('#by-name').within(() => {}).then(($form) => {
expect($form.get(0)).to.eq(form.get(0))
})
})
it('can be chained off an alias', () => {
const form = cy.$$('#by-name')
cy.get('#by-name').as('nameForm')
.within(() => {})
.then(($form) => {
expect($form.get(0)).to.eq(form.get(0))
})
cy.get('#by-name').as('nameForm')
.within(() => {
cy.get('input').should('be.visible')
})
cy.get('@nameForm').should('be.visible')
})
it('can call child commands after within on the same subject', () => {
const input = cy.$$('#by-name input:first')
cy.get('#by-name').within(() => {}).find('input:first').then(($input) => {
expect($input.get(0)).to.eq(input.get(0))
})
})
it('supports nested withins', () => {
const span = cy.$$('#button-text button span')
cy.get('#button-text').within(() => {
cy.get('button').within(() => {
cy.get('span').then(($span) => {
expect($span.get(0)).to.eq(span.get(0))
})
})
})
})
it('supports complicated nested withins', () => {
const span1 = cy.$$('#button-text a span')
const span2 = cy.$$('#button-text button span')
cy.get('#button-text').within(() => {
cy.get('a').within(() => {
cy.get('span').then(($span) => {
expect($span.get(0)).to.eq(span1.get(0))
})
})
cy.get('button').within(() => {
cy.get('span').then(($span) => {
expect($span.get(0)).to.eq(span2.get(0))
})
})
})
})
it('clears withinSubjectChain after within is over', () => {
const input = cy.$$('input:first')
const span = cy.$$('#button-text button span')
cy.get('#button-text').within(() => {
cy.get('button').within(() => {
cy.get('span').then(($span) => {
expect($span.get(0)).to.eq(span.get(0))
})
})
})
cy.get('input:first').then(($input) => {
expect($input.get(0)).to.eq(input.get(0))
})
})
it('removes command:start listeners after within is over', () => {
cy.get('#button-text').within(() => {
cy.get('button').within(() => {
cy.get('span')
})
})
cy.then(() => {
expect(cy._events).not.to.have.property('command:start')
})
})
it('clears withinSubjectChain even if next is null', (done) => {
const span = cy.$$('#button-text button span')
// should be defined here because next would have been
// null and withinSubjectChain would not have been cleared
cy.once('command:queue:before:end', () => {
expect(cy.state('withinSubjectChain')).not.to.be.undefined
})
cy.once('command:queue:end', () => {
expect(cy.state('withinSubjectChain')).to.be.null
done()
})
cy.get('#button-text').within(() => {
cy.get('button span').then(($span) => {
expect($span.get(0)).to.eq(span.get(0))
})
})
})
// https://github.com/cypress-io/cypress/issues/4757
it('subject is restored after within() call', () => {
cy.get('#wrapper').within(() => {
cy.get('#upper').should('contain.text', 'New York')
})
.should('have.id', 'wrapper')
})
// https://github.com/cypress-io/cypress/issues/5183
it('contains() works after within() call', () => {
cy.get(`#wrapper`).within(() => cy.get(`#upper`)).should(`contain.text`, `New York`)
cy.contains(`button`, `button`).should(`exist`)
})
it('re-queries if withinSubject is detached from dom', () => {
cy.on('command:retry', _.after(2, (options) => {
cy.$$('#wrapper').replaceWith('<div id="wrapper"><div id="upper">Newer York</div></div>')
}))
cy.get('#wrapper').within(() => {
cy.get(`#upper`).should(`contain.text`, `Newer York`)
})
})
describe('.log', () => {
beforeEach(function () {
this.logs = []
cy.on('log:added', (attrs, log) => {
if (attrs.name === 'within') {
this.lastLog = log
this.logs.push(log)
}
})
return null
})
it('can silence logging', () => {
cy.get('div:first').within({ log: false }, () => {}).then(function () {
assertLogLength(this.logs, 0)
})
})
it('logs immediately before resolving', (done) => {
const div = cy.$$('div:first')
cy.on('log:added', (attrs, log) => {
if (log.get('name') === 'within') {
expect(log.get('state')).to.eq('pending')
expect(log.get('message')).to.eq('')
expect(log.get('$el').get(0)).to.eq(div.get(0))
done()
}
})
cy.get('div:first').within(() => {})
})
it('snapshots after clicking', () => {
cy.get('div:first').within(() => {})
.then(function () {
const { lastLog } = this
expect(lastLog.get('snapshots').length).to.eq(1)
expect(lastLog.get('snapshots')[0]).to.be.an('object')
})
})
it('provides additional information in console prop', () => {
cy.get('div').first().within(() => {})
cy.then(function () {
const { lastLog } = this
const consoleProps = lastLog.get('consoleProps')()
expect(consoleProps).to.be.an('object')
expect(consoleProps.Command).to.eq('within')
expect(consoleProps.Yielded).to.not.be.null
})
})
})
describe('errors', {
defaultCommandTimeout: 100,
}, () => {
beforeEach(function () {
this.logs = []
cy.on('log:added', (attrs, log) => {
this.lastLog = log
this.logs.push(log)
})
return null
})
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()
})
cy.noop().within(() => {})
})
it('throws when not a DOM subject', (done) => {
cy.on('fail', (err) => {
done()
})
cy.noop().within(() => {})
})
_.each(['', [], {}, 1, null, undefined], (value) => {
it(`throws if passed anything other than a function, such as: ${value}`, (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('`cy.within()` must be called with a function.')
expect(err.docsUrl).to.eq('https://on.cypress.io/within')
done()
})
cy.get('body').within(value)
})
})
it('throws when subject is not in the document', (done) => {
cy.on('command:end', () => {
cy.$$('#list').remove()
})
cy.on('fail', (err) => {
expect(err.message).to.include('`cy.within()` failed because it requires a DOM element')
done()
})
cy.get('#list').within(() => {})
})
it('throws when given multiple subjects', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('`cy.within()` can only be called on a single element. Your subject contained 9 elements.')
done()
})
cy.get('ul').within(() => {})
})
})
})
context('#within - shadow dom', () => {
beforeEach(() => {
cy.visit('/fixtures/shadow-dom.html')
})
it('finds element within shadow dom with includeShadowDom option', () => {
cy.get('#parent-of-shadow-container-0').within(() => {
cy
.get('p', { includeShadowDom: true })
.should('have.length', 1)
.should('have.text', 'Shadow Content 3')
})
})
it('when within subject is shadow root, finds element without needing includeShadowDom option', () => {
cy.get('#shadow-element-1').shadow().within(() => {
cy
.get('p')
.should('have.length', 1)
.should('have.text', 'Shadow Content 1')
})
})
it('when within subject is already in shadow dom, finds element without needing includeShadowDom option', () => {
cy.get('.shadow-8-nested-1', { includeShadowDom: true }).within(() => {
cy
.get('.shadow-8-nested-5')
.should('have.text', '8')
})
})
})
})