821 lines
33 KiB
JavaScript
821 lines
33 KiB
JavaScript
import { expect, test } from '@playwright/test';
|
|
import { injectAxe } from 'axe-playwright';
|
|
|
|
import { containsPhpDiagnostic } from './response-diagnostics.mjs';
|
|
|
|
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( ( url ) => url.pathname.startsWith( '/wp-admin/' ) ),
|
|
page.getByRole( 'button', { name: /log in/i } ).click(),
|
|
] );
|
|
}
|
|
|
|
async function closeWelcomeGuide( page ) {
|
|
const welcomeDialog = page.getByRole( 'dialog', {
|
|
name: /^Welcome to the (?:site )?editor$/,
|
|
} );
|
|
const isVisible = await welcomeDialog
|
|
.waitFor( { state: 'visible', timeout: 5000 } )
|
|
.then( () => true )
|
|
.catch( () => false );
|
|
if ( isVisible ) {
|
|
const close = welcomeDialog.getByRole( 'button', { name: 'Close' } );
|
|
if ( await close.isVisible() ) {
|
|
await close.click();
|
|
} else {
|
|
await welcomeDialog.getByRole( 'button', { name: 'Get started' } ).click();
|
|
}
|
|
}
|
|
}
|
|
|
|
async function openSettings( page ) {
|
|
const settings = page.getByRole( 'button', { name: /^settings$/i } ).last();
|
|
if ( ( await settings.getAttribute( 'aria-expanded' ) ) !== 'true' ) {
|
|
await settings.click();
|
|
}
|
|
}
|
|
|
|
function tabShortcut( browserName, reverse = false ) {
|
|
const modifiers = [];
|
|
if ( browserName === 'webkit' ) {
|
|
modifiers.push( 'Alt' );
|
|
}
|
|
if ( reverse ) {
|
|
modifiers.push( 'Shift' );
|
|
}
|
|
return [ ...modifiers, 'Tab' ].join( '+' );
|
|
}
|
|
|
|
async function scrollRailNatively( page, track, browserName ) {
|
|
await track.hover();
|
|
if ( browserName === 'webkit' ) {
|
|
await track.focus();
|
|
await page.keyboard.press( 'ArrowRight' );
|
|
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',
|
|
);
|
|
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();
|
|
}
|
|
|
|
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 );
|
|
|
|
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"\} \/-->$/,
|
|
);
|
|
} );
|
|
} );
|
|
|
|
const layoutCases = [
|
|
{ limit: 3, layout: 'adaptive' },
|
|
{ limit: 5, layout: 'adaptive' },
|
|
{ limit: 3, layout: 'grid' },
|
|
{ limit: 5, layout: 'grid' },
|
|
{ limit: 3, layout: 'carousel' },
|
|
{ limit: 5, layout: 'carousel' },
|
|
];
|
|
const unbrokenRelatedTitle =
|
|
'DifficultyIsAConversationNotAGateEvenWhenOneUnbrokenEditorialTitleKeepsGoingWithoutAnyNaturalPlaceForTheCardToWrapSafely';
|
|
|
|
function colorChannels( color ) {
|
|
const channels = color.match( /rgba?\(\s*([\d.]+)[, ]+\s*([\d.]+)[, ]+\s*([\d.]+)/i );
|
|
if ( channels ) {
|
|
return channels.slice( 1, 4 ).map( Number );
|
|
}
|
|
const srgb = color.match( /color\(srgb\s+([\d.]+)\s+([\d.]+)\s+([\d.]+)/i );
|
|
if ( srgb ) {
|
|
return srgb.slice( 1, 4 ).map( ( channel ) => Number( channel ) * 255 );
|
|
}
|
|
throw new Error( `Expected an RGB color, received ${ color }` );
|
|
}
|
|
|
|
function luminance( color ) {
|
|
const linear = colorChannels( color ).map( ( channel ) => {
|
|
const value = channel / 255;
|
|
return value <= 0.04045 ? value / 12.92 : ( ( value + 0.055 ) / 1.055 ) ** 2.4;
|
|
} );
|
|
return 0.2126 * linear[ 0 ] + 0.7152 * linear[ 1 ] + 0.0722 * linear[ 2 ];
|
|
}
|
|
|
|
function contrast( foreground, background ) {
|
|
const first = luminance( foreground );
|
|
const second = luminance( background );
|
|
return ( Math.max( first, second ) + 0.05 ) / ( Math.min( first, second ) + 0.05 );
|
|
}
|
|
|
|
for ( const theme of [ 'light', 'dark' ] ) {
|
|
for ( const width of [ 375, 768, 900, 901, 1440, 2560 ] ) {
|
|
test( `layout matrix ${ theme } at ${ width }px`, async ( { page } ) => {
|
|
await page.setViewportSize( { width, height: 900 } );
|
|
await page.goto( '/long-title-night-writing-fixture/' );
|
|
await page.evaluate( ( selectedTheme ) => {
|
|
window.localStorage.setItem( 'chrnx-theme', selectedTheme );
|
|
}, theme );
|
|
await page.reload();
|
|
await expect( page.locator( 'html' ) ).toHaveAttribute( 'data-theme', theme );
|
|
|
|
for ( const layoutCase of layoutCases ) {
|
|
const anchor = `related-stories-${ layoutCase.limit }-${ layoutCase.layout }`;
|
|
const section = page.locator( `#${ anchor } .chrnx-related-posts` );
|
|
const track = section.locator( '[data-chrnx-related-track]' );
|
|
const cards = track.locator( '.chrnx-related-card' );
|
|
const controls = section.locator( '.chrnx-related-posts__controls' );
|
|
const rail =
|
|
layoutCase.layout === 'carousel' || ( layoutCase.layout === 'adaptive' && width <= 900 );
|
|
const gridColumns = width <= 900 ? 1 : Math.min( 3, layoutCase.limit );
|
|
|
|
await expect( section ).toHaveCount( 1 );
|
|
await expect( cards ).toHaveCount( layoutCase.limit );
|
|
if ( rail ) {
|
|
await expect( controls ).toBeVisible();
|
|
await expect( controls.getByRole( 'button' ) ).toHaveCount( 2 );
|
|
} else if ( layoutCase.layout === 'grid' ) {
|
|
await expect( controls ).toHaveCount( 0 );
|
|
} else {
|
|
await expect( controls ).toBeHidden();
|
|
}
|
|
|
|
const geometry = await section.evaluate( ( element ) => {
|
|
const relatedTrack = element.querySelector( '[data-chrnx-related-track]' );
|
|
const relatedCards = [ ...relatedTrack.querySelectorAll( '.chrnx-related-card' ) ];
|
|
const relatedControls = element.querySelector( '.chrnx-related-posts__controls' );
|
|
const firstButton = relatedControls?.querySelector( 'button' );
|
|
const trackStyle = window.getComputedStyle( relatedTrack );
|
|
const buttonStyle = firstButton ? window.getComputedStyle( firstButton ) : null;
|
|
return {
|
|
sectionClientWidth: element.clientWidth,
|
|
sectionScrollWidth: element.scrollWidth,
|
|
trackClientWidth: relatedTrack.clientWidth,
|
|
trackScrollWidth: relatedTrack.scrollWidth,
|
|
display: trackStyle.display,
|
|
columns: trackStyle.gridTemplateColumns.split( ' ' ).filter( Boolean ).length,
|
|
gap: Number.parseFloat( trackStyle.columnGap || trackStyle.gap ) || 0,
|
|
cardRects: relatedCards.map( ( card ) => {
|
|
const rect = card.getBoundingClientRect();
|
|
return {
|
|
left: rect.left,
|
|
right: rect.right,
|
|
top: rect.top,
|
|
bottom: rect.bottom,
|
|
width: rect.width,
|
|
};
|
|
} ),
|
|
button: firstButton
|
|
? {
|
|
width: firstButton.getBoundingClientRect().width,
|
|
height: firstButton.getBoundingClientRect().height,
|
|
color: buttonStyle.color,
|
|
background: buttonStyle.backgroundColor,
|
|
}
|
|
: null,
|
|
};
|
|
} );
|
|
|
|
expect( geometry.sectionScrollWidth ).toBeLessThanOrEqual(
|
|
geometry.sectionClientWidth + 1,
|
|
);
|
|
if ( rail ) {
|
|
expect( geometry.display ).toBe( 'flex' );
|
|
if ( width <= 900 || layoutCase.limit > 3 ) {
|
|
expect( geometry.trackScrollWidth ).toBeGreaterThan( geometry.trackClientWidth + 1 );
|
|
} else {
|
|
expect( geometry.trackScrollWidth ).toBeLessThanOrEqual(
|
|
geometry.trackClientWidth + 1,
|
|
);
|
|
}
|
|
const expectedWidth =
|
|
width <= 900
|
|
? geometry.trackClientWidth - Math.min( 112, Math.max( 48, width * 0.12 ) )
|
|
: ( geometry.trackClientWidth - 2 * geometry.gap ) / 3;
|
|
expect( Math.abs( geometry.cardRects[ 0 ].width - expectedWidth ) ).toBeLessThanOrEqual(
|
|
2,
|
|
);
|
|
if ( width <= 900 ) {
|
|
const preview =
|
|
geometry.trackClientWidth - geometry.cardRects[ 0 ].width - geometry.gap;
|
|
expect( preview ).toBeGreaterThanOrEqual( 24 );
|
|
expect( preview ).toBeLessThanOrEqual( geometry.cardRects[ 1 ].width * 0.25 );
|
|
}
|
|
expect( geometry.button.width ).toBeGreaterThanOrEqual( 45 );
|
|
expect( geometry.button.height ).toBeGreaterThanOrEqual( 45 );
|
|
expect(
|
|
contrast( geometry.button.color, geometry.button.background ),
|
|
).toBeGreaterThanOrEqual( 4.5 );
|
|
} else {
|
|
expect( geometry.display ).toBe( 'grid' );
|
|
expect( geometry.columns ).toBe( gridColumns );
|
|
expect( geometry.trackScrollWidth ).toBeLessThanOrEqual( geometry.trackClientWidth + 1 );
|
|
const expectedWidth =
|
|
( geometry.trackClientWidth - geometry.gap * ( gridColumns - 1 ) ) / gridColumns;
|
|
expect( Math.abs( geometry.cardRects[ 0 ].width - expectedWidth ) ).toBeLessThanOrEqual(
|
|
2,
|
|
);
|
|
}
|
|
|
|
for ( let first = 0; first < geometry.cardRects.length; first += 1 ) {
|
|
for ( let second = first + 1; second < geometry.cardRects.length; second += 1 ) {
|
|
const a = geometry.cardRects[ first ];
|
|
const b = geometry.cardRects[ second ];
|
|
const intersects =
|
|
a.left < b.right && a.right > b.left && a.top < b.bottom && a.bottom > b.top;
|
|
expect( intersects ).toBe( false );
|
|
}
|
|
}
|
|
}
|
|
|
|
const focusButton = page
|
|
.locator(
|
|
'#related-stories-5-carousel .chrnx-related-posts__controls button:not(:disabled)',
|
|
)
|
|
.first();
|
|
await expect( focusButton ).toBeVisible();
|
|
await focusButton.focus();
|
|
const focusContrast = await focusButton.evaluate( ( button ) => {
|
|
const style = window.getComputedStyle( button );
|
|
return {
|
|
background: style.backgroundColor,
|
|
ring: style.boxShadow.match( /rgba?\([^)]+\)/ )?.[ 0 ] || null,
|
|
};
|
|
} );
|
|
expect( focusContrast.ring ).toBeTruthy();
|
|
expect( contrast( focusContrast.ring, focusContrast.background ) ).toBeGreaterThanOrEqual(
|
|
3,
|
|
);
|
|
|
|
expect(
|
|
await page.evaluate(
|
|
() => document.documentElement.scrollWidth <= document.documentElement.clientWidth,
|
|
),
|
|
).toBe( true );
|
|
|
|
await page.evaluate( () => {
|
|
document.documentElement.style.fontSize = '200%';
|
|
} );
|
|
const longRelatedLink = page.locator(
|
|
'#related-stories-5-grid .chrnx-related-card__link[href$="/difficulty-is-a-conversation-not-a-gate/"]',
|
|
);
|
|
await expect( longRelatedLink ).toContainText( unbrokenRelatedTitle );
|
|
const textIntegrity = await longRelatedLink.evaluate( ( link ) => {
|
|
const card = link.closest( '.chrnx-related-card' );
|
|
const content = link.querySelector( '.chrnx-related-card__content' );
|
|
const title = link.querySelector( '.chrnx-related-card__title' );
|
|
const section = link.closest( '.chrnx-related-posts' );
|
|
const contains = ( parent, child ) => {
|
|
const parentRect = parent.getBoundingClientRect();
|
|
const childRect = child.getBoundingClientRect();
|
|
return (
|
|
childRect.left >= parentRect.left - 1 &&
|
|
childRect.right <= parentRect.right + 1 &&
|
|
childRect.top >= parentRect.top - 1 &&
|
|
childRect.bottom <= parentRect.bottom + 1
|
|
);
|
|
};
|
|
const unclipped = ( element ) =>
|
|
element.scrollWidth <= element.clientWidth + 1 &&
|
|
element.scrollHeight <= element.clientHeight + 1;
|
|
const titleStyle = window.getComputedStyle( title );
|
|
const titleRange = document.createRange();
|
|
titleRange.selectNodeContents( title );
|
|
return {
|
|
card: unclipped( card ) && contains( section, card ),
|
|
link: unclipped( link ) && contains( card, link ),
|
|
content: unclipped( content ) && contains( link, content ),
|
|
title:
|
|
! [ 'hidden', 'clip' ].includes( titleStyle.overflow ) &&
|
|
contains( content, title ) &&
|
|
contains( content, {
|
|
getBoundingClientRect: () => titleRange.getBoundingClientRect(),
|
|
} ),
|
|
};
|
|
} );
|
|
expect( textIntegrity ).toEqual( {
|
|
card: true,
|
|
link: true,
|
|
content: true,
|
|
title: true,
|
|
} );
|
|
expect(
|
|
await page.evaluate(
|
|
() => document.documentElement.scrollWidth <= document.documentElement.clientWidth,
|
|
),
|
|
).toBe( true );
|
|
} );
|
|
}
|
|
}
|
|
|
|
for ( const theme of [ 'light', 'dark' ] ) {
|
|
test( `full-card keyboard focus remains visible inside clipped ${ theme } cards`, async ( {
|
|
page,
|
|
browserName,
|
|
} ) => {
|
|
await page.setViewportSize( { width: 1440, height: 900 } );
|
|
await page.goto( '/long-title-night-writing-fixture/' );
|
|
await page.evaluate( ( selectedTheme ) => {
|
|
window.localStorage.setItem( 'chrnx-theme', selectedTheme );
|
|
}, theme );
|
|
await page.reload();
|
|
|
|
const links = page.locator( '#related-stories-5-grid .chrnx-related-card__link' );
|
|
const focusedLink = links.nth( 1 );
|
|
await links.first().focus();
|
|
await page.keyboard.press( tabShortcut( browserName ) );
|
|
await expect( focusedLink ).toBeFocused();
|
|
|
|
const appearance = await focusedLink.evaluate( ( link ) => {
|
|
const card = link.closest( '.chrnx-related-card' );
|
|
const cardStyle = window.getComputedStyle( card );
|
|
const linkStyle = window.getComputedStyle( link );
|
|
const colors = cardStyle.boxShadow.match(
|
|
/rgba?\([^)]+\)|color\(srgb\s+[\d.]+\s+[\d.]+\s+[\d.]+(?:\s*\/\s*[\d.]+)?\)/gi,
|
|
);
|
|
return {
|
|
background: cardStyle.backgroundColor,
|
|
cardOverflow: cardStyle.overflow,
|
|
cardShadow: cardStyle.boxShadow,
|
|
linkShadow: linkStyle.boxShadow,
|
|
ring: colors?.at( -1 ) || null,
|
|
};
|
|
} );
|
|
|
|
expect( appearance.cardOverflow ).toBe( 'hidden' );
|
|
expect( appearance.linkShadow ).not.toBe( 'none' );
|
|
expect( appearance.cardShadow ).toContain( 'inset' );
|
|
expect( appearance.ring ).toBeTruthy();
|
|
expect( contrast( appearance.ring, appearance.background ) ).toBeGreaterThanOrEqual( 3 );
|
|
} );
|
|
}
|
|
|
|
for ( const theme of [ 'light', 'dark' ] ) {
|
|
for ( const railCase of [
|
|
{ name: 'adaptive mobile', anchor: 'related-stories-5-adaptive', width: 900 },
|
|
{ name: 'carousel desktop', anchor: 'related-stories-5-carousel', width: 1440 },
|
|
] ) {
|
|
test( `${ railCase.name } ${ theme } card keeps its keyboard focus ring while hovered`, async ( {
|
|
page,
|
|
browserName,
|
|
} ) => {
|
|
await page.setViewportSize( { width: railCase.width, height: 900 } );
|
|
await page.goto( '/long-title-night-writing-fixture/' );
|
|
await page.evaluate( ( selectedTheme ) => {
|
|
window.localStorage.setItem( 'chrnx-theme', selectedTheme );
|
|
}, theme );
|
|
await page.reload();
|
|
|
|
const section = page.locator( `#${ railCase.anchor } .chrnx-related-posts` );
|
|
const track = section.locator( '[data-chrnx-related-track]' );
|
|
const link = section.locator( '.chrnx-related-card__link' ).first();
|
|
await track.focus();
|
|
await page.keyboard.press( tabShortcut( browserName ) );
|
|
await expect( link ).toBeFocused();
|
|
await link.hover();
|
|
await expect( link ).toBeFocused();
|
|
await page.waitForTimeout( 220 );
|
|
|
|
const appearance = await link.evaluate( ( focusedLink ) => {
|
|
const card = focusedLink.closest( '.chrnx-related-card' );
|
|
const trackElement = card.parentElement;
|
|
const cardRect = card.getBoundingClientRect();
|
|
const trackRect = trackElement.getBoundingClientRect();
|
|
const style = window.getComputedStyle( card );
|
|
const token = style.getPropertyValue( '--chrnx-related-focus-ring' ).trim();
|
|
const probe = document.createElement( 'span' );
|
|
probe.style.color = token;
|
|
card.append( probe );
|
|
const normalizedToken = window.getComputedStyle( probe ).color;
|
|
probe.remove();
|
|
const colors = style.boxShadow.match(
|
|
/rgba?\([^)]+\)|color\(srgb\s+[\d.]+\s+[\d.]+\s+[\d.]+(?:\s*\/\s*[\d.]+)?\)/gi,
|
|
);
|
|
return {
|
|
background: style.backgroundColor,
|
|
ring: colors?.at( -1 ) || null,
|
|
shadow: style.boxShadow,
|
|
token: normalizedToken,
|
|
transform: style.transform,
|
|
topDelta: cardRect.top - trackRect.top,
|
|
bottomDelta: trackRect.bottom - cardRect.bottom,
|
|
};
|
|
} );
|
|
|
|
expect( appearance.transform ).toBe( 'none' );
|
|
expect( appearance.topDelta ).toBeGreaterThanOrEqual( -1 );
|
|
expect( appearance.bottomDelta ).toBeGreaterThanOrEqual( -1 );
|
|
expect( appearance.shadow ).toContain( 'inset' );
|
|
expect( appearance.shadow ).toMatch( /0px 0px 0px 3px/ );
|
|
expect( appearance.ring ).toBeTruthy();
|
|
expect( colorChannels( appearance.ring ) ).toEqual( colorChannels( appearance.token ) );
|
|
expect( contrast( appearance.ring, appearance.background ) ).toBeGreaterThanOrEqual( 3 );
|
|
} );
|
|
}
|
|
}
|
|
|
|
test( 'layout matrix rail hover stays visibly contained in its scrollport', async ( { page } ) => {
|
|
await page.setViewportSize( { width: 900, height: 900 } );
|
|
await page.goto( '/long-title-night-writing-fixture/' );
|
|
const track = page.locator( '#related-stories-5-carousel [data-chrnx-related-track]' );
|
|
const card = track.locator( '.chrnx-related-card' ).first();
|
|
const baselineBorder = await card.evaluate(
|
|
( element ) => window.getComputedStyle( element ).borderColor,
|
|
);
|
|
|
|
await card.hover();
|
|
await expect
|
|
.poll( () => card.evaluate( ( element ) => window.getComputedStyle( element ).transform ) )
|
|
.toBe( 'none' );
|
|
const hover = await card.evaluate( ( element ) => {
|
|
const cardRect = element.getBoundingClientRect();
|
|
const trackRect = element.parentElement.getBoundingClientRect();
|
|
const style = window.getComputedStyle( element );
|
|
return {
|
|
border: style.borderColor,
|
|
shadow: style.boxShadow,
|
|
contained: cardRect.top >= trackRect.top && cardRect.bottom <= trackRect.bottom,
|
|
};
|
|
} );
|
|
expect( hover.contained ).toBe( true );
|
|
expect( hover.border ).not.toBe( baselineBorder );
|
|
expect( hover.shadow ).toContain( 'inset' );
|
|
} );
|
|
|
|
for ( const theme of [ 'light', 'dark' ] ) {
|
|
test( `layout matrix ${ theme } controls have distinct hover and active responses`, async ( {
|
|
page,
|
|
} ) => {
|
|
await page.setViewportSize( { width: 375, height: 900 } );
|
|
await page.goto( '/long-title-night-writing-fixture/' );
|
|
await page.evaluate( ( selectedTheme ) => {
|
|
window.localStorage.setItem( 'chrnx-theme', selectedTheme );
|
|
}, theme );
|
|
await page.reload();
|
|
|
|
const button = page
|
|
.locator( '#related-stories-5-carousel .chrnx-related-posts__controls button:not(:disabled)' )
|
|
.first();
|
|
const readStyle = () =>
|
|
button.evaluate( ( element ) => {
|
|
const style = window.getComputedStyle( element );
|
|
return {
|
|
background: style.backgroundColor,
|
|
border: style.borderColor,
|
|
color: style.color,
|
|
};
|
|
} );
|
|
const baseline = await readStyle();
|
|
await button.hover();
|
|
const hovered = await readStyle();
|
|
expect( hovered.background ).not.toBe( baseline.background );
|
|
expect( hovered.border ).not.toBe( baseline.border );
|
|
expect( contrast( hovered.color, hovered.background ) ).toBeGreaterThanOrEqual( 4.5 );
|
|
|
|
await page.mouse.down();
|
|
const active = await readStyle();
|
|
expect( active.background ).not.toBe( hovered.background );
|
|
expect( contrast( active.color, active.background ) ).toBeGreaterThanOrEqual( 4.5 );
|
|
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 scrolling remains available through real user input', 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 firstCardLink = section.locator( '.chrnx-related-card__link' ).first();
|
|
const controls = section.locator( '.chrnx-related-posts__controls' );
|
|
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( firstCardLink ).toBeFocused();
|
|
await expect( controls ).toBeHidden();
|
|
expect(
|
|
await controls.evaluate( ( element ) =>
|
|
element.contains( element.ownerDocument.activeElement ),
|
|
),
|
|
).toBe( false );
|
|
} );
|
|
}
|
|
|
|
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( containsPhpDiagnostic( await response.text() ) ).toBe( false );
|
|
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( [] );
|
|
}
|
|
} );
|