style: add Related Stories layouts
This commit is contained in:
4 files changed
+388
-3
No files matched your search
@@ -1,7 +1,5 @@
|
||||
import { expect, test } from '@playwright/test';
|
||||
|
||||
test.describe.configure( { mode: 'serial' } );
|
||||
|
||||
async function logIn( page ) {
|
||||
await page.goto( '/wp-login.php' );
|
||||
await page.locator( '#user_login' ).fill( 'admin' );
|
||||
@@ -139,3 +137,201 @@ test( 'Related Stories persists typed attributes with null saved HTML in the Sit
|
||||
/^<!-- 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' },
|
||||
];
|
||||
|
||||
function colorChannels( color ) {
|
||||
const channels = color.match( /rgba?\(\s*([\d.]+)[, ]+\s*([\d.]+)[, ]+\s*([\d.]+)/i );
|
||||
if ( ! channels ) {
|
||||
throw new Error( `Expected an RGB color, received ${ color }` );
|
||||
}
|
||||
return channels.slice( 1, 4 ).map( Number );
|
||||
}
|
||||
|
||||
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 textIntegrity = await page
|
||||
.locator( '[id^="related-stories-"] .chrnx-related-card__content' )
|
||||
.evaluateAll( ( elements ) =>
|
||||
elements.every(
|
||||
( element ) =>
|
||||
element.scrollWidth <= element.clientWidth + 1 &&
|
||||
element.scrollHeight <= element.clientHeight + 1,
|
||||
),
|
||||
);
|
||||
expect( textIntegrity ).toBe( true );
|
||||
expect(
|
||||
await page.evaluate(
|
||||
() => document.documentElement.scrollWidth <= document.documentElement.clientWidth,
|
||||
),
|
||||
).toBe( true );
|
||||
} );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user