feat: enhance Related Stories carousels
This commit is contained in:
5 files changed
+605
No files matched your search
@@ -0,0 +1,236 @@
|
||||
( function () {
|
||||
'use strict';
|
||||
|
||||
let cleanupCurrent = null;
|
||||
|
||||
function validMediaQuery( media ) {
|
||||
return Boolean(
|
||||
media &&
|
||||
typeof media.matches === 'boolean' &&
|
||||
typeof media.addEventListener === 'function' &&
|
||||
typeof media.removeEventListener === 'function',
|
||||
);
|
||||
}
|
||||
|
||||
function mediaQuery( provided, win, query ) {
|
||||
if ( provided !== undefined ) {
|
||||
return provided;
|
||||
}
|
||||
if ( ! win || typeof win.matchMedia !== 'function' ) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return win.matchMedia( query );
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function rememberAttribute( element, name ) {
|
||||
return element.hasAttribute( name ) ? element.getAttribute( name ) : null;
|
||||
}
|
||||
|
||||
function restoreAttribute( element, name, value ) {
|
||||
if ( value === null ) {
|
||||
element.removeAttribute( name );
|
||||
} else {
|
||||
element.setAttribute( name, value );
|
||||
}
|
||||
}
|
||||
|
||||
function focusWithoutScroll( element ) {
|
||||
try {
|
||||
element.focus( { preventScroll: true } );
|
||||
} catch {
|
||||
element.focus();
|
||||
}
|
||||
}
|
||||
|
||||
function initializeSection( section, environment ) {
|
||||
if ( section.classList.contains( 'is-layout-grid' ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const adaptive = section.classList.contains( 'is-layout-adaptive' );
|
||||
const carousel = section.classList.contains( 'is-layout-carousel' );
|
||||
const headingId = section.getAttribute( 'aria-labelledby' );
|
||||
const heading = headingId && environment.document.getElementById( headingId );
|
||||
const track = section.querySelector( '[data-chrnx-related-track]' );
|
||||
const controls = section.querySelector( '.chrnx-related-posts__controls' );
|
||||
const previous = controls && controls.querySelector( '[data-chrnx-related-previous]' );
|
||||
const next = controls && controls.querySelector( '[data-chrnx-related-next]' );
|
||||
const cards = track ? [ ...track.querySelectorAll( 'article' ) ] : [];
|
||||
|
||||
if (
|
||||
( ! adaptive && ! carousel ) ||
|
||||
! heading ||
|
||||
! track ||
|
||||
! controls ||
|
||||
! previous ||
|
||||
! next ||
|
||||
! cards.length ||
|
||||
typeof cards[ 0 ].getBoundingClientRect !== 'function' ||
|
||||
typeof track.scrollBy !== 'function'
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const original = {
|
||||
controlsHidden: controls.hidden,
|
||||
previousDisabled: previous.disabled,
|
||||
nextDisabled: next.disabled,
|
||||
role: rememberAttribute( track, 'role' ),
|
||||
labelledBy: rememberAttribute( track, 'aria-labelledby' ),
|
||||
tabIndex: rememberAttribute( track, 'tabindex' ),
|
||||
};
|
||||
let rail = carousel || environment.media.matches;
|
||||
|
||||
const updateEdges = () => {
|
||||
const maximum = Math.max( 0, track.scrollWidth - track.clientWidth );
|
||||
previous.disabled = track.scrollLeft <= 1;
|
||||
next.disabled = track.scrollLeft >= maximum - 1;
|
||||
};
|
||||
const measuredStep = () => {
|
||||
const style = environment.getComputedStyle( track );
|
||||
let gap = Number.parseFloat( style.columnGap );
|
||||
if ( ! Number.isFinite( gap ) ) {
|
||||
gap = Number.parseFloat( style.gap );
|
||||
}
|
||||
if ( ! Number.isFinite( gap ) ) {
|
||||
gap = 0;
|
||||
}
|
||||
return cards[ 0 ].getBoundingClientRect().width + gap;
|
||||
};
|
||||
const scroll = ( direction ) => {
|
||||
if ( ! rail ) {
|
||||
return;
|
||||
}
|
||||
track.scrollBy( {
|
||||
left: direction * measuredStep(),
|
||||
behavior: environment.reducedMotion.matches ? 'auto' : 'smooth',
|
||||
} );
|
||||
};
|
||||
const focusHeadingBeforeHide = () => {
|
||||
const active = environment.document.activeElement;
|
||||
if ( active !== previous && active !== next ) {
|
||||
return;
|
||||
}
|
||||
const previousTabIndex = rememberAttribute( heading, 'tabindex' );
|
||||
heading.setAttribute( 'tabindex', '-1' );
|
||||
focusWithoutScroll( heading );
|
||||
restoreAttribute( heading, 'tabindex', previousTabIndex );
|
||||
};
|
||||
const applyViewport = () => {
|
||||
const nextRail = carousel || environment.media.matches;
|
||||
if ( rail && ! nextRail ) {
|
||||
focusHeadingBeforeHide();
|
||||
}
|
||||
rail = nextRail;
|
||||
if ( rail ) {
|
||||
track.setAttribute( 'role', 'region' );
|
||||
track.setAttribute( 'aria-labelledby', headingId );
|
||||
track.setAttribute( 'tabindex', '0' );
|
||||
controls.hidden = false;
|
||||
updateEdges();
|
||||
} else {
|
||||
controls.hidden = true;
|
||||
track.removeAttribute( 'role' );
|
||||
track.removeAttribute( 'aria-labelledby' );
|
||||
track.removeAttribute( 'tabindex' );
|
||||
}
|
||||
};
|
||||
const onPrevious = () => scroll( -1 );
|
||||
const onNext = () => scroll( 1 );
|
||||
const onMediaChange = () => applyViewport();
|
||||
const onResize = () => updateEdges();
|
||||
|
||||
previous.addEventListener( 'click', onPrevious );
|
||||
next.addEventListener( 'click', onNext );
|
||||
track.addEventListener( 'scroll', updateEdges );
|
||||
environment.window.addEventListener( 'resize', onResize );
|
||||
if ( adaptive ) {
|
||||
environment.media.addEventListener( 'change', onMediaChange );
|
||||
}
|
||||
applyViewport();
|
||||
|
||||
return () => {
|
||||
previous.removeEventListener( 'click', onPrevious );
|
||||
next.removeEventListener( 'click', onNext );
|
||||
track.removeEventListener( 'scroll', updateEdges );
|
||||
environment.window.removeEventListener( 'resize', onResize );
|
||||
if ( adaptive ) {
|
||||
environment.media.removeEventListener( 'change', onMediaChange );
|
||||
}
|
||||
controls.hidden = original.controlsHidden;
|
||||
previous.disabled = original.previousDisabled;
|
||||
next.disabled = original.nextDisabled;
|
||||
restoreAttribute( track, 'role', original.role );
|
||||
restoreAttribute( track, 'aria-labelledby', original.labelledBy );
|
||||
restoreAttribute( track, 'tabindex', original.tabIndex );
|
||||
};
|
||||
}
|
||||
|
||||
function cleanup() {
|
||||
if ( cleanupCurrent ) {
|
||||
const activeCleanup = cleanupCurrent;
|
||||
cleanupCurrent = null;
|
||||
activeCleanup();
|
||||
}
|
||||
}
|
||||
|
||||
function initialize( options = {} ) {
|
||||
cleanup();
|
||||
|
||||
const doc = options.document || ( typeof document === 'undefined' ? null : document );
|
||||
const win = options.window || ( doc && doc.defaultView );
|
||||
if (
|
||||
! doc ||
|
||||
! win ||
|
||||
typeof win.addEventListener !== 'function' ||
|
||||
typeof win.removeEventListener !== 'function'
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const media = mediaQuery( options.media, win, '(max-width: 900px)' );
|
||||
const reducedMotion = mediaQuery(
|
||||
options.reducedMotion,
|
||||
win,
|
||||
'(prefers-reduced-motion: reduce)',
|
||||
);
|
||||
const getStyle =
|
||||
options.getComputedStyle ||
|
||||
( typeof win.getComputedStyle === 'function' ? win.getComputedStyle.bind( win ) : null );
|
||||
if ( ! validMediaQuery( media ) || ! validMediaQuery( reducedMotion ) || ! getStyle ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const environment = {
|
||||
document: doc,
|
||||
window: win,
|
||||
media,
|
||||
reducedMotion,
|
||||
getComputedStyle: getStyle,
|
||||
};
|
||||
const cleanups = [ ...doc.querySelectorAll( '.chrnx-related-posts' ) ]
|
||||
.map( ( section ) => initializeSection( section, environment ) )
|
||||
.filter( Boolean );
|
||||
if ( ! cleanups.length ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
cleanupCurrent = () => cleanups.forEach( ( callback ) => callback() );
|
||||
return cleanupCurrent;
|
||||
}
|
||||
|
||||
const helpers = { cleanup, initialize };
|
||||
if ( typeof module !== 'undefined' && module.exports ) {
|
||||
module.exports = helpers;
|
||||
} else if ( typeof document !== 'undefined' ) {
|
||||
if ( document.readyState === 'loading' ) {
|
||||
document.addEventListener( 'DOMContentLoaded', () => initialize(), { once: true } );
|
||||
} else {
|
||||
initialize();
|
||||
}
|
||||
}
|
||||
} )();
|
||||
@@ -15,6 +15,7 @@
|
||||
},
|
||||
"supports": { "html": false, "align": ["wide", "full"] },
|
||||
"editorScript": "file:../../assets/js/editor-related-posts.js",
|
||||
"viewScript": "file:../../assets/js/related-posts.js",
|
||||
"style": "file:./style.css",
|
||||
"render": "file:./render.php"
|
||||
}
|
||||
@@ -0,0 +1,342 @@
|
||||
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 or media listener APIs fail closed without throwing', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
const fixture = installFixture().get( 'one' );
|
||||
expect( () => initialize( { document, window: {} } ) ).not.toThrow();
|
||||
expect( fixture.controls.hidden ).toBe( true );
|
||||
|
||||
const incomplete = { matches: true };
|
||||
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.hasAttribute( 'tabindex' ) ).toBe( false );
|
||||
expect( fixture.controls.hidden ).toBe( true );
|
||||
},
|
||||
);
|
||||
|
||||
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( '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/,
|
||||
);
|
||||
} );
|
||||
} );
|
||||
@@ -168,4 +168,22 @@ class EditorAssetsTest extends WP_UnitTestCase {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** Related Stories view metadata registers the exact existing local controller only. */
|
||||
public function test_related_posts_view_script_resolves_to_local_controller() {
|
||||
do_action( 'init' );
|
||||
$type = WP_Block_Type_Registry::get_instance()->get_registered( 'chrnx/related-posts' );
|
||||
|
||||
$this->assertInstanceOf( WP_Block_Type::class, $type );
|
||||
$this->assertCount( 1, $type->view_script_handles );
|
||||
$handle = $type->view_script_handles[0];
|
||||
$this->assertNotEmpty( $handle );
|
||||
|
||||
$scripts = wp_scripts();
|
||||
$this->assertArrayHasKey( $handle, $scripts->registered );
|
||||
$registered = $scripts->registered[ $handle ];
|
||||
$this->assertSame( get_theme_file_uri( 'assets/js/related-posts.js' ), $registered->src );
|
||||
$this->assertSame( array(), $registered->deps );
|
||||
$this->assertFileExists( get_theme_file_path( 'assets/js/related-posts.js' ) );
|
||||
}
|
||||
}
|
||||
@@ -16,3 +16,11 @@ test( 'Related Stories metadata exposes only the approved server attributes', as
|
||||
enum: [ 'adaptive', 'grid', 'carousel' ],
|
||||
} );
|
||||
} );
|
||||
|
||||
test( 'Related Stories owns one exact local progressive controller', async () => {
|
||||
const metadata = JSON.parse( await readFile( 'blocks/related-posts/block.json', 'utf8' ) );
|
||||
|
||||
assert.equal( metadata.viewScript, 'file:../../assets/js/related-posts.js' );
|
||||
assert.equal( Object.hasOwn( metadata, 'viewScriptModule' ), false );
|
||||
await readFile( 'assets/js/related-posts.js', 'utf8' );
|
||||
} );
|
||||
Reference in new issue
Block a user