forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrapping_your_app_spec.js
107 lines (97 loc) · 3.15 KB
/
bootstrapping_your_app_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
// This recipe shows you how to modify bootstrapped data before
// it's sent to your application during startup.
//
// By modifying bootstrapped data you could force your application
// to use different configuration only when running under Cypress.
//
// Let's look at some different strategies based on how our app works
describe('Bootstrapping App Test Data', function () {
// bootstrap.html is an html file generated by our node server
// with the seed data inlined
context('solution #1: bootstrap.html', function () {
it('works by default using development bootstrap data', function () {
// Let's test that the normal
// way of development data seeding works.
cy.visit('/bootstrap.html')
cy.get('pre')
.invoke('text')
.should(
'eq',
JSON.stringify({
env: 'development',
api: 'https://api.company.com',
})
)
})
it('can modify window._bootstrappedData', function () {
// in this solution we use cy.visit({onBeforeLoad: ...})
// to modify the window._bootstrappedData global so that
// it's passed into our App.start() method
const data = {
env: 'test',
api: 'https://test-api.company.com',
}
cy.visit('/bootstrap.html', {
onBeforeLoad: (win) => {
win._bootstrappedData = data
},
})
cy.get('pre')
.invoke('text')
.should('eq', JSON.stringify(data))
})
})
// xhr.html is an html file generated by our node server
// that makes an XHR request for the initial seed data
context('solution #2: xhr.html', function () {
it('works by default using development bootstrap data', function () {
// Let's test that the normal
// way of development data seeding works.
cy.visit('/xhr.html')
cy.get('pre')
.invoke('text')
.should(
'eq',
JSON.stringify({
env: 'development',
api: 'https://api.company.com',
})
)
})
it('can modify the seed data by stubbing the XHR', function () {
// store our test bootstrap data as a fixture
// data in /fixtures/bootstrap.json
cy.fixture('bootstrap.json').then((data) => {
cy.server()
cy.route('GET', '/data.json', data)
cy.visit('/xhr.html')
cy.get('pre')
.invoke('text')
.should('eq', JSON.stringify(data))
})
})
it('waits for the XHR before asserting', function () {
// In our local solution above the XHR happens
// so fast that we don't need to explictly wait on it
//
// However these two previous solutions should both probably wait
// on the XHR using an aliased route
//
// This insulates you from making assertions prior to the data
// coming in
cy.fixture('bootstrap.json').then((data) => {
cy.server()
cy.route({
delay: 2000, // simulate a slow XHR request
url: '/data.json',
response: data,
}).as('getData')
cy.visit('/xhr.html')
cy.wait('@getData')
cy.get('pre')
.invoke('text')
.should('eq', JSON.stringify(data))
})
})
})
})