forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-spec.js
128 lines (102 loc) · 3.85 KB
/
count-spec.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
/// <reference types="Cypress" />
describe('intercept', () => {
context('counting network calls', () => {
it('makes 3 calls', () => {
let count = 0
cy.intercept('/favorite-fruits', () => {
// we are not changing the request or response here
// just counting the matched calls
count += 1
})
cy.visit('/fruits.html')
// ensure the fruits are loaded
cy.get('.favorite-fruits li').should('have.length', 5)
cy.reload()
cy.get('.favorite-fruits li').should('have.length', 5)
cy.reload()
cy.get('.favorite-fruits li').should('have.length', 5)
.then(() => {
// by now the count should have been updated
expect(count, 'network calls to fetch fruits').to.equal(3)
})
})
it('stubs 3 calls', () => {
let count = 0
cy.intercept('/favorite-fruits', (req) => {
count += 1
req.reply({ fixture: 'fruits.json' })
})
cy.visit('/fruits.html')
// ensure the fruits are loaded
cy.get('.favorite-fruits li').should('have.length', 3)
cy.reload()
cy.get('.favorite-fruits li').should('have.length', 3)
cy.reload()
cy.get('.favorite-fruits li').should('have.length', 3)
.then(() => {
// by now the count should have been updated
expect(count, 'network calls to fetch fruits').to.equal(3)
})
})
})
context('counting network calls (spy)', () => {
it('fetches every 30 seconds', () => {
cy.clock()
// create a cy.spy() that will be called
// by the intercept. we later can use that spy
// to check the number of times it was called
cy.intercept('/favorite-fruits', cy.spy().as('reqForFruits'))
cy.visit('/fruits.html')
cy.get('@reqForFruits').should('have.been.calledOnce')
// application fetches fruits again 30 seconds later
cy.tick(30000)
cy.get('@reqForFruits').should('have.been.calledTwice')
cy.tick(30000)
cy.get('@reqForFruits').should('have.been.calledThrice')
cy.tick(30000)
// after that we should retrieve the call count property
cy.get('@reqForFruits').its('callCount').should('equal', 4)
})
})
context('counting network calls (stub)', () => {
it('fetches every 30 seconds', () => {
cy.clock()
// prepare a stub function we can call to "count" intercepts
const replyStub = cy.stub().as('stubFruits')
// cy.intercept replying with a pre-defined response
// expects the route handler in the form
// (req) => req.reply(...)
cy.intercept('/favorite-fruits', (req) => {
replyStub(req) // remember the request for example
req.reply({ fixture: 'fruits.json' })
})
cy.visit('/fruits.html')
cy.get('@stubFruits').should('have.been.calledOnce')
// application fetches fruits again 30 seconds later
cy.tick(30000)
cy.get('@stubFruits').should('have.been.calledTwice')
cy.tick(30000)
cy.get('@stubFruits').should('have.been.calledThrice')
cy.tick(30000)
// after that we should retrieve the call count property
cy.get('@stubFruits').its('callCount').should('equal', 4)
})
it('fetches every 30 seconds (stub.callsFake)', () => {
cy.clock()
cy.intercept('/favorite-fruits',
cy.stub()
.callsFake((req) => req.reply({ fixture: 'fruits.json' }))
.as('stubFruits'))
cy.visit('/fruits.html')
cy.get('@stubFruits').should('have.been.calledOnce')
// application fetches fruits again 30 seconds later
cy.tick(30000)
cy.get('@stubFruits').should('have.been.calledTwice')
cy.tick(30000)
cy.get('@stubFruits').should('have.been.calledThrice')
cy.tick(30000)
// after that we should retrieve the call count property
cy.get('@stubFruits').its('callCount').should('equal', 4)
})
})
})