feat: coordinate CHRNX mobile header panels
This commit is contained in:
6 files changed
+536
-262
No files matched your search
+331
-52
@@ -4,8 +4,16 @@ function createMedia( matches = true ) {
|
||||
const listeners = new Set();
|
||||
return {
|
||||
matches,
|
||||
addEventListener: jest.fn( ( _type, listener ) => listeners.add( listener ) ),
|
||||
removeEventListener: jest.fn( ( _type, listener ) => listeners.delete( listener ) ),
|
||||
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 } ) );
|
||||
@@ -16,83 +24,354 @@ function createMedia( matches = true ) {
|
||||
|
||||
function shellMarkup() {
|
||||
document.body.innerHTML = `
|
||||
<button hidden data-chrnx-menu-toggle aria-controls="chrnx-primary-navigation"
|
||||
aria-expanded="false" data-label-open="Open primary navigation"
|
||||
data-label-close="Close primary navigation">Open primary navigation</button>
|
||||
<nav id="chrnx-primary-navigation"><a href="/journal/">Journal</a></nav>
|
||||
<input aria-label="Search">
|
||||
<header data-chrnx-header-shell>
|
||||
<a href="/" class="chrnx-site-brand" data-part="brand">CHRNX</a>
|
||||
<button hidden data-part="menu" data-chrnx-menu-toggle
|
||||
aria-controls="chrnx-primary-navigation" aria-expanded="false"
|
||||
data-label-open="Open primary navigation"
|
||||
data-label-close="Close primary navigation">Menu</button>
|
||||
<nav id="chrnx-primary-navigation" data-part="nav">
|
||||
<a href="/journal/">Journal</a><a href="/projects/">Projects</a>
|
||||
</nav>
|
||||
<form data-part="search" data-chrnx-search action="/" method="get"
|
||||
role="search" data-expanded="true">
|
||||
<div id="chrnx-search-panel" data-chrnx-search-panel>
|
||||
<label for="chrnx-search-input">Search</label>
|
||||
<input id="chrnx-search-input" name="s" type="search">
|
||||
</div>
|
||||
<button type="submit" data-chrnx-search-button data-label-open="Open search"
|
||||
data-label-submit="Search" aria-controls="chrnx-search-panel"
|
||||
aria-expanded="true">Search</button>
|
||||
</form>
|
||||
<div data-part="theme" class="chrnx-theme-control">
|
||||
<button hidden type="button" data-chrnx-theme-button
|
||||
aria-controls="chrnx-theme-panel" aria-expanded="false">Theme</button>
|
||||
<fieldset id="chrnx-theme-panel" data-chrnx-theme-panel data-chrnx-theme-toggle>
|
||||
<label><input type="radio" name="theme" value="light"> Light</label>
|
||||
<label><input type="radio" name="theme" value="dark"> Dark</label>
|
||||
<label><input type="radio" name="theme" value="system" checked> System</label>
|
||||
</fieldset>
|
||||
</div>
|
||||
</header>
|
||||
<button data-outside>Outside</button>
|
||||
`;
|
||||
}
|
||||
|
||||
describe( 'progressive mobile navigation', () => {
|
||||
function controls() {
|
||||
return {
|
||||
shell: document.querySelector( '[data-chrnx-header-shell]' ),
|
||||
brand: document.querySelector( '[data-part="brand"]' ),
|
||||
menuButton: document.querySelector( '[data-chrnx-menu-toggle]' ),
|
||||
nav: document.querySelector( '[data-part="nav"]' ),
|
||||
firstLink: document.querySelector( '[data-part="nav"] a' ),
|
||||
search: document.querySelector( '[data-chrnx-search]' ),
|
||||
searchPanel: document.querySelector( '[data-chrnx-search-panel]' ),
|
||||
searchInput: document.querySelector( 'input[name="s"]' ),
|
||||
searchButton: document.querySelector( '[data-chrnx-search-button]' ),
|
||||
theme: document.querySelector( '[data-part="theme"]' ),
|
||||
themeButton: document.querySelector( '[data-chrnx-theme-button]' ),
|
||||
themePanel: document.querySelector( '[data-chrnx-theme-panel]' ),
|
||||
checkedRadio: document.querySelector( '[data-chrnx-theme-panel] input:checked' ),
|
||||
firstRadio: document.querySelector( '[data-chrnx-theme-panel] input' ),
|
||||
outside: document.querySelector( '[data-outside]' ),
|
||||
};
|
||||
}
|
||||
|
||||
function directOrder( shell ) {
|
||||
return [ ...shell.children ].map( ( element ) => element.dataset.part );
|
||||
}
|
||||
|
||||
function submit( form ) {
|
||||
const event = new Event( 'submit', { bubbles: true, cancelable: true } );
|
||||
form.dispatchEvent( event );
|
||||
return event;
|
||||
}
|
||||
|
||||
function press( target, key ) {
|
||||
const event = new window.KeyboardEvent( 'keydown', {
|
||||
key,
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
} );
|
||||
target.dispatchEvent( event );
|
||||
return event;
|
||||
}
|
||||
|
||||
function visibleFocusableElements() {
|
||||
return [ ...document.querySelectorAll( 'a[href], button, input' ) ].filter( ( element ) => {
|
||||
if ( element.hidden || element.closest( '[hidden]' ) ) {
|
||||
return false;
|
||||
}
|
||||
if ( element.type !== 'radio' || element.checked ) {
|
||||
return true;
|
||||
}
|
||||
return ! document.querySelector( `input[type="radio"][name="${ element.name }"]:checked` );
|
||||
} );
|
||||
}
|
||||
|
||||
function nextVisibleFocusable( element ) {
|
||||
const focusables = visibleFocusableElements();
|
||||
return focusables[ focusables.indexOf( element ) + 1 ];
|
||||
}
|
||||
|
||||
function activePanelCount( control ) {
|
||||
return [ control.searchButton, control.themeButton, control.menuButton ].filter(
|
||||
( button ) => button.getAttribute( 'aria-expanded' ) === 'true',
|
||||
).length;
|
||||
}
|
||||
|
||||
describe( 'coordinated CHRNX header shell', () => {
|
||||
beforeEach( () => {
|
||||
jest.resetModules();
|
||||
document.documentElement.className = '';
|
||||
shellMarkup();
|
||||
} );
|
||||
|
||||
test( 'mobile toggle opens one target without intercepting Tab', () => {
|
||||
test( 'mobile order is brand, Search, Theme, Menu, then Navigation', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
const media = createMedia( true );
|
||||
expect( initialize( { document, media } ) ).toBe( true );
|
||||
const button = document.querySelector( '[data-chrnx-menu-toggle]' );
|
||||
const nav = document.querySelector( 'nav' );
|
||||
expect( button.hidden ).toBe( false );
|
||||
expect( nav.hidden ).toBe( true );
|
||||
button.click();
|
||||
expect( nav.hidden ).toBe( false );
|
||||
expect( button.getAttribute( 'aria-expanded' ) ).toBe( 'true' );
|
||||
expect( button.getAttribute( 'aria-label' ) ).toBe( 'Close primary navigation' );
|
||||
const tabEvent = new window.KeyboardEvent( 'keydown', {
|
||||
key: 'Tab',
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
} );
|
||||
document.dispatchEvent( tabEvent );
|
||||
expect( tabEvent.defaultPrevented ).toBe( false );
|
||||
expect( initialize( { document, media: createMedia( true ) } ) ).toBe( true );
|
||||
const control = controls();
|
||||
expect( directOrder( control.shell ) ).toEqual( [ 'brand', 'search', 'theme', 'menu', 'nav' ] );
|
||||
expect( control.searchPanel.hidden ).toBe( true );
|
||||
expect( control.themePanel.hidden ).toBe( true );
|
||||
expect( control.nav.hidden ).toBe( true );
|
||||
expect( activePanelCount( control ) ).toBe( 0 );
|
||||
} );
|
||||
|
||||
test( 'Escape closes from a descendant and restores trigger focus', () => {
|
||||
test( 'Search, Theme, and Menu are mutually exclusive', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( true ) } );
|
||||
const button = document.querySelector( '[data-chrnx-menu-toggle]' );
|
||||
const nav = document.querySelector( 'nav' );
|
||||
button.click();
|
||||
nav.querySelector( 'a' ).focus();
|
||||
nav.dispatchEvent( new window.KeyboardEvent( 'keydown', { key: 'Escape', bubbles: true } ) );
|
||||
expect( nav.hidden ).toBe( true );
|
||||
expect( document.activeElement ).toBe( button );
|
||||
const control = controls();
|
||||
|
||||
submit( control.search );
|
||||
expect( control.searchPanel.hidden ).toBe( false );
|
||||
expect( activePanelCount( control ) ).toBe( 1 );
|
||||
|
||||
control.themeButton.click();
|
||||
expect( control.searchPanel.hidden ).toBe( true );
|
||||
expect( control.themePanel.hidden ).toBe( false );
|
||||
expect( activePanelCount( control ) ).toBe( 1 );
|
||||
|
||||
control.menuButton.click();
|
||||
expect( control.themePanel.hidden ).toBe( true );
|
||||
expect( control.nav.hidden ).toBe( false );
|
||||
expect( activePanelCount( control ) ).toBe( 1 );
|
||||
} );
|
||||
|
||||
test( 'desktop and reinitialization reset state without duplicate listeners', () => {
|
||||
test( 'empty Search opens and focuses while a non-empty GET remains native', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( true ) } );
|
||||
const control = controls();
|
||||
expect( control.search.dataset.expanded ).toBe( 'false' );
|
||||
expect( control.searchPanel.hidden ).toBe( true );
|
||||
expect( control.searchButton.getAttribute( 'aria-expanded' ) ).toBe( 'false' );
|
||||
|
||||
const emptyEvent = submit( control.search );
|
||||
expect( emptyEvent.defaultPrevented ).toBe( true );
|
||||
expect( control.search.dataset.expanded ).toBe( 'true' );
|
||||
expect( control.searchPanel.hidden ).toBe( false );
|
||||
expect( control.searchButton.getAttribute( 'aria-expanded' ) ).toBe( 'true' );
|
||||
expect( control.searchButton.getAttribute( 'aria-label' ) ).toBe( 'Search' );
|
||||
expect( document.activeElement ).toBe( control.searchInput );
|
||||
|
||||
control.searchInput.value = 'cat notes';
|
||||
expect( submit( control.search ).defaultPrevented ).toBe( false );
|
||||
} );
|
||||
|
||||
test( 'Escape closes Search and restores its trigger', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( true ) } );
|
||||
const control = controls();
|
||||
submit( control.search );
|
||||
press( control.searchInput, 'Escape' );
|
||||
expect( control.search.dataset.expanded ).toBe( 'false' );
|
||||
expect( control.searchPanel.hidden ).toBe( true );
|
||||
expect( control.searchButton.getAttribute( 'aria-expanded' ) ).toBe( 'false' );
|
||||
expect( control.searchButton.getAttribute( 'aria-label' ) ).toBe( 'Open search' );
|
||||
expect( document.activeElement ).toBe( control.searchButton );
|
||||
} );
|
||||
|
||||
test( 'Theme and Menu retain trigger focus and precede their panel contents', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( true ) } );
|
||||
const control = controls();
|
||||
|
||||
control.themeButton.focus();
|
||||
control.themeButton.click();
|
||||
expect( document.activeElement ).toBe( control.themeButton );
|
||||
expect( nextVisibleFocusable( control.themeButton ) ).toBe( control.checkedRadio );
|
||||
|
||||
control.menuButton.focus();
|
||||
control.menuButton.click();
|
||||
expect( document.activeElement ).toBe( control.menuButton );
|
||||
expect( nextVisibleFocusable( control.menuButton ) ).toBe( control.firstLink );
|
||||
} );
|
||||
|
||||
test.each( [
|
||||
[ 'Theme', 'themeButton', 'themePanel' ],
|
||||
[ 'Menu', 'menuButton', 'nav' ],
|
||||
] )( 'Escape closes %s and restores its trigger', ( _name, buttonName, panelName ) => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( true ) } );
|
||||
const control = controls();
|
||||
control[ buttonName ].click();
|
||||
control[ panelName ].querySelector( 'input, a' ).focus();
|
||||
press( control[ panelName ], 'Escape' );
|
||||
expect( control[ panelName ].hidden ).toBe( true );
|
||||
expect( document.activeElement ).toBe( control[ buttonName ] );
|
||||
} );
|
||||
|
||||
test( 'outside pointer and focus close Theme without stealing destination focus', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( true ) } );
|
||||
const control = controls();
|
||||
|
||||
control.themeButton.click();
|
||||
control.outside.focus();
|
||||
control.outside.dispatchEvent( new Event( 'pointerdown', { bubbles: true } ) );
|
||||
expect( control.themePanel.hidden ).toBe( true );
|
||||
expect( document.activeElement ).toBe( control.outside );
|
||||
|
||||
control.themeButton.click();
|
||||
control.outside.focus();
|
||||
control.outside.dispatchEvent( new Event( 'focusin', { bubbles: true } ) );
|
||||
expect( control.themePanel.hidden ).toBe( true );
|
||||
expect( document.activeElement ).toBe( control.outside );
|
||||
} );
|
||||
|
||||
test( 'Tab is never prevented', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( true ) } );
|
||||
const control = controls();
|
||||
control.themeButton.click();
|
||||
expect( press( control.themeButton, 'Tab' ).defaultPrevented ).toBe( false );
|
||||
} );
|
||||
|
||||
test( '599 to 600 moves focus only when the focused control becomes hidden', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
const media = createMedia( true );
|
||||
initialize( { document, media } );
|
||||
initialize( { document, media } );
|
||||
expect( media.listenerCount() ).toBe( 1 );
|
||||
let control = controls();
|
||||
|
||||
submit( control.search );
|
||||
control.searchInput.focus();
|
||||
media.change( false );
|
||||
expect( document.querySelector( 'nav' ).hidden ).toBe( false );
|
||||
expect( document.querySelector( '[data-chrnx-menu-toggle]' ).hidden ).toBe( true );
|
||||
expect( document.documentElement.classList ).toContain( 'chrnx-navigation-enhanced' );
|
||||
expect( document.activeElement ).toBe( control.searchButton );
|
||||
|
||||
media.change( true );
|
||||
control = controls();
|
||||
control.menuButton.focus();
|
||||
media.change( false );
|
||||
expect( document.activeElement ).toBe( control.firstLink );
|
||||
|
||||
media.change( true );
|
||||
control.themeButton.focus();
|
||||
media.change( false );
|
||||
expect( document.activeElement ).toBe( control.checkedRadio );
|
||||
} );
|
||||
|
||||
test( 'resize moves focus out of a navigation that becomes hidden', () => {
|
||||
test( '599 to 600 preserves focus on a navigation link or visible radio', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
const media = createMedia( true );
|
||||
initialize( { document, media } );
|
||||
const control = controls();
|
||||
|
||||
control.menuButton.click();
|
||||
control.firstLink.focus();
|
||||
media.change( false );
|
||||
expect( document.activeElement ).toBe( control.firstLink );
|
||||
|
||||
media.change( true );
|
||||
control.themeButton.click();
|
||||
control.checkedRadio.focus();
|
||||
media.change( false );
|
||||
expect( document.activeElement ).toBe( control.checkedRadio );
|
||||
} );
|
||||
|
||||
test( '600 to 599 moves focus from newly hidden desktop panels to their triggers', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
const media = createMedia( false );
|
||||
initialize( { document, media } );
|
||||
const button = document.querySelector( '[data-chrnx-menu-toggle]' );
|
||||
const nav = document.querySelector( 'nav' );
|
||||
nav.querySelector( 'a' ).focus();
|
||||
const control = controls();
|
||||
|
||||
control.firstLink.focus();
|
||||
media.change( true );
|
||||
expect( nav.hidden ).toBe( true );
|
||||
expect( button.hidden ).toBe( false );
|
||||
expect( document.activeElement ).toBe( button );
|
||||
expect( document.activeElement ).toBe( control.menuButton );
|
||||
|
||||
button.click();
|
||||
nav.querySelector( 'a' ).focus();
|
||||
media.change( false );
|
||||
expect( nav.hidden ).toBe( false );
|
||||
expect( button.hidden ).toBe( true );
|
||||
expect( document.activeElement ).toBe( nav.querySelector( 'a' ) );
|
||||
control.checkedRadio.focus();
|
||||
media.change( true );
|
||||
expect( document.activeElement ).toBe( control.themeButton );
|
||||
expect( document.activeElement.hidden ).toBe( false );
|
||||
expect( document.activeElement.closest( '[hidden]' ) ).toBeNull();
|
||||
} );
|
||||
|
||||
test( 'desktop restores server order and progressive visibility', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( false ) } );
|
||||
const control = controls();
|
||||
expect( directOrder( control.shell ) ).toEqual( [ 'brand', 'menu', 'nav', 'search', 'theme' ] );
|
||||
expect( control.nav.hidden ).toBe( false );
|
||||
expect( control.themePanel.hidden ).toBe( false );
|
||||
expect( control.menuButton.hidden ).toBe( true );
|
||||
expect( control.themeButton.hidden ).toBe( true );
|
||||
expect( control.searchPanel.hidden ).toBe( true );
|
||||
} );
|
||||
|
||||
test( 'reinitialization leaves one listener set and cleanup restores original DOM', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
const media = createMedia( true );
|
||||
const originalElements = [ ...controls().shell.children ];
|
||||
initialize( { document, media } );
|
||||
initialize( { document, media } );
|
||||
expect( media.listenerCount() ).toBe( 1 );
|
||||
|
||||
const incompleteDocument = document.implementation.createHTMLDocument( 'incomplete' );
|
||||
expect( initialize( { document: incompleteDocument, media } ) ).toBe( false );
|
||||
const control = controls();
|
||||
expect( [ ...control.shell.children ] ).toEqual( originalElements );
|
||||
expect(
|
||||
[ ...control.shell.childNodes ].filter( ( node ) => node.nodeType === 8 ),
|
||||
).toHaveLength( 0 );
|
||||
expect( media.listenerCount() ).toBe( 0 );
|
||||
expect( document.documentElement.classList ).not.toContain( 'chrnx-navigation-enhanced' );
|
||||
expect( control.nav.hidden ).toBe( false );
|
||||
expect( control.searchPanel.hidden ).toBe( false );
|
||||
expect( control.searchButton.getAttribute( 'aria-expanded' ) ).toBe( 'true' );
|
||||
expect( control.themePanel.hidden ).toBe( false );
|
||||
expect( control.menuButton.hidden ).toBe( true );
|
||||
} );
|
||||
|
||||
test.each( [ null, {}, { matches: true, addEventListener() {} } ] )(
|
||||
'fails closed without a complete media API (%p)',
|
||||
( media ) => {
|
||||
const { initialize } = require( modulePath );
|
||||
const originalOrder = [ ...controls().shell.children ];
|
||||
expect( initialize( { document, media } ) ).toBe( false );
|
||||
expect( [ ...controls().shell.children ] ).toEqual( originalOrder );
|
||||
expect( document.documentElement.className ).toBe( '' );
|
||||
},
|
||||
);
|
||||
|
||||
test( 'fails closed when any required header element is missing', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
controls().themeButton.remove();
|
||||
const originalOrder = [ ...controls().shell.children ];
|
||||
expect( initialize( { document, media: createMedia( true ) } ) ).toBe( false );
|
||||
expect( [ ...controls().shell.children ] ).toEqual( originalOrder );
|
||||
expect( document.documentElement.className ).toBe( '' );
|
||||
} );
|
||||
|
||||
test.each( [
|
||||
[ 'brand', '.chrnx-site-brand' ],
|
||||
[ 'navigation link', '[data-part="nav"] a' ],
|
||||
[ 'checked theme radio', '[data-chrnx-theme-panel] input:checked' ],
|
||||
] )( 'fails closed without the required %s', ( _name, selector ) => {
|
||||
const { initialize } = require( modulePath );
|
||||
document.querySelectorAll( selector ).forEach( ( element ) => element.remove() );
|
||||
const originalOrder = [ ...controls().shell.children ];
|
||||
expect( initialize( { document, media: createMedia( true ) } ) ).toBe( false );
|
||||
expect( [ ...controls().shell.children ] ).toEqual( originalOrder );
|
||||
expect( document.documentElement.className ).toBe( '' );
|
||||
} );
|
||||
} );
|
||||
@@ -1,71 +0,0 @@
|
||||
const modulePath = '../../assets/js/search.js';
|
||||
|
||||
function markup() {
|
||||
document.body.innerHTML = `
|
||||
<form data-chrnx-search action="/" role="search" data-expanded="true">
|
||||
<div id="chrnx-search-panel" data-chrnx-search-panel>
|
||||
<label for="chrnx-search-input">Search</label>
|
||||
<input id="chrnx-search-input" name="s" type="search">
|
||||
</div>
|
||||
<button type="submit" data-chrnx-search-button data-label-open="Open search"
|
||||
data-label-submit="Search" aria-controls="chrnx-search-panel"
|
||||
aria-expanded="true">Search</button>
|
||||
</form>
|
||||
`;
|
||||
}
|
||||
|
||||
describe( 'progressive header search', () => {
|
||||
beforeEach( () => {
|
||||
jest.resetModules();
|
||||
document.documentElement.className = 'js';
|
||||
markup();
|
||||
} );
|
||||
|
||||
test( 'first submit expands and focuses the empty search instead of submitting', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
expect( initialize( { document } ) ).toBe( true );
|
||||
const form = document.querySelector( 'form' );
|
||||
const input = document.querySelector( 'input' );
|
||||
const button = document.querySelector( 'button' );
|
||||
const panel = document.querySelector( '[data-chrnx-search-panel]' );
|
||||
expect( panel.hidden ).toBe( true );
|
||||
expect( button.getAttribute( 'aria-expanded' ) ).toBe( 'false' );
|
||||
const event = new Event( 'submit', { bubbles: true, cancelable: true } );
|
||||
form.dispatchEvent( event );
|
||||
expect( event.defaultPrevented ).toBe( true );
|
||||
expect( form.dataset.expanded ).toBe( 'true' );
|
||||
expect( panel.hidden ).toBe( false );
|
||||
expect( button.getAttribute( 'aria-expanded' ) ).toBe( 'true' );
|
||||
expect( document.activeElement ).toBe( input );
|
||||
expect( button.getAttribute( 'aria-label' ) ).toBe( 'Search' );
|
||||
} );
|
||||
|
||||
test( 'a non-empty query submits natively without interception', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document } );
|
||||
const form = document.querySelector( 'form' );
|
||||
const input = document.querySelector( 'input' );
|
||||
form.dispatchEvent( new Event( 'submit', { bubbles: true, cancelable: true } ) );
|
||||
input.value = 'cat notes';
|
||||
const event = new Event( 'submit', { bubbles: true, cancelable: true } );
|
||||
form.dispatchEvent( event );
|
||||
expect( event.defaultPrevented ).toBe( false );
|
||||
} );
|
||||
|
||||
test( 'Escape collapses and restores focus without duplicate listeners', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document } );
|
||||
initialize( { document } );
|
||||
const form = document.querySelector( 'form' );
|
||||
const input = document.querySelector( 'input' );
|
||||
const button = document.querySelector( 'button' );
|
||||
const panel = document.querySelector( '[data-chrnx-search-panel]' );
|
||||
form.dispatchEvent( new Event( 'submit', { bubbles: true, cancelable: true } ) );
|
||||
input.dispatchEvent( new window.KeyboardEvent( 'keydown', { key: 'Escape', bubbles: true } ) );
|
||||
expect( form.dataset.expanded ).toBe( 'false' );
|
||||
expect( panel.hidden ).toBe( true );
|
||||
expect( button.getAttribute( 'aria-expanded' ) ).toBe( 'false' );
|
||||
expect( document.activeElement ).toBe( button );
|
||||
expect( button.getAttribute( 'aria-label' ) ).toBe( 'Open search' );
|
||||
} );
|
||||
} );
|
||||
Reference in new issue
Block a user