-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
events.cy.ts
205 lines (159 loc) · 5.32 KB
/
events.cy.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
describe('src/cypress', () => {
describe('events', () => {
it('fail event', (done) => {
cy.on('fail', (err, runnable) => {
expect(err.message).to.equal('foo')
expect(runnable).to.equal(Cypress.state('runnable'))
done()
})
throw new Error('foo')
})
it('viewport:changed event', () => {
let called = false
cy.on('viewport:changed', (viewport) => {
expect(viewport).to.deep.equal({ viewportWidth: 100, viewportHeight: 100 })
called = true
})
cy.viewport(100, 100).then(() => {
expect(called).to.be.true
})
})
it('scrolled event', (done) => {
cy.viewport(100, 100)
Cypress.$('<button>button</button>')
.attr('id', 'button')
.css({
position: 'absolute',
left: '0px',
top: '50px',
})
.appendTo(cy.$$('body'))
cy.on('scrolled', ($el, type) => {
expect($el[0]).to.eq(Cypress.$('#button')[0])
expect(type).to.eq('element')
done()
})
cy.get('#button').trigger('mousedown')
})
context('command events', () => {
it('command:enqueued event', () => {
let called = false
const handler = (command) => {
expect(command.name).to.eq('log')
called = true
cy.off('command:enqueued', handler)
}
cy.on('command:enqueued', handler)
cy.log('foo').then(() => {
expect(called).to.be.true
})
})
it('command:start event', () => {
let called = false
const handler = (command) => {
expect(command.attributes.name).to.eq('log')
called = true
cy.off('command:start', handler)
}
cy.on('command:start', handler)
cy.log('foo').then(() => {
expect(called).to.be.true
})
})
it('command:end event', () => {
let called = false
const handler = (command) => {
expect(command.attributes.name).to.eq('log')
called = true
cy.off('command:end', handler)
}
cy.on('command:end', handler)
cy.log('foo').then(() => {
expect(called).to.be.true
})
})
it('command:retry event', (done) => {
const handler = (options) => {
expect(options._retries).to.equal(1)
expect(options.error.message).to.equal('Expected to find element: `#foo`, but never found it.')
done()
}
cy.on('command:retry', handler)
cy.get('#foo')
})
})
context('log events', () => {
it('log:added event', () => {
const attrs: any[] = []
const logs: any[] = []
const handler = (attr, log) => {
attrs.push(attr)
logs.push(log)
}
cy.on('log:added', handler)
Cypress.log({ name: 'log', message: `foo` })
cy.log('foo').then(() => {
expect(attrs[0].name).to.eq('log')
expect(logs[0].attributes.name).to.eq('log')
})
})
it('log:changed event', (done) => {
const handler = (attr, log) => {
cy.off('log:changed', handler)
expect(attr.name).to.eq('bar')
expect(log.attributes.name).to.eq('bar')
done()
}
const log = Cypress.log({ message: `foo` })
cy.on('log:changed', handler)
log?.set('name', 'bar')
})
})
// these tests need to be run together since they are testing lifecycle events
context('lifecycle (test:before/after:run) events', () => {
let afterRunnable
let beforeRunnable
let beforeAsyncRunnable
let expectedAfterRunnable
const beforeHandler = (test, runnable) => {
expect(test.title).to.eq('test 2')
beforeRunnable = runnable
Cypress.off('test:before:run', beforeHandler)
}
const beforeAsyncHandler = (test, runnable) => {
expect(test.title).to.eq('test 2')
beforeAsyncRunnable = runnable
Cypress.off('test:before:run:async', beforeAsyncHandler)
}
const afterHandler = (test, runnable) => {
expect(test.title).to.eq('test 1')
afterRunnable = runnable
Cypress.off('test:after:run', afterHandler)
}
before(() => {
Cypress.on('test:before:run', beforeHandler)
Cypress.on('test:before:run:async', beforeAsyncHandler)
Cypress.on('test:after:run', afterHandler)
})
after(() => {
Cypress.off('test:before:run', beforeHandler)
Cypress.off('test:before:run:async', beforeAsyncHandler)
Cypress.off('test:after:run', afterHandler)
})
it('test 1', () => {
// this is the runnable that we expect to be passed to the test:after:run event
// and it will be verified in the next test since we need to wait for the test to finish
expectedAfterRunnable = Cypress.state('runnable')
})
it('test 2', () => {
// the before runnables should be from this test
const runnable = Cypress.state('runnable')
// use === to avoid the circular references
expect(beforeAsyncRunnable === runnable).to.be.true
expect(beforeRunnable === runnable).to.be.true
// the after runnable should be from the previous test
expect(afterRunnable).to.deep.equal(expectedAfterRunnable)
})
})
})
})