Files
chrnx-dev-wp/tests/e2e/navigation.spec.mjs
T

192 lines
8.6 KiB
JavaScript

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 );
}
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,
}, 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$/,
);
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', new URL( path.slice( 1 ), homeURL ).href );
}
await expect( header.locator( '[data-chrnx-menu-toggle]' ) ).toBeHidden();
const compactSearch = header.locator( '[data-chrnx-search-button]' );
await expect( compactSearch ).toBeVisible();
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( 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();
expect( after.y ).toBe( 0 );
expect( after.height ).toBe( before.height );
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(
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( 'banner' ).getByRole( 'button', { name: 'Open search' } ),
);
} );
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.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();
const homeURL = await getHomeURL( page );
for ( const [ name, path ] of destinations ) {
await expect( nav.getByRole( 'link', { name, exact: true } ) ).toHaveAttribute(
'href',
new URL( path.slice( 1 ), homeURL ).href,
);
}
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: '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();
await page.keyboard.press( 'Escape' );
await expect( open ).toBeFocused();
await expect( open ).toHaveAccessibleName( 'Open primary navigation' );
await expect( nav ).toBeHidden();
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();
await page.setViewportSize( { width: 1280, height: 800 } );
await expect( open ).toBeHidden();
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(?:&|$)` ) );
} );
} );
} );