Skip to content

Commit

Permalink
test: use setup/teardown to override setTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Mar 12, 2021
1 parent fcd51f6 commit 2770e69
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,36 +426,42 @@ suite('include-fragment-element', function() {
})
})

test('loading events fire in guaranteed order', function() {
const elem = document.createElement('include-fragment')
const order = []
const connected = []
const events = [
when(elem, 'loadend').then(() => {
order.push('loadend')
connected.push(elem.isConnected)
}),
when(elem, 'load').then(() => {
order.push('load')
connected.push(elem.isConnected)
}),
when(elem, 'loadstart').then(() => {
order.push('loadstart')
connected.push(elem.isConnected)
})
]
elem.src = '/hello'

// Emulate some kind of timer clamping
suite('event order', () => {
const originalSetTimeout = window.setTimeout
let i = 60
window.setTimeout = (fn, ms, ...rest) => originalSetTimeout.call(window, fn, ms + (i -= 20), ...rest)

document.body.appendChild(elem)
return Promise.all(events).then(() => {
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
assert.deepStrictEqual(order, ['loadstart', 'load', 'loadend'])
assert.deepStrictEqual(connected, [true, false, false])
})

test('loading events fire in guaranteed order', function() {
const elem = document.createElement('include-fragment')
const order = []
const connected = []
const events = [
when(elem, 'loadend').then(() => {
order.push('loadend')
connected.push(elem.isConnected)
}),
when(elem, 'load').then(() => {
order.push('load')
connected.push(elem.isConnected)
}),
when(elem, 'loadstart').then(() => {
order.push('loadstart')
connected.push(elem.isConnected)
})
]
elem.src = '/hello'
document.body.appendChild(elem)

return Promise.all(events).then(() => {
assert.deepStrictEqual(order, ['loadstart', 'load', 'loadend'])
assert.deepStrictEqual(connected, [true, false, false])
})
})
})

Expand Down

0 comments on commit 2770e69

Please sign in to comment.