Repository navigation
Expand file tree
/
Copy pathgatsby-script-scripts-with-sources.js
More file actions
166 lines (143 loc) · 5.59 KB
/
Copy pathgatsby-script-scripts-with-sources.js
File metadata and controls
166 lines (143 loc) · 5.59 KB
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
import { script } from "../../../gatsby-script-scripts"
import { resourceRecord } from "../../../gatsby-script-records"
const page = {
target: `/gatsby-script-scripts-with-sources/`,
navigation: `/gatsby-script-navigation/`,
}
describe(`scripts with sources`, () => {
describe(`using the post-hydrate strategy`, () => {
it(`should load successfully`, () => {
cy.visit(page.target).waitForRouteChange()
cy.getRecord(script.three, `success`, true).should(`equal`, `true`)
})
it(`should load after the framework bundle has loaded`, () => {
cy.visit(page.target).waitForRouteChange()
// Assert framework is loaded before three starts loading
cy.getRecord(script.three, resourceRecord.fetchStart).then(
threeFetchStart => {
cy.getRecord(`framework`, resourceRecord.responseEnd).should(
`be.lessThan`,
threeFetchStart
)
}
)
})
it(`should call an on load callback once the script has loaded`, () => {
cy.visit(page.target).waitForRouteChange()
cy.getRecord(script.three, resourceRecord.responseEnd).then(() => {
cy.get(`[data-on-load-result=post-hydrate]`)
})
})
it(`should call an on error callback if an error occurred`, () => {
cy.visit(page.target).waitForRouteChange()
cy.get(`[data-on-error-result=post-hydrate]`)
})
})
describe(`using the idle strategy`, () => {
it(`should load successfully`, () => {
cy.visit(page.target).waitForRouteChange()
cy.getRecord(script.marked, `success`, true).should(`equal`, `true`)
})
it(`should load after other strategies`, () => {
cy.visit(page.target).waitForRouteChange()
cy.getRecord(script.marked, resourceRecord.fetchStart).then(
markedFetchStart => {
cy.getRecord(script.three, resourceRecord.fetchStart).should(
`be.lessThan`,
markedFetchStart
)
}
)
})
it(`should call an on load callback once the script has loaded`, () => {
cy.visit(page.target).waitForRouteChange()
cy.getRecord(script.marked, resourceRecord.responseEnd).then(() => {
cy.get(`[data-on-load-result=idle]`)
})
})
it(`should call an on error callback if an error occurred`, () => {
cy.visit(page.target).waitForRouteChange()
cy.get(`[data-on-error-result=idle]`)
})
})
describe(`when navigation occurs`, () => {
it(`should load only once on initial page load`, () => {
cy.visit(page.target).waitForRouteChange()
cy.get(`table[id=script-resource-records] tbody`)
.children()
.should(`have.length`, 5)
cy.getRecord(script.three, `strategy`, true).should(
`equal`,
`post-hydrate`
)
cy.getRecord(script.marked, `strategy`, true).should(`equal`, `idle`)
})
it(`should load only once after the page is refreshed`, () => {
cy.visit(page.target).waitForRouteChange()
cy.reload()
cy.get(`table[id=script-resource-records] tbody`)
.children()
.should(`have.length`, 5)
cy.getRecord(script.three, `strategy`, true).should(
`equal`,
`post-hydrate`
)
cy.getRecord(script.marked, `strategy`, true).should(`equal`, `idle`)
})
it(`should load only once after anchor link navigation`, () => {
cy.visit(page.target).waitForRouteChange()
cy.get(`a[id=anchor-link-back-to-index]`).click()
cy.url().should(`contain`, page.navigation)
cy.get(`a[href="${page.target}"][id=anchor-link]`).click()
cy.get(`table[id=script-resource-records] tbody`)
.children()
.should(`have.length`, 5)
cy.getRecord(script.three, `strategy`, true).should(
`equal`,
`post-hydrate`
)
cy.getRecord(script.marked, `strategy`, true).should(`equal`, `idle`)
})
it(`should load only once if the page is revisited via browser back/forward buttons after anchor link navigation`, () => {
cy.visit(page.navigation).waitForRouteChange()
cy.get(`a[href="${page.target}"][id=anchor-link]`).click()
cy.go(`back`).waitForRouteChange()
cy.go(`forward`).waitForRouteChange()
cy.get(`table[id=script-resource-records] tbody`)
.children()
.should(`have.length`, 5)
cy.getRecord(script.three, `strategy`, true).should(
`equal`,
`post-hydrate`
)
cy.getRecord(script.marked, `strategy`, true).should(`equal`, `idle`)
})
it(`should load only once after Gatsby link navigation`, () => {
cy.visit(page.target).waitForRouteChange()
cy.get(`a[id=gatsby-link-back-to-index]`).click()
cy.get(`a[href="${page.target}"][id=gatsby-link]`).click()
cy.get(`table[id=script-resource-records] tbody`)
.children()
.should(`have.length`, 5)
cy.getRecord(script.three, `strategy`, true).should(
`equal`,
`post-hydrate`
)
cy.getRecord(script.marked, `strategy`, true).should(`equal`, `idle`)
})
it(`should load only once if the page is revisited via browser back/forward buttons after Gatsby link navigation`, () => {
cy.visit(page.navigation).waitForRouteChange()
cy.get(`a[href="${page.target}"][id=gatsby-link]`).click()
cy.go(`back`).waitForRouteChange()
cy.go(`forward`).waitForRouteChange()
cy.get(`table[id=script-resource-records] tbody`)
.children()
.should(`have.length`, 5)
cy.getRecord(script.three, `strategy`, true).should(
`equal`,
`post-hydrate`
)
cy.getRecord(script.marked, `strategy`, true).should(`equal`, `idle`)
})
})
})