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 more unit tests #65255

Merged
merged 7 commits into from
Sep 18, 2024
3 changes: 0 additions & 3 deletions packages/components/src/combobox-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,6 @@ function ComboboxControl( props: ComboboxControlProps ) {
if ( expandOnFocus ) {
setIsExpanded( true );
}

onFilterValueChange( '' );
setInputValue( '' );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamsilverstein @youknowriad, do we have any context why that was added? I'm struggling to understand it.

Copy link
Member Author

@zaguiini zaguiini Sep 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I understand it now. We're clearing the input value so all the suggestions show up. But that's a bit weird because there is already a selected value, so I can't see a good reason not to narrow the options to this value and the user can progressively see more as they delete characters from the input. Thoughts?

};

const onClick = () => {
Expand Down
20 changes: 20 additions & 0 deletions packages/components/src/combobox-control/test/index.tsx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really appreciate the added tests here. I think we'll want to merge them in some form, regardless of the outcome of this PR.

Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,26 @@ describe.each( [
expect( input ).toHaveValue( targetOption.label );
} );

it( 'calls onFilterValueChange whenever the textbox changes', async () => {
const user = userEvent.setup();
const onChangeSpy = jest.fn();
render(
<Component
options={ timezones }
label={ defaultLabelText }
onFilterValueChange={ onChangeSpy }
/>
);
const input = getInput( defaultLabelText );

await user.click( input );
expect( onChangeSpy ).not.toHaveBeenCalled();

await user.type( input, 'a' );
expect( onChangeSpy ).toHaveBeenCalledTimes( 1 );
expect( onChangeSpy ).toHaveBeenCalledWith( 'a' );
} );

it( 'should select the correct option from a search', async () => {
const user = await userEvent.setup();
const targetOption = timezones[ 13 ];
Expand Down
Loading