fix: harden CHRNX header state transitions
This commit is contained in:
2 files changed
+126
-19
No files matched your search
+104
-8
@@ -54,6 +54,7 @@ function shellMarkup() {
|
||||
</div>
|
||||
</header>
|
||||
<button data-outside>Outside</button>
|
||||
<div data-outside-content>Outside content</div>
|
||||
`;
|
||||
}
|
||||
|
||||
@@ -74,6 +75,7 @@ function controls() {
|
||||
checkedRadio: document.querySelector( '[data-chrnx-theme-panel] input:checked' ),
|
||||
firstRadio: document.querySelector( '[data-chrnx-theme-panel] input' ),
|
||||
outside: document.querySelector( '[data-outside]' ),
|
||||
outsideContent: document.querySelector( '[data-outside-content]' ),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -271,6 +273,27 @@ describe( 'coordinated CHRNX header shell', () => {
|
||||
expect( submit( control.search ).defaultPrevented ).toBe( false );
|
||||
} );
|
||||
|
||||
test( 'retained query reopens closed Search before a second native submit', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( true ) } );
|
||||
const control = controls();
|
||||
submit( control.search );
|
||||
control.searchInput.value = 'retained cats';
|
||||
press( control.searchInput, 'Escape' );
|
||||
expect( control.searchPanel.hidden ).toBe( true );
|
||||
|
||||
const reopenEvent = submit( control.search );
|
||||
expect( reopenEvent.defaultPrevented ).toBe( true );
|
||||
expect( control.searchInput.value ).toBe( 'retained cats' );
|
||||
expect( control.searchPanel.hidden ).toBe( false );
|
||||
expect( control.searchButton.getAttribute( 'aria-expanded' ) ).toBe( 'true' );
|
||||
expect( document.activeElement ).toBe( control.searchInput );
|
||||
|
||||
const nativeEvent = submit( control.search );
|
||||
expect( nativeEvent.defaultPrevented ).toBe( false );
|
||||
expect( control.searchInput.value ).toBe( 'retained cats' );
|
||||
} );
|
||||
|
||||
test( 'Escape closes Search and restores its trigger', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( true ) } );
|
||||
@@ -314,24 +337,66 @@ describe( 'coordinated CHRNX header shell', () => {
|
||||
expect( document.activeElement ).toBe( control[ buttonName ] );
|
||||
} );
|
||||
|
||||
test( 'outside pointer and focus close Theme without stealing destination focus', () => {
|
||||
test( 'real outside focusable click order closes Theme after focus transfers', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( true ) } );
|
||||
const control = controls();
|
||||
|
||||
control.themeButton.click();
|
||||
control.outside.focus();
|
||||
|
||||
let hidden = control.themePanel.hidden;
|
||||
let focusAtHide = null;
|
||||
Object.defineProperty( control.themePanel, 'hidden', {
|
||||
configurable: true,
|
||||
get: () => hidden,
|
||||
set: ( value ) => {
|
||||
if ( value ) {
|
||||
focusAtHide = document.activeElement;
|
||||
}
|
||||
hidden = value;
|
||||
},
|
||||
} );
|
||||
|
||||
control.outside.dispatchEvent( new Event( 'pointerdown', { bubbles: true } ) );
|
||||
expect( control.themePanel.hidden ).toBe( true );
|
||||
expect( document.activeElement ).toBe( control.outside );
|
||||
|
||||
control.themeButton.click();
|
||||
expect( control.themePanel.hidden ).toBe( false );
|
||||
control.outside.focus();
|
||||
control.outside.dispatchEvent( new Event( 'focusin', { bubbles: true } ) );
|
||||
control.outside.dispatchEvent( new Event( 'click', { bubbles: true } ) );
|
||||
expect( control.themePanel.hidden ).toBe( true );
|
||||
expect( focusAtHide ).toBe( control.outside );
|
||||
expect( document.activeElement ).toBe( control.outside );
|
||||
} );
|
||||
|
||||
test( 'outside nonfocusable click closes Theme without hiding focused content', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( true ) } );
|
||||
const control = controls();
|
||||
control.themeButton.click();
|
||||
control.checkedRadio.focus();
|
||||
|
||||
let hidden = control.themePanel.hidden;
|
||||
let focusAtHide = null;
|
||||
Object.defineProperty( control.themePanel, 'hidden', {
|
||||
configurable: true,
|
||||
get: () => hidden,
|
||||
set: ( value ) => {
|
||||
if ( value ) {
|
||||
focusAtHide = document.activeElement;
|
||||
}
|
||||
hidden = value;
|
||||
},
|
||||
} );
|
||||
|
||||
control.outsideContent.dispatchEvent( new Event( 'pointerdown', { bubbles: true } ) );
|
||||
expect( control.themePanel.hidden ).toBe( false );
|
||||
control.checkedRadio.blur();
|
||||
expect( document.activeElement ).toBe( document.body );
|
||||
control.outsideContent.dispatchEvent( new Event( 'click', { bubbles: true } ) );
|
||||
expect( control.themePanel.hidden ).toBe( true );
|
||||
expect( focusAtHide ).toBe( control.themeButton );
|
||||
expect( document.activeElement ).toBe( control.themeButton );
|
||||
expect( document.activeElement.closest( '[hidden]' ) ).toBeNull();
|
||||
} );
|
||||
|
||||
test( 'Tab is never prevented', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
initialize( { document, media: createMedia( true ) } );
|
||||
@@ -363,6 +428,37 @@ describe( 'coordinated CHRNX header shell', () => {
|
||||
expect( document.activeElement ).toBe( control.checkedRadio );
|
||||
} );
|
||||
|
||||
test.each( [ 'light', 'dark' ] )(
|
||||
'599 to 600 focuses the current %s radio selected after initialization',
|
||||
( value ) => {
|
||||
const { initialize } = require( modulePath );
|
||||
const media = createMedia( true );
|
||||
initialize( { document, media } );
|
||||
const control = controls();
|
||||
const currentRadio = control.themePanel.querySelector( `input[value="${ value }"]` );
|
||||
control.checkedRadio.checked = false;
|
||||
currentRadio.checked = true;
|
||||
control.themeButton.focus();
|
||||
|
||||
media.change( false );
|
||||
expect( document.activeElement ).toBe( currentRadio );
|
||||
},
|
||||
);
|
||||
|
||||
test( '599 to 600 safely falls back to the first radio when none is checked', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
const media = createMedia( true );
|
||||
initialize( { document, media } );
|
||||
const control = controls();
|
||||
control.themePanel.querySelectorAll( 'input[type="radio"]' ).forEach( ( radio ) => {
|
||||
radio.checked = false;
|
||||
} );
|
||||
control.themeButton.focus();
|
||||
|
||||
media.change( false );
|
||||
expect( document.activeElement ).toBe( control.firstRadio );
|
||||
} );
|
||||
|
||||
test( '599 to 600 preserves focus on a navigation link or visible radio', () => {
|
||||
const { initialize } = require( modulePath );
|
||||
const media = createMedia( true );
|
||||
@@ -458,7 +554,7 @@ describe( 'coordinated CHRNX header shell', () => {
|
||||
test.each( [
|
||||
[ 'brand', '.chrnx-site-brand' ],
|
||||
[ 'navigation link', '[data-part="nav"] a' ],
|
||||
[ 'checked theme radio', '[data-chrnx-theme-panel] input:checked' ],
|
||||
[ 'theme radio', '[data-chrnx-theme-panel] input' ],
|
||||
] )( 'fails closed without the required %s', ( _name, selector ) => {
|
||||
const { initialize } = require( modulePath );
|
||||
document.querySelectorAll( selector ).forEach( ( element ) => element.remove() );
|
||||
|
||||
Reference in new issue
Block a user