fix: harden CHRNX shell accessibility
This commit is contained in:
14 files changed
+520
-54
No files matched your search
@@ -45,7 +45,15 @@ test.describe( 'site navigation shell', () => {
|
||||
const link = nav.getByRole( 'link', { name, exact: true } );
|
||||
await expect( link ).toHaveAttribute( 'href', path );
|
||||
}
|
||||
await expect( header.getByRole( 'button', { name: /menu/i } ) ).toBeHidden();
|
||||
await expect( header.locator( '[data-chrnx-menu-toggle]' ) ).toBeHidden();
|
||||
const compactSearch = header.locator( '.wp-block-search__button' );
|
||||
await expect( compactSearch ).toBeVisible();
|
||||
await expect( compactSearch ).toHaveAccessibleName( 'Expand search field' );
|
||||
await expect( header.getByRole( 'searchbox' ) ).toBeHidden();
|
||||
await expectTargetSize( compactSearch );
|
||||
await compactSearch.click();
|
||||
await expect( header.getByRole( 'searchbox' ) ).toBeVisible();
|
||||
await expect( compactSearch ).toHaveAccessibleName( 'Submit Search' );
|
||||
const before = await header.boundingBox();
|
||||
await page.evaluate( () => window.scrollTo( 0, document.body.scrollHeight ) );
|
||||
const after = await header.boundingBox();
|
||||
@@ -54,6 +62,11 @@ test.describe( 'site navigation shell', () => {
|
||||
|
||||
const footer = page.getByRole( 'contentinfo' );
|
||||
await expect( footer ).toBeVisible();
|
||||
expect(
|
||||
await footer
|
||||
.locator( '.chrnx-site-footer__inner' )
|
||||
.evaluate( ( element ) => window.getComputedStyle( element ).display ),
|
||||
).toBe( 'flex' );
|
||||
await expect( footer.getByRole( 'navigation', { name: 'Footer navigation' } ) ).toBeVisible();
|
||||
await expect( footer.getByText( 'Made in Sinaloa, shared from home.' ) ).toBeVisible();
|
||||
await expect(
|
||||
@@ -72,19 +85,23 @@ test.describe( 'site navigation shell', () => {
|
||||
await journal.evaluate( ( element ) => window.getComputedStyle( element ).outlineStyle ),
|
||||
).not.toBe( 'none' );
|
||||
await expectTargetSize( journal );
|
||||
await expectTargetSize( page.getByRole( 'searchbox' ) );
|
||||
await expectTargetSize(
|
||||
page.getByRole( 'banner' ).getByRole( 'button', { name: 'Expand search field' } ),
|
||||
);
|
||||
} );
|
||||
|
||||
test( 'mobile core navigation opens, closes with Escape, and restores focus without trapping Tab', async ( {
|
||||
test( 'mobile navigation follows normal Tab order and Escape restores the trigger', async ( {
|
||||
page,
|
||||
} ) => {
|
||||
}, testInfo ) => {
|
||||
await page.setViewportSize( { width: 390, height: 844 } );
|
||||
await page.goto( '/' );
|
||||
const header = page.getByRole( 'banner' );
|
||||
const open = header.getByRole( 'button', { name: /open menu/i } );
|
||||
const open = header.locator( '[data-chrnx-menu-toggle]' );
|
||||
await expect( open ).toBeVisible();
|
||||
await expect( open ).toHaveAccessibleName( 'Open primary navigation' );
|
||||
await expectTargetSize( open );
|
||||
await open.click();
|
||||
await expect( open ).toHaveAccessibleName( 'Close primary navigation' );
|
||||
const nav = header.getByRole( 'navigation', { name: 'Primary navigation' } );
|
||||
await expect( nav ).toBeVisible();
|
||||
for ( const [ name, path ] of destinations ) {
|
||||
@@ -93,17 +110,29 @@ test.describe( 'site navigation shell', () => {
|
||||
path,
|
||||
);
|
||||
}
|
||||
await open.focus();
|
||||
const focusKey = testInfo.project.name === 'webkit' ? 'Alt+Tab' : 'Tab';
|
||||
for ( const [ name ] of destinations ) {
|
||||
await page.keyboard.press( focusKey );
|
||||
await expect( nav.getByRole( 'link', { name, exact: true } ) ).toBeFocused();
|
||||
}
|
||||
await page.keyboard.press( focusKey );
|
||||
await expect( header.getByRole( 'button', { name: 'Expand search field' } ) ).toBeFocused();
|
||||
await page.keyboard.press( focusKey );
|
||||
await expect( header.getByRole( 'radio', { name: 'System', exact: true } ) ).toBeFocused();
|
||||
await nav.getByRole( 'link', { name: 'Music', exact: true } ).focus();
|
||||
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();
|
||||
await expect( open ).toHaveAccessibleName( 'Open primary navigation' );
|
||||
await expect( nav ).toBeHidden();
|
||||
const compactSearch = header.locator( '.wp-block-search__button' );
|
||||
await expect( compactSearch ).toHaveAccessibleName( 'Expand search field' );
|
||||
await expectTargetSize( compactSearch );
|
||||
await compactSearch.click();
|
||||
await expect( header.getByRole( 'searchbox' ) ).toBeVisible();
|
||||
await page.setViewportSize( { width: 1280, height: 800 } );
|
||||
await expect( open ).toBeHidden();
|
||||
await expect( nav ).toBeVisible();
|
||||
await expect( open ).toHaveAttribute( 'aria-expanded', 'false' );
|
||||
} );
|
||||
} );
|
||||
@@ -136,6 +136,7 @@ test.describe( 'real theme preference shell', () => {
|
||||
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.',
|
||||
);
|
||||
@@ -144,4 +145,32 @@ test.describe( 'real theme preference shell', () => {
|
||||
).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();
|
||||
} );
|
||||
}
|
||||
} );
|
||||
@@ -0,0 +1,78 @@
|
||||
const modulePath = '../../assets/js/navigation.js';
|
||||
|
||||
function createMedia( matches = true ) {
|
||||
const listeners = new Set();
|
||||
return {
|
||||
matches,
|
||||
addEventListener: jest.fn( ( _type, listener ) => listeners.add( listener ) ),
|
||||
removeEventListener: jest.fn( ( _type, listener ) => listeners.delete( listener ) ),
|
||||
change( next ) {
|
||||
this.matches = next;
|
||||
listeners.forEach( ( listener ) => listener( { matches: next } ) );
|
||||
},
|
||||
listenerCount: () => listeners.size,
|
||||
};
|
||||
}
|
||||
|
||||
function shellMarkup() {
|
||||
document.body.innerHTML = `
|
||||
<button hidden data-chrnx-menu-toggle aria-controls="chrnx-primary-navigation"
|
||||
aria-expanded="false" data-label-open="Open primary navigation"
|
||||
data-label-close="Close primary navigation">Open primary navigation</button>
|
||||
<nav id="chrnx-primary-navigation"><a href="/journal/">Journal</a></nav>
|
||||
<input aria-label="Search">
|
||||
`;
|
||||
}
|
||||
|
||||
describe( 'progressive mobile navigation', () => {
|
||||
beforeEach( () => {
|
||||
jest.resetModules();
|
||||
document.documentElement.className = '';
|
||||
shellMarkup();
|
||||
} );
|
||||
|
||||
test( 'mobile toggle opens one target without intercepting Tab', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
const media = createMedia( true );
|
||||
expect( initialize( { document, media } ) ).toBe( true );
|
||||
const button = document.querySelector( '[data-chrnx-menu-toggle]' );
|
||||
const nav = document.querySelector( 'nav' );
|
||||
expect( button.hidden ).toBe( false );
|
||||
expect( nav.hidden ).toBe( true );
|
||||
button.click();
|
||||
expect( nav.hidden ).toBe( false );
|
||||
expect( button.getAttribute( 'aria-expanded' ) ).toBe( 'true' );
|
||||
expect( button.getAttribute( 'aria-label' ) ).toBe( 'Close primary navigation' );
|
||||
const tabEvent = new window.KeyboardEvent( 'keydown', {
|
||||
key: 'Tab',
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
} );
|
||||
document.dispatchEvent( tabEvent );
|
||||
expect( tabEvent.defaultPrevented ).toBe( false );
|
||||
} );
|
||||
|
||||
test( 'Escape closes from a descendant and restores trigger focus', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( true ) } );
|
||||
const button = document.querySelector( '[data-chrnx-menu-toggle]' );
|
||||
const nav = document.querySelector( 'nav' );
|
||||
button.click();
|
||||
nav.querySelector( 'a' ).focus();
|
||||
nav.dispatchEvent( new window.KeyboardEvent( 'keydown', { key: 'Escape', bubbles: true } ) );
|
||||
expect( nav.hidden ).toBe( true );
|
||||
expect( document.activeElement ).toBe( button );
|
||||
} );
|
||||
|
||||
test( 'desktop and reinitialization reset state without duplicate listeners', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
const media = createMedia( true );
|
||||
initialize( { document, media } );
|
||||
initialize( { document, media } );
|
||||
expect( media.listenerCount() ).toBe( 1 );
|
||||
media.change( false );
|
||||
expect( document.querySelector( 'nav' ).hidden ).toBe( false );
|
||||
expect( document.querySelector( '[data-chrnx-menu-toggle]' ).hidden ).toBe( true );
|
||||
expect( document.documentElement.classList ).toContain( 'chrnx-navigation-enhanced' );
|
||||
} );
|
||||
} );
|
||||
@@ -15,9 +15,9 @@ class BlockRenderingTest extends WP_UnitTestCase {
|
||||
delete_option( 'chrnx_subscribe_page_id' );
|
||||
}
|
||||
|
||||
/** All nine block types register from metadata. */
|
||||
/** All eleven block types register from metadata. */
|
||||
public function test_all_editorial_blocks_are_registered() {
|
||||
$names = array( 'featured-stories', 'journal-stream', 'latest-podcast', 'podcast-meta', 'topic-hero', 'related-posts', 'subscribe-cta', 'theme-toggle', 'site-brand' );
|
||||
$names = array( 'featured-stories', 'journal-stream', 'latest-podcast', 'podcast-meta', 'topic-hero', 'related-posts', 'subscribe-cta', 'theme-toggle', 'site-brand', 'mobile-menu-toggle', 'footer-content' );
|
||||
foreach ( $names as $name ) {
|
||||
$type = WP_Block_Type_Registry::get_instance()->get_registered( 'chrnx/' . $name );
|
||||
$this->assertInstanceOf( WP_Block_Type::class, $type, $name );
|
||||
@@ -25,6 +25,48 @@ class BlockRenderingTest extends WP_UnitTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
/** Mobile menu control is localized, initially hidden, and targets the one primary navigation. */
|
||||
public function test_mobile_menu_toggle_renders_progressive_control() {
|
||||
$output = render_block(
|
||||
array(
|
||||
'blockName' => 'chrnx/mobile-menu-toggle',
|
||||
'attrs' => array(),
|
||||
'innerBlocks' => array(),
|
||||
'innerHTML' => '',
|
||||
'innerContent' => array(),
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertStringContainsString( 'data-chrnx-menu-toggle', $output );
|
||||
$this->assertStringContainsString( 'aria-controls="chrnx-primary-navigation"', $output );
|
||||
$this->assertStringContainsString( 'aria-expanded="false"', $output );
|
||||
$this->assertStringContainsString( 'hidden', $output );
|
||||
$this->assertStringContainsString( 'Open primary navigation', $output );
|
||||
$this->assertStringContainsString( 'Close primary navigation', $output );
|
||||
}
|
||||
|
||||
/** Footer destinations and personal copy render through escaped translation functions. */
|
||||
public function test_footer_content_renders_local_internal_navigation() {
|
||||
$output = render_block(
|
||||
array(
|
||||
'blockName' => 'chrnx/footer-content',
|
||||
'attrs' => array(),
|
||||
'innerBlocks' => array(),
|
||||
'innerHTML' => '',
|
||||
'innerContent' => array(),
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertStringContainsString( 'aria-label="Footer navigation"', $output );
|
||||
foreach ( array( 'journal', 'projects', 'music', 'podcasts', 'about' ) as $path ) {
|
||||
$this->assertStringContainsString( 'href="' . esc_url( home_url( '/' . $path . '/' ) ) . '"', $output );
|
||||
}
|
||||
$this->assertStringContainsString( 'Made in Sinaloa, shared from home.', $output );
|
||||
$this->assertStringContainsString( esc_html( get_bloginfo( 'name' ) ), $output );
|
||||
$this->assertStringNotContainsString( 'facebook', strtolower( $output ) );
|
||||
$this->assertStringNotContainsString( '<form', $output );
|
||||
}
|
||||
|
||||
/** The approved local wordmark links to the portable site home URL. */
|
||||
public function test_site_brand_renders_escaped_local_home_and_wordmark_urls() {
|
||||
$output = render_block(
|
||||
|
||||
@@ -33,3 +33,13 @@ test('wordmark uses real fallback-aware SVG text and shares exact cat geometry',
|
||||
assert.match(wordmark, /<text\b[^>]*>CHRNX<\/text>/);
|
||||
assert.match(wordmark, /Zen Maru Gothic/);
|
||||
});
|
||||
|
||||
test('footer user-facing copy lives in an extractable PHP render block', async () => {
|
||||
const part = await readProjectFile('parts/footer.html');
|
||||
const render = await readProjectFile('blocks/footer-content/render.php');
|
||||
assert.match(part, /<!-- wp:chrnx\/footer-content \/-->/);
|
||||
assert.doesNotMatch(part, /Made in Sinaloa|Journal|Footer navigation/);
|
||||
for (const text of ['Footer navigation', 'Journal', 'Projects', 'Music', 'Podcasts', 'About', 'Made in Sinaloa, shared from home.']) {
|
||||
assert.ok(render.includes(`'${text}', 'chrnx'`), `${text} must use the chrnx text domain`);
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user