diff --git a/tests/js/related-posts.test.js b/tests/js/related-posts.test.js index d864868..06c5f75 100644 --- a/tests/js/related-posts.test.js +++ b/tests/js/related-posts.test.js @@ -229,17 +229,46 @@ describe( 'Related Stories progressive controllers', () => { }, ); - test( 'missing matchMedia or media listener APIs fail closed without throwing', () => { + test( 'missing matchMedia on an otherwise valid window fails closed', () => { const { initialize } = require( modulePath ); const fixture = installFixture().get( 'one' ); - expect( () => initialize( { document, window: {} } ) ).not.toThrow(); - expect( fixture.controls.hidden ).toBe( true ); + 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 ); + }, + }, + ); - const incomplete = { matches: true }; - expect( () => initialize( options( incomplete ) ) ).not.toThrow(); + 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 ) => { @@ -333,6 +362,18 @@ describe( 'Related Stories progressive controllers', () => { 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(