-
Notifications
You must be signed in to change notification settings - Fork 412
/
rendering_tests.ts
359 lines (287 loc) · 12.4 KB
/
rendering_tests.ts
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import { TurboDriveTestCase } from "../helpers/turbo_drive_test_case"
import { Element } from "@theintern/leadfoot"
export class RenderingTests extends TurboDriveTestCase {
async setup() {
await this.goToLocation("/src/tests/fixtures/rendering.html")
}
async teardown() {
await this.remote.execute(() => localStorage.clear())
}
async "test triggers before-render and render events"() {
this.clickSelector("#same-origin-link")
const { newBody } = await this.nextEventNamed("turbo:before-render")
const h1 = await this.querySelector("h1")
this.assert.equal(await h1.getVisibleText(), "One")
await this.nextEventNamed("turbo:render")
this.assert(await newBody.equals(await this.body))
}
async "test triggers before-render and render events for error pages"() {
this.clickSelector("#nonexistent-link")
const { newBody } = await this.nextEventNamed("turbo:before-render")
this.assert.equal(await newBody.getVisibleText(), "404 Not Found: /nonexistent")
await this.nextEventNamed("turbo:render")
this.assert(await newBody.equals(await this.body))
}
async "test reloads when tracked elements change"() {
await this.remote.execute(() =>
window.addEventListener("turbo:reload", (e: any) => {
localStorage.setItem("reloadReason", e.detail.reason)
})
)
this.clickSelector("#tracked-asset-change-link")
await this.nextBody
const reason = await this.remote.execute(() => localStorage.getItem("reloadReason"))
this.assert.equal(await this.pathname, "/src/tests/fixtures/tracked_asset_change.html")
this.assert.equal(await this.visitAction, "load")
this.assert.equal(reason, "tracked_element_mismatch")
}
async "test wont reload when tracked elements has a nonce"() {
this.clickSelector("#tracked-nonce-tag-link")
await this.nextBody
this.assert.equal(await this.pathname, "/src/tests/fixtures/tracked_nonce_tag.html")
this.assert.equal(await this.visitAction, "advance")
}
async "test reloads when turbo-visit-control setting is reload"() {
await this.remote.execute(() =>
window.addEventListener("turbo:reload", (e: any) => {
localStorage.setItem("reloadReason", e.detail.reason)
})
)
this.clickSelector("#visit-control-reload-link")
await this.nextBody
const reason = await this.remote.execute(() => localStorage.getItem("reloadReason"))
this.assert.equal(await this.pathname, "/src/tests/fixtures/visit_control_reload.html")
this.assert.equal(await this.visitAction, "load")
this.assert.equal(reason, "turbo_visit_control_is_reload")
}
async "test maintains scroll position before visit when turbo-visit-control setting is reload"() {
await this.scrollToSelector("#below-the-fold-visit-control-reload-link")
this.assert.notOk(await this.isScrolledToTop(), "scrolled down")
await this.remote.execute(() => localStorage.setItem("scrolls", "false"))
this.remote.execute(() =>
addEventListener("scroll", () => {
localStorage.setItem("scrolls", "true")
})
)
this.clickSelector("#below-the-fold-visit-control-reload-link")
await this.nextBody
const scrolls = await this.remote.execute(() => localStorage.getItem("scrolls"))
this.assert.ok(scrolls === "false", "scroll position is preserved")
this.assert.equal(await this.pathname, "/src/tests/fixtures/visit_control_reload.html")
this.assert.equal(await this.visitAction, "load")
}
async "test accumulates asset elements in head"() {
const originalElements = await this.assetElements
this.clickSelector("#additional-assets-link")
await this.nextBody
const newElements = await this.assetElements
this.assert.notDeepEqual(newElements, originalElements)
this.goBack()
await this.nextBody
const finalElements = await this.assetElements
this.assert.deepEqual(finalElements, newElements)
}
async "test replaces provisional elements in head"() {
const originalElements = await this.provisionalElements
this.assert(!(await this.hasSelector("meta[name=test]")))
this.clickSelector("#same-origin-link")
await this.nextBody
const newElements = await this.provisionalElements
this.assert.notDeepEqual(newElements, originalElements)
this.assert(await this.hasSelector("meta[name=test]"))
this.goBack()
await this.nextBody
const finalElements = await this.provisionalElements
this.assert.notDeepEqual(finalElements, newElements)
this.assert(!(await this.hasSelector("meta[name=test]")))
}
async "test evaluates head stylesheet elements"() {
this.assert.equal(await this.isStylesheetEvaluated, false)
this.clickSelector("#additional-assets-link")
await this.nextEventNamed("turbo:render")
this.assert.equal(await this.isStylesheetEvaluated, true)
}
async "test does not evaluate head stylesheet elements inside noscript elements"() {
this.assert.equal(await this.isNoscriptStylesheetEvaluated, false)
this.clickSelector("#additional-assets-link")
await this.nextEventNamed("turbo:render")
this.assert.equal(await this.isNoscriptStylesheetEvaluated, false)
}
async "skip evaluates head script elements once"() {
this.assert.equal(await this.headScriptEvaluationCount, undefined)
this.clickSelector("#head-script-link")
await this.nextEventNamed("turbo:render")
this.assert.equal(await this.headScriptEvaluationCount, 1)
this.goBack()
await this.nextEventNamed("turbo:render")
this.assert.equal(await this.headScriptEvaluationCount, 1)
this.clickSelector("#head-script-link")
await this.nextEventNamed("turbo:render")
this.assert.equal(await this.headScriptEvaluationCount, 1)
}
async "test evaluates body script elements on each render"() {
this.assert.equal(await this.bodyScriptEvaluationCount, undefined)
this.clickSelector("#body-script-link")
await this.nextEventNamed("turbo:render")
this.assert.equal(await this.bodyScriptEvaluationCount, 1)
this.goBack()
await this.nextEventNamed("turbo:render")
this.assert.equal(await this.bodyScriptEvaluationCount, 1)
this.clickSelector("#body-script-link")
await this.nextEventNamed("turbo:render")
this.assert.equal(await this.bodyScriptEvaluationCount, 2)
}
async "test does not evaluate data-turbo-eval=false scripts"() {
this.clickSelector("#eval-false-script-link")
await this.nextEventNamed("turbo:render")
this.assert.equal(await this.bodyScriptEvaluationCount, undefined)
}
async "test preserves permanent elements"() {
const permanentElement = await this.permanentElement
this.assert.equal(await permanentElement.getVisibleText(), "Rendering")
this.clickSelector("#permanent-element-link")
await this.nextEventNamed("turbo:render")
this.assert(await permanentElement.equals(await this.permanentElement))
this.assert.equal(await permanentElement.getVisibleText(), "Rendering")
this.goBack()
await this.nextEventNamed("turbo:render")
this.assert(await permanentElement.equals(await this.permanentElement))
}
async "test preserves permanent elements within turbo-frames"() {
let permanentElement = await this.querySelector("#permanent-in-frame")
this.assert.equal(await permanentElement.getVisibleText(), "Rendering")
await this.clickSelector("#permanent-in-frame-element-link")
await this.nextBeat
permanentElement = await this.querySelector("#permanent-in-frame")
this.assert.equal(await permanentElement.getVisibleText(), "Rendering")
}
async "test preserves permanent elements within turbo-frames rendered without layouts"() {
let permanentElement = await this.querySelector("#permanent-in-frame")
this.assert.equal(await permanentElement.getVisibleText(), "Rendering")
await this.clickSelector("#permanent-in-frame-without-layout-element-link")
await this.nextBeat
permanentElement = await this.querySelector("#permanent-in-frame")
this.assert.equal(await permanentElement.getVisibleText(), "Rendering")
}
async "test preserves permanent element video playback"() {
let videoElement = await this.querySelector("#permanent-video")
await this.clickSelector("#permanent-video-button")
await this.sleep(500)
const timeBeforeRender = await videoElement.getProperty("currentTime")
this.assert.notEqual(timeBeforeRender, 0, "playback has started")
await this.clickSelector("#permanent-element-link")
await this.nextBody
videoElement = await this.querySelector("#permanent-video")
const timeAfterRender = await videoElement.getProperty("currentTime")
this.assert.equal(timeAfterRender, timeBeforeRender, "element state is preserved")
}
async "test before-cache event"() {
this.beforeCache((body) => (body.innerHTML = "Modified"))
this.clickSelector("#same-origin-link")
await this.nextBody
await this.goBack()
const body = await this.nextBody
this.assert(await body.getVisibleText(), "Modified")
}
async "test mutation record as before-cache notification"() {
this.modifyBodyAfterRemoval()
this.clickSelector("#same-origin-link")
await this.nextBody
await this.goBack()
const body = await this.nextBody
this.assert(await body.getVisibleText(), "Modified")
}
async "test error pages"() {
this.clickSelector("#nonexistent-link")
const body = await this.nextBody
this.assert.equal(await body.getVisibleText(), "404 Not Found: /nonexistent")
await this.goBack()
}
get assetElements(): Promise<Element[]> {
return filter(this.headElements, isAssetElement)
}
get provisionalElements(): Promise<Element[]> {
return filter(this.headElements, async (element) => !(await isAssetElement(element)))
}
get headElements(): Promise<Element[]> {
return this.evaluate(() => Array.from(document.head.children) as any[])
}
get permanentElement(): Promise<Element> {
return this.querySelector("#permanent")
}
get headScriptEvaluationCount(): Promise<number | undefined> {
return this.evaluate(() => window.headScriptEvaluationCount)
}
get bodyScriptEvaluationCount(): Promise<number | undefined> {
return this.evaluate(() => window.bodyScriptEvaluationCount)
}
get isStylesheetEvaluated(): Promise<boolean> {
return this.evaluate(
() => getComputedStyle(document.body).getPropertyValue("--black-if-evaluated").trim() === "black"
)
}
get isNoscriptStylesheetEvaluated(): Promise<boolean> {
return this.evaluate(
() => getComputedStyle(document.body).getPropertyValue("--black-if-noscript-evaluated").trim() === "black"
)
}
async modifyBodyBeforeCaching() {
return this.remote.execute(() =>
addEventListener(
"turbo:before-cache",
function eventListener() {
removeEventListener("turbo:before-cache", eventListener, false)
document.body.innerHTML = "Modified"
},
false
)
)
}
async beforeCache(callback: (body: HTMLElement) => void) {
return this.remote.execute(
(callback: (body: HTMLElement) => void) => {
addEventListener(
"turbo:before-cache",
function eventListener() {
removeEventListener("turbo:before-cache", eventListener, false)
callback(document.body)
},
false
)
},
[callback]
)
}
async modifyBodyAfterRemoval() {
return this.remote.execute(() => {
const { documentElement, body } = document
const observer = new MutationObserver((records) => {
for (const record of records) {
if (Array.from(record.removedNodes).indexOf(body) > -1) {
body.innerHTML = "Modified"
observer.disconnect()
break
}
}
})
observer.observe(documentElement, { childList: true })
})
}
}
async function filter<T>(promisedValues: Promise<T[]>, predicate: (value: T) => Promise<boolean>): Promise<T[]> {
const values = await promisedValues
const matches = await Promise.all(values.map((value) => predicate(value)))
return matches.reduce((result, match, index) => result.concat(match ? values[index] : []), [] as T[])
}
async function isAssetElement(element: Element): Promise<boolean> {
const tagName = await element.getTagName()
const relValue = await element.getAttribute("rel")
return tagName == "script" || tagName == "style" || (tagName == "link" && relValue == "stylesheet")
}
declare global {
interface Window {
headScriptEvaluationCount?: number
bodyScriptEvaluationCount?: number
}
}
RenderingTests.registerSuite()