-
Notifications
You must be signed in to change notification settings - Fork 4.7k
ComboboxControl: add unit tests #42403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
ee33dcd
ComboboxControl: add initial rendering tests
chad1008 207ac87
ComboboxControl: add test to validate rendered options
chad1008 59b1b07
ComboboxControl: add test to validate value after click
chad1008 8c325be
ComboboxControl: use `getAllByRole` to identify rendered options
chad1008 564be2e
ComboboxContorl: implement `userEvent.setup()` and `jest.advanceTimer…
chad1008 57c8694
ComboboxControl: update dataset object keys
chad1008 848c597
ComboboxControl: ensure unit tests validate the input label
chad1008 4044db8
ComboboxControl: rename `getRenderedOptions` function
chad1008 28a0c76
ComboboxControl: add `onChange` prop to unit tests, refactor click ev…
chad1008 521f393
ComboboxControl: unit tests - `label` prop is no longer passed by def…
chad1008 545587b
ComboboxControl: unit tests - replace `focus()` with `userEvent.click()`
chad1008 ab8a6e7
ComboboxControl: refactor test assertion
chad1008 8af073d
ComboboxControl: add keypress unit test
chad1008 035c5d0
ComboboxControl: unit test - fix misnamed variable
chad1008 f82d59e
ComboboxControl: add search unit test
chad1008 83d5ce1
remove extraneous test
chad1008 f80d540
remove outdated assertion statement
chad1008 61c01a7
add multiple interaction test
chad1008 1bf112b
abstract setTargetOption helper function
chad1008 4ab6bc5
fix outdated variable names
chad1008 8cd25b4
add controlled vs uncontrolled testing
chad1008 9b0ca96
add selection announcement test
chad1008 6dffd46
assert that visible label should be in the document
chad1008 7a55123
update CHANGELOG
chad1008 93be502
rename setTargetOption to getTargetOption, add options param
chad1008 d9989ee
Update packages/components/src/combobox-control/test/index.js
chad1008 4edad41
ComboboxControl: replace unit test util `getTargetOption` with `getOp…
chad1008 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,311 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useState } from '@wordpress/element'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import ComboboxControl from '../'; | ||
|
|
||
| const timezones = [ | ||
| { label: 'Greenwich Mean Time', value: 'GMT' }, | ||
| { label: 'Universal Coordinated Time', value: 'UTC' }, | ||
| { label: 'European Central Time', value: 'ECT' }, | ||
| { label: '(Arabic) Egypt Standard Time', value: 'ART' }, | ||
| { label: 'Eastern African Time', value: 'EAT' }, | ||
| { label: 'Middle East Time', value: 'MET' }, | ||
| { label: 'Near East Time', value: 'NET' }, | ||
| { label: 'Pakistan Lahore Time', value: 'PLT' }, | ||
| { label: 'India Standard Time', value: 'IST' }, | ||
| { label: 'Bangladesh Standard Time', value: 'BST' }, | ||
| { label: 'Vietnam Standard Time', value: 'VST' }, | ||
| { label: 'China Taiwan Time', value: 'CTT' }, | ||
| { label: 'Japan Standard Time', value: 'JST' }, | ||
| { label: 'Australia Central Time', value: 'ACT' }, | ||
| { label: 'Australia Eastern Time', value: 'AET' }, | ||
| { label: 'Solomon Standard Time', value: 'SST' }, | ||
| { label: 'New Zealand Standard Time', value: 'NST' }, | ||
| { label: 'Midway Islands Time', value: 'MIT' }, | ||
| { label: 'Hawaii Standard Time', value: 'HST' }, | ||
| { label: 'Alaska Standard Time', value: 'AST' }, | ||
| { label: 'Pacific Standard Time', value: 'PST' }, | ||
| { label: 'Phoenix Standard Time', value: 'PNT' }, | ||
| { label: 'Mountain Standard Time', value: 'MST' }, | ||
| { label: 'Central Standard Time', value: 'CST' }, | ||
| { label: 'Eastern Standard Time', value: 'EST' }, | ||
| { label: 'Indiana Eastern Standard Time', value: 'IET' }, | ||
| { label: 'Puerto Rico and US Virgin Islands Time', value: 'PRT' }, | ||
| { label: 'Canada Newfoundland Time', value: 'CNT' }, | ||
| { label: 'Argentina Standard Time', value: 'AGT' }, | ||
| { label: 'Brazil Eastern Time', value: 'BET' }, | ||
| { label: 'Central African Time', value: 'CAT' }, | ||
| ]; | ||
|
|
||
| const defaultLabelText = 'Select a timezone'; | ||
| const getLabel = ( labelText ) => screen.getByText( labelText ); | ||
| const getInput = ( name ) => screen.getByRole( 'combobox', { name } ); | ||
| const getOption = ( name ) => screen.getByRole( 'option', { name } ); | ||
| const getAllOptions = () => screen.getAllByRole( 'option' ); | ||
| const getOptionSearchString = ( option ) => option.label.substring( 0, 11 ); | ||
| const setupUser = () => | ||
| userEvent.setup( { | ||
| advanceTimers: jest.advanceTimersByTime, | ||
| } ); | ||
|
|
||
| const ControlledComboboxControl = ( { | ||
| value: valueProp, | ||
| onChange, | ||
| ...props | ||
| } ) => { | ||
| const [ value, setValue ] = useState( valueProp ); | ||
| const handleOnChange = ( newValue ) => { | ||
| setValue( newValue ); | ||
| onChange?.( newValue ); | ||
| }; | ||
| return ( | ||
| <> | ||
| <ComboboxControl | ||
| { ...props } | ||
| value={ value } | ||
| onChange={ handleOnChange } | ||
| /> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| describe.each( [ | ||
| [ 'uncontrolled', ComboboxControl ], | ||
| [ 'controlled', ControlledComboboxControl ], | ||
| ] )( 'ComboboxControl %s', ( ...modeAndComponent ) => { | ||
| const [ , Component ] = modeAndComponent; | ||
|
|
||
| it( 'should render with visible label', () => { | ||
| render( | ||
| <Component options={ timezones } label={ defaultLabelText } /> | ||
| ); | ||
| const label = getLabel( defaultLabelText ); | ||
| expect( label ).toBeInTheDocument(); | ||
| expect( label ).toBeVisible(); | ||
| } ); | ||
|
|
||
| it( 'should render with hidden label', () => { | ||
| render( | ||
| <Component | ||
| options={ timezones } | ||
| label={ defaultLabelText } | ||
| hideLabelFromVision={ true } | ||
| /> | ||
| ); | ||
| const label = getLabel( defaultLabelText ); | ||
|
|
||
| expect( label ).toBeInTheDocument(); | ||
| expect( label ).toHaveAttribute( | ||
| 'data-wp-component', | ||
| 'VisuallyHidden' | ||
| ); | ||
| } ); | ||
|
|
||
| it( 'should render with the correct options', async () => { | ||
| const user = setupUser(); | ||
| render( | ||
| <Component options={ timezones } label={ defaultLabelText } /> | ||
| ); | ||
| const input = getInput( defaultLabelText ); | ||
|
|
||
| // Clicking on the input shows the options | ||
| await user.click( input ); | ||
|
|
||
| const renderedOptions = getAllOptions(); | ||
|
|
||
| // Confirm the rendered options match the provided dataset. | ||
| expect( renderedOptions ).toHaveLength( timezones.length ); | ||
| renderedOptions.forEach( ( option, optionIndex ) => { | ||
| expect( option ).toHaveTextContent( | ||
| timezones[ optionIndex ].label | ||
| ); | ||
| } ); | ||
| } ); | ||
|
|
||
| it( 'should select the correct option via click events', async () => { | ||
| const user = setupUser(); | ||
| const targetOption = timezones[ 2 ]; | ||
| const onChangeSpy = jest.fn(); | ||
| render( | ||
| <Component | ||
| options={ timezones } | ||
| label={ defaultLabelText } | ||
| onChange={ onChangeSpy } | ||
| /> | ||
| ); | ||
| const input = getInput( defaultLabelText ); | ||
|
|
||
| // Clicking on the input shows the options | ||
| await user.click( input ); | ||
|
|
||
| // Select the target option | ||
| await user.click( getOption( targetOption.label ) ); | ||
|
|
||
| expect( onChangeSpy ).toHaveBeenCalledTimes( 1 ); | ||
| expect( onChangeSpy ).toHaveBeenCalledWith( targetOption.value ); | ||
| expect( input ).toHaveValue( targetOption.label ); | ||
| } ); | ||
|
|
||
| it( 'should select the correct option via keypress events', async () => { | ||
| const user = setupUser(); | ||
| const targetIndex = 4; | ||
| const targetOption = timezones[ targetIndex ]; | ||
| const onChangeSpy = jest.fn(); | ||
| render( | ||
| <Component | ||
| options={ timezones } | ||
| label={ defaultLabelText } | ||
| onChange={ onChangeSpy } | ||
| /> | ||
| ); | ||
| const input = getInput( defaultLabelText ); | ||
|
|
||
| // Pressing tab selects the input and shows the options | ||
| await user.tab(); | ||
|
|
||
| // Navigate the options using the down arrow | ||
| for ( let i = 0; i < targetIndex; i++ ) { | ||
| await user.keyboard( '{ArrowDown}' ); | ||
| } | ||
|
|
||
| // Pressing Enter/Return selects the currently focused option | ||
| await user.keyboard( '{Enter}' ); | ||
|
|
||
| expect( onChangeSpy ).toHaveBeenCalledTimes( 1 ); | ||
| expect( onChangeSpy ).toHaveBeenCalledWith( targetOption.value ); | ||
| expect( input ).toHaveValue( targetOption.label ); | ||
| } ); | ||
|
|
||
| it( 'should select the correct option from a search', async () => { | ||
| const user = setupUser(); | ||
| const targetOption = timezones[ 13 ]; | ||
| const onChangeSpy = jest.fn(); | ||
| render( | ||
| <Component | ||
| options={ timezones } | ||
| label={ defaultLabelText } | ||
| onChange={ onChangeSpy } | ||
| /> | ||
| ); | ||
| const input = getInput( defaultLabelText ); | ||
|
|
||
| // Pressing tab selects the input and shows the options | ||
| await user.tab(); | ||
|
|
||
| // Type enough characters to ensure a predictable search result | ||
| await user.keyboard( getOptionSearchString( targetOption ) ); | ||
|
|
||
| // Pressing Enter/Return selects the currently focused option | ||
| await user.keyboard( '{Enter}' ); | ||
|
|
||
| expect( onChangeSpy ).toHaveBeenCalledTimes( 1 ); | ||
| expect( onChangeSpy ).toHaveBeenCalledWith( targetOption.value ); | ||
| expect( input ).toHaveValue( targetOption.label ); | ||
| } ); | ||
|
|
||
| it( 'should render aria-live announcement upon selection', async () => { | ||
| const user = setupUser(); | ||
| const targetOption = timezones[ 9 ]; | ||
| const onChangeSpy = jest.fn(); | ||
| render( | ||
| <Component | ||
| options={ timezones } | ||
| label={ defaultLabelText } | ||
| onChange={ onChangeSpy } | ||
| /> | ||
| ); | ||
|
|
||
| // Pressing tab selects the input and shows the options | ||
| await user.tab(); | ||
|
|
||
| // Type enough characters to ensure a predictable search result | ||
| await user.keyboard( getOptionSearchString( targetOption ) ); | ||
|
|
||
| // Pressing Enter/Return selects the currently focused option | ||
| await user.keyboard( '{Enter}' ); | ||
|
|
||
| expect( | ||
| screen.getByText( 'Item selected.', { | ||
| selector: '[aria-live]', | ||
| } ) | ||
| ).toBeInTheDocument(); | ||
| } ); | ||
|
|
||
| it( 'should process multiple entries in a single session', async () => { | ||
| const user = setupUser(); | ||
| const unmatchedString = 'Mordor'; | ||
| const targetOption = timezones[ 6 ]; | ||
| const onChangeSpy = jest.fn(); | ||
| render( | ||
| <Component | ||
| options={ timezones } | ||
| label={ defaultLabelText } | ||
| onChange={ onChangeSpy } | ||
| /> | ||
| ); | ||
| const input = getInput( defaultLabelText ); | ||
|
|
||
| // Pressing tab selects the input and shows the options | ||
| await user.tab(); | ||
|
|
||
| const initialRenderedOptions = getAllOptions(); | ||
|
|
||
| // Rendered options match the provided dataset. | ||
| expect( initialRenderedOptions ).toHaveLength( timezones.length ); | ||
| initialRenderedOptions.forEach( ( option, optionIndex ) => { | ||
| expect( option ).toHaveTextContent( | ||
| timezones[ optionIndex ].label | ||
| ); | ||
| } ); | ||
|
|
||
| // No options are rendered if no match is found | ||
| await user.keyboard( unmatchedString ); | ||
| expect( screen.queryByRole( 'option' ) ).toBeNull(); | ||
|
|
||
| // Clearing the input renders all options again | ||
| await user.clear( input ); | ||
|
|
||
| const postClearRenderedOptions = getAllOptions(); | ||
|
|
||
| expect( postClearRenderedOptions ).toHaveLength( timezones.length ); | ||
| postClearRenderedOptions.forEach( ( option, optionIndex ) => { | ||
| expect( option ).toHaveTextContent( | ||
| timezones[ optionIndex ].label | ||
| ); | ||
| } ); | ||
|
|
||
| // Run a second search with a valid string. | ||
| const searchString = getOptionSearchString( targetOption ); | ||
| await user.keyboard( searchString ); | ||
| const validSearchRenderedOptions = getAllOptions(); | ||
|
|
||
| // Find option that match the search string. | ||
| const matches = timezones.filter( ( option ) => | ||
| option.label.includes( searchString ) | ||
| ); | ||
|
|
||
| // Confirm the rendered options match the provided dataset based on the current string. | ||
| expect( validSearchRenderedOptions ).toHaveLength( matches.length ); | ||
| validSearchRenderedOptions.forEach( ( option, optionIndex ) => { | ||
| expect( option ).toHaveTextContent( matches[ optionIndex ].label ); | ||
| } ); | ||
|
|
||
| // Confirm that the corrent option is selected | ||
| await user.keyboard( '{Enter}' ); | ||
|
|
||
| expect( onChangeSpy ).toHaveBeenCalledTimes( 1 ); | ||
| expect( onChangeSpy ).toHaveBeenCalledWith( targetOption.value ); | ||
| expect( input ).toHaveValue( targetOption.label ); | ||
| } ); | ||
| } ); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.