forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Composite: make items tabbable if active element gets removed (WordPr…
…ess#65720) * Composite: make items tabbable when the active element is disconnected * Add unit test * Better test name * CHANGELOG --- Co-authored-by: ciampo <mciampini@git.wordpress.org> Co-authored-by: diegohaz <hazdiego@git.wordpress.org> Co-authored-by: tyxla <tyxla@git.wordpress.org> Co-authored-by: mirka <0mirka00@git.wordpress.org> Co-authored-by: afercia <afercia@git.wordpress.org>
- Loading branch information
1 parent
f7c4cac
commit edcd976
Showing
3 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { queryByAttribute, render, screen } from '@testing-library/react'; | ||
import { click, press, waitFor } from '@ariakit/test'; | ||
import type { ComponentProps } from 'react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { Composite } from '..'; | ||
|
||
// This is necessary because of how Ariakit calculates page up and | ||
// page down. Without this, nothing has a height, and so paging up | ||
// and down doesn't behave as expected in tests. | ||
|
||
let clientHeightSpy: jest.SpiedGetter< | ||
typeof HTMLElement.prototype.clientHeight | ||
>; | ||
|
||
beforeAll( () => { | ||
clientHeightSpy = jest | ||
.spyOn( HTMLElement.prototype, 'clientHeight', 'get' ) | ||
.mockImplementation( function getClientHeight( this: HTMLElement ) { | ||
if ( this.tagName === 'BODY' ) { | ||
return window.outerHeight; | ||
} | ||
return 50; | ||
} ); | ||
} ); | ||
|
||
afterAll( () => { | ||
clientHeightSpy?.mockRestore(); | ||
} ); | ||
|
||
async function renderAndValidate( ...args: Parameters< typeof render > ) { | ||
const view = render( ...args ); | ||
await waitFor( () => { | ||
const activeButton = queryByAttribute( | ||
'data-active-item', | ||
view.baseElement, | ||
'true' | ||
); | ||
expect( activeButton ).not.toBeNull(); | ||
} ); | ||
return view; | ||
} | ||
|
||
function RemoveItemTest( props: ComponentProps< typeof Composite > ) { | ||
const [ showThirdItem, setShowThirdItem ] = useState( true ); | ||
return ( | ||
<> | ||
<button>Focus trap before composite</button> | ||
<Composite { ...props }> | ||
<Composite.Item>Item 1</Composite.Item> | ||
<Composite.Item>Item 2</Composite.Item> | ||
{ showThirdItem && <Composite.Item>Item 3</Composite.Item> } | ||
</Composite> | ||
<button onClick={ () => setShowThirdItem( ( value ) => ! value ) }> | ||
Toggle third item | ||
</button> | ||
</> | ||
); | ||
} | ||
|
||
describe( 'Composite', () => { | ||
it( 'should remain focusable even when there are no elements in the DOM associated with the currently active ID', async () => { | ||
await renderAndValidate( <RemoveItemTest /> ); | ||
|
||
const toggleButton = screen.getByRole( 'button', { | ||
name: 'Toggle third item', | ||
} ); | ||
|
||
await press.Tab(); | ||
await press.Tab(); | ||
|
||
expect( | ||
screen.getByRole( 'button', { name: 'Item 1' } ) | ||
).toHaveFocus(); | ||
|
||
await press.ArrowRight(); | ||
await press.ArrowRight(); | ||
|
||
expect( | ||
screen.getByRole( 'button', { name: 'Item 3' } ) | ||
).toHaveFocus(); | ||
|
||
await click( toggleButton ); | ||
|
||
expect( | ||
screen.queryByRole( 'button', { name: 'Item 3' } ) | ||
).not.toBeInTheDocument(); | ||
|
||
await press.ShiftTab(); | ||
|
||
expect( | ||
screen.getByRole( 'button', { name: 'Item 2' } ) | ||
).toHaveFocus(); | ||
|
||
await click( toggleButton ); | ||
|
||
expect( | ||
screen.getByRole( 'button', { name: 'Item 3' } ) | ||
).toBeVisible(); | ||
|
||
await press.ShiftTab(); | ||
|
||
expect( | ||
screen.getByRole( 'button', { name: 'Item 2' } ) | ||
).toHaveFocus(); | ||
|
||
await press.ArrowRight(); | ||
|
||
expect( | ||
screen.getByRole( 'button', { name: 'Item 3' } ) | ||
).toHaveFocus(); | ||
} ); | ||
} ); |