2020-12-20 03:33:13 +00:00
|
|
|
// test utils used in e2e tests for playgrounds.
|
|
|
|
// this can be directly imported in any playground tests as 'testUtils', e.g.
|
|
|
|
// `import { getColor } from 'testUtils'`
|
|
|
|
|
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
|
|
|
import slash from 'slash'
|
|
|
|
import colors from 'css-color-names'
|
|
|
|
import { ElementHandle } from 'playwright-chromium'
|
|
|
|
|
2020-12-21 23:37:04 +00:00
|
|
|
export const browserLogs = []
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
page.on('console', (msg) => {
|
|
|
|
browserLogs.push(msg.text())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
export const isBuild = !!process.env.VITE_TEST_BUILD
|
|
|
|
|
2020-12-20 03:33:13 +00:00
|
|
|
const testPath = expect.getState().testPath
|
|
|
|
const testName = slash(testPath).match(/playground\/(\w+)\//)?.[1]
|
2020-12-22 20:34:02 +00:00
|
|
|
export const testDir = path.resolve(__dirname, '../../temp', testName)
|
2020-12-20 03:33:13 +00:00
|
|
|
|
|
|
|
const hexToNameMap: Record<string, string> = {}
|
|
|
|
Object.keys(colors).forEach((color) => {
|
|
|
|
hexToNameMap[colors[color]] = color
|
|
|
|
})
|
|
|
|
|
|
|
|
function componentToHex(c: number): string {
|
|
|
|
var hex = c.toString(16)
|
|
|
|
return hex.length == 1 ? '0' + hex : hex
|
|
|
|
}
|
|
|
|
|
|
|
|
function rgbToHex(rgb: string): string {
|
2020-12-22 21:27:36 +00:00
|
|
|
const match = rgb.match(/rgb\((\d+),\s*(\d+),\s*(\d+)\)/)
|
|
|
|
if (match) {
|
|
|
|
const [_, rs, gs, bs] = match
|
|
|
|
return (
|
|
|
|
'#' +
|
|
|
|
componentToHex(parseInt(rs, 10)) +
|
|
|
|
componentToHex(parseInt(gs, 10)) +
|
|
|
|
componentToHex(parseInt(bs, 10))
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return '#000000'
|
|
|
|
}
|
2020-12-20 03:33:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const timeout = (n: number) => new Promise((r) => setTimeout(r, n))
|
|
|
|
|
|
|
|
async function toEl(el: string | ElementHandle): Promise<ElementHandle> {
|
|
|
|
if (typeof el === 'string') {
|
|
|
|
return await page.$(el)
|
|
|
|
}
|
|
|
|
return el
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getColor(el: string | ElementHandle) {
|
|
|
|
el = await toEl(el)
|
|
|
|
const rgb = await el.evaluate((el) => getComputedStyle(el as Element).color)
|
|
|
|
return hexToNameMap[rgbToHex(rgb)] || rgb
|
|
|
|
}
|
|
|
|
|
2020-12-21 23:37:04 +00:00
|
|
|
export async function getBg(el: string | ElementHandle) {
|
|
|
|
el = await toEl(el)
|
|
|
|
return el.evaluate((el) => getComputedStyle(el as Element).backgroundImage)
|
|
|
|
}
|
|
|
|
|
2020-12-20 03:33:13 +00:00
|
|
|
export function editFile(filename: string, replacer: (str: string) => string) {
|
2020-12-21 23:37:04 +00:00
|
|
|
if (isBuild) return
|
2020-12-20 03:33:13 +00:00
|
|
|
filename = path.resolve(testDir, filename)
|
|
|
|
const content = fs.readFileSync(filename, 'utf-8')
|
|
|
|
const modified = replacer(content)
|
|
|
|
fs.writeFileSync(filename, modified)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Poll a getter until the value it returns includes the expected value.
|
|
|
|
*/
|
|
|
|
export async function untilUpdated(
|
|
|
|
poll: () => Promise<string>,
|
|
|
|
expected: string
|
|
|
|
) {
|
2020-12-21 23:37:04 +00:00
|
|
|
if (isBuild) return
|
2020-12-22 03:18:53 +00:00
|
|
|
const maxTries = process.env.CI ? 100 : 20
|
2020-12-20 03:33:13 +00:00
|
|
|
for (let tries = 0; tries < maxTries; tries++) {
|
|
|
|
const actual = (await poll()) || ''
|
|
|
|
if (actual.indexOf(expected) > -1 || tries === maxTries - 1) {
|
|
|
|
expect(actual).toMatch(expected)
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
await timeout(50)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|