feat: add CHRNX shell and theme preference
This commit is contained in:
18 files changed
+633
-16
No files matched your search
@@ -0,0 +1,109 @@
|
||||
import { expect, test } from '@playwright/test';
|
||||
|
||||
const destinations = [
|
||||
[ 'Journal', '/journal/' ],
|
||||
[ 'Projects', '/projects/' ],
|
||||
[ 'Music', '/music/' ],
|
||||
[ 'Podcasts', '/podcasts/' ],
|
||||
[ 'About', '/about/' ],
|
||||
];
|
||||
|
||||
async function expectTargetSize( locator ) {
|
||||
const box = await locator.boundingBox();
|
||||
expect( box.width ).toBeGreaterThanOrEqual( 44 );
|
||||
expect( box.height ).toBeGreaterThanOrEqual( 44 );
|
||||
}
|
||||
|
||||
test.describe( 'site navigation shell', () => {
|
||||
test( 'desktop has a stable accessible header, approved local brand, and compact footer', async ( {
|
||||
page,
|
||||
}, testInfo ) => {
|
||||
await page.setViewportSize( { width: 1280, height: 800 } );
|
||||
await page.goto( '/' );
|
||||
const skip = page.getByRole( 'link', { name: 'Skip to content' } );
|
||||
await page.keyboard.press( testInfo.project.name === 'webkit' ? 'Alt+Tab' : 'Tab' );
|
||||
await expect( skip ).toBeFocused();
|
||||
await expect( skip ).toHaveAttribute( 'href', '#chrnx-main' );
|
||||
await skip.press( 'Enter' );
|
||||
await expect( page.locator( '#chrnx-main' ) ).toBeFocused();
|
||||
|
||||
const header = page.getByRole( 'banner' );
|
||||
const nav = header.getByRole( 'navigation', { name: 'Primary navigation' } );
|
||||
await expect( header ).toBeVisible();
|
||||
await expect( nav ).toBeVisible();
|
||||
await expect( header.getByRole( 'search' ) ).toBeVisible();
|
||||
await expect( header.locator( '[data-chrnx-theme-toggle]' ) ).toBeVisible();
|
||||
const brand = header.getByRole( 'img', { name: 'CHRNX' } );
|
||||
await expect( brand ).toBeVisible();
|
||||
const brandSource = await brand.getAttribute( 'src' );
|
||||
expect( new URL( brandSource, page.url() ).origin ).toBe( new URL( page.url() ).origin );
|
||||
expect( new URL( brandSource, page.url() ).pathname ).toMatch(
|
||||
/\/wp-content\/themes\/chrnx\/assets\/icons\/chrnx-wordmark\.svg$/,
|
||||
);
|
||||
|
||||
for ( const [ name, path ] of destinations ) {
|
||||
const link = nav.getByRole( 'link', { name, exact: true } );
|
||||
await expect( link ).toHaveAttribute( 'href', path );
|
||||
}
|
||||
await expect( header.getByRole( 'button', { name: /menu/i } ) ).toBeHidden();
|
||||
const before = await header.boundingBox();
|
||||
await page.evaluate( () => window.scrollTo( 0, document.body.scrollHeight ) );
|
||||
const after = await header.boundingBox();
|
||||
expect( after.y ).toBe( 0 );
|
||||
expect( after.height ).toBe( before.height );
|
||||
|
||||
const footer = page.getByRole( 'contentinfo' );
|
||||
await expect( footer ).toBeVisible();
|
||||
await expect( footer.getByRole( 'navigation', { name: 'Footer navigation' } ) ).toBeVisible();
|
||||
await expect( footer.getByText( 'Made in Sinaloa, shared from home.' ) ).toBeVisible();
|
||||
await expect(
|
||||
footer.locator( 'form, a[href*="facebook"], a[href*="spotify"], a[href*="youtube"]' ),
|
||||
).toHaveCount( 0 );
|
||||
} );
|
||||
|
||||
test( 'visible keyboard focus and minimum target sizes survive layout', async ( { page } ) => {
|
||||
await page.setViewportSize( { width: 1280, height: 800 } );
|
||||
await page.goto( '/' );
|
||||
const journal = page
|
||||
.getByRole( 'banner' )
|
||||
.getByRole( 'link', { name: 'Journal', exact: true } );
|
||||
await journal.focus();
|
||||
expect(
|
||||
await journal.evaluate( ( element ) => window.getComputedStyle( element ).outlineStyle ),
|
||||
).not.toBe( 'none' );
|
||||
await expectTargetSize( journal );
|
||||
await expectTargetSize( page.getByRole( 'searchbox' ) );
|
||||
} );
|
||||
|
||||
test( 'mobile core navigation opens, closes with Escape, and restores focus without trapping Tab', async ( {
|
||||
page,
|
||||
} ) => {
|
||||
await page.setViewportSize( { width: 390, height: 844 } );
|
||||
await page.goto( '/' );
|
||||
const header = page.getByRole( 'banner' );
|
||||
const open = header.getByRole( 'button', { name: /open menu/i } );
|
||||
await expect( open ).toBeVisible();
|
||||
await expectTargetSize( open );
|
||||
await open.click();
|
||||
const nav = header.getByRole( 'navigation', { name: 'Primary navigation' } );
|
||||
await expect( nav ).toBeVisible();
|
||||
for ( const [ name, path ] of destinations ) {
|
||||
await expect( nav.getByRole( 'link', { name, exact: true } ) ).toHaveAttribute(
|
||||
'href',
|
||||
path,
|
||||
);
|
||||
}
|
||||
await page.keyboard.press( 'Escape' );
|
||||
await expect( open ).toBeFocused();
|
||||
await expect( nav.getByRole( 'link', { name: 'Journal', exact: true } ) ).toBeHidden();
|
||||
|
||||
await open.click();
|
||||
const close = header.getByRole( 'button', { name: /close menu/i } );
|
||||
await expectTargetSize( close );
|
||||
await close.focus();
|
||||
await page.keyboard.press( 'Tab' );
|
||||
await expect( close ).not.toBeFocused();
|
||||
await close.click();
|
||||
await expect( open ).toBeFocused();
|
||||
} );
|
||||
} );
|
||||
@@ -0,0 +1,147 @@
|
||||
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( 'radio', { name: 'Light', exact: true } ).check();
|
||||
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( 'radio', { name: 'Light', exact: true } ) ).toBeChecked();
|
||||
await page.getByRole( 'radio', { name: 'Dark', exact: true } ).check();
|
||||
await page.reload();
|
||||
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', 'dark' );
|
||||
await expect( page.getByRole( 'radio', { name: 'Dark', exact: true } ) ).toBeChecked();
|
||||
const instances = await page.locator( '[data-chrnx-theme-toggle]' ).count();
|
||||
if ( instances > 1 ) {
|
||||
await expect( page.getByRole( 'radio', { name: 'Dark', exact: true } ) ).toHaveCount(
|
||||
instances,
|
||||
);
|
||||
for ( const radio of await page.getByRole( 'radio', { name: 'Dark', exact: true } ).all() ) {
|
||||
await expect( radio ).toBeChecked();
|
||||
}
|
||||
}
|
||||
} );
|
||||
|
||||
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( 'radio', { name: 'System', exact: true } ).check();
|
||||
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' );
|
||||
} );
|
||||
|
||||
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 radios expose state, keyboard interaction, status, and 44px targets', async ( {
|
||||
page,
|
||||
} ) => {
|
||||
await page.goto( '/' );
|
||||
const light = page.getByRole( 'radio', { name: 'Light', exact: true } );
|
||||
await light.focus();
|
||||
await page.keyboard.press( 'ArrowRight' );
|
||||
await expect( page.getByRole( 'radio', { name: 'Dark', exact: true } ) ).toBeChecked();
|
||||
await expect( page.locator( '[data-chrnx-theme-status]' ) ).toHaveText( 'Dark theme active' );
|
||||
for ( const label of await page.locator( '[data-chrnx-theme-toggle] label' ).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( '.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();
|
||||
} );
|
||||
} );
|
||||
Reference in new issue
Block a user