forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
offline-spec.js
177 lines (147 loc) · 4.91 KB
/
offline-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
/// <reference types="cypress" />
/* global window */
// use window.navigator.onLine property to determine
// if the browser is offline or online
// https://caniuse.com/online-status
const assertOnline = () => {
return cy.wrap(window).its('navigator.onLine').should('be.true')
}
const assertOffline = () => {
return cy.wrap(window).its('navigator.onLine').should('be.false')
}
const goOffline = () => {
cy.log('**go offline**')
.then(() => {
return Cypress.automation('remote:debugger:protocol',
{
command: 'Network.enable',
})
})
.then(() => {
return Cypress.automation('remote:debugger:protocol',
{
command: 'Network.emulateNetworkConditions',
params: {
offline: true,
latency: -1,
downloadThroughput: -1,
uploadThroughput: -1,
},
})
})
}
const goOnline = () => {
// disable offline mode, otherwise we will break our tests :)
cy.log('**go online**')
.then(() => {
// https://chromedevtools.github.io/devtools-protocol/1-3/Network/#method-emulateNetworkConditions
return Cypress.automation('remote:debugger:protocol',
{
command: 'Network.emulateNetworkConditions',
params: {
offline: false,
latency: -1,
downloadThroughput: -1,
uploadThroughput: -1,
},
})
})
.then(() => {
return Cypress.automation('remote:debugger:protocol',
{
command: 'Network.disable',
})
})
}
// since we are using Chrome debugger protocol API
// we should only run these tests when NOT in Firefox browser
// see https://on.cypress.io/configuration#Test-Configuration
describe('offline mode', { browser: '!firefox' }, () => {
// the application is making request to this url
const url = 'https://jsonplaceholder.cypress.io/users'
// make sure we get back online, even if a test fails
// otherwise the Cypress can lose the browser connection
beforeEach(goOnline)
afterEach(goOnline)
it('shows network status', () => {
cy.visit('/')
cy.contains('#network-status', 'online')
.wait(1000) // for demo purpose
goOffline()
cy.contains('#network-status', 'offline')
.wait(1000) // for demo purpose
})
it('shows error if we stub the network call', () => {
assertOnline()
cy.visit('/')
cy.intercept(`${url}*`, { forceNetworkError: true }).as('users')
cy.get('#load-users').click()
cy.contains('#users', 'Problem fetching users Failed to fetch')
// cannot wait for the intercept that forces network error
// https://github.com/cypress-io/cypress/issues/9062
// cy.wait('@users', { timeout: 1000 }) // the network call happens
})
it('shows error trying to fetch users in offline mode', () => {
cy.visit('/')
assertOnline()
// since this call returns a promise, must tell Cypress to wait
// for it to be resolved
goOffline()
assertOffline()
cy.get('#load-users').click()
cy.contains('#users', 'Problem fetching users Failed to fetch')
// now let's go back online and fetch the users
goOnline()
assertOnline()
cy.get('#load-users').click()
cy.get('.user').should('have.length', 3)
})
it('makes fetch request when offline', () => {
cy.visit('/')
goOffline()
assertOffline()
// let's spy on the "fetch" method the app calls
cy.window().then((w) => cy.spy(w, 'fetch').withArgs(`${url}?_limit=3`).as('fetchUsers'))
cy.get('#load-users').click()
cy.get('@fetchUsers').should('have.been.calledOnce')
// now let's go back online and fetch the users
goOnline()
assertOnline()
cy.get('#load-users').click()
cy.get('.user').should('have.length', 3)
cy.get('@fetchUsers').should('have.been.calledTwice')
})
it('does not reach the outside network when offline', () => {
cy.visit('/')
// before we go offline we have to set up network intercepts
// since they need to be communicated outside the browser
// and lets keep track the number of network calls made
let callCount = 0
cy.intercept(`${url}*`, () => {
callCount += 1
}).as('users')
goOffline()
assertOffline()
cy.get('#load-users').click()
cy.contains('#users', 'Problem fetching users Failed to fetch')
// the cy.intercept network call does NOT happen
// because the browser does not fire it
// and thus our network proxy does not see it
cy.then(() => {
expect(callCount, 'no network calls made').to.equal(0)
})
// now let's go back online and fetch the users
goOnline()
assertOnline()
cy.get('#load-users').click()
// we can retry the assertion to know when the network call has happened
// using .should callback function with an assertion inside
.should(() => {
expect(callCount, 'single network call').to.equal(1)
})
cy.wait('@users')
.its('response.body')
.should('have.length', 3)
cy.get('.user').should('have.length', 3)
})
})