Files
chrnx-dev-wp/tests/js/navigation.test.js
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

579 lines
22 KiB
JavaScript

const modulePath = '../../assets/js/navigation.js';
function createMedia( matches = true ) {
const listeners = new Set();
return {
matches,
addEventListener: jest.fn( ( type, listener ) => {
if ( type === 'change' ) {
listeners.add( listener );
}
} ),
removeEventListener: jest.fn( ( type, listener ) => {
if ( type === 'change' ) {
listeners.delete( listener );
}
} ),
change( next ) {
this.matches = next;
listeners.forEach( ( listener ) => listener( { matches: next } ) );
},
listenerCount: () => listeners.size,
};
}
function shellMarkup() {
document.body.innerHTML = `
<header data-chrnx-header-shell>
<a href="/" class="chrnx-site-brand" data-part="brand">CHRNX</a>
<button hidden data-part="menu" data-chrnx-menu-toggle
aria-controls="chrnx-primary-navigation" aria-expanded="false"
data-label-open="Open primary navigation"
data-label-close="Close primary navigation">Menu</button>
<nav id="chrnx-primary-navigation" data-part="nav">
<a href="/journal/">Journal</a><a href="/projects/">Projects</a>
</nav>
<form data-part="search" data-chrnx-search action="/" method="get"
role="search" data-expanded="true">
<div id="chrnx-search-panel" data-chrnx-search-panel>
<label for="chrnx-search-input">Search</label>
<input id="chrnx-search-input" name="s" type="search">
</div>
<button type="submit" data-chrnx-search-button data-label-open="Open search"
data-label-submit="Search" aria-controls="chrnx-search-panel"
aria-expanded="true">Search</button>
</form>
<div data-part="theme" class="chrnx-theme-control">
<button hidden type="button" data-chrnx-theme-button
aria-controls="chrnx-theme-panel" aria-expanded="false">Theme</button>
<div id="chrnx-theme-panel" role="group" aria-label="Color theme" data-chrnx-theme-panel data-chrnx-theme-toggle>
<button type="button" data-chrnx-theme-value="light" aria-pressed="false">Light</button>
<button type="button" data-chrnx-theme-value="dark" aria-pressed="false">Dark</button>
<button type="button" data-chrnx-theme-value="system" aria-pressed="true">System</button>
</div>
</div>
</header>
<button data-outside>Outside</button>
<div data-outside-content>Outside content</div>
`;
}
function controls() {
return {
shell: document.querySelector( '[data-chrnx-header-shell]' ),
brand: document.querySelector( '[data-part="brand"]' ),
menuButton: document.querySelector( '[data-chrnx-menu-toggle]' ),
nav: document.querySelector( '[data-part="nav"]' ),
firstLink: document.querySelector( '[data-part="nav"] a' ),
search: document.querySelector( '[data-chrnx-search]' ),
searchPanel: document.querySelector( '[data-chrnx-search-panel]' ),
searchInput: document.querySelector( 'input[name="s"]' ),
searchButton: document.querySelector( '[data-chrnx-search-button]' ),
theme: document.querySelector( '[data-part="theme"]' ),
themeButton: document.querySelector( '[data-chrnx-theme-button]' ),
themePanel: document.querySelector( '[data-chrnx-theme-panel]' ),
checkedRadio: document.querySelector(
'[data-chrnx-theme-panel] [data-chrnx-theme-value][aria-pressed="true"]',
),
firstRadio: document.querySelector( '[data-chrnx-theme-panel] [data-chrnx-theme-value]' ),
outside: document.querySelector( '[data-outside]' ),
outsideContent: document.querySelector( '[data-outside-content]' ),
};
}
function directOrder( shell ) {
return [ ...shell.children ].map( ( element ) => element.dataset.part );
}
function submit( form ) {
const event = new Event( 'submit', { bubbles: true, cancelable: true } );
form.dispatchEvent( event );
return event;
}
function press( target, key ) {
const event = new window.KeyboardEvent( 'keydown', {
key,
bubbles: true,
cancelable: true,
} );
target.dispatchEvent( event );
return event;
}
function visibleFocusableElements() {
return [ ...document.querySelectorAll( 'a[href], button, input' ) ].filter( ( element ) => {
if ( element.hidden || element.closest( '[hidden]' ) ) {
return false;
}
if ( ! element.dataset.chrnxThemeValue ) {
return true;
}
return element.getAttribute( 'aria-pressed' ) === 'true';
} );
}
function nextVisibleFocusable( element ) {
const focusables = visibleFocusableElements();
return focusables[ focusables.indexOf( element ) + 1 ];
}
function activePanelCount( control ) {
return [ control.searchButton, control.themeButton, control.menuButton ].filter(
( button ) => button.getAttribute( 'aria-expanded' ) === 'true',
).length;
}
describe( 'coordinated CHRNX header shell', () => {
beforeEach( () => {
jest.resetModules();
document.documentElement.className = '';
shellMarkup();
} );
test( 'mobile order is brand, Search, Theme, Menu, then Navigation', () => {
const { initialize } = require( modulePath );
expect( initialize( { document, media: createMedia( true ) } ) ).toBe( true );
const control = controls();
expect( directOrder( control.shell ) ).toEqual( [ 'brand', 'search', 'theme', 'menu', 'nav' ] );
expect( control.searchPanel.hidden ).toBe( true );
expect( control.themePanel.hidden ).toBe( true );
expect( control.nav.hidden ).toBe( true );
expect( activePanelCount( control ) ).toBe( 0 );
} );
test( 'Search, Theme, and Menu are mutually exclusive', () => {
const { initialize } = require( modulePath );
initialize( { document, media: createMedia( true ) } );
const control = controls();
submit( control.search );
expect( control.searchPanel.hidden ).toBe( false );
expect( activePanelCount( control ) ).toBe( 1 );
control.themeButton.click();
expect( control.searchPanel.hidden ).toBe( true );
expect( control.themePanel.hidden ).toBe( false );
expect( activePanelCount( control ) ).toBe( 1 );
control.menuButton.click();
expect( control.themePanel.hidden ).toBe( true );
expect( control.nav.hidden ).toBe( false );
expect( activePanelCount( control ) ).toBe( 1 );
} );
test.each( [
[ 'Theme', 'themeButton', 'searchPanel' ],
[ 'Menu', 'menuButton', 'searchPanel' ],
] )(
'opening %s focuses its trigger before hiding Search',
( _name, buttonName, hiddenPanelName ) => {
const { initialize } = require( modulePath );
initialize( { document, media: createMedia( true ) } );
const control = controls();
submit( control.search );
expect( document.activeElement ).toBe( control.searchInput );
let hidden = control[ hiddenPanelName ].hidden;
let focusAtHide = null;
Object.defineProperty( control[ hiddenPanelName ], 'hidden', {
configurable: true,
get: () => hidden,
set: ( value ) => {
if ( value ) {
focusAtHide = document.activeElement;
}
hidden = value;
},
} );
control[ buttonName ].click();
expect( focusAtHide ).toBe( control[ buttonName ] );
expect( document.activeElement ).toBe( control[ buttonName ] );
expect( control[ hiddenPanelName ].hidden ).toBe( true );
expect( document.activeElement.closest( '[hidden]' ) ).toBeNull();
},
);
test( 'opening Menu focuses its trigger before hiding Theme', () => {
const { initialize } = require( modulePath );
initialize( { document, media: createMedia( true ) } );
const control = controls();
control.themeButton.click();
control.checkedRadio.focus();
let hidden = control.themePanel.hidden;
let focusAtHide = null;
Object.defineProperty( control.themePanel, 'hidden', {
configurable: true,
get: () => hidden,
set: ( value ) => {
if ( value ) {
focusAtHide = document.activeElement;
}
hidden = value;
},
} );
control.menuButton.click();
expect( focusAtHide ).toBe( control.menuButton );
expect( document.activeElement ).toBe( control.menuButton );
expect( control.themePanel.hidden ).toBe( true );
expect( document.activeElement.closest( '[hidden]' ) ).toBeNull();
} );
test.each( [
[ 'Theme', 'themeButton', 'themePanel', 'checkedRadio' ],
[ 'Menu', 'menuButton', 'nav', 'firstLink' ],
] )(
'programmatic %s toggle-close restores its trigger before hiding the panel',
( _name, buttonName, panelName, descendantName ) => {
const { initialize } = require( modulePath );
initialize( { document, media: createMedia( true ) } );
const control = controls();
control[ buttonName ].click();
control[ descendantName ].focus();
let hidden = control[ panelName ].hidden;
let focusAtHide = null;
Object.defineProperty( control[ panelName ], 'hidden', {
configurable: true,
get: () => hidden,
set: ( value ) => {
if ( value ) {
focusAtHide = document.activeElement;
}
hidden = value;
},
} );
control[ buttonName ].click();
expect( focusAtHide ).toBe( control[ buttonName ] );
expect( document.activeElement ).toBe( control[ buttonName ] );
expect( control[ panelName ].hidden ).toBe( true );
expect( document.activeElement.closest( '[hidden]' ) ).toBeNull();
},
);
test( 'empty Search opens and focuses while a non-empty GET remains native', () => {
const { initialize } = require( modulePath );
initialize( { document, media: createMedia( true ) } );
const control = controls();
expect( control.search.dataset.expanded ).toBe( 'false' );
expect( control.searchPanel.hidden ).toBe( true );
expect( control.searchButton.getAttribute( 'aria-expanded' ) ).toBe( 'false' );
const emptyEvent = submit( control.search );
expect( emptyEvent.defaultPrevented ).toBe( true );
expect( control.search.dataset.expanded ).toBe( 'true' );
expect( control.searchPanel.hidden ).toBe( false );
expect( control.searchButton.getAttribute( 'aria-expanded' ) ).toBe( 'true' );
expect( control.searchButton.getAttribute( 'aria-label' ) ).toBe( 'Search' );
expect( document.activeElement ).toBe( control.searchInput );
control.searchInput.value = 'cat notes';
expect( submit( control.search ).defaultPrevented ).toBe( false );
} );
test( 'retained query reopens closed Search before a second native submit', () => {
const { initialize } = require( modulePath );
initialize( { document, media: createMedia( true ) } );
const control = controls();
submit( control.search );
control.searchInput.value = 'retained cats';
press( control.searchInput, 'Escape' );
expect( control.searchPanel.hidden ).toBe( true );
const reopenEvent = submit( control.search );
expect( reopenEvent.defaultPrevented ).toBe( true );
expect( control.searchInput.value ).toBe( 'retained cats' );
expect( control.searchPanel.hidden ).toBe( false );
expect( control.searchButton.getAttribute( 'aria-expanded' ) ).toBe( 'true' );
expect( document.activeElement ).toBe( control.searchInput );
const nativeEvent = submit( control.search );
expect( nativeEvent.defaultPrevented ).toBe( false );
expect( control.searchInput.value ).toBe( 'retained cats' );
} );
test( 'Escape closes Search and restores its trigger', () => {
const { initialize } = require( modulePath );
initialize( { document, media: createMedia( true ) } );
const control = controls();
submit( control.search );
press( control.searchInput, 'Escape' );
expect( control.search.dataset.expanded ).toBe( 'false' );
expect( control.searchPanel.hidden ).toBe( true );
expect( control.searchButton.getAttribute( 'aria-expanded' ) ).toBe( 'false' );
expect( control.searchButton.getAttribute( 'aria-label' ) ).toBe( 'Open search' );
expect( document.activeElement ).toBe( control.searchButton );
} );
test( 'Theme and Menu retain trigger focus and precede their panel contents', () => {
const { initialize } = require( modulePath );
initialize( { document, media: createMedia( true ) } );
const control = controls();
control.themeButton.focus();
control.themeButton.click();
expect( document.activeElement ).toBe( control.themeButton );
expect( nextVisibleFocusable( control.themeButton ) ).toBe( control.checkedRadio );
control.menuButton.focus();
control.menuButton.click();
expect( document.activeElement ).toBe( control.menuButton );
expect( nextVisibleFocusable( control.menuButton ) ).toBe( control.firstLink );
} );
test.each( [
[ 'Theme', 'themeButton', 'themePanel' ],
[ 'Menu', 'menuButton', 'nav' ],
] )( 'Escape closes %s and restores its trigger', ( _name, buttonName, panelName ) => {
const { initialize } = require( modulePath );
initialize( { document, media: createMedia( true ) } );
const control = controls();
control[ buttonName ].click();
control[ panelName ].querySelector( 'input, a, button' ).focus();
press( control[ panelName ], 'Escape' );
expect( control[ panelName ].hidden ).toBe( true );
expect( document.activeElement ).toBe( control[ buttonName ] );
} );
test( 'real outside focusable click order closes Theme after focus transfers', () => {
const { initialize } = require( modulePath );
initialize( { document, media: createMedia( true ) } );
const control = controls();
control.themeButton.click();
let hidden = control.themePanel.hidden;
let focusAtHide = null;
Object.defineProperty( control.themePanel, 'hidden', {
configurable: true,
get: () => hidden,
set: ( value ) => {
if ( value ) {
focusAtHide = document.activeElement;
}
hidden = value;
},
} );
control.outside.dispatchEvent( new Event( 'pointerdown', { bubbles: true } ) );
expect( control.themePanel.hidden ).toBe( false );
control.outside.focus();
control.outside.dispatchEvent( new Event( 'focusin', { bubbles: true } ) );
control.outside.dispatchEvent( new Event( 'click', { bubbles: true } ) );
expect( control.themePanel.hidden ).toBe( true );
expect( focusAtHide ).toBe( control.outside );
expect( document.activeElement ).toBe( control.outside );
} );
test( 'outside nonfocusable click closes Theme without hiding focused content', () => {
const { initialize } = require( modulePath );
initialize( { document, media: createMedia( true ) } );
const control = controls();
control.themeButton.click();
control.checkedRadio.focus();
let hidden = control.themePanel.hidden;
let focusAtHide = null;
Object.defineProperty( control.themePanel, 'hidden', {
configurable: true,
get: () => hidden,
set: ( value ) => {
if ( value ) {
focusAtHide = document.activeElement;
}
hidden = value;
},
} );
control.outsideContent.dispatchEvent( new Event( 'pointerdown', { bubbles: true } ) );
expect( control.themePanel.hidden ).toBe( false );
control.checkedRadio.blur();
expect( document.activeElement ).toBe( document.body );
control.outsideContent.dispatchEvent( new Event( 'click', { bubbles: true } ) );
expect( control.themePanel.hidden ).toBe( true );
expect( focusAtHide ).toBe( control.themeButton );
expect( document.activeElement ).toBe( control.themeButton );
expect( document.activeElement.closest( '[hidden]' ) ).toBeNull();
} );
test( 'Tab is never prevented', () => {
const { initialize } = require( modulePath );
initialize( { document, media: createMedia( true ) } );
const control = controls();
control.themeButton.click();
expect( press( control.themeButton, 'Tab' ).defaultPrevented ).toBe( false );
} );
test( '599 to 600 moves focus only when the focused control becomes hidden', () => {
const { initialize } = require( modulePath );
const media = createMedia( true );
initialize( { document, media } );
let control = controls();
submit( control.search );
control.searchInput.focus();
media.change( false );
expect( document.activeElement ).toBe( control.searchButton );
media.change( true );
control = controls();
control.menuButton.focus();
media.change( false );
expect( document.activeElement ).toBe( control.firstLink );
media.change( true );
control.themeButton.focus();
media.change( false );
expect( document.activeElement ).toBe( control.checkedRadio );
} );
test.each( [ 'light', 'dark' ] )(
'599 to 600 focuses the current %s radio selected after initialization',
( value ) => {
const { initialize } = require( modulePath );
const media = createMedia( true );
initialize( { document, media } );
const control = controls();
const currentRadio = control.themePanel.querySelector(
`[data-chrnx-theme-value="${ value }"]`,
);
control.checkedRadio.setAttribute( 'aria-pressed', 'false' );
currentRadio.setAttribute( 'aria-pressed', 'true' );
control.themeButton.focus();
media.change( false );
expect( document.activeElement ).toBe( currentRadio );
},
);
test( '599 to 600 safely falls back to the first radio when none is checked', () => {
const { initialize } = require( modulePath );
const media = createMedia( true );
initialize( { document, media } );
const control = controls();
control.themePanel.querySelectorAll( '[data-chrnx-theme-value]' ).forEach( ( radio ) => {
radio.setAttribute( 'aria-pressed', 'false' );
} );
control.themeButton.focus();
media.change( false );
expect( document.activeElement ).toBe( control.firstRadio );
} );
test( '599 to 600 preserves focus on a navigation link or visible radio', () => {
const { initialize } = require( modulePath );
const media = createMedia( true );
initialize( { document, media } );
const control = controls();
control.menuButton.click();
control.firstLink.focus();
media.change( false );
expect( document.activeElement ).toBe( control.firstLink );
media.change( true );
control.themeButton.click();
control.checkedRadio.focus();
media.change( false );
expect( document.activeElement ).toBe( control.checkedRadio );
} );
test( '600 to 599 moves focus from newly hidden desktop panels to their triggers', () => {
const { initialize } = require( modulePath );
const media = createMedia( false );
initialize( { document, media } );
const control = controls();
submit( control.search );
control.searchInput.focus();
media.change( true );
expect( control.searchPanel.hidden ).toBe( true );
expect( control.searchButton.getAttribute( 'aria-expanded' ) ).toBe( 'false' );
expect( document.activeElement ).toBe( control.searchButton );
media.change( false );
control.firstLink.focus();
media.change( true );
expect( document.activeElement ).toBe( control.menuButton );
media.change( false );
control.checkedRadio.focus();
media.change( true );
expect( document.activeElement ).toBe( control.themeButton );
expect( document.activeElement.hidden ).toBe( false );
expect( document.activeElement.closest( '[hidden]' ) ).toBeNull();
} );
test( 'desktop restores server order and progressive visibility', () => {
const { initialize } = require( modulePath );
initialize( { document, media: createMedia( false ) } );
const control = controls();
expect( directOrder( control.shell ) ).toEqual( [ 'brand', 'menu', 'nav', 'search', 'theme' ] );
expect( control.nav.hidden ).toBe( false );
expect( control.themePanel.hidden ).toBe( false );
expect( control.menuButton.hidden ).toBe( true );
expect( control.themeButton.hidden ).toBe( true );
expect( control.searchPanel.hidden ).toBe( true );
} );
test( 'reinitialization leaves one listener set and cleanup restores original DOM', () => {
const { initialize } = require( modulePath );
const media = createMedia( true );
const originalElements = [ ...controls().shell.children ];
initialize( { document, media } );
initialize( { document, media } );
expect( media.listenerCount() ).toBe( 1 );
const incompleteDocument = document.implementation.createHTMLDocument( 'incomplete' );
expect( initialize( { document: incompleteDocument, media } ) ).toBe( false );
const control = controls();
expect( [ ...control.shell.children ] ).toEqual( originalElements );
expect(
[ ...control.shell.childNodes ].filter( ( node ) => node.nodeType === 8 ),
).toHaveLength( 0 );
expect( media.listenerCount() ).toBe( 0 );
expect( document.documentElement.classList ).not.toContain( 'chrnx-navigation-enhanced' );
expect( control.nav.hidden ).toBe( false );
expect( control.searchPanel.hidden ).toBe( false );
expect( control.searchButton.getAttribute( 'aria-expanded' ) ).toBe( 'true' );
expect( control.themePanel.hidden ).toBe( false );
expect( control.menuButton.hidden ).toBe( true );
} );
test.each( [ null, {}, { matches: true, addEventListener() {} } ] )(
'fails closed without a complete media API (%p)',
( media ) => {
const { initialize } = require( modulePath );
const originalOrder = [ ...controls().shell.children ];
expect( initialize( { document, media } ) ).toBe( false );
expect( [ ...controls().shell.children ] ).toEqual( originalOrder );
expect( document.documentElement.className ).toBe( '' );
},
);
test( 'fails closed when any required header element is missing', () => {
const { initialize } = require( modulePath );
controls().themeButton.remove();
const originalOrder = [ ...controls().shell.children ];
expect( initialize( { document, media: createMedia( true ) } ) ).toBe( false );
expect( [ ...controls().shell.children ] ).toEqual( originalOrder );
expect( document.documentElement.className ).toBe( '' );
} );
test.each( [
[ 'brand', '.chrnx-site-brand' ],
[ 'navigation link', '[data-part="nav"] a' ],
[ 'theme button', '[data-chrnx-theme-panel] [data-chrnx-theme-value]' ],
] )( 'fails closed without the required %s', ( _name, selector ) => {
const { initialize } = require( modulePath );
document.querySelectorAll( selector ).forEach( ( element ) => element.remove() );
const originalOrder = [ ...controls().shell.children ];
expect( initialize( { document, media: createMedia( true ) } ) ).toBe( false );
expect( [ ...controls().shell.children ] ).toEqual( originalOrder );
expect( document.documentElement.className ).toBe( '' );
} );
} );