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' }, ]; 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 ); } ); } }