This repository was archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathanswer-cypress-on.js
65 lines (54 loc) · 1.64 KB
/
answer-cypress-on.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
/// <reference types="cypress" />
// variable that will hold cy.stub created in the test
let stub
// use Cypress.on in its own spec to avoid every test running it
// https://on.cypress.io/catalog-of-events
Cypress.on('window:before:load', (win) => {
// if the test has prepared a stub
if (stub) {
// the stub function is ready
// always returns it when the application
// is trying to use "window.track"
Object.defineProperty(win, 'track', {
get() {
return stub
}
})
}
})
describe('Cypress.on', () => {
beforeEach(() => {
// let the test create the stub if it needs it
stub = null
})
it('works', () => {
// note: cy.stub returns a function
stub = cy.stub().as('track')
cy.visit('/')
// make sure the page called the "window.track" with expected arguments
cy.get('@track').should('have.been.calledOnceWith', 'window.load')
cy.reload()
cy.reload()
cy.get('@track')
.should('have.been.calledThrice')
// confirm every call was with "window.load" argument
.invoke('getCalls')
.should((calls) => {
calls.forEach((trackCall, k) => {
expect(trackCall.args, `call ${k + 1}`).to.deep.equal(['window.load'])
})
})
})
it('works with reset', () => {
stub = cy.stub().as('track')
cy.visit('/')
// make sure the page called the "window.track" with expected arguments
cy.get('@track')
.should('have.been.calledOnceWith', 'window.load')
// cy.stub().reset() brings the counts back to 0
.invoke('reset')
cy.reload()
cy.reload()
cy.get('@track').should('have.been.calledTwice')
})
})