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.js
110 lines (90 loc) · 2.87 KB
/
answer.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
/// <reference types="cypress" />
import { enterTodo, resetData, removeTodo } from '../../support/utils'
describe('Stubbing window.track', () => {
beforeEach(resetData)
it('works on click', () => {
cy.visit('/').then((win) => {
cy.stub(win, 'track').as('track')
})
enterTodo('write code')
cy.get('@track').should(
'have.been.calledOnceWithExactly',
'todo.add',
'write code'
)
})
it('tracks item delete', () => {
cy.visit('/').then((win) => {
cy.stub(win, 'track').as('track')
})
enterTodo('write code')
removeTodo('write code')
cy.get('@track').should('have.been.calledWith', 'todo.remove', 'write code')
})
it('resets the count', () => {
cy.visit('/').then((win) => {
cy.stub(win, 'track').as('track')
})
enterTodo('write code')
cy.get('@track').should('be.calledOnce')
enterTodo('write tests')
cy.get('@track')
.should('be.calledTwice')
// reset the stub
.invoke('reset')
cy.get('@track').should('not.be.called')
enterTodo('control the state')
cy.get('@track').should('be.calledOnce')
})
it('stops working if window changes', () => {
cy.visit('/').then((win) => {
cy.stub(win, 'track').as('track')
})
enterTodo('write code')
cy.get('@track').should('be.calledOnce')
cy.reload()
enterTodo('write tests')
/* eslint-disable-next-line cypress/no-unnecessary-waiting */
cy.wait(500) // wait just in case the call happens late
// note that our stub was still called once
// meaning the second todo was never counted
cy.get('@track').should('be.calledOnce')
})
it('adds stub after reload', () => {
const trackStub = cy.stub().as('track')
cy.visit('/').then((win) => {
cy.stub(win, 'track').callsFake(trackStub)
})
enterTodo('write code')
cy.get('@track').should('be.calledOnce')
cy.reload().then((win) => {
cy.stub(win, 'track').callsFake(trackStub)
})
enterTodo('write tests')
// our stub is called twice: the second time
// from the new window object after cy.reload()
cy.get('@track').should('be.calledTwice')
})
it('works on load', () => {
cy.visit('/', {
onBeforeLoad(win) {
// there is no "window.track" yet,
// thus we cannot stub just yet
let track // the real track when set by the app
let trackStub // our stub around the real track
Object.defineProperty(win, 'track', {
get() {
return trackStub
},
set(fn) {
track = fn
// give the created stub an alias so we can retrieve it later
trackStub = cy.stub().callsFake(track).as('track')
}
})
}
})
// make sure the page called the "window.track" with expected arguments
cy.get('@track').should('have.been.calledOnceWith', 'window.load')
})
})