fix: make CHRNX header fully progressive
This commit is contained in:
13 files changed
+414
-36
No files matched your search
@@ -14,6 +14,10 @@ async function expectTargetSize( locator ) {
|
||||
expect( box.height ).toBeGreaterThanOrEqual( 44 );
|
||||
}
|
||||
|
||||
async function getHomeURL( page ) {
|
||||
return page.getByRole( 'banner' ).getByRole( 'link', { name: 'CHRNX' } ).getAttribute( 'href' );
|
||||
}
|
||||
|
||||
test.describe( 'site navigation shell', () => {
|
||||
test( 'desktop has a stable accessible header, approved local brand, and compact footer', async ( {
|
||||
page,
|
||||
@@ -41,19 +45,29 @@ test.describe( 'site navigation shell', () => {
|
||||
/\/wp-content\/themes\/chrnx\/assets\/icons\/chrnx-wordmark\.svg$/,
|
||||
);
|
||||
|
||||
expect( await header.getByRole( 'navigation' ).count() ).toBe( 1 );
|
||||
const homeURL = await getHomeURL( page );
|
||||
for ( const [ name, path ] of destinations ) {
|
||||
const link = nav.getByRole( 'link', { name, exact: true } );
|
||||
await expect( link ).toHaveAttribute( 'href', path );
|
||||
await expect( link ).toHaveAttribute( 'href', new URL( path.slice( 1 ), homeURL ).href );
|
||||
}
|
||||
await expect( header.locator( '[data-chrnx-menu-toggle]' ) ).toBeHidden();
|
||||
const compactSearch = header.locator( '.wp-block-search__button' );
|
||||
const compactSearch = header.locator( '[data-chrnx-search-button]' );
|
||||
await expect( compactSearch ).toBeVisible();
|
||||
await expect( compactSearch ).toHaveAccessibleName( 'Expand search field' );
|
||||
await expect( compactSearch ).toHaveAccessibleName( 'Open search' );
|
||||
await expect( header.getByRole( 'searchbox' ) ).toBeHidden();
|
||||
await expectTargetSize( compactSearch );
|
||||
await compactSearch.click();
|
||||
await expect( header.getByRole( 'searchbox' ) ).toBeVisible();
|
||||
await expect( compactSearch ).toHaveAccessibleName( 'Submit Search' );
|
||||
await expect( header.getByRole( 'searchbox' ) ).toBeFocused();
|
||||
await expect( compactSearch ).toHaveAccessibleName( 'Search' );
|
||||
await page.keyboard.press( 'Escape' );
|
||||
await expect( compactSearch ).toBeFocused();
|
||||
await expect( header.getByRole( 'searchbox' ) ).toBeHidden();
|
||||
await compactSearch.click();
|
||||
await header.getByRole( 'searchbox' ).fill( 'portable cats' );
|
||||
await compactSearch.click();
|
||||
await expect( page ).toHaveURL( new RegExp( `[?&]s=portable(?:\\+|%20)cats(?:&|$)` ) );
|
||||
const before = await header.boundingBox();
|
||||
await page.evaluate( () => window.scrollTo( 0, document.body.scrollHeight ) );
|
||||
const after = await header.boundingBox();
|
||||
@@ -86,7 +100,7 @@ test.describe( 'site navigation shell', () => {
|
||||
).not.toBe( 'none' );
|
||||
await expectTargetSize( journal );
|
||||
await expectTargetSize(
|
||||
page.getByRole( 'banner' ).getByRole( 'button', { name: 'Expand search field' } ),
|
||||
page.getByRole( 'banner' ).getByRole( 'button', { name: 'Open search' } ),
|
||||
);
|
||||
} );
|
||||
|
||||
@@ -104,10 +118,11 @@ test.describe( 'site navigation shell', () => {
|
||||
await expect( open ).toHaveAccessibleName( 'Close primary navigation' );
|
||||
const nav = header.getByRole( 'navigation', { name: 'Primary navigation' } );
|
||||
await expect( nav ).toBeVisible();
|
||||
const homeURL = await getHomeURL( page );
|
||||
for ( const [ name, path ] of destinations ) {
|
||||
await expect( nav.getByRole( 'link', { name, exact: true } ) ).toHaveAttribute(
|
||||
'href',
|
||||
path,
|
||||
new URL( path.slice( 1 ), homeURL ).href,
|
||||
);
|
||||
}
|
||||
await open.focus();
|
||||
@@ -117,7 +132,7 @@ test.describe( 'site navigation shell', () => {
|
||||
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 expect( header.getByRole( 'button', { name: 'Open search' } ) ).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();
|
||||
@@ -125,8 +140,8 @@ test.describe( 'site navigation shell', () => {
|
||||
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' );
|
||||
const compactSearch = header.locator( '[data-chrnx-search-button]' );
|
||||
await expect( compactSearch ).toHaveAccessibleName( 'Open search' );
|
||||
await expectTargetSize( compactSearch );
|
||||
await compactSearch.click();
|
||||
await expect( header.getByRole( 'searchbox' ) ).toBeVisible();
|
||||
@@ -135,4 +150,42 @@ test.describe( 'site navigation shell', () => {
|
||||
await expect( nav ).toBeVisible();
|
||||
await expect( open ).toHaveAttribute( 'aria-expanded', 'false' );
|
||||
} );
|
||||
|
||||
test( 'resize never leaves focus inside hidden navigation', async ( { page } ) => {
|
||||
await page.setViewportSize( { width: 1280, height: 800 } );
|
||||
await page.goto( '/' );
|
||||
const header = page.getByRole( 'banner' );
|
||||
const nav = header.getByRole( 'navigation', { name: 'Primary navigation' } );
|
||||
const journal = nav.getByRole( 'link', { name: 'Journal', exact: true } );
|
||||
const toggle = header.locator( '[data-chrnx-menu-toggle]' );
|
||||
await journal.focus();
|
||||
await page.setViewportSize( { width: 390, height: 844 } );
|
||||
await expect( nav ).toBeHidden();
|
||||
await expect( toggle ).toBeFocused();
|
||||
await toggle.click();
|
||||
await journal.focus();
|
||||
await page.setViewportSize( { width: 1280, height: 800 } );
|
||||
await expect( nav ).toBeVisible();
|
||||
await expect( journal ).toBeFocused();
|
||||
} );
|
||||
|
||||
test.describe( 'without JavaScript', () => {
|
||||
test.use( { javaScriptEnabled: false } );
|
||||
|
||||
test( 'navigation and named search remain fully usable', async ( { page } ) => {
|
||||
await page.setViewportSize( { width: 390, height: 844 } );
|
||||
await page.goto( '/' );
|
||||
const header = page.getByRole( 'banner' );
|
||||
await expect(
|
||||
header.getByRole( 'navigation', { name: 'Primary navigation' } ),
|
||||
).toBeVisible();
|
||||
const search = header.getByRole( 'searchbox', { name: 'Search' } );
|
||||
const submit = header.getByRole( 'button', { name: 'Search', exact: true } );
|
||||
await expect( search ).toBeVisible();
|
||||
await expect( submit ).toBeVisible();
|
||||
await search.fill( 'no js cats' );
|
||||
await submit.click();
|
||||
await expect( page ).toHaveURL( new RegExp( `[?&]s=no(?:\\+|%20)js(?:\\+|%20)cats(?:&|$)` ) );
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
@@ -75,4 +75,24 @@ describe( 'progressive mobile navigation', () => {
|
||||
expect( document.querySelector( '[data-chrnx-menu-toggle]' ).hidden ).toBe( true );
|
||||
expect( document.documentElement.classList ).toContain( 'chrnx-navigation-enhanced' );
|
||||
} );
|
||||
|
||||
test( 'resize moves focus out of a navigation that becomes hidden', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
const media = createMedia( false );
|
||||
initialize( { document, media } );
|
||||
const button = document.querySelector( '[data-chrnx-menu-toggle]' );
|
||||
const nav = document.querySelector( 'nav' );
|
||||
nav.querySelector( 'a' ).focus();
|
||||
media.change( true );
|
||||
expect( nav.hidden ).toBe( true );
|
||||
expect( button.hidden ).toBe( false );
|
||||
expect( document.activeElement ).toBe( button );
|
||||
|
||||
button.click();
|
||||
nav.querySelector( 'a' ).focus();
|
||||
media.change( false );
|
||||
expect( nav.hidden ).toBe( false );
|
||||
expect( button.hidden ).toBe( true );
|
||||
expect( document.activeElement ).toBe( nav.querySelector( 'a' ) );
|
||||
} );
|
||||
} );
|
||||
@@ -0,0 +1,62 @@
|
||||
const modulePath = '../../assets/js/search.js';
|
||||
|
||||
function markup() {
|
||||
document.body.innerHTML = `
|
||||
<form data-chrnx-search action="/" role="search">
|
||||
<label for="chrnx-search-input">Search</label>
|
||||
<input id="chrnx-search-input" name="s" type="search">
|
||||
<button type="submit" data-chrnx-search-button data-label-open="Open search"
|
||||
data-label-submit="Search">Search</button>
|
||||
</form>
|
||||
`;
|
||||
}
|
||||
|
||||
describe( 'progressive header search', () => {
|
||||
beforeEach( () => {
|
||||
jest.resetModules();
|
||||
document.documentElement.className = 'js';
|
||||
markup();
|
||||
} );
|
||||
|
||||
test( 'first submit expands and focuses the empty search instead of submitting', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
expect( initialize( { document } ) ).toBe( true );
|
||||
const form = document.querySelector( 'form' );
|
||||
const input = document.querySelector( 'input' );
|
||||
const button = document.querySelector( 'button' );
|
||||
const event = new Event( 'submit', { bubbles: true, cancelable: true } );
|
||||
form.dispatchEvent( event );
|
||||
expect( event.defaultPrevented ).toBe( true );
|
||||
expect( form.dataset.expanded ).toBe( 'true' );
|
||||
expect( input.hidden ).toBe( false );
|
||||
expect( document.activeElement ).toBe( input );
|
||||
expect( button.getAttribute( 'aria-label' ) ).toBe( 'Search' );
|
||||
} );
|
||||
|
||||
test( 'a non-empty query submits natively without interception', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document } );
|
||||
const form = document.querySelector( 'form' );
|
||||
const input = document.querySelector( 'input' );
|
||||
form.dispatchEvent( new Event( 'submit', { bubbles: true, cancelable: true } ) );
|
||||
input.value = 'cat notes';
|
||||
const event = new Event( 'submit', { bubbles: true, cancelable: true } );
|
||||
form.dispatchEvent( event );
|
||||
expect( event.defaultPrevented ).toBe( false );
|
||||
} );
|
||||
|
||||
test( 'Escape collapses and restores focus without duplicate listeners', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document } );
|
||||
initialize( { document } );
|
||||
const form = document.querySelector( 'form' );
|
||||
const input = document.querySelector( 'input' );
|
||||
const button = document.querySelector( 'button' );
|
||||
form.dispatchEvent( new Event( 'submit', { bubbles: true, cancelable: true } ) );
|
||||
input.dispatchEvent( new window.KeyboardEvent( 'keydown', { key: 'Escape', bubbles: true } ) );
|
||||
expect( form.dataset.expanded ).toBe( 'false' );
|
||||
expect( input.hidden ).toBe( true );
|
||||
expect( document.activeElement ).toBe( button );
|
||||
expect( button.getAttribute( 'aria-label' ) ).toBe( 'Open search' );
|
||||
} );
|
||||
} );
|
||||
@@ -15,9 +15,9 @@ class BlockRenderingTest extends WP_UnitTestCase {
|
||||
delete_option( 'chrnx_subscribe_page_id' );
|
||||
}
|
||||
|
||||
/** All eleven block types register from metadata. */
|
||||
/** All twelve 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', 'mobile-menu-toggle', 'footer-content' );
|
||||
$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', 'header-tools' );
|
||||
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,53 @@ class BlockRenderingTest extends WP_UnitTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
/** Header navigation and search use translated labels and portable home URLs. */
|
||||
public function test_header_tools_render_localized_portable_navigation_and_search() {
|
||||
$translate = static function ( $translated, $text, $domain ) {
|
||||
if ( 'chrnx' !== $domain ) {
|
||||
return $translated;
|
||||
}
|
||||
return array(
|
||||
'Primary navigation' => 'Navegación principal',
|
||||
'Journal' => 'Diario',
|
||||
'Search' => 'Buscar',
|
||||
'Open search' => 'Abrir búsqueda',
|
||||
)[ $text ] ?? $translated;
|
||||
};
|
||||
$portable_home = static function ( $url, $path ) {
|
||||
return 'https://example.test/subdirectory/' . ltrim( (string) $path, '/' );
|
||||
};
|
||||
add_filter( 'gettext', $translate, 10, 3 );
|
||||
add_filter( 'home_url', $portable_home, 10, 2 );
|
||||
try {
|
||||
$output = render_block(
|
||||
array(
|
||||
'blockName' => 'chrnx/header-tools',
|
||||
'attrs' => array(),
|
||||
'innerBlocks' => array(),
|
||||
'innerHTML' => '',
|
||||
'innerContent' => array(),
|
||||
)
|
||||
);
|
||||
} finally {
|
||||
remove_filter( 'home_url', $portable_home, 10 );
|
||||
remove_filter( 'gettext', $translate, 10 );
|
||||
}
|
||||
|
||||
$this->assertSame( 1, substr_count( $output, '<nav ' ) );
|
||||
$this->assertStringContainsString( 'aria-label="Navegación principal"', $output );
|
||||
$this->assertSame( 1, substr_count( $output, 'aria-label="Navegación principal"' ) );
|
||||
$this->assertStringContainsString( '>Diario</a>', $output );
|
||||
foreach ( array( 'journal', 'projects', 'music', 'podcasts', 'about' ) as $path ) {
|
||||
$this->assertStringContainsString( 'href="https://example.test/subdirectory/' . $path . '/"', $output );
|
||||
}
|
||||
$this->assertStringContainsString( 'role="search"', $output );
|
||||
$this->assertStringContainsString( 'action="https://example.test/subdirectory/"', $output );
|
||||
$this->assertStringContainsString( 'name="s"', $output );
|
||||
$this->assertStringContainsString( 'chrnx-screen-reader-text">Buscar</span>', $output );
|
||||
$this->assertStringContainsString( 'data-label-open="Abrir búsqueda"', $output );
|
||||
}
|
||||
|
||||
/** 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(
|
||||
|
||||
@@ -43,3 +43,15 @@ test('footer user-facing copy lives in an extractable PHP render block', async (
|
||||
assert.ok(render.includes(`'${text}', 'chrnx'`), `${text} must use the chrnx text domain`);
|
||||
}
|
||||
});
|
||||
|
||||
test('header navigation and search copy lives in one extractable PHP render block', async () => {
|
||||
const part = await readProjectFile('parts/header.html');
|
||||
const render = await readProjectFile('blocks/header-tools/render.php');
|
||||
assert.match(part, /<!-- wp:chrnx\/header-tools \/-->/);
|
||||
assert.doesNotMatch(part, /wp:navigation|wp:navigation-link|wp:search|Journal|Primary navigation/);
|
||||
for (const text of ['Primary navigation', 'Journal', 'Projects', 'Music', 'Podcasts', 'About', 'Search', 'Open search']) {
|
||||
assert.ok(render.includes(`'${text}', 'chrnx'`), `${text} must use the chrnx text domain`);
|
||||
}
|
||||
assert.match(render, /home_url\( '\/journal\/' \)/);
|
||||
assert.match(render, /home_url\( '\/' \)/);
|
||||
});
|
||||
Reference in new issue
Block a user