78 lines
4.3 KiB
TypeScript
78 lines
4.3 KiB
TypeScript
import { test, expect } from '@playwright/test'
|
|
import { readFileSync } from 'node:fs'
|
|
import { resolve } from 'node:path'
|
|
|
|
// Exercise the real Element Plus selectors in either stylesheet order: Vite may
|
|
// load a component's on-demand CSS after the application's global stylesheet.
|
|
for (const componentCssLast of [false, true]) {
|
|
test(`button variants stay legible (component CSS ${componentCssLast ? 'last' : 'first'})`, async ({ page }) => {
|
|
const elementCss = readFileSync(resolve('node_modules/element-plus/theme-chalk/el-button.css'), 'utf8')
|
|
const appCss = readFileSync(resolve('src/style.css'), 'utf8')
|
|
await page.setContent('<main style="background: white"></main>')
|
|
for (const content of componentCssLast ? [appCss, elementCss] : [elementCss, appCss]) {
|
|
await page.addStyleTag({ content })
|
|
}
|
|
const results = await page.evaluate(() => {
|
|
const main = document.querySelector('main')!
|
|
const luminance = (color: string) => {
|
|
const channels = color.match(/[\d.]+/g)!.slice(0, 3).map(Number).map(value => {
|
|
const channel = value / 255
|
|
return channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4
|
|
})
|
|
return 0.2126 * channels[0]! + 0.7152 * channels[1]! + 0.0722 * channels[2]!
|
|
}
|
|
const failures: string[] = []
|
|
for (const type of ['', 'primary', 'success', 'info', 'warning', 'danger']) {
|
|
for (const variant of ['', 'is-plain', 'is-text', 'is-link', 'is-dashed']) {
|
|
for (const disabled of [false, true]) {
|
|
const button = document.createElement('button')
|
|
button.className = ['el-button', type && `el-button--${type}`, variant, disabled && 'is-disabled'].filter(Boolean).join(' ')
|
|
button.textContent = '操作 Action'
|
|
main.appendChild(button)
|
|
const style = getComputedStyle(button)
|
|
const background = style.backgroundColor === 'rgba(0, 0, 0, 0)' ? 'rgb(255, 255, 255)' : style.backgroundColor
|
|
const values = [luminance(style.color), luminance(background)].sort((a, b) => b - a)
|
|
const ratio = (values[0]! + 0.05) / (values[1]! + 0.05)
|
|
if (ratio < 4.5) failures.push(`${button.className}: ${style.color} on ${background} = ${ratio.toFixed(2)}`)
|
|
button.remove()
|
|
}
|
|
}
|
|
}
|
|
return failures
|
|
})
|
|
expect(results).toEqual([])
|
|
})
|
|
}
|
|
|
|
test('selection cards retain readable secondary labels and statuses', async ({ page }) => {
|
|
await page.setContent(`
|
|
<div class="registry-switch"><button><span>类别</span></button></div>
|
|
<div class="plan-list"><button><small>说明</small><i>草稿</i><i class="published">已发布</i><i class="archived">已归档</i></button></div>
|
|
<div class="schedule-version-strip"><button><small>版本说明</small></button></div>
|
|
<div class="selection-round-strip"><button><span>轮次说明</span><i>待开始</i><i class="open">开放中</i></button></div>
|
|
<div class="grade-sheet-list"><button><i>草稿</i><i class="submitted">已提交</i><i class="published">已发布</i><i class="returned">已退回</i></button></div>
|
|
`)
|
|
await page.addStyleTag({ content: readFileSync(resolve('src/style.css'), 'utf8') })
|
|
const failures = await page.evaluate(() => {
|
|
const lightness = (color: string) => {
|
|
const rgb = color.match(/[\d.]+/g)!.slice(0, 3).map(Number).map(x => {
|
|
const value = x / 255
|
|
return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4
|
|
})
|
|
return 0.2126 * rgb[0]! + 0.7152 * rgb[1]! + 0.0722 * rgb[2]!
|
|
}
|
|
return [...document.querySelectorAll('button span, button small, button i')].flatMap(element => {
|
|
const color = getComputedStyle(element).color
|
|
let background = 'rgb(255, 255, 255)'
|
|
for (let current: Element | null = element; current; current = current.parentElement) {
|
|
const candidate = getComputedStyle(current).backgroundColor
|
|
if (candidate !== 'rgba(0, 0, 0, 0)') { background = candidate; break }
|
|
}
|
|
const values = [lightness(color), lightness(background)].sort((a, b) => b - a)
|
|
const contrast = (values[0]! + 0.05) / (values[1]! + 0.05)
|
|
return contrast < 4.5 ? [`${element.parentElement?.parentElement?.className} ${element.className}: ${contrast.toFixed(2)}`] : []
|
|
})
|
|
})
|
|
expect(failures).toEqual([])
|
|
})
|