From e56c7c7c134d4e04772e28e8e07e7ab555d54ab9 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Fri, 8 Sep 2023 18:41:35 +0200 Subject: [PATCH 1/5] Tooltip: render dynamically --- packages/components/src/tooltip/index.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/components/src/tooltip/index.tsx b/packages/components/src/tooltip/index.tsx index 9d2a272b810fc6..5fedfedb81cb89 100644 --- a/packages/components/src/tooltip/index.tsx +++ b/packages/components/src/tooltip/index.tsx @@ -49,6 +49,8 @@ function Tooltip( props: TooltipProps ) { timeout: delay, } ); + const isTooltipOpen = tooltipStore.useState( 'open' ); + return ( <> { isOnlyChild ? undefined : children } - { isOnlyChild && ( text || shortcut ) && ( + { isOnlyChild && ( text || shortcut ) && isTooltipOpen && ( Date: Sat, 9 Sep 2023 12:41:46 +0200 Subject: [PATCH 2/5] Fix unit tests considering the fact that tooltip is not in the DOM until open --- packages/components/src/button/test/index.tsx | 8 +++---- .../components/src/tab-panel/test/index.tsx | 18 +++++++------- .../test/__snapshots__/index.tsx.snap | 4 ---- .../components/src/tooltip/test/index.tsx | 24 ++++++++++++++----- 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/packages/components/src/button/test/index.tsx b/packages/components/src/button/test/index.tsx index af1afa801b507f..7f65337508bd89 100644 --- a/packages/components/src/button/test/index.tsx +++ b/packages/components/src/button/test/index.tsx @@ -194,7 +194,7 @@ describe( 'Button', () => { render( ); - expect( screen.queryByText( 'WordPress' ) ).not.toBeVisible(); + expect( screen.queryByText( 'WordPress' ) ).not.toBeInTheDocument(); // Move focus to the button await user.tab(); diff --git a/packages/components/src/tab-panel/test/index.tsx b/packages/components/src/tab-panel/test/index.tsx index aa9cec2d993383..c07685935ce162 100644 --- a/packages/components/src/tab-panel/test/index.tsx +++ b/packages/components/src/tab-panel/test/index.tsx @@ -117,7 +117,7 @@ describe.each( [ for ( let i = 0; i < allTabs.length; i++ ) { expect( screen.queryByText( TABS_WITH_ICON[ i ].title ) - ).not.toBeVisible(); + ).not.toBeInTheDocument(); await user.hover( allTabs[ i ] ); @@ -160,37 +160,37 @@ describe.each( [ // Tab to focus the tablist. Make sure alpha is focused, and that the // corresponding tooltip is shown. - expect( screen.queryByText( 'Alpha' ) ).not.toBeVisible(); + expect( screen.queryByText( 'Alpha' ) ).not.toBeInTheDocument(); await user.keyboard( '[Tab]' ); expect( mockOnSelect ).toHaveBeenCalledTimes( 1 ); - expect( screen.getByText( 'Alpha' ) ).toBeVisible(); + expect( screen.getByText( 'Alpha' ) ).toBeInTheDocument(); expect( await getSelectedTab() ).toHaveFocus(); // Move selection with arrow keys. Make sure beta is focused, and that // the corresponding tooltip is shown. - expect( screen.queryByText( 'Beta' ) ).not.toBeVisible(); + expect( screen.queryByText( 'Beta' ) ).not.toBeInTheDocument(); await user.keyboard( '[ArrowRight]' ); expect( mockOnSelect ).toHaveBeenCalledTimes( 2 ); expect( mockOnSelect ).toHaveBeenLastCalledWith( 'beta' ); - expect( screen.getByText( 'Beta' ) ).toBeVisible(); + expect( screen.getByText( 'Beta' ) ).toBeInTheDocument(); expect( await getSelectedTab() ).toHaveFocus(); // Move selection with arrow keys. Make sure gamma is focused, and that // the corresponding tooltip is shown. - expect( screen.queryByText( 'Gamma' ) ).not.toBeVisible(); + expect( screen.queryByText( 'Gamma' ) ).not.toBeInTheDocument(); await user.keyboard( '[ArrowRight]' ); expect( mockOnSelect ).toHaveBeenCalledTimes( 3 ); expect( mockOnSelect ).toHaveBeenLastCalledWith( 'gamma' ); - expect( screen.getByText( 'Gamma' ) ).toBeVisible(); + expect( screen.getByText( 'Gamma' ) ).toBeInTheDocument(); expect( await getSelectedTab() ).toHaveFocus(); // Move selection with arrow keys. Make sure beta is focused, and that // the corresponding tooltip is shown. - expect( screen.queryByText( 'Beta' ) ).not.toBeVisible(); + expect( screen.queryByText( 'Beta' ) ).not.toBeInTheDocument(); await user.keyboard( '[ArrowLeft]' ); expect( mockOnSelect ).toHaveBeenCalledTimes( 4 ); expect( mockOnSelect ).toHaveBeenLastCalledWith( 'beta' ); - expect( screen.getByText( 'Beta' ) ).toBeVisible(); + expect( screen.getByText( 'Beta' ) ).toBeInTheDocument(); expect( await getSelectedTab() ).toHaveFocus(); await cleanupTooltip( user ); diff --git a/packages/components/src/toggle-group-control/test/__snapshots__/index.tsx.snap b/packages/components/src/toggle-group-control/test/__snapshots__/index.tsx.snap index ce95e3d739c2cc..296e9483b9f705 100644 --- a/packages/components/src/toggle-group-control/test/__snapshots__/index.tsx.snap +++ b/packages/components/src/toggle-group-control/test/__snapshots__/index.tsx.snap @@ -246,7 +246,6 @@ exports[`ToggleGroupControl controlled should render correctly with icons 1`] = >