'use strict'
const { runServer, getBrowserContext } = require('@browserless/test')
const { default: test } = require('ava')
const IMPORT_DELAY_MS = 500
const box =
'
box
'
const html = (head = '') => `
${head}
${box}
`
const mainWorldStyleHook = ``
const wrapsCreateElement = html().replace(
'',
`
`
)
const serve = (t, body = html()) =>
runServer(t, ({ req, res }) => {
if (req.url === '/slow.css') {
setTimeout(() => {
res.setHeader('content-type', 'text/css')
res.end('#box { color: rgb(1, 2, 3) }')
}, IMPORT_DELAY_MS)
return
}
res.setHeader('content-type', 'text/html')
res.end(body)
})
const gotoAndRead = async (t, buildOpts, body) => {
const url = await serve(t, body)
const opts = typeof buildOpts === 'function' ? buildOpts(url) : buildOpts
const browserless = await getBrowserContext(t)
return browserless.withPage((page, goto) => async () => {
await goto(page, { url, adblock: false, waitUntil: 'load', ...opts })
return page.evaluate(() => {
const el = document.getElementById('box')
const style = window.getComputedStyle(el)
const inHead = [...(document.head ? document.head.children : [])].some(
node => node.tagName === 'STYLE' && node.textContent.includes('animation-duration')
)
return {
transitionDuration: style.transitionDuration,
transitionDelay: style.transitionDelay,
animationDuration: style.animationDuration,
color: style.color,
inHead
}
})
})()
}
test('animations are disabled by the time goto returns', async t => {
const style = await gotoAndRead(t, { animations: false })
t.is(style.transitionDuration, '0s')
t.is(style.transitionDelay, '0s')
t.is(style.animationDuration, '0s')
t.true(style.inHead)
})
test('animations are disabled on a document that has no head', async t => {
const svg = ``
const url = await runServer(t, ({ res }) => {
res.setHeader('content-type', 'image/svg+xml')
res.end(svg)
})
const browserless = await getBrowserContext(t)
const style = await browserless.withPage((page, goto) => async () => {
await goto(page, { url, adblock: false, waitUntil: 'load', animations: false })
return page.evaluate(() => {
const computed = window.getComputedStyle(document.getElementById('box'))
return {
transitionDuration: computed.transitionDuration,
transitionDelay: computed.transitionDelay,
animationDuration: computed.animationDuration,
hasHead: document.head !== null
}
})
})()
t.false(style.hasHead)
t.is(style.transitionDuration, '0s')
t.is(style.transitionDelay, '0s')
t.is(style.animationDuration, '0s')
})
test('a page that hooks createElement still gets animations disabled', async t => {
const style = await gotoAndRead(t, { animations: false }, html(mainWorldStyleHook))
t.is(style.transitionDuration, '0s')
t.is(style.transitionDelay, '0s')
t.is(style.animationDuration, '0s')
})
test('animations stay when they are left on', async t => {
const style = await gotoAndRead(t, { animations: true })
t.is(style.transitionDuration, '2s')
t.is(style.transitionDelay, '1s')
t.is(style.animationDuration, '3s')
})
test('animations are disabled even when the page wraps createElement', async t => {
const style = await gotoAndRead(t, { animations: false }, wrapsCreateElement)
t.is(style.transitionDuration, '0s')
})
test('an inline style that imports a slow sheet is applied by the time goto returns', async t => {
const style = await gotoAndRead(t, url => ({ styles: [`@import url("${url}slow.css");`] }))
t.is(style.color, 'rgb(1, 2, 3)')
})