Files
chrnx-dev-wp/tests/e2e/theme-preference.spec.mjs
T
chrnxandClaude Opus 5 a142ece51d feat: rebuild the header controls as icon buttons
The theme switch was three labelled radios that wrapped the header onto
a second line with long translations. It is three SVG buttons now, with
search and menu sharing the same language: no resting outline, a filled
chip for the active choice.

Radios gave arrow-key roving for free, so the group reimplements it
rather than losing it. The first chip colour put accent on accent and
failed WCAG AA; the label carries full contrast instead. Mobile targets
sit at 45px because a floor of exactly 44 rounds to 43.99997 in Firefox.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-13 19:41:24 -05:00

261 lines
10 KiB
JavaScript

import { expect, test } from '@playwright/test';
const preferenceKey = 'chrnx-theme';
async function observeFirstPaintTheme( page ) {
await page.addInitScript( () => {
window.__chrnxThemeFrames = [];
const sample = () => {
window.__chrnxThemeFrames.push( document.documentElement?.dataset.theme || '' );
if ( document.readyState === 'loading' ) {
window.requestAnimationFrame( sample );
}
};
window.requestAnimationFrame( sample );
} );
}
test.describe( 'real theme preference shell', () => {
test.beforeEach( async ( { page } ) => {
await page.goto( '/' );
await page.evaluate( ( key ) => localStorage.removeItem( key ), preferenceKey );
} );
test( 'inline bootstrap precedes styles and the first observable frame', async ( { page } ) => {
await observeFirstPaintTheme( page );
await page.emulateMedia( { colorScheme: 'dark' } );
const response = await page.goto( '/' );
const html = await response.text();
expect( html.indexOf( 'chrnx-theme-preference-bootstrap' ) ).toBeGreaterThan( -1 );
expect( html.indexOf( 'chrnx-theme-preference-bootstrap' ) ).toBeLessThan(
html.indexOf( 'chrnx-base-css' ),
);
await page.waitForLoadState( 'domcontentloaded' );
const result = await page.evaluate( () => ( {
frames: window.__chrnxThemeFrames,
theme: document.documentElement.dataset.theme,
} ) );
expect( result.theme ).toBe( 'dark' );
expect( result.frames.length ).toBeGreaterThan( 0 );
expect( result.frames.every( ( value ) => value === 'dark' ) ).toBe( true );
} );
test( 'saved choice wins, persists across reload, and synchronizes controls', async ( {
page,
} ) => {
await page.emulateMedia( { colorScheme: 'dark' } );
await page.goto( '/' );
const groups = page.locator( '[data-chrnx-theme-toggle]' );
await expect( groups.first() ).toHaveAccessibleName( 'Color theme' );
await groups.first().getByRole( 'button', { name: 'Light', exact: true } ).click();
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'light' );
await expect
.poll( () => page.evaluate( ( key ) => localStorage.getItem( key ), preferenceKey ) )
.toBe( 'light' );
await page.reload();
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'light' );
await expect( page.getByRole( 'button', { name: 'Light', exact: true } ) ).toHaveAttribute(
'aria-pressed',
'true',
);
await page.getByRole( 'button', { name: 'Dark', exact: true } ).click();
await page.reload();
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'dark' );
await expect( page.getByRole( 'button', { name: 'Dark', exact: true } ) ).toHaveAttribute(
'aria-pressed',
'true',
);
const instances = await page.locator( '[data-chrnx-theme-toggle]' ).count();
if ( instances > 1 ) {
await expect( page.getByRole( 'button', { name: 'Dark', exact: true } ) ).toHaveCount(
instances,
);
for ( const radio of await page.getByRole( 'button', { name: 'Dark', exact: true } ).all() ) {
await expect( radio ).toHaveAttribute( 'aria-pressed', 'true' );
}
}
} );
for ( const [ saved, operatingSystem ] of [
[ 'light', 'dark' ],
[ 'dark', 'light' ],
] ) {
test( `saved ${ saved } overrides initial and later operating-system changes`, async ( {
page,
} ) => {
await page.evaluate( ( { key, value } ) => localStorage.setItem( key, value ), {
key: preferenceKey,
value: saved,
} );
await page.emulateMedia( { colorScheme: operatingSystem } );
await page.reload();
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', saved );
const savedName = saved[ 0 ].toUpperCase() + saved.slice( 1 );
await expect( page.getByRole( 'button', { name: savedName, exact: true } ) ).toHaveAttribute(
'aria-pressed',
'true',
);
await page.emulateMedia( { colorScheme: saved } );
await page.emulateMedia( { colorScheme: operatingSystem } );
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', saved );
await expect
.poll( () => page.evaluate( ( key ) => localStorage.getItem( key ), preferenceKey ) )
.toBe( saved );
} );
}
test( 'system removes storage and follows emulated operating-system changes', async ( {
page,
} ) => {
await page.evaluate( ( key ) => localStorage.setItem( key, 'dark' ), preferenceKey );
await page.emulateMedia( { colorScheme: 'light' } );
await page.reload();
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'dark' );
await page.getByRole( 'button', { name: 'System', exact: true } ).click();
await expect
.poll( () => page.evaluate( ( key ) => localStorage.getItem( key ), preferenceKey ) )
.toBeNull();
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'light' );
await page.emulateMedia( { colorScheme: 'dark' } );
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'dark' );
await page.reload();
await expect( page.getByRole( 'button', { name: 'System', exact: true } ) ).toHaveAttribute(
'aria-pressed',
'true',
);
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'dark' );
await expect
.poll( () => page.evaluate( ( key ) => localStorage.getItem( key ), preferenceKey ) )
.toBeNull();
} );
test( 'mobile theme popover dismisses with safe, intentional focus', async ( { page } ) => {
await page.setViewportSize( { width: 390, height: 844 } );
await page.goto( '/' );
const header = page.getByRole( 'banner' );
const trigger = header.locator( '[data-chrnx-theme-button]' );
const panel = header.locator( '[data-chrnx-theme-panel]' );
const search = header.locator( '[data-chrnx-search-button]' );
await trigger.click();
await expect( trigger ).toHaveAttribute( 'aria-expanded', 'true' );
await page.keyboard.press( 'Escape' );
await expect( panel ).toBeHidden();
await expect( trigger ).toHaveAttribute( 'aria-expanded', 'false' );
await expect( trigger ).toBeFocused();
await trigger.click();
await search.focus();
await expect( panel ).toBeHidden();
await expect( search ).toBeFocused();
await expect( trigger ).toHaveAttribute( 'aria-expanded', 'false' );
await trigger.click();
await page.getByRole( 'button', { name: 'System', exact: true } ).focus();
await page.getByText( 'Made in Sinaloa, shared from home.', { exact: true } ).click();
await expect( panel ).toBeHidden();
await expect( trigger ).toBeVisible();
await expect( trigger ).toBeFocused();
await expect( trigger ).toHaveAttribute( 'aria-expanded', 'false' );
await trigger.click();
await expect( panel ).toBeVisible();
await trigger.click();
await expect( panel ).toBeHidden();
await expect( trigger ).toHaveAttribute( 'aria-expanded', 'false' );
} );
test( 'invalid storage and storage denial fall back safely', async ( { page } ) => {
await page.evaluate( ( key ) => localStorage.setItem( key, 'invalid' ), preferenceKey );
await page.emulateMedia( { colorScheme: 'light' } );
await page.reload();
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'light' );
await page.addInitScript( () => {
Object.defineProperty( window, 'localStorage', {
configurable: true,
get() {
throw new DOMException( 'Denied', 'SecurityError' );
},
} );
} );
await page.addInitScript( () => {
window.matchMedia = () => {
throw new Error( 'Unavailable' );
};
} );
await expect( page.goto( '/' ) ).resolves.toBeTruthy();
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'dark' );
} );
test( 'native toggle buttons expose state, keyboard interaction, status, and 44px targets', async ( {
page,
} ) => {
await page.goto( '/' );
const light = page.getByRole( 'button', { name: 'Light', exact: true } );
await light.focus();
await page.keyboard.press( 'ArrowRight' );
await expect( page.getByRole( 'button', { name: 'Dark', exact: true } ) ).toHaveAttribute(
'aria-pressed',
'true',
);
await expect( page.locator( '[data-chrnx-theme-status]' ) ).toHaveText( 'Dark theme active' );
for ( const label of await page
.locator( '[data-chrnx-theme-toggle] [data-chrnx-theme-value]' )
.all() ) {
const box = await label.boundingBox();
expect( box.width ).toBeGreaterThanOrEqual( 44 );
expect( box.height ).toBeGreaterThanOrEqual( 44 );
}
} );
test( 'without JavaScript the inert control is hidden and a clear explanation remains', async ( {
browser,
baseURL,
} ) => {
const context = await browser.newContext( {
baseURL,
javaScriptEnabled: false,
} );
const page = await context.newPage();
await page.goto( '/' );
await expect( page.locator( '[data-chrnx-theme-toggle]' ) ).toBeHidden();
await expect( page.locator( '[data-chrnx-menu-toggle]' ) ).toBeHidden();
await expect( page.locator( '.chrnx-theme-noscript' ) ).toHaveText(
'Enable JavaScript to choose a color theme.',
);
await expect(
page.getByRole( 'banner' ).getByRole( 'link', { name: 'Journal', exact: true } ),
).toBeVisible();
await context.close();
} );
for ( const [ scheme, background, colorScheme ] of [
[ 'light', 'rgb(246, 242, 233)', 'light' ],
[ 'dark', 'rgb(13, 17, 23)', 'dark' ],
] ) {
test( `without JavaScript follows ${ scheme } system colors`, async ( {
browser,
baseURL,
} ) => {
const context = await browser.newContext( {
baseURL,
colorScheme: scheme,
javaScriptEnabled: false,
} );
const page = await context.newPage();
await page.goto( '/' );
await expect( page.locator( 'html' ) ).not.toHaveAttribute( 'data-theme' );
const actual = await page.evaluate( () => ( {
background: window.getComputedStyle( document.body ).backgroundColor,
colorScheme: window.getComputedStyle( document.documentElement ).colorScheme,
} ) );
expect( actual ).toEqual( { background, colorScheme } );
await expect(
page.getByRole( 'banner' ).getByRole( 'link', { name: 'Journal', exact: true } ),
).toBeVisible();
await context.close();
} );
}
} );