-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathtest.js
719 lines (612 loc) · 22.1 KB
/
test.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
import {assert} from '@open-wc/testing'
import {default as IncludeFragmentElement} from '../src/index.ts'
let count
const responses = {
'/hello': function () {
return new Response('<div id="replaced">hello</div>', {
status: 200,
headers: {
'Content-Type': 'text/html; charset=utf-8',
},
})
},
'/slow-hello': async function () {
await new Promise(resolve => {
setTimeout(resolve, 100)
})
return responses['/hello']()
},
'/one-two': function () {
return new Response('<p id="one">one</p><p id="two">two</p>', {
status: 200,
headers: {
'Content-Type': 'text/html',
},
})
},
'/blank-type': function () {
return new Response('<div id="replaced">hello</div>', {
status: 200,
headers: {
'Content-Type': null,
},
})
},
'/x-server-sanitized': function () {
return new Response('This response should be marked as sanitized using a custom header!', {
status: 200,
headers: {
'Content-Type': 'text/html; charset=utf-8',
'X-Server-Sanitized': 'sanitized=true',
},
})
},
'/boom': function () {
return new Response('boom', {
status: 500,
})
},
'/count': function () {
count++
return new Response(`${count}`, {
status: 200,
headers: {
'Content-Type': 'text/html',
},
})
},
'/fragment': function (request) {
if (request.headers.get('Accept') === 'text/fragment+html') {
return new Response('<div id="fragment">fragment</div>', {
status: 200,
headers: {
'Content-Type': 'text/fragment+html',
},
})
} else {
return new Response('406', {
status: 406,
})
}
},
'/test.js': function () {
return new Response('alert("what")', {
status: 200,
headers: {
'Content-Type': 'text/javascript',
},
})
},
}
function when(el, eventType) {
return new Promise(function (resolve) {
el.addEventListener(eventType, resolve)
})
}
setup(function () {
count = 0
window.IncludeFragmentElement.prototype.fetch = function (request) {
const pathname = new URL(request.url, window.location.origin).pathname
return Promise.resolve(responses[pathname](request))
}
})
suite('include-fragment-element', function () {
teardown(() => {
document.body.innerHTML = ''
})
test('create from document.createElement', function () {
const el = document.createElement('include-fragment')
assert.equal('INCLUDE-FRAGMENT', el.nodeName)
})
test('create from constructor', function () {
const el = new window.IncludeFragmentElement()
assert.equal('INCLUDE-FRAGMENT', el.nodeName)
})
test('src property', function () {
const el = document.createElement('include-fragment')
assert.equal(null, el.getAttribute('src'))
assert.equal('', el.src)
el.src = '/hello'
assert.equal('/hello', el.getAttribute('src'))
const link = document.createElement('a')
link.href = '/hello'
assert.equal(link.href, el.src)
})
test('initial data is in error state', function () {
const el = document.createElement('include-fragment')
return el.data['catch'](function (error) {
assert.ok(error)
})
})
test('data with src property', async function () {
const el = document.createElement('include-fragment')
el.src = '/hello'
const html = await el.data
assert.equal('<div id="replaced">hello</div>', html)
})
test('skips cache when using refetch', async function () {
const el = document.createElement('include-fragment')
el.src = '/count'
let data = await el.data
assert.equal('1', data)
el.refetch()
data = await el.data
assert.equal('2', data)
})
test('data with src attribute', async function () {
const el = document.createElement('include-fragment')
el.setAttribute('src', '/hello')
const html = await el.data
assert.equal('<div id="replaced">hello</div>', html)
})
test('setting data with src property multiple times', async function () {
const el = document.createElement('include-fragment')
el.src = '/count'
const text = await el.data
assert.equal('1', text)
el.src = '/count'
const text2 = await el.data
assert.equal('1', text2)
})
test('setting data with src attribute multiple times', async function () {
const el = document.createElement('include-fragment')
el.setAttribute('src', '/count')
const text = await el.data
assert.equal('1', text)
el.setAttribute('src', '/count')
const text2 = await el.data
assert.equal('1', text2)
})
test('throws on incorrect Content-Type', async function () {
const el = document.createElement('include-fragment')
el.setAttribute('src', '/test.js')
try {
await el.data
throw new Error('el.data did not throw')
} catch (error) {
assert.match(error, /expected text\/html but was text\/javascript/)
}
})
test('throws on non-matching Content-Type', async function () {
const el = document.createElement('include-fragment')
el.setAttribute('accept', 'text/fragment+html')
el.setAttribute('src', '/hello')
try {
await el.data
throw new Error('el.data did not throw')
} catch (error) {
assert.match(error, /expected text\/fragment\+html but was text\/html; charset=utf-8/)
}
})
test('throws on 406', async function () {
const el = document.createElement('include-fragment')
el.setAttribute('src', '/fragment')
try {
await el.data
throw new Error('el.data did not throw')
} catch (error) {
assert.match(error, /the server responded with a status of 406/)
}
})
test('data is not writable', async function () {
const el = document.createElement('include-fragment')
let data
try {
data = await el.data
} catch {
data = null
}
assert.ok(data !== 42)
try {
el.data = 42
} catch (e) {
assert.ok(e)
} finally {
let data2
try {
data2 = await el.data
} catch {
data2 = null
}
assert.ok(data2 !== 42)
}
})
test('data is not configurable', async function () {
const el = document.createElement('include-fragment')
let data
try {
data = await el.data
} catch {
data = null
}
assert.ok(data !== undefined)
try {
delete el.data
} catch (e) {
assert.ok(e)
} finally {
let data2
try {
data2 = await el.data
} catch {
data2 = null
}
assert.ok(data2 !== undefined)
}
})
test('replaces element on 200 status', async function () {
const div = document.createElement('div')
div.innerHTML = '<include-fragment src="/hello">loading</include-fragment>'
document.body.appendChild(div)
await when(div.firstChild, 'load')
assert.equal(document.querySelector('include-fragment'), null)
assert.equal(document.querySelector('#replaced').textContent, 'hello')
})
test('does not replace element if it has no parent', async function () {
const div = document.createElement('div')
div.innerHTML = '<include-fragment>loading</include-fragment>'
document.body.appendChild(div)
const fragment = div.firstChild
fragment.remove()
fragment.src = '/hello'
let didRun = false
window.addEventListener('unhandledrejection', function () {
assert.ok(false)
})
fragment.addEventListener('loadstart', () => {
didRun = true
})
setTimeout(() => {
assert.ok(!didRun)
div.appendChild(fragment)
}, 10)
await when(fragment, 'load')
assert.equal(document.querySelector('#replaced').textContent, 'hello')
})
test('replaces with several new elements on 200 status', async function () {
const div = document.createElement('div')
div.innerHTML = '<include-fragment src="/one-two">loading</include-fragment>'
document.body.appendChild(div)
await when(div.firstChild, 'load')
assert.equal(document.querySelector('include-fragment'), null)
assert.equal(document.querySelector('#one').textContent, 'one')
assert.equal(document.querySelector('#two').textContent, 'two')
})
test('replaces with response with accept header for any', async function () {
const div = document.createElement('div')
div.innerHTML = '<include-fragment src="/test.js" accept="*/*">loading</include-fragment>'
document.body.appendChild(div)
await when(div.firstChild, 'load')
assert.equal(document.querySelector('include-fragment'), null)
assert.match(document.body.textContent, /alert\("what"\)/)
})
test('replaces with response with the right accept header', async function () {
const div = document.createElement('div')
div.innerHTML = '<include-fragment src="/fragment" accept="text/fragment+html">loading</include-fragment>'
document.body.appendChild(div)
await when(div.firstChild, 'load')
assert.equal(document.querySelector('include-fragment'), null)
assert.equal(document.querySelector('#fragment').textContent, 'fragment')
})
test('error event is not cancelable or bubbles', async function () {
const div = document.createElement('div')
div.innerHTML = '<include-fragment src="/boom">loading</include-fragment>'
document.body.appendChild(div)
const event = await when(div.firstChild, 'error')
assert.equal(event.bubbles, false)
assert.equal(event.cancelable, false)
})
test('adds is-error class on 500 status', async function () {
const div = document.createElement('div')
div.innerHTML = '<include-fragment src="/boom">loading</include-fragment>'
document.body.appendChild(div)
await when(div.firstChild, 'error')
return assert.ok(document.querySelector('include-fragment').classList.contains('is-error'))
})
test('adds is-error class on mising Content-Type', async function () {
const div = document.createElement('div')
div.innerHTML = '<include-fragment src="/blank-type">loading</include-fragment>'
document.body.appendChild(div)
await when(div.firstChild, 'error')
return assert.ok(document.querySelector('include-fragment').classList.contains('is-error'))
})
test('adds is-error class on incorrect Content-Type', async function () {
const div = document.createElement('div')
div.innerHTML = '<include-fragment src="/fragment">loading</include-fragment>'
document.body.appendChild(div)
await when(div.firstChild, 'error')
return assert.ok(document.querySelector('include-fragment').classList.contains('is-error'))
})
test('replaces element when src attribute is changed', async function () {
const elem = document.createElement('include-fragment')
document.body.appendChild(elem)
setTimeout(function () {
elem.src = '/hello'
}, 10)
await when(elem, 'load')
assert.equal(document.querySelector('include-fragment'), null)
assert.equal(document.querySelector('#replaced').textContent, 'hello')
})
test('fires replaced event', async function () {
const elem = document.createElement('include-fragment')
document.body.appendChild(elem)
setTimeout(function () {
elem.src = '/hello'
}, 10)
await when(elem, 'include-fragment-replaced')
assert.equal(document.querySelector('include-fragment'), null)
assert.equal(document.querySelector('#replaced').textContent, 'hello')
})
test('fires events for include-fragment node replacement operations for fragment manipulation', async function () {
const elem = document.createElement('include-fragment')
document.body.appendChild(elem)
setTimeout(function () {
elem.src = '/hello'
}, 10)
elem.addEventListener('include-fragment-replace', event => {
event.detail.fragment.querySelector('*').textContent = 'hey'
})
await when(elem, 'include-fragment-replaced')
assert.equal(document.querySelector('include-fragment'), null)
assert.equal(document.querySelector('#replaced').textContent, 'hey')
})
test('does not replace node if event was canceled ', async function () {
const elem = document.createElement('include-fragment')
document.body.appendChild(elem)
setTimeout(function () {
elem.src = '/hello'
}, 10)
elem.addEventListener('include-fragment-replace', event => {
event.preventDefault()
})
await when(elem, 'load')
assert(document.querySelector('include-fragment'), 'Node should not be replaced')
})
suite('event order', () => {
const originalSetTimeout = window.setTimeout
setup(() => {
// Emulate some kind of timer clamping
let i = 60
window.setTimeout = (fn, ms, ...rest) => originalSetTimeout.call(window, fn, ms + (i -= 20), ...rest)
})
teardown(() => {
window.setTimeout = originalSetTimeout
})
test('loading events fire in guaranteed order', async function () {
const elem = document.createElement('include-fragment')
const order = []
const connected = []
const events = [
(async () => {
await when(elem, 'loadend')
order.push('loadend')
connected.push(elem.isConnected)
})(),
(async () => {
await when(elem, 'load')
order.push('load')
connected.push(elem.isConnected)
})(),
(async () => {
await when(elem, 'loadstart')
order.push('loadstart')
connected.push(elem.isConnected)
})(),
]
elem.src = '/hello'
document.body.appendChild(elem)
await Promise.all(events)
assert.deepStrictEqual(order, ['loadstart', 'load', 'loadend'])
assert.deepStrictEqual(connected, [true, false, false])
})
})
test('sets loading to "eager" by default', function () {
const div = document.createElement('div')
div.innerHTML = '<include-fragment loading="lazy" src="/hello">loading</include-fragment>'
document.body.appendChild(div)
assert(div.firstChild.loading, 'eager')
})
test('loading will return "eager" even if set to junk value', function () {
const div = document.createElement('div')
div.innerHTML = '<include-fragment loading="junk" src="/hello">loading</include-fragment>'
document.body.appendChild(div)
assert(div.firstChild.loading, 'eager')
})
test('loading=lazy loads if already visible on page', async function () {
const div = document.createElement('div')
div.innerHTML = '<include-fragment loading="lazy" src="/hello">loading</include-fragment>'
document.body.appendChild(div)
await when(div.firstChild, 'include-fragment-replaced')
assert.equal(document.querySelector('include-fragment'), null)
assert.equal(document.querySelector('#replaced').textContent, 'hello')
})
test('loading=lazy does not load if not visible on page', function () {
const div = document.createElement('div')
div.innerHTML = '<include-fragment loading="lazy" src="/hello">loading</include-fragment>'
div.hidden = true
document.body.appendChild(div)
return Promise.race([
(async () => {
await when(div.firstChild, 'load')
throw new Error('<include-fragment loading=lazy> loaded too early')
})(),
new Promise(resolve => setTimeout(resolve, 100)),
])
})
test('loading=lazy does not load when src is changed', function () {
const div = document.createElement('div')
div.innerHTML = '<include-fragment loading="lazy" src="">loading</include-fragment>'
div.hidden = true
document.body.appendChild(div)
div.firstChild.src = '/hello'
return Promise.race([
(async () => {
await when(div.firstChild, 'load')
throw new Error('<include-fragment loading=lazy> loaded too early')
})(),
new Promise(resolve => setTimeout(resolve, 100)),
])
})
test('loading=lazy loads as soon as element visible on page', async function () {
const div = document.createElement('div')
div.innerHTML = '<include-fragment loading="lazy" src="/hello">loading</include-fragment>'
div.hidden = true
let failed = false
document.body.appendChild(div)
const fail = () => (failed = true)
div.firstChild.addEventListener('load', fail)
setTimeout(function () {
div.hidden = false
div.firstChild.removeEventListener('load', fail)
}, 100)
await when(div.firstChild, 'load')
assert.ok(!failed, 'Load occured too early')
})
test('loading=lazy does not observably change during load cycle', async function () {
const div = document.createElement('div')
div.innerHTML = '<include-fragment loading="lazy" src="/hello">loading</include-fragment>'
const elem = div.firstChild
document.body.appendChild(div)
await when(elem, 'loadstart')
assert.equal(elem.loading, 'lazy', 'loading mode changed observably')
})
test('loading=lazy can be switched to eager to load', async function () {
const div = document.createElement('div')
div.innerHTML = '<include-fragment loading="lazy" src="/hello">loading</include-fragment>'
div.hidden = true
let failed = false
document.body.appendChild(div)
const fail = () => (failed = true)
div.firstChild.addEventListener('load', fail)
setTimeout(function () {
div.firstChild.loading = 'eager'
div.firstChild.removeEventListener('load', fail)
}, 100)
await when(div.firstChild, 'load')
assert.ok(!failed, 'Load occured too early')
})
test('loading=lazy wont load twice even if load is manually called', async function () {
const div = document.createElement('div')
div.innerHTML = '<include-fragment loading="lazy" src="/slow-hello">loading</include-fragment>'
div.hidden = true
document.body.appendChild(div)
let loadCount = 0
div.firstChild.addEventListener('loadstart', () => (loadCount += 1))
const load = div.firstChild.load()
setTimeout(() => {
div.hidden = false
}, 0)
const replacedPromise = when(div.firstChild, 'include-fragment-replaced')
await load
await replacedPromise
assert.equal(loadCount, 1, 'Load occured too many times')
assert.equal(document.querySelector('include-fragment'), null)
assert.equal(document.querySelector('#replaced').textContent, 'hello')
})
test('include-fragment-replaced is only called once', async function () {
const div = document.createElement('div')
div.hidden = true
document.body.append(div)
div.innerHTML = `<include-fragment src="/hello">loading</include-fragment>`
div.firstChild.addEventListener('include-fragment-replaced', () => (loadCount += 1))
let loadCount = 0
setTimeout(() => {
div.hidden = false
}, 0)
await when(div.firstChild, 'include-fragment-replaced')
assert.equal(loadCount, 1, 'Load occured too many times')
assert.equal(document.querySelector('include-fragment'), null)
assert.equal(document.querySelector('#replaced').textContent, 'hello')
})
suite('CSP trusted types', () => {
teardown(() => {
IncludeFragmentElement.setCSPTrustedTypesPolicy(null)
})
test('can set a pass-through mock CSP trusted types policy', async function () {
let policyCalled = false
IncludeFragmentElement.setCSPTrustedTypesPolicy({
createHTML: htmlText => {
policyCalled = true
return htmlText
},
})
const el = document.createElement('include-fragment')
el.src = '/hello'
const data = await el.data
assert.equal('<div id="replaced">hello</div>', data)
assert.ok(policyCalled)
})
test('can set and clear a mutating mock CSP trusted types policy', async function () {
let policyCalled = false
IncludeFragmentElement.setCSPTrustedTypesPolicy({
createHTML: () => {
policyCalled = true
return '<b>replacement</b>'
},
})
const el = document.createElement('include-fragment')
el.src = '/hello'
const data = await el.data
assert.equal('<b>replacement</b>', data)
assert.ok(policyCalled)
IncludeFragmentElement.setCSPTrustedTypesPolicy(null)
const el2 = document.createElement('include-fragment')
el2.src = '/hello'
const data2 = await el2.data
assert.equal('<div id="replaced">hello</div>', data2)
})
test('can set a real CSP trusted types policy in Chromium', async function () {
let policyCalled = false
const policy = globalThis.trustedTypes.createPolicy('test1', {
createHTML: htmlText => {
policyCalled = true
return htmlText
},
})
IncludeFragmentElement.setCSPTrustedTypesPolicy(policy)
const el = document.createElement('include-fragment')
el.src = '/hello'
const data = await el.data
assert.equal('<div id="replaced">hello</div>', data)
assert.ok(policyCalled)
})
test('can reject data using a mock CSP trusted types policy', async function () {
IncludeFragmentElement.setCSPTrustedTypesPolicy({
createHTML: () => {
throw new Error('Rejected data!')
},
})
const el = document.createElement('include-fragment')
el.src = '/hello'
try {
await el.data
assert.ok(false)
} catch (error) {
assert.match(error, /Rejected data!/)
}
})
test('can access headers using a mock CSP trusted types policy', async function () {
IncludeFragmentElement.setCSPTrustedTypesPolicy({
createHTML: (htmlText, response) => {
if (response.headers.get('X-Server-Sanitized') !== 'sanitized=true') {
// Note: this will reject the contents, but the error may be caught before it shows in the JS console.
throw new Error('Rejecting HTML that was not marked by the server as sanitized.')
}
return htmlText
},
})
const el = document.createElement('include-fragment')
el.src = '/hello'
try {
await el.data
assert.ok(false)
} catch (error) {
assert.match(error, /Rejecting HTML that was not marked by the server as sanitized./)
}
const el2 = document.createElement('include-fragment')
el2.src = '/x-server-sanitized'
const data2 = await el2.data
assert.equal('This response should be marked as sanitized using a custom header!', data2)
})
})
})