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.',
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 ) {
+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' );
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/ );