|
| 1 | +import { expect, test } from "../../../../playwright/coverage-fixture" |
| 2 | +import { mountedStory } from "../../../../playwright/mounted-story" |
| 3 | +import { applyVisualTheme, visualThemes } from "../../../../playwright/themes" |
| 4 | + |
| 5 | +test("finishes theme color transitions before contrast checks read them", async ({ mount, page }) => { |
| 6 | + const component = mountedStory(await mount("welcome")) |
| 7 | + const action = component.getByRole("button", { name: /provider/i }).first() |
| 8 | + await expect(action).toBeVisible() |
| 9 | + |
| 10 | + // Stretch the about 150 ms transition so it cannot end on its own before the assertions read it. |
| 11 | + // Reading the computed style flushes it, so the theme switch below starts the long transition. |
| 12 | + const transitionDuration = await action.evaluate((element) => { |
| 13 | + element.style.transitionDuration = "10s" |
| 14 | + return getComputedStyle(element).transitionDuration |
| 15 | + }) |
| 16 | + expect(transitionDuration).toBe("10s") |
| 17 | + |
| 18 | + // finish() throws on an infinite animation, so the theme switch must leave CSS animations running. |
| 19 | + await page.evaluate(() => { |
| 20 | + const style = document.createElement("style") |
| 21 | + style.id = "visual-theme-infinite-animation" |
| 22 | + style.textContent = |
| 23 | + "@keyframes visual-theme-pulse { to { opacity: 0.5; } }" + |
| 24 | + "#visual-theme-infinite-animation-probe { animation: visual-theme-pulse 1s infinite; }" |
| 25 | + const probe = document.createElement("div") |
| 26 | + probe.id = "visual-theme-infinite-animation-probe" |
| 27 | + document.head.append(style) |
| 28 | + document.body.append(probe) |
| 29 | + }) |
| 30 | + |
| 31 | + // The gallery starts in the dark theme, so this switch starts the Button border-color transition. |
| 32 | + await applyVisualTheme( |
| 33 | + page, |
| 34 | + visualThemes.find((theme) => theme.name === "high-contrast-light")!, |
| 35 | + ) |
| 36 | + |
| 37 | + const { runningTransitions, runningAnimations } = await page.evaluate(() => { |
| 38 | + const running = document.getAnimations().filter((animation) => animation.playState === "running") |
| 39 | + return { |
| 40 | + runningTransitions: running.filter((animation) => animation instanceof CSSTransition).length, |
| 41 | + runningAnimations: running.filter( |
| 42 | + (animation) => animation instanceof CSSAnimation && animation.animationName === "visual-theme-pulse", |
| 43 | + ).length, |
| 44 | + } |
| 45 | + }) |
| 46 | + expect(runningTransitions).toBe(0) |
| 47 | + expect(runningAnimations).toBe(1) |
| 48 | + // Read once instead of toHaveCSS, which retries until the transition ends on its own. |
| 49 | + expect(await action.evaluate((element) => getComputedStyle(element).borderTopColor)).toBe("rgb(15, 74, 133)") |
| 50 | + |
| 51 | + await page.evaluate(() => { |
| 52 | + document.getElementById("visual-theme-infinite-animation")?.remove() |
| 53 | + document.getElementById("visual-theme-infinite-animation-probe")?.remove() |
| 54 | + }) |
| 55 | +}) |
0 commit comments