import { expect, test } from '@playwright/test'; 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.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(); } } 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( /^$/, ); } ); 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 ); } ); } } 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(); } ); }