test: require CHRNX podcast media

This commit is contained in:
chrnx committed 2026-08-13 08:05:53 -05:00
1 parent 62df36ef81
commit 54ea215895
2 files changed
+81 -31

No files matched your search

+74 -31
View File
@@ -1220,56 +1220,99 @@ const musicPodcastLandingCases = [
copy: 'Conversations about the work behind software, research, music, and games.', copy: 'Conversations about the work behind software, research, music, and games.',
atmosphere: 'img[src*="podcast-frequency-grunge.png"]', atmosphere: 'img[src*="podcast-frequency-grunge.png"]',
art: 'img[src*="alter-ego-podcasts.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', supporting: '.chrnx-latest-podcast',
}, },
]; ];
async function waitForDecodedLandingImages( page, landing, { awaitDecode = true } = {} ) { async function waitForDecodedLandingImages( page, landing, { awaitDecode = true } = {} ) {
const selectors = [ landing.atmosphere, landing.art ]; const root = page.locator( landing.root );
if ( landing.latestMedia ) { const imageExpectations = [
selectors.push( landing.latestMedia ); { name: 'atmosphere', selector: landing.atmosphere, count: 1 },
} { name: 'art', selector: landing.art, count: 1 },
const images = page.locator( landing.root ).locator( selectors.join( ', ' ) ); ];
await expect const decodeImages = async ( images ) => {
.poll( () => await expect
images.evaluateAll( .poll( () =>
( elements ) => images.evaluateAll( ( elements ) =>
elements.length >= 2 &&
elements.every( elements.every(
( image ) => image.complete && image.naturalWidth > 0 && image.naturalHeight > 0, ( image ) => image.complete && image.naturalWidth > 0 && image.naturalHeight > 0,
), ),
), ),
) )
.toBe( true ); .toBe( true );
if ( ! awaitDecode ) { if ( ! awaitDecode ) {
await images.evaluateAll( ( elements ) => { await images.evaluateAll( ( elements ) => {
elements.forEach( ( image ) => { 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' ) { 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 ) { if ( ! image.complete || image.naturalWidth <= 0 || image.naturalHeight <= 0 ) {
throw new Error( `Landing image failed to decode: ${ image.currentSrc || image.src }` ); 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; return;
} }
await images.evaluateAll( async ( elements ) => {
const decodePromises = elements.map( ( image ) => { const latestMedia = root.locator( landing.latestMedia );
if ( typeof image.decode === 'function' ) { await expect( latestMedia, `${ landing.name } latest media` ).toHaveCount( 1 );
return image.decode(); const latestMediaKind = await latestMedia.evaluate( ( media ) => media.tagName );
} if ( latestMediaKind === 'IMG' ) {
return Promise.resolve(); await expect( latestMedia ).not.toHaveAttribute( 'aria-hidden', 'true' );
} ); await decodeImages( latestMedia );
await Promise.all( decodePromises ); return;
elements.forEach( ( image ) => { }
if ( ! image.complete || image.naturalWidth <= 0 || image.naturalHeight <= 0 ) {
throw new Error( `Landing image failed to decode: ${ image.currentSrc || image.src }` ); 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 ) { async function readMusicPodcastLandingState( page, landing ) {
+7
View File
@@ -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' ); const visualStates = read( 'tests/e2e/visual-states.spec.mjs' );
assert.match( visualStates, /async function waitForDecodedLandingImages/ ); 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\.complete/ );
assert.match( visualStates, /image\.naturalWidth > 0/ ); assert.match( visualStates, /image\.naturalWidth > 0/ );
assert.match( visualStates, /image\.naturalHeight > 0/ ); assert.match( visualStates, /image\.naturalHeight > 0/ );