fix: contain CHRNX theme controls
This commit is contained in:
2 files changed
+138
-13
No files matched your search
@@ -60,6 +60,35 @@ async function expectNoHorizontalOverflow( page, header ) {
|
||||
).toBe( true );
|
||||
}
|
||||
|
||||
function parseColor( value ) {
|
||||
const channels = value.match( /[\d.]+/g ).map( Number );
|
||||
if ( value.startsWith( 'color(srgb' ) ) {
|
||||
return channels.slice( 0, 3 ).map( ( channel ) => channel * 255 );
|
||||
}
|
||||
return channels.slice( 0, 3 );
|
||||
}
|
||||
|
||||
function contrastRatio( first, second ) {
|
||||
const luminance = ( color ) => {
|
||||
const channels = parseColor( color ).map( ( channel ) => {
|
||||
const normalized = channel / 255;
|
||||
return normalized <= 0.04045 ? normalized / 12.92 : ( ( normalized + 0.055 ) / 1.055 ) ** 2.4;
|
||||
} );
|
||||
return 0.2126 * channels[ 0 ] + 0.7152 * channels[ 1 ] + 0.0722 * channels[ 2 ];
|
||||
};
|
||||
const lighter = Math.max( luminance( first ), luminance( second ) );
|
||||
const darker = Math.min( luminance( first ), luminance( second ) );
|
||||
return ( lighter + 0.05 ) / ( darker + 0.05 );
|
||||
}
|
||||
|
||||
async function expectContainedTarget( locator, viewportWidth ) {
|
||||
const box = await getBox( locator );
|
||||
expect( box.x ).toBeGreaterThanOrEqual( 0 );
|
||||
expect( box.x + box.width ).toBeLessThanOrEqual( viewportWidth );
|
||||
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,
|
||||
@@ -319,11 +348,90 @@ test.describe( 'site navigation shell', () => {
|
||||
const radioBox = await getBox( radio );
|
||||
expect( radioBox.width ).toBeGreaterThan( 0 );
|
||||
expect( radioBox.height ).toBeGreaterThan( 0 );
|
||||
await expectContainedTarget( radio.locator( '..' ), width );
|
||||
}
|
||||
await expectNoHorizontalOverflow( page, header );
|
||||
} );
|
||||
}
|
||||
|
||||
for ( const scenario of [ 'long translated labels', '200% root text' ] ) {
|
||||
test( `compact mobile header contains theme controls with ${ scenario }`, async ( {
|
||||
page,
|
||||
} ) => {
|
||||
const width = 375;
|
||||
await page.setViewportSize( { width, height: 844 } );
|
||||
await page.goto( '/' );
|
||||
if ( scenario === 'long translated labels' ) {
|
||||
await page.locator( '[data-chrnx-theme-panel] label' ).evaluateAll( ( labels ) => {
|
||||
const translations = [
|
||||
'Helles Erscheinungsbild verwenden',
|
||||
'Dunkles Erscheinungsbild verwenden',
|
||||
'Automatisch dem Farbschema des Betriebssystems folgen',
|
||||
];
|
||||
labels.forEach( ( label, index ) => {
|
||||
const text = Array.from( label.childNodes ).find( ( node ) => node.nodeType === 3 );
|
||||
text.nodeValue = ` ${ translations[ index ] }`;
|
||||
} );
|
||||
} );
|
||||
} else {
|
||||
await page.evaluate( () => {
|
||||
document.documentElement.style.fontSize = '200%';
|
||||
} );
|
||||
}
|
||||
|
||||
const header = page.getByRole( 'banner' );
|
||||
const panel = header.locator( '[data-chrnx-theme-panel]' );
|
||||
await header.locator( '[data-chrnx-theme-button]' ).click();
|
||||
await expect( panel ).toBeVisible();
|
||||
const contained = [ panel, ...( await panel.locator( 'label, input[type="radio"]' ).all() ) ];
|
||||
for ( const control of contained ) {
|
||||
const box = await getBox( control );
|
||||
expect( box.x ).toBeGreaterThanOrEqual( 0 );
|
||||
expect( box.x + box.width ).toBeLessThanOrEqual( width );
|
||||
}
|
||||
for ( const label of await panel.locator( 'label' ).all() ) {
|
||||
await expectContainedTarget( label, width );
|
||||
}
|
||||
await expectNoHorizontalOverflow( page, header );
|
||||
} );
|
||||
}
|
||||
|
||||
for ( const colorScheme of [ 'light', 'dark' ] ) {
|
||||
test( `compact mobile header has contrasting ${ colorScheme } focus outlines`, async ( {
|
||||
page,
|
||||
} ) => {
|
||||
await page.emulateMedia( { colorScheme } );
|
||||
await page.setViewportSize( { width: 375, height: 844 } );
|
||||
await page.goto( '/' );
|
||||
await page.evaluate( ( theme ) => {
|
||||
document.documentElement.dataset.theme = theme;
|
||||
document.documentElement.style.colorScheme = theme;
|
||||
}, colorScheme );
|
||||
const header = page.getByRole( 'banner' );
|
||||
for ( const selector of [
|
||||
'[data-chrnx-search-button]',
|
||||
'[data-chrnx-theme-button]',
|
||||
'[data-chrnx-menu-toggle]',
|
||||
] ) {
|
||||
const control = header.locator( selector );
|
||||
await control.focus();
|
||||
const colors = await control.evaluate( ( element ) => {
|
||||
const controlStyle = window.getComputedStyle( element );
|
||||
const headerStyle = window.getComputedStyle( element.closest( 'header' ) );
|
||||
return {
|
||||
outline: controlStyle.outlineColor,
|
||||
outlineStyle: controlStyle.outlineStyle,
|
||||
outlineWidth: Number.parseFloat( controlStyle.outlineWidth ),
|
||||
background: headerStyle.backgroundColor,
|
||||
};
|
||||
} );
|
||||
expect( colors.outlineStyle ).not.toBe( 'none' );
|
||||
expect( colors.outlineWidth ).toBeGreaterThanOrEqual( 3 );
|
||||
expect( contrastRatio( colors.outline, colors.background ) ).toBeGreaterThanOrEqual( 3 );
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
test( 'compact mobile header stops at the exact 599px boundary', async ( { page } ) => {
|
||||
for ( const width of [ 600, 1280 ] ) {
|
||||
await page.setViewportSize( { width, height: 800 } );
|
||||
|
||||
Reference in new issue
Block a user