diff --git a/tests/e2e/visual-states.spec.mjs b/tests/e2e/visual-states.spec.mjs index 0d0b1ed..49a26f6 100644 --- a/tests/e2e/visual-states.spec.mjs +++ b/tests/e2e/visual-states.spec.mjs @@ -1220,56 +1220,99 @@ const musicPodcastLandingCases = [ copy: 'Conversations about the work behind software, research, music, and games.', atmosphere: 'img[src*="podcast-frequency-grunge.png"]', art: 'img[src*="alter-ego-podcasts.png"]', - latestMedia: '.chrnx-latest-podcast img', + latestMedia: + '.chrnx-latest-podcast .chrnx-podcast-card__image, .chrnx-latest-podcast .chrnx-podcast-card__fallback', supporting: '.chrnx-latest-podcast', }, ]; async function waitForDecodedLandingImages( page, landing, { awaitDecode = true } = {} ) { - const selectors = [ landing.atmosphere, landing.art ]; - if ( landing.latestMedia ) { - selectors.push( landing.latestMedia ); - } - const images = page.locator( landing.root ).locator( selectors.join( ', ' ) ); + const root = page.locator( landing.root ); + const imageExpectations = [ + { name: 'atmosphere', selector: landing.atmosphere, count: 1 }, + { name: 'art', selector: landing.art, count: 1 }, + ]; - await expect - .poll( () => - images.evaluateAll( - ( elements ) => - elements.length >= 2 && + const decodeImages = async ( images ) => { + await expect + .poll( () => + images.evaluateAll( ( elements ) => elements.every( ( image ) => image.complete && image.naturalWidth > 0 && image.naturalHeight > 0, ), - ), - ) - .toBe( true ); - if ( ! awaitDecode ) { - await images.evaluateAll( ( elements ) => { - elements.forEach( ( image ) => { + ), + ) + .toBe( true ); + if ( ! awaitDecode ) { + await images.evaluateAll( ( elements ) => { + elements.forEach( ( image ) => { + if ( typeof image.decode === 'function' ) { + image.decode().catch( () => {} ); + } + if ( ! image.complete || image.naturalWidth <= 0 || image.naturalHeight <= 0 ) { + throw new Error( `Landing image failed to decode: ${ image.currentSrc || image.src }` ); + } + } ); + } ); + return; + } + await images.evaluateAll( async ( elements ) => { + const decodePromises = elements.map( ( image ) => { if ( typeof image.decode === 'function' ) { - image.decode().catch( () => {} ); + return image.decode(); } + return Promise.resolve(); + } ); + await Promise.all( decodePromises ); + elements.forEach( ( image ) => { if ( ! image.complete || image.naturalWidth <= 0 || image.naturalHeight <= 0 ) { throw new Error( `Landing image failed to decode: ${ image.currentSrc || image.src }` ); } } ); } ); + }; + + for ( const imageExpectation of imageExpectations ) { + const images = root.locator( imageExpectation.selector ); + await expect( images, `${ landing.name } ${ imageExpectation.name } image` ).toHaveCount( + imageExpectation.count, + ); + await decodeImages( images ); + } + + if ( ! landing.latestMedia ) { return; } - await images.evaluateAll( async ( elements ) => { - const decodePromises = elements.map( ( image ) => { - if ( typeof image.decode === 'function' ) { - return image.decode(); - } - return Promise.resolve(); - } ); - await Promise.all( decodePromises ); - elements.forEach( ( image ) => { - if ( ! image.complete || image.naturalWidth <= 0 || image.naturalHeight <= 0 ) { - throw new Error( `Landing image failed to decode: ${ image.currentSrc || image.src }` ); - } - } ); + + const latestMedia = root.locator( landing.latestMedia ); + await expect( latestMedia, `${ landing.name } latest media` ).toHaveCount( 1 ); + const latestMediaKind = await latestMedia.evaluate( ( media ) => media.tagName ); + if ( latestMediaKind === 'IMG' ) { + await expect( latestMedia ).not.toHaveAttribute( 'aria-hidden', 'true' ); + await decodeImages( latestMedia ); + return; + } + + expect( latestMediaKind ).toBe( 'SPAN' ); + await expect( latestMedia ).toHaveClass( /chrnx-podcast-card__fallback/ ); + await expect( latestMedia ).toHaveAttribute( 'aria-hidden', 'true' ); + await expect( latestMedia ).not.toHaveAttribute( 'role', /.+/ ); + await expect( root.locator( '.chrnx-latest-podcast img' ) ).toHaveCount( 0 ); + const fallbackState = await latestMedia.evaluate( ( media ) => { + const bounds = media.getBoundingClientRect(); + const style = window.getComputedStyle( media ); + return { + height: bounds.height, + visible: + style.display !== 'none' && + style.visibility !== 'hidden' && + Number.parseFloat( style.opacity ) > 0, + width: bounds.width, + }; } ); + expect( fallbackState.visible ).toBeTruthy(); + expect( fallbackState.width ).toBeGreaterThan( 0 ); + expect( fallbackState.height ).toBeGreaterThan( 0 ); } async function readMusicPodcastLandingState( page, landing ) { diff --git a/tests/smoke/visual-integration.test.mjs b/tests/smoke/visual-integration.test.mjs index 894d3fa..8bbb9c9 100644 --- a/tests/smoke/visual-integration.test.mjs +++ b/tests/smoke/visual-integration.test.mjs @@ -533,6 +533,13 @@ test( 'Music and Podcast visual states decode every relevant image before measur const visualStates = read( 'tests/e2e/visual-states.spec.mjs' ); assert.match( visualStates, /async function waitForDecodedLandingImages/ ); + assert.doesNotMatch( visualStates, /elements\.length >= 2/ ); + assert.match( visualStates, /for \( const imageExpectation of imageExpectations \)/ ); + assert.match( visualStates, /toHaveCount\(\s*imageExpectation\.count/ ); + assert.match( visualStates, /latestMedia[^]*?toHaveCount\( 1 \)/ ); + assert.match( visualStates, /latestMediaKind === 'IMG'[^]*?decodeImages\( latestMedia \)/ ); + assert.match( visualStates, /chrnx-podcast-card__fallback[^]*?aria-hidden[^]*?'true'/ ); + assert.match( visualStates, /chrnx-latest-podcast img' \) \)\.toHaveCount\( 0 \)/ ); assert.match( visualStates, /image\.complete/ ); assert.match( visualStates, /image\.naturalWidth > 0/ ); assert.match( visualStates, /image\.naturalHeight > 0/ );