From ac57369fe5308696f41aba2fbd54adeb326364cd Mon Sep 17 00:00:00 2001 From: Chad Chadbourne <13856531+chad1008@users.noreply.github.com> Date: Tue, 19 Dec 2023 17:40:54 -0500 Subject: [PATCH] Components: add unit test `__experimentalExpandOnFocus` unit tests for `FormTokenField` (#57122) * add unit test * add tests for suggestion visibility after selection --- .../src/form-token-field/test/index.tsx | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/packages/components/src/form-token-field/test/index.tsx b/packages/components/src/form-token-field/test/index.tsx index 76e308d5993be..961214a574c90 100644 --- a/packages/components/src/form-token-field/test/index.tsx +++ b/packages/components/src/form-token-field/test/index.tsx @@ -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( + <> + + + ); + + 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( + <> + + + ); + + 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( + <> + + + ); + + 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();