forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spy-on-fetch-spec.js
248 lines (206 loc) · 7.17 KB
/
spy-on-fetch-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
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
/// <reference types="Cypress" />
// https://on.cypress.io/intercept
describe('intercept', () => {
/**
* Implementation detail, returns number of times an intercept route
* was matched during the test
* @param {string} alias
*/
const getAliasCount = (alias) => {
const testRoutes = cy.state('routes')
const aliasRoute = Cypress._.find(testRoutes, { alias })
if (!aliasRoute) {
return
}
return Cypress._.keys(aliasRoute.requests || {}).length
}
context('fruits', () => {
it('requests favorite fruits', function () {
cy.intercept('/favorite-fruits').as('fetchFruits')
cy.visit('/fruits.html')
cy.wait('@fetchFruits').its('response.body')
.then((fruits) => {
cy.get('.favorite-fruits li')
.should('have.length', fruits.length)
fruits.forEach((fruit) => {
cy.contains('.favorite-fruits li', fruit)
})
})
})
it('requests favorite fruits: wait and get', function () {
cy.intercept('/favorite-fruits').as('fetchFruits')
cy.visit('/fruits.html')
// wait on the request once
cy.wait('@fetchFruits')
// but get the latest request as many times as needed
cy.get('@fetchFruits').its('response.statusCode')
.should('eq', 200)
cy.get('@fetchFruits').its('response.body')
.should('have.length.gt', 3)
})
it('requests favorite fruits: multiple assertions', function () {
cy.intercept('/favorite-fruits').as('fetchFruits')
cy.visit('/fruits.html')
cy.wait('@fetchFruits').then((intercept) => {
expect(intercept.response.statusCode, 'status code').to.equal(200)
expect(intercept.response.body, 'at least 3 fruits').to.have.length.gt(3)
})
// also see https://www.cypress.io/blog/2019/12/23/asserting-network-calls-from-cypress-tests/
})
})
context('spying', function () {
beforeEach(function () {
cy.visit('/')
})
it('spying on 2nd domain', () => {
cy.intercept('https://jsonplaceholder.cypress.io/users*').as('users')
cy.get('#load-users').click()
cy.wait('@users').its('response.body').should('have.length', 3)
.its('0') // grab the first user from the list
.then((user) => {
expect(user).to.have.property('id')
expect(user).to.have.property('username')
expect(user).to.have.property('email')
// the user should be shown on the page
cy.contains('.user', `${user.id} - ${user.email}`).should('be.visible')
})
// there should be three users displayed on the page
cy.get('.user').should('have.length', 3)
})
it('spies on multiple requests', () => {
cy.intercept({
method: 'POST',
pathname: '/users',
}).as('postUser')
cy.get('#post-user').click()
cy.wait('@postUser').its('response.statusCode').should('equal', 201)
// post 2nd time
cy.get('#post-user').click()
cy.wait('@postUser').its('response.statusCode').should('equal', 201)
// post 3rd time
cy.get('#post-user').click()
cy.wait('@postUser').its('response.statusCode').should('equal', 201)
// post 4th time
cy.get('#post-user').click()
cy.wait('@postUser').its('response.statusCode').should('equal', 201)
})
it('checks the status code', () => {
cy.intercept('/users*').as('users')
cy.get('#load-five-users').click()
cy.wait('@users').its('response').then((response) => {
expect(response).to.include({
statusCode: 200,
statusMessage: 'OK',
})
// or check every property separately
expect(response).property('statusCode').to.equal(200)
expect(response).property('body').to.have.length(5)
})
})
// NOTE: shows how an assertion inside the intercept fails the test
it.skip('fails if XHR has wrong data', () => {
cy.intercept('/users', (req) => {
const url = new URL(req.url)
const limit = parseFloat(url.searchParams.get('_limit'))
// make the assertion fail on purpose
expect(limit, 'limit').to.equal(100)
}).as('users')
cy.get('#load-five-users').click()
cy.wait('@users')
})
})
context('spying on PUT request', () => {
it('can spy using path', () => {
cy.visit('/')
cy.intercept('PUT', '/users/1').as('updateUser')
cy.get('#put-user').click()
cy.wait('@updateUser').then((xhr) => {
expect(xhr.response.statusCode).to.equal(200)
})
})
// there is no difference how to write the URL matcher
it('can spy using URL regexp', () => {
cy.visit('/')
cy.intercept('PUT', /\/users\/\d+$/).as('updateUser')
cy.get('#put-user').click()
cy.wait('@updateUser')
.its('response')
.should('deep.include', {
statusCode: 200,
body: {
id: 1,
name: 'Joe Smith',
},
})
})
it('can spy using URL case-insensitive regexp', () => {
cy.visit('/')
cy.intercept('PUT', /users/i).as('updateUser')
cy.get('#put-user').click()
cy.wait('@updateUser')
.its('response.statusCode')
.should('equal', 200)
})
})
context('uses query', () => {
beforeEach(() => {
cy.visit('/')
})
// there are two buttons on the page
// one loads 3 users with "/users?_limit=3"
// another loads 5 users with "/users?_limit=5"
// let's spy on each request separately
it('spies using full url', () => {
cy.intercept('/users?_limit=3').as('users3')
cy.intercept('/users?_limit=5').as('users5')
cy.get('#load-users').click()
cy.wait('@users3')
cy.get('#load-five-users').click()
cy.wait('@users5')
// let's click the buttons again
cy.get('#load-users').click()
cy.wait('@users3').its('response.statusCode').should('equal', 200)
cy.get('#load-five-users').click()
cy.wait('@users5').its('response.statusCode').should('equal', 200)
})
it('confirms the number of times an intercept was called', () => {
cy.intercept('/users?_limit=3').as('users3')
cy.intercept('/users?_limit=5').as('users5')
cy.get('#load-users').click().click()
cy.wait('@users3')
// to avoid clicking too quickly, add small pauses
cy.get('#load-five-users').click()
.wait(20).click()
.wait(20).click()
.wait(20).click()
// use "cy.should" to re-run the callback function
// until it times out or the assertions pass
cy.should(() => {
// IMPLEMENTATION DETAILS
// count the number of requests matching "users3"
const users3 = getAliasCount('users3')
expect(users3, 'limit=3').to.equal(2)
const users5 = getAliasCount('users5')
expect(users5, 'limit=5').to.equal(4)
})
})
it('spies using query parameter', () => {
cy.intercept({
pathname: '/users',
query: {
_limit: '3',
},
}).as('users3')
cy.intercept({
pathname: '/users',
query: {
_limit: '5',
},
}).as('users5')
cy.get('#load-users').click()
cy.wait('@users3')
cy.get('#load-five-users').click()
cy.wait('@users5')
})
})
})