Repository navigation
Expand file tree
/
Copy pathgatsby-script-inline-scripts.js
More file actions
206 lines (182 loc) · 6.48 KB
/
Copy pathgatsby-script-inline-scripts.js
File metadata and controls
206 lines (182 loc) · 6.48 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
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
import { inlineScript } from "../../gatsby-script-scripts"
import { resourceRecord, markRecord } from "../../gatsby-script-records"
const page = {
target: `/gatsby-script-inline-scripts/`,
navigation: `/gatsby-script-navigation/`,
}
const typesOfInlineScripts = [
{
descriptor: `dangerouslySetInnerHTML`,
inlineScriptType: inlineScript.dangerouslySet,
},
{
descriptor: `template literals`,
inlineScriptType: inlineScript.templateLiteral,
},
]
Cypress.on(`uncaught:exception`, err => {
if (
(err.message.includes(`Minified React error #418`) ||
err.message.includes(`Minified React error #423`) ||
err.message.includes(`Minified React error #425`)) &&
Cypress.env(`TEST_PLUGIN_OFFLINE`)
) {
return false
}
})
/**
* Normally we would duplicate the tests so they're flatter and easier to debug,
* but since the test count grew and the cases are exactly the same we'll iterate.
*/
for (const { descriptor, inlineScriptType } of typesOfInlineScripts) {
describe(`inline scripts set via ${descriptor}`, () => {
describe(`using the post-hydrate strategy`, () => {
it(`should execute successfully`, () => {
cy.visit(page.target).waitForRouteChange()
cy.getRecord(
`post-hydrate-${inlineScriptType}`,
`success`,
true
).should(`equal`, `true`)
})
it(`should load after the framework bundle has loaded`, () => {
cy.visit(page.target).waitForRouteChange()
// Assert framework is loaded before inline script is executed
cy.getRecord(
`post-hydrate-${inlineScriptType}`,
markRecord.executeStart
).then(dangerouslySetExecuteStart => {
cy.getRecord(`framework`, resourceRecord.responseEnd).should(
`be.lessThan`,
dangerouslySetExecuteStart
)
})
})
})
describe(`using the idle strategy`, () => {
it(`should execute successfully`, () => {
cy.visit(page.target).waitForRouteChange()
cy.getRecord(`idle-${inlineScriptType}`, `success`, true).should(
`equal`,
`true`
)
})
it(`should load after other strategies`, () => {
cy.visit(page.target).waitForRouteChange()
cy.getRecord(`idle-${inlineScriptType}`, markRecord.executeStart).then(
dangerouslySetExecuteStart => {
cy.getRecord(
`post-hydrate-${inlineScriptType}`,
markRecord.executeStart
).should(`be.lessThan`, dangerouslySetExecuteStart)
}
)
})
})
describe(`when navigation occurs`, () => {
it(`should load only once on initial page load`, () => {
cy.visit(page.target).waitForRouteChange()
cy.get(`table[id=script-mark-records] tbody`)
.children()
.should(`have.length`, 4)
cy.getRecord(
`post-hydrate-${inlineScriptType}`,
`strategy`,
true
).should(`equal`, `post-hydrate`)
cy.getRecord(`idle-${inlineScriptType}`, `strategy`, true).should(
`equal`,
`idle`
)
})
it(`should load only once after the page is refreshed`, () => {
cy.visit(page.target).waitForRouteChange()
cy.reload().url().should(`contain`, page.target)
cy.get(`table[id=script-mark-records] tbody`)
.children()
.should(`have.length`, 4)
cy.getRecord(
`post-hydrate-${inlineScriptType}`,
`strategy`,
true
).should(`equal`, `post-hydrate`)
cy.getRecord(`idle-${inlineScriptType}`, `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-mark-records] tbody`)
.children()
.should(`have.length`, 4)
cy.getRecord(
`post-hydrate-${inlineScriptType}`,
`strategy`,
true
).should(`equal`, `post-hydrate`)
cy.getRecord(`idle-${inlineScriptType}`, `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.get(`table[id=script-mark-records] tbody`) // Make sure history has time to change
cy.go(`back`).waitForRouteChange()
cy.go(`forward`).waitForRouteChange()
cy.get(`table[id=script-mark-records] tbody`)
.children()
.should(`have.length`, 4)
cy.getRecord(
`post-hydrate-${inlineScriptType}`,
`strategy`,
true
).should(`equal`, `post-hydrate`)
cy.getRecord(`idle-${inlineScriptType}`, `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-mark-records] tbody`)
.children()
.should(`have.length`, 4)
cy.getRecord(
`post-hydrate-${inlineScriptType}`,
`strategy`,
true
).should(`equal`, `post-hydrate`)
cy.getRecord(`idle-${inlineScriptType}`, `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-mark-records] tbody`)
.children()
.should(`have.length`, 4)
cy.getRecord(
`post-hydrate-${inlineScriptType}`,
`strategy`,
true
).should(`equal`, `post-hydrate`)
cy.getRecord(`idle-${inlineScriptType}`, `strategy`, true).should(
`equal`,
`idle`
)
})
})
})
}