405 lines
15 KiB
JavaScript
405 lines
15 KiB
JavaScript
const fs = require( 'node:fs' );
|
|
|
|
const modulePath = '../../assets/js/related-posts.js';
|
|
|
|
function createMedia( matches ) {
|
|
const listeners = new Set();
|
|
return {
|
|
matches,
|
|
addEventListener: jest.fn( ( type, listener ) => {
|
|
if ( type === 'change' ) {
|
|
listeners.add( listener );
|
|
}
|
|
} ),
|
|
removeEventListener: jest.fn( ( type, listener ) => {
|
|
if ( type === 'change' ) {
|
|
listeners.delete( listener );
|
|
}
|
|
} ),
|
|
change( next ) {
|
|
this.matches = next;
|
|
listeners.forEach( ( listener ) => listener( { matches: next } ) );
|
|
},
|
|
listenerCount: () => listeners.size,
|
|
};
|
|
}
|
|
|
|
function sectionMarkup( id, layout = 'adaptive', cards = 3 ) {
|
|
return `
|
|
<section id="${ id }" class="chrnx-related-posts is-layout-${ layout }"
|
|
aria-labelledby="${ id }-title">
|
|
<h2 id="${ id }-title">Related stories</h2>
|
|
<div class="chrnx-related-posts__items" data-chrnx-related-track
|
|
role="region" aria-labelledby="${ id }-title" tabindex="0">
|
|
${ Array.from(
|
|
{ length: cards },
|
|
( _, index ) => `<article><a href="/${ id }-${ index + 1 }/">Story</a></article>`,
|
|
).join( '' ) }
|
|
</div>
|
|
<div class="chrnx-related-posts__controls" role="group" hidden>
|
|
<button type="button" data-chrnx-related-previous>Previous stories</button>
|
|
<button type="button" data-chrnx-related-next>Next stories</button>
|
|
</div>
|
|
</section>`;
|
|
}
|
|
|
|
function installFixture( sections = [ [ 'one', 'adaptive', 3 ] ] ) {
|
|
document.body.innerHTML = `${ sections
|
|
.map( ( section ) => sectionMarkup( ...section ) )
|
|
.join( '' ) }<button data-unrelated>Outside</button>`;
|
|
|
|
const controls = new Map();
|
|
sections.forEach( ( [ id, , cardCount ] ) => {
|
|
const section = document.getElementById( id );
|
|
const track = section.querySelector( '[data-chrnx-related-track]' );
|
|
const cards = [ ...track.querySelectorAll( 'article' ) ];
|
|
const scrollBy = jest.fn();
|
|
const geometry = {
|
|
cardWidth: 200,
|
|
clientWidth: 420,
|
|
scrollLeft: 0,
|
|
scrollWidth: cardCount * 220 - 20,
|
|
};
|
|
cards.forEach( ( card ) => {
|
|
card.getBoundingClientRect = jest.fn( () => ( { width: geometry.cardWidth } ) );
|
|
} );
|
|
Object.defineProperties( track, {
|
|
clientWidth: { configurable: true, get: () => geometry.clientWidth },
|
|
scrollLeft: {
|
|
configurable: true,
|
|
get: () => geometry.scrollLeft,
|
|
set: ( value ) => {
|
|
geometry.scrollLeft = value;
|
|
},
|
|
},
|
|
scrollWidth: { configurable: true, get: () => geometry.scrollWidth },
|
|
} );
|
|
track.scrollBy = scrollBy;
|
|
controls.set( id, {
|
|
section,
|
|
heading: section.querySelector( 'h2' ),
|
|
track,
|
|
cards,
|
|
controls: section.querySelector( '.chrnx-related-posts__controls' ),
|
|
previous: section.querySelector( '[data-chrnx-related-previous]' ),
|
|
next: section.querySelector( '[data-chrnx-related-next]' ),
|
|
geometry,
|
|
scrollBy,
|
|
} );
|
|
} );
|
|
return controls;
|
|
}
|
|
|
|
function options( media, reducedMotion = createMedia( false ) ) {
|
|
return {
|
|
document,
|
|
media,
|
|
reducedMotion,
|
|
getComputedStyle: () => ( { columnGap: '20px', gap: '20px' } ),
|
|
};
|
|
}
|
|
|
|
describe( 'Related Stories progressive controllers', () => {
|
|
beforeEach( () => {
|
|
jest.resetModules();
|
|
document.body.innerHTML = '';
|
|
} );
|
|
|
|
afterEach( () => {
|
|
try {
|
|
const loaded = require.cache[ require.resolve( modulePath ) ];
|
|
if ( loaded ) {
|
|
loaded.exports.cleanup();
|
|
}
|
|
} catch {
|
|
// RED begins before the controller module exists.
|
|
}
|
|
} );
|
|
|
|
test( 'initializes valid sections independently and never enhances grid', () => {
|
|
const { initialize } = require( modulePath );
|
|
const fixtures = installFixture( [
|
|
[ 'first', 'adaptive', 3 ],
|
|
[ 'second', 'carousel', 3 ],
|
|
[ 'fixed', 'grid', 3 ],
|
|
] );
|
|
|
|
const cleanup = initialize( options( createMedia( true ) ) );
|
|
expect( cleanup ).toEqual( expect.any( Function ) );
|
|
expect( fixtures.get( 'first' ).controls.hidden ).toBe( false );
|
|
expect( fixtures.get( 'second' ).controls.hidden ).toBe( false );
|
|
expect( fixtures.get( 'fixed' ).controls.hidden ).toBe( true );
|
|
|
|
fixtures.get( 'first' ).next.click();
|
|
expect( fixtures.get( 'first' ).scrollBy ).toHaveBeenCalledTimes( 1 );
|
|
expect( fixtures.get( 'second' ).scrollBy ).not.toHaveBeenCalled();
|
|
expect( fixtures.get( 'fixed' ).scrollBy ).not.toHaveBeenCalled();
|
|
} );
|
|
|
|
test( 'adaptive is a rail through 900 and a semantic grid above while carousel stays a rail', () => {
|
|
const { initialize } = require( modulePath );
|
|
const media = createMedia( true );
|
|
const fixtures = installFixture( [
|
|
[ 'adaptive', 'adaptive', 3 ],
|
|
[ 'carousel', 'carousel', 3 ],
|
|
] );
|
|
initialize( options( media ) );
|
|
|
|
expect( fixtures.get( 'adaptive' ).controls.hidden ).toBe( false );
|
|
expect( fixtures.get( 'adaptive' ).track.getAttribute( 'role' ) ).toBe( 'region' );
|
|
expect( fixtures.get( 'adaptive' ).track.tabIndex ).toBe( 0 );
|
|
expect( fixtures.get( 'carousel' ).controls.hidden ).toBe( false );
|
|
|
|
media.change( false );
|
|
expect( fixtures.get( 'adaptive' ).controls.hidden ).toBe( true );
|
|
expect( fixtures.get( 'adaptive' ).track.hasAttribute( 'role' ) ).toBe( false );
|
|
expect( fixtures.get( 'adaptive' ).track.hasAttribute( 'tabindex' ) ).toBe( false );
|
|
expect( fixtures.get( 'carousel' ).controls.hidden ).toBe( false );
|
|
expect( fixtures.get( 'carousel' ).track.getAttribute( 'role' ) ).toBe( 'region' );
|
|
} );
|
|
|
|
test( 'moves by one measured card plus computed gap in either direction', () => {
|
|
const { initialize } = require( modulePath );
|
|
const fixture = installFixture().get( 'one' );
|
|
initialize( options( createMedia( true ) ) );
|
|
|
|
fixture.next.click();
|
|
fixture.geometry.scrollLeft = 100;
|
|
fixture.track.dispatchEvent( new Event( 'scroll' ) );
|
|
fixture.previous.click();
|
|
expect( fixture.cards[ 0 ].getBoundingClientRect ).toHaveBeenCalled();
|
|
expect( fixture.scrollBy ).toHaveBeenNthCalledWith( 1, { left: 220, behavior: 'smooth' } );
|
|
expect( fixture.scrollBy ).toHaveBeenNthCalledWith( 2, { left: -220, behavior: 'smooth' } );
|
|
} );
|
|
|
|
test( 'updates native edge disabled states after scroll and resize', () => {
|
|
const { initialize } = require( modulePath );
|
|
const fixture = installFixture().get( 'one' );
|
|
initialize( options( createMedia( true ) ) );
|
|
|
|
expect( fixture.previous.disabled ).toBe( true );
|
|
expect( fixture.next.disabled ).toBe( false );
|
|
fixture.geometry.scrollLeft = 100;
|
|
fixture.track.dispatchEvent( new Event( 'scroll' ) );
|
|
expect( fixture.previous.disabled ).toBe( false );
|
|
expect( fixture.next.disabled ).toBe( false );
|
|
fixture.geometry.scrollLeft = fixture.geometry.scrollWidth - fixture.geometry.clientWidth;
|
|
fixture.track.dispatchEvent( new Event( 'scroll' ) );
|
|
expect( fixture.next.disabled ).toBe( true );
|
|
|
|
fixture.geometry.scrollLeft = 0;
|
|
fixture.geometry.clientWidth = fixture.geometry.scrollWidth;
|
|
window.dispatchEvent( new Event( 'resize' ) );
|
|
expect( fixture.previous.disabled ).toBe( true );
|
|
expect( fixture.next.disabled ).toBe( true );
|
|
} );
|
|
|
|
test( 'reduced motion uses auto while the default permits smooth movement', () => {
|
|
const { initialize } = require( modulePath );
|
|
const fixture = installFixture().get( 'one' );
|
|
initialize( options( createMedia( true ), createMedia( true ) ) );
|
|
fixture.next.click();
|
|
expect( fixture.scrollBy ).toHaveBeenLastCalledWith( { left: 220, behavior: 'auto' } );
|
|
|
|
fixture.scrollBy.mockClear();
|
|
initialize( options( createMedia( true ), createMedia( false ) ) );
|
|
fixture.next.click();
|
|
expect( fixture.scrollBy ).toHaveBeenLastCalledWith( { left: 220, behavior: 'smooth' } );
|
|
} );
|
|
|
|
test.each( [ 'track', 'controls', 'previous', 'next', 'cards', 'scrollBy' ] )(
|
|
'missing %s leaves controls hidden and does not throw',
|
|
( missing ) => {
|
|
const { initialize } = require( modulePath );
|
|
const fixture = installFixture().get( 'one' );
|
|
if ( missing === 'track' ) {
|
|
fixture.track.remove();
|
|
} else if ( missing === 'controls' ) {
|
|
fixture.controls.remove();
|
|
} else if ( missing === 'previous' || missing === 'next' ) {
|
|
fixture[ missing ].remove();
|
|
} else if ( missing === 'cards' ) {
|
|
fixture.cards.forEach( ( card ) => card.remove() );
|
|
} else {
|
|
delete fixture.track.scrollBy;
|
|
}
|
|
|
|
expect( () => initialize( options( createMedia( true ) ) ) ).not.toThrow();
|
|
expect( fixture.controls.hidden ).toBe( true );
|
|
},
|
|
);
|
|
|
|
test( 'missing matchMedia on an otherwise valid window fails closed', () => {
|
|
const { initialize } = require( modulePath );
|
|
const fixture = installFixture().get( 'one' );
|
|
const matchMediaReads = jest.fn();
|
|
const windowWithoutMatchMedia = new Proxy(
|
|
{
|
|
addEventListener: jest.fn(),
|
|
removeEventListener: jest.fn(),
|
|
getComputedStyle: jest.fn( () => ( { columnGap: '20px' } ) ),
|
|
},
|
|
{
|
|
get( target, property ) {
|
|
if ( property === 'matchMedia' ) {
|
|
matchMediaReads();
|
|
return undefined;
|
|
}
|
|
return Reflect.get( target, property );
|
|
},
|
|
},
|
|
);
|
|
|
|
expect( initialize( { document, window: windowWithoutMatchMedia } ) ).toBe( false );
|
|
expect( matchMediaReads ).toHaveBeenCalled();
|
|
expect( fixture.controls.hidden ).toBe( true );
|
|
expect( windowWithoutMatchMedia.addEventListener ).not.toHaveBeenCalled();
|
|
} );
|
|
|
|
test.each( [ 'addEventListener', 'removeEventListener' ] )(
|
|
'missing media %s API fails closed without throwing',
|
|
( missingApi ) => {
|
|
const { initialize } = require( modulePath );
|
|
const fixture = installFixture().get( 'one' );
|
|
const incomplete = createMedia( true );
|
|
delete incomplete[ missingApi ];
|
|
|
|
expect( () => initialize( options( incomplete ) ) ).not.toThrow();
|
|
expect( fixture.controls.hidden ).toBe( true );
|
|
},
|
|
);
|
|
|
|
test.each( [ 'previous', 'next' ] )(
|
|
'900 to 901 moves %s focus to heading before controls hide',
|
|
( button ) => {
|
|
const { initialize } = require( modulePath );
|
|
const media = createMedia( true );
|
|
const fixture = installFixture().get( 'one' );
|
|
initialize( options( media ) );
|
|
if ( button === 'previous' ) {
|
|
fixture.geometry.scrollLeft = 100;
|
|
fixture.track.dispatchEvent( new Event( 'scroll' ) );
|
|
}
|
|
fixture[ button ].focus();
|
|
|
|
let focusAtHide = null;
|
|
let hidden = fixture.controls.hidden;
|
|
Object.defineProperty( fixture.controls, 'hidden', {
|
|
configurable: true,
|
|
get: () => hidden,
|
|
set: ( value ) => {
|
|
if ( value ) {
|
|
focusAtHide = document.activeElement;
|
|
}
|
|
hidden = value;
|
|
},
|
|
} );
|
|
|
|
media.change( false );
|
|
expect( focusAtHide ).toBe( fixture.heading );
|
|
expect( document.activeElement ).toBe( fixture.heading );
|
|
expect( fixture.heading.getAttribute( 'tabindex' ) ).toBe( '-1' );
|
|
document.querySelector( '[data-unrelated]' ).focus();
|
|
expect( fixture.heading.hasAttribute( 'tabindex' ) ).toBe( false );
|
|
expect( fixture.controls.hidden ).toBe( true );
|
|
},
|
|
);
|
|
|
|
test( 'resize repairs focused controls before the media change event arrives', () => {
|
|
const { initialize } = require( modulePath );
|
|
const media = createMedia( true );
|
|
const fixture = installFixture().get( 'one' );
|
|
initialize( options( media ) );
|
|
fixture.next.focus();
|
|
|
|
media.matches = false;
|
|
fixture.geometry.clientWidth = fixture.geometry.scrollWidth;
|
|
window.dispatchEvent( new Event( 'resize' ) );
|
|
|
|
expect( document.activeElement ).toBe( fixture.heading );
|
|
expect( fixture.controls.hidden ).toBe( true );
|
|
expect( fixture.next.disabled ).toBe( false );
|
|
expect( fixture.heading.getAttribute( 'tabindex' ) ).toBe( '-1' );
|
|
document.querySelector( '[data-unrelated]' ).focus();
|
|
expect( fixture.heading.hasAttribute( 'tabindex' ) ).toBe( false );
|
|
} );
|
|
|
|
test( 'unrelated focus remains unchanged when adaptive controls hide', () => {
|
|
const { initialize } = require( modulePath );
|
|
const media = createMedia( true );
|
|
const fixture = installFixture().get( 'one' );
|
|
const unrelated = document.querySelector( '[data-unrelated]' );
|
|
initialize( options( media ) );
|
|
unrelated.focus();
|
|
media.change( false );
|
|
|
|
expect( document.activeElement ).toBe( unrelated );
|
|
expect( fixture.controls.hidden ).toBe( true );
|
|
} );
|
|
|
|
test( '901 to 900 restores rail semantics, controls, and current edge state', () => {
|
|
const { initialize } = require( modulePath );
|
|
const media = createMedia( false );
|
|
const fixture = installFixture().get( 'one' );
|
|
initialize( options( media ) );
|
|
expect( fixture.controls.hidden ).toBe( true );
|
|
|
|
media.change( true );
|
|
expect( fixture.controls.hidden ).toBe( false );
|
|
expect( fixture.track.getAttribute( 'role' ) ).toBe( 'region' );
|
|
expect( fixture.track.getAttribute( 'aria-labelledby' ) ).toBe( fixture.heading.id );
|
|
expect( fixture.track.tabIndex ).toBe( 0 );
|
|
expect( fixture.previous.disabled ).toBe( true );
|
|
} );
|
|
|
|
test( 'reinitialization avoids duplicate listeners and cleanup restores server state', () => {
|
|
const helpers = require( modulePath );
|
|
const media = createMedia( true );
|
|
const fixture = installFixture().get( 'one' );
|
|
const addTrack = jest.spyOn( fixture.track, 'addEventListener' );
|
|
const removeTrack = jest.spyOn( fixture.track, 'removeEventListener' );
|
|
const addWindow = jest.spyOn( window, 'addEventListener' );
|
|
const removeWindow = jest.spyOn( window, 'removeEventListener' );
|
|
|
|
helpers.initialize( options( media ) );
|
|
helpers.initialize( options( media ) );
|
|
fixture.next.click();
|
|
expect( fixture.scrollBy ).toHaveBeenCalledTimes( 1 );
|
|
expect( media.listenerCount() ).toBe( 1 );
|
|
expect( addTrack ).toHaveBeenCalledTimes( 2 );
|
|
expect( removeTrack ).toHaveBeenCalledTimes( 1 );
|
|
|
|
helpers.cleanup();
|
|
expect( media.listenerCount() ).toBe( 0 );
|
|
expect( removeTrack ).toHaveBeenCalledTimes( 2 );
|
|
expect( removeWindow ).toHaveBeenCalledTimes( addWindow.mock.calls.length );
|
|
expect( fixture.controls.hidden ).toBe( true );
|
|
expect( fixture.track.getAttribute( 'role' ) ).toBe( 'region' );
|
|
expect( fixture.track.tabIndex ).toBe( 0 );
|
|
|
|
addTrack.mockRestore();
|
|
removeTrack.mockRestore();
|
|
addWindow.mockRestore();
|
|
removeWindow.mockRestore();
|
|
} );
|
|
|
|
test( 'cleanup removes the Previous click listener', () => {
|
|
const helpers = require( modulePath );
|
|
const fixture = installFixture().get( 'one' );
|
|
fixture.geometry.scrollLeft = 100;
|
|
helpers.initialize( options( createMedia( true ) ) );
|
|
|
|
helpers.cleanup();
|
|
fixture.previous.click();
|
|
|
|
expect( fixture.scrollBy ).not.toHaveBeenCalled();
|
|
} );
|
|
|
|
test( 'source contains no autoplay, timers, clones, globals, link changes, or network APIs', () => {
|
|
const source = fs.readFileSync( require.resolve( modulePath ), 'utf8' );
|
|
expect( source ).not.toMatch(
|
|
/setTimeout|setInterval|requestAnimationFrame|autoplay|cloneNode|window\.[A-Za-z_$][\w$]*\s*=|\.href\s*=|setAttribute\(\s*['"]href|fetch\s*\(|XMLHttpRequest|WebSocket/,
|
|
);
|
|
} );
|
|
} );
|