Skip to content
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

ComboboxControl: add unit tests #42403

Merged
merged 27 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
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 Jul 7, 2022
207ac87
ComboboxControl: add test to validate rendered options
chad1008 Jul 11, 2022
59b1b07
ComboboxControl: add test to validate value after click
chad1008 Jul 11, 2022
8c325be
ComboboxControl: use `getAllByRole` to identify rendered options
chad1008 Jul 13, 2022
564be2e
ComboboxContorl: implement `userEvent.setup()` and `jest.advanceTimer…
chad1008 Jul 18, 2022
57c8694
ComboboxControl: update dataset object keys
chad1008 Jul 18, 2022
848c597
ComboboxControl: ensure unit tests validate the input label
chad1008 Jul 18, 2022
4044db8
ComboboxControl: rename `getRenderedOptions` function
chad1008 Jul 18, 2022
28a0c76
ComboboxControl: add `onChange` prop to unit tests, refactor click ev…
chad1008 Jul 18, 2022
521f393
ComboboxControl: unit tests - `label` prop is no longer passed by def…
chad1008 Jul 18, 2022
545587b
ComboboxControl: unit tests - replace `focus()` with `userEvent.click()`
chad1008 Jul 18, 2022
ab8a6e7
ComboboxControl: refactor test assertion
chad1008 Jul 18, 2022
8af073d
ComboboxControl: add keypress unit test
chad1008 Jul 20, 2022
035c5d0
ComboboxControl: unit test - fix misnamed variable
chad1008 Jul 20, 2022
f82d59e
ComboboxControl: add search unit test
chad1008 Jul 20, 2022
83d5ce1
remove extraneous test
chad1008 Jul 25, 2022
f80d540
remove outdated assertion statement
chad1008 Jul 25, 2022
61c01a7
add multiple interaction test
chad1008 Jul 25, 2022
1bf112b
abstract setTargetOption helper function
chad1008 Jul 25, 2022
4ab6bc5
fix outdated variable names
chad1008 Jul 27, 2022
8cd25b4
add controlled vs uncontrolled testing
chad1008 Jul 27, 2022
9b0ca96
add selection announcement test
chad1008 Jul 27, 2022
6dffd46
assert that visible label should be in the document
chad1008 Jul 27, 2022
7a55123
update CHANGELOG
chad1008 Aug 3, 2022
93be502
rename setTargetOption to getTargetOption, add options param
chad1008 Aug 3, 2022
d9989ee
Update packages/components/src/combobox-control/test/index.js
chad1008 Aug 3, 2022
4edad41
ComboboxControl: replace unit test util `getTargetOption` with `getOp…
chad1008 Aug 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Refactor `FocalPointPicker` to function component ([#39168](https://github.com/WordPress/gutenberg/pull/39168)).
- `Guide`: use `code` instead of `keyCode` for keyboard events ([#43604](https://github.com/WordPress/gutenberg/pull/43604/)).
- `Navigation`: use `code` instead of `keyCode` for keyboard events ([#43644](https://github.com/WordPress/gutenberg/pull/43644/)).
- `ComboboxControl`: Add unit tests ([#42403](https://github.com/WordPress/gutenberg/pull/42403)).

## 20.0.0 (2022-08-24)

Expand Down
311 changes: 311 additions & 0 deletions packages/components/src/combobox-control/test/index.js
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'
);
chad1008 marked this conversation as resolved.
Show resolved Hide resolved
} );

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 );
} );
} );