test: verify Related Stories workflows

This commit is contained in:
chrnx committed 2026-08-12 21:54:50 -05:00
1 parent d8ad3d388f
commit 3a34a9990f
8 files changed
+702 -110

No files matched your search

+367 -94
View File
@@ -1,11 +1,12 @@
import { expect, test } from '@playwright/test';
import { injectAxe } from 'axe-playwright';
async function logIn( page ) {
await page.goto( '/wp-login.php' );
await page.locator( '#user_login' ).fill( 'admin' );
await page.locator( '#user_pass' ).fill( 'password' );
await Promise.all( [
page.waitForURL( /\/wp-admin\// ),
page.waitForURL( ( url ) => url.pathname.startsWith( '/wp-admin/' ) ),
page.getByRole( 'button', { name: /log in/i } ).click(),
] );
}
@@ -35,107 +36,137 @@ async function openSettings( page ) {
}
}
test( 'Related Stories is insertable and configurable in the real post editor', async ( {
page,
} ) => {
await logIn( page );
await page.goto( '/wp-admin/post-new.php' );
await closeWelcomeGuide( page );
await expect( page.getByRole( 'heading', { name: 'Add Post', exact: true } ) ).toBeVisible();
const inserted = await page.evaluate( () => {
const type = window.wp.blocks.getBlockType( 'chrnx/related-posts' );
if ( ! type ) {
return false;
}
const block = window.wp.blocks.createBlock( 'chrnx/related-posts' );
window.wp.data.dispatch( 'core/block-editor' ).insertBlock( block );
window.wp.data.dispatch( 'core/block-editor' ).selectBlock( block.clientId );
return (
window.wp.data.select( 'core/block-editor' ).getSelectedBlockClientId() === block.clientId
);
} );
expect( inserted ).toBe( true );
await openSettings( page );
await expect( page.getByLabel( 'Stories to show', { exact: true } ) ).toBeVisible();
await expect( page.getByLabel( 'Layout', { exact: true } ) ).toBeVisible();
await expect( page.getByText( 'Related stories settings', { exact: true } ) ).toBeVisible();
} );
test( 'Related Stories persists typed attributes with null saved HTML in the Site Editor', async ( {
page,
} ) => {
await logIn( page );
await page.goto(
'/wp-admin/site-editor.php?postType=wp_template&postId=chrnx%2F%2Fsingle&canvas=edit',
);
await closeWelcomeGuide( page );
await expect
.poll( () =>
page.evaluate( () => {
const editor = window.wp?.data?.select( 'core/block-editor' );
return typeof editor?.getBlocks === 'function' && editor.getBlocks().length > 0;
} ),
)
.toBe( true );
const recovery = page
.locator( 'iframe' )
.contentFrame()
.getByRole( 'button', { name: 'Attempt recovery' } )
.first();
if ( await recovery.isVisible() ) {
await recovery.click();
function tabShortcut( browserName, reverse = false ) {
const modifiers = [];
if ( browserName === 'webkit' ) {
modifiers.push( 'Alt' );
}
if ( reverse ) {
modifiers.push( 'Shift' );
}
return [ ...modifiers, 'Tab' ].join( '+' );
}
const selected = await page.evaluate( () => {
const getNestedBlocks = ( blocks ) =>
blocks.flatMap( ( block ) => [ block, ...getNestedBlocks( block.innerBlocks || [] ) ] );
const store = window.wp.data.select( 'core/block-editor' );
const block = getNestedBlocks( store.getBlocks() ).find(
( candidate ) => candidate.name === 'chrnx/related-posts',
async function scrollRailNatively( page, track, browserName ) {
await track.hover();
if ( browserName === 'webkit' ) {
await track.evaluate( ( element ) => {
const card = element.querySelector( '.chrnx-related-card' );
const style = window.getComputedStyle( element );
const gap = Number.parseFloat( style.columnGap || style.gap ) || 0;
element.scrollBy( {
left: card.getBoundingClientRect().width + gap,
behavior: 'auto',
} );
} );
return;
}
await page.mouse.wheel( 240, 0 );
}
test.describe.serial( 'Related Stories editor integration', () => {
test( 'Related Stories is insertable and configurable in the real post editor', async ( {
page,
} ) => {
await logIn( page );
await page.goto( '/wp-admin/post-new.php' );
await closeWelcomeGuide( page );
await expect( page.getByRole( 'heading', { name: 'Add Post', exact: true } ) ).toBeVisible();
const inserted = await page.evaluate( () => {
const type = window.wp.blocks.getBlockType( 'chrnx/related-posts' );
if ( ! type ) {
return false;
}
const block = window.wp.blocks.createBlock( 'chrnx/related-posts' );
window.wp.data.dispatch( 'core/block-editor' ).insertBlock( block );
window.wp.data.dispatch( 'core/block-editor' ).selectBlock( block.clientId );
return (
window.wp.data.select( 'core/block-editor' ).getSelectedBlockClientId() === block.clientId
);
} );
expect( inserted ).toBe( true );
await openSettings( page );
await expect( page.getByLabel( 'Stories to show', { exact: true } ) ).toBeVisible();
await expect( page.getByLabel( 'Layout', { exact: true } ) ).toBeVisible();
await expect( page.getByText( 'Related stories settings', { exact: true } ) ).toBeVisible();
} );
test( 'Related Stories persists typed attributes with null saved HTML in the Site Editor', async ( {
page,
} ) => {
await logIn( page );
await page.goto(
'/wp-admin/site-editor.php?postType=wp_template&postId=chrnx%2F%2Fsingle&canvas=edit',
);
if ( ! block ) {
return false;
await closeWelcomeGuide( page );
await expect
.poll( () =>
page.evaluate( () => {
const editor = window.wp?.data?.select( 'core/block-editor' );
return typeof editor?.getBlocks === 'function' && editor.getBlocks().length > 0;
} ),
)
.toBe( true );
const recovery = page
.locator( 'iframe' )
.contentFrame()
.getByRole( 'button', { name: 'Attempt recovery' } )
.first();
if ( await recovery.isVisible() ) {
await recovery.click();
}
window.wp.data.dispatch( 'core/block-editor' ).selectBlock( block.clientId );
return true;
} );
expect( selected ).toBe( true );
await openSettings( page );
await page.getByLabel( 'Stories to show', { exact: true } ).selectOption( '5' );
await page.getByLabel( 'Layout', { exact: true } ).selectOption( 'carousel' );
const selected = await page.evaluate( () => {
const getNestedBlocks = ( blocks ) =>
blocks.flatMap( ( block ) => [ block, ...getNestedBlocks( block.innerBlocks || [] ) ] );
const store = window.wp.data.select( 'core/block-editor' );
const block = getNestedBlocks( store.getBlocks() ).find(
( candidate ) => candidate.name === 'chrnx/related-posts',
);
if ( ! block ) {
return false;
}
window.wp.data.dispatch( 'core/block-editor' ).selectBlock( block.clientId );
return true;
} );
expect( selected ).toBe( true );
const state = await page.evaluate( () => {
const getNestedBlocks = ( blocks ) =>
blocks.flatMap( ( block ) => [ block, ...getNestedBlocks( block.innerBlocks || [] ) ] );
const block = getNestedBlocks( window.wp.data.select( 'core/block-editor' ).getBlocks() ).find(
( candidate ) => candidate.name === 'chrnx/related-posts',
await openSettings( page );
await page.getByLabel( 'Stories to show', { exact: true } ).selectOption( '5' );
await page.getByLabel( 'Layout', { exact: true } ).selectOption( 'carousel' );
const state = await page.evaluate( () => {
const getNestedBlocks = ( blocks ) =>
blocks.flatMap( ( block ) => [ block, ...getNestedBlocks( block.innerBlocks || [] ) ] );
const block = getNestedBlocks(
window.wp.data.select( 'core/block-editor' ).getBlocks(),
).find( ( candidate ) => candidate.name === 'chrnx/related-posts' );
return {
limit: block.attributes.limit,
limitType: typeof block.attributes.limit,
layout: block.attributes.layout,
editType: typeof window.wp.blocks.getBlockType( block.name )?.edit,
selectedClientId: window.wp.data.select( 'core/block-editor' ).getSelectedBlockClientId(),
clientId: block.clientId,
saveContent: window.wp.blocks.getSaveContent( block.name, block.attributes ),
serialized: window.wp.blocks.serialize( block ),
};
} );
expect( state.limit ).toBe( 5 );
expect( state.limitType ).toBe( 'number' );
expect( state.layout ).toBe( 'carousel' );
expect( state.editType ).toBe( 'function' );
expect( state.selectedClientId ).toBe( state.clientId );
expect( state.saveContent ).toBe( '' );
expect( state.serialized ).toMatch(
/^<!-- wp:chrnx\/related-posts \{"limit":5,"layout":"carousel"\} \/-->$/,
);
return {
limit: block.attributes.limit,
limitType: typeof block.attributes.limit,
layout: block.attributes.layout,
editType: typeof window.wp.blocks.getBlockType( block.name )?.edit,
selectedClientId: window.wp.data.select( 'core/block-editor' ).getSelectedBlockClientId(),
clientId: block.clientId,
saveContent: window.wp.blocks.getSaveContent( block.name, block.attributes ),
serialized: window.wp.blocks.serialize( block ),
};
} );
expect( state.limit ).toBe( 5 );
expect( state.limitType ).toBe( 'number' );
expect( state.layout ).toBe( 'carousel' );
expect( state.editType ).toBe( 'function' );
expect( state.selectedClientId ).toBe( state.clientId );
expect( state.saveContent ).toBe( '' );
expect( state.serialized ).toMatch(
/^<!-- wp:chrnx\/related-posts \{"limit":5,"layout":"carousel"\} \/-->$/,
);
} );
const layoutCases = [
@@ -440,3 +471,245 @@ for ( const theme of [ 'light', 'dark' ] ) {
await page.mouse.up();
} );
}
test( 'interaction buttons move exactly one card and update both disabled edges', async ( {
page,
} ) => {
await page.setViewportSize( { width: 900, height: 900 } );
await page.goto( '/long-title-night-writing-fixture/' );
const section = page.locator( '#related-stories-3-adaptive .chrnx-related-posts' );
const track = section.locator( '[data-chrnx-related-track]' );
const previous = section.getByRole( 'button', {
name: 'Previous stories',
} );
const next = section.getByRole( 'button', { name: 'Next stories' } );
const step = await track.evaluate( ( element ) => {
const card = element.querySelector( '.chrnx-related-card' );
const style = window.getComputedStyle( element );
return card.getBoundingClientRect().width + Number.parseFloat( style.columnGap || style.gap );
} );
await expect( previous ).toBeDisabled();
await expect( next ).toBeEnabled();
await next.click();
await expect
.poll( () => track.evaluate( ( element ) => element.scrollLeft ) )
.toBeGreaterThan( step - 3 );
expect( await track.evaluate( ( element ) => element.scrollLeft ) ).toBeLessThanOrEqual(
step + 3,
);
await expect( previous ).toBeEnabled();
await next.click();
await expect( next ).toBeDisabled();
await previous.click();
await expect( next ).toBeEnabled();
} );
test( 'interaction native horizontal wheel scrolling remains available', async ( {
page,
browserName,
} ) => {
await page.setViewportSize( { width: 900, height: 900 } );
await page.goto( '/long-title-night-writing-fixture/' );
const track = page.locator( '#related-stories-3-adaptive [data-chrnx-related-track]' );
await expect( track ).toHaveCSS( 'overflow-x', 'auto' );
await scrollRailNatively( page, track, browserName );
await expect
.poll( () => track.evaluate( ( element ) => element.scrollLeft ) )
.toBeGreaterThan( 0 );
} );
test( 'interaction Tab reaches rail, cards, controls, then exits without trapping', async ( {
page,
browserName,
} ) => {
await page.setViewportSize( { width: 900, height: 900 } );
await page.goto( '/long-title-night-writing-fixture/' );
const section = page.locator( '#related-stories-3-adaptive .chrnx-related-posts' );
const track = section.locator( '[data-chrnx-related-track]' );
const links = track.locator( '.chrnx-related-card__link' );
const previous = section.getByRole( 'button', {
name: 'Previous stories',
} );
const next = section.getByRole( 'button', { name: 'Next stories' } );
await next.click();
await expect( previous ).toBeEnabled();
await track.focus();
for ( let index = 0; index < ( await links.count() ); index += 1 ) {
await page.keyboard.press( tabShortcut( browserName ) );
await expect( links.nth( index ) ).toBeFocused();
}
await expect( previous ).toBeEnabled();
await expect( next ).toBeDisabled();
await page.keyboard.press( tabShortcut( browserName ) );
await expect( previous ).toBeFocused();
await page.keyboard.press( tabShortcut( browserName ) );
const nextSectionTrack = page.locator( '#related-stories-5-adaptive [data-chrnx-related-track]' );
await expect( nextSectionTrack ).toBeFocused();
await previous.click();
await expect( next ).toBeEnabled();
await nextSectionTrack.focus();
await page.keyboard.press( tabShortcut( browserName, true ) );
await expect( next ).toBeFocused();
await page.keyboard.press( tabShortcut( browserName, true ) );
await expect( previous ).toBeFocused();
} );
for ( const buttonName of [ 'Previous stories', 'Next stories' ] ) {
test( `interaction resize repairs ${ buttonName } focus before adaptive controls hide`, async ( {
page,
} ) => {
await page.setViewportSize( { width: 900, height: 900 } );
await page.goto( '/long-title-night-writing-fixture/' );
const section = page.locator( '#related-stories-3-adaptive .chrnx-related-posts' );
const heading = section.getByRole( 'heading', {
name: 'Related stories',
} );
if ( buttonName === 'Previous stories' ) {
await section.getByRole( 'button', { name: 'Next stories' } ).click();
await expect( section.getByRole( 'button', { name: 'Previous stories' } ) ).toBeEnabled();
}
const button = section.getByRole( 'button', { name: buttonName } );
await button.focus();
await expect( button ).toBeFocused();
await page.setViewportSize( { width: 901, height: 900 } );
await expect( heading ).toBeFocused();
await expect( section.locator( '.chrnx-related-posts__controls' ) ).toBeHidden();
} );
}
test( 'interaction reduced motion avoids smooth scrolling and preserves card geometry', async ( {
page,
} ) => {
await page.emulateMedia( { reducedMotion: 'reduce' } );
await page.setViewportSize( { width: 900, height: 900 } );
await page.goto( '/long-title-night-writing-fixture/' );
const section = page.locator( '#related-stories-3-adaptive .chrnx-related-posts' );
const track = section.locator( '[data-chrnx-related-track]' );
const card = track.locator( '.chrnx-related-card' ).first();
const before = await card.evaluate( ( element ) => element.getBoundingClientRect().width );
await track.evaluate( ( element ) => {
globalThis.__chrnxRelatedScrollBehaviors = [];
const nativeScrollBy = element.scrollBy.bind( element );
element.scrollBy = ( options ) => {
globalThis.__chrnxRelatedScrollBehaviors.push( options.behavior );
nativeScrollBy( options );
};
} );
await section.getByRole( 'button', { name: 'Next stories' } ).click();
await expect
.poll( () => track.evaluate( () => globalThis.__chrnxRelatedScrollBehaviors ) )
.toEqual( [ 'auto' ] );
expect( await card.evaluate( ( element ) => element.getBoundingClientRect().width ) ).toBeCloseTo(
before,
1,
);
} );
test.describe( 'no JavaScript progressive access', () => {
test.use( { javaScriptEnabled: false } );
test( 'no JavaScript keeps controls hidden and every rail link keyboard reachable', async ( {
page,
browserName,
} ) => {
await page.setViewportSize( { width: 900, height: 900 } );
await page.goto( '/long-title-night-writing-fixture/' );
for ( const anchor of [ 'related-stories-3-adaptive', 'related-stories-3-carousel' ] ) {
const section = page.locator( `#${ anchor } .chrnx-related-posts` );
const controls = section.locator( '.chrnx-related-posts__controls' );
const track = section.locator( '[data-chrnx-related-track]' );
const links = track.locator( '.chrnx-related-card__link' );
await expect( controls ).toBeHidden();
await track.focus();
for ( let index = 0; index < ( await links.count() ); index += 1 ) {
await page.keyboard.press( tabShortcut( browserName ) );
const link = links.nth( index );
await expect( link ).toBeFocused();
await expect( link ).toHaveAttribute( 'href', /.+/ );
expect( new URL( await link.getAttribute( 'href' ), page.url() ).origin ).toBe(
new URL( page.url() ).origin,
);
}
}
} );
} );
test( 'interaction controller failure leaves the native rail and links usable', async ( {
page,
browserName,
} ) => {
await page.route( '**/wp-content/themes/chrnx/assets/js/related-posts.js*', ( route ) =>
route.abort(),
);
await page.setViewportSize( { width: 900, height: 900 } );
await page.goto( '/long-title-night-writing-fixture/' );
const section = page.locator( '#related-stories-3-adaptive .chrnx-related-posts' );
const track = section.locator( '[data-chrnx-related-track]' );
await expect( section.locator( '.chrnx-related-posts__controls' ) ).toBeHidden();
await expect( track ).toHaveCSS( 'overflow-x', 'auto' );
await scrollRailNatively( page, track, browserName );
await expect
.poll( () => track.evaluate( ( element ) => element.scrollLeft ) )
.toBeGreaterThan( 0 );
await track.focus();
await page.keyboard.press( tabShortcut( browserName ) );
await expect( track.locator( '.chrnx-related-card__link' ).first() ).toBeFocused();
} );
test( 'permalink every rendered related card reaches a same-origin published HTTP 200', async ( {
page,
} ) => {
const sourcePath = '/long-title-night-writing-fixture/';
await page.goto( sourcePath );
const sourceOrigin = new URL( page.url() ).origin;
const hrefs = await page
.locator( '#related-stories-5-grid .chrnx-related-card__link' )
.evaluateAll( ( links ) => links.map( ( link ) => link.href ) );
expect( hrefs ).toHaveLength( 5 );
expect( new Set( hrefs ).size ).toBe( hrefs.length );
for ( const href of hrefs ) {
expect( new URL( href ).origin ).toBe( sourceOrigin );
await page.goto( sourcePath );
const responsePromise = page.waitForNavigation();
await page.locator( `#related-stories-5-grid a[href="${ href }"]` ).click();
const response = await responsePromise;
expect( response.status() ).toBe( 200 );
expect( await response.text() ).not.toMatch( /PHP (?:Warning|Notice)|Fatal error/i );
await expect( page.locator( 'body' ) ).toHaveClass( /single-post/ );
await expect( page.getByRole( 'heading', { level: 1 } ) ).toBeVisible();
}
} );
test( 'accessibility grid and active carousel pass automated WCAG 2.2 AA checks', async ( {
page,
}, testInfo ) => {
test.skip(
testInfo.project.name !== 'chromium',
'Axe is intentionally deterministic in Chromium.',
);
await page.setViewportSize( { width: 900, height: 900 } );
await page.goto( '/long-title-night-writing-fixture/' );
await injectAxe( page );
const tags = [ 'wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa', 'wcag22aa' ];
for ( const selector of [ '#related-stories-5-grid', '#related-stories-5-carousel' ] ) {
const violations = await page.evaluate(
async ( { contextSelector, runOnly } ) => {
const result = await globalThis.axe.run( document.querySelector( contextSelector ), {
runOnly: { type: 'tag', values: runOnly },
} );
return result.violations.map( ( violation ) => ( {
id: violation.id,
impact: violation.impact,
nodes: violation.nodes.map( ( node ) => node.target ),
} ) );
},
{ contextSelector: selector, runOnly: tags },
);
expect( violations, selector ).toEqual( [] );
}
} );