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

Components: add unit test __experimentalExpandOnFocus unit tests for FormTokenField #57122

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Changes from all commits
Commits
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
97 changes: 97 additions & 0 deletions packages/components/src/form-token-field/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,103 @@ describe( 'FormTokenField', () => {
] );
} );

it( 'should render suggestions after a selection is made when the `__experimentalExpandOnFocus` prop is set to `true`', async () => {
const user = userEvent.setup();

const onFocusSpy = jest.fn();

const suggestions = [ 'Green', 'Emerald', 'Seaweed' ];

render(
<>
<FormTokenFieldWithState
onFocus={ onFocusSpy }
suggestions={ suggestions }
__experimentalExpandOnFocus
/>
</>
);

const input = screen.getByRole( 'combobox' );

await user.type( input, 'ee' );

expectVisibleSuggestionsToBe( screen.getByRole( 'listbox' ), [
'Green',
'Seaweed',
] );

// Select the first suggestion ("Green")
await user.keyboard( '[ArrowDown][Enter]' );

expect( screen.getByRole( 'listbox' ) ).toBeVisible();
} );

it( 'should not render suggestions after a selection is made when the `__experimentalExpandOnFocus` prop is set to `false` or not defined', async () => {
const user = userEvent.setup();

const onFocusSpy = jest.fn();

const suggestions = [ 'Green', 'Emerald', 'Seaweed' ];

render(
<>
<FormTokenFieldWithState
onFocus={ onFocusSpy }
suggestions={ suggestions }
/>
</>
);

const input = screen.getByRole( 'combobox' );

await user.type( input, 'ee' );

expectVisibleSuggestionsToBe( screen.getByRole( 'listbox' ), [
'Green',
'Seaweed',
] );

// Select the first suggestion ("Green")
await user.keyboard( '[ArrowDown][Enter]' );

expect( screen.queryByRole( 'listbox' ) ).not.toBeInTheDocument();
} );

it( 'should not render suggestions after the input is blurred', async () => {
const user = userEvent.setup();

const onFocusSpy = jest.fn();

const suggestions = [ 'Green', 'Emerald', 'Seaweed' ];

render(
<>
<FormTokenFieldWithState
onFocus={ onFocusSpy }
suggestions={ suggestions }
/>
</>
);

const input = screen.getByRole( 'combobox' );

await user.type( input, 'ee' );

expectVisibleSuggestionsToBe( screen.getByRole( 'listbox' ), [
'Green',
'Seaweed',
] );

// Select the first suggestion ("Green")
await user.keyboard( '[ArrowDown][Enter]' );

// Click the body, blurring the input.
await user.click( document.body );

expect( screen.queryByRole( 'listbox' ) ).not.toBeInTheDocument();
} );

it( 'should not render suggestions if the text input is not matching any of the suggestions', async () => {
const user = userEvent.setup();

Expand Down
Loading