From 5151bde6ce7515fd393b7cf06a838d7deb7b67e2 Mon Sep 17 00:00:00 2001 From: Yossi Saadi Date: Mon, 29 Jan 2024 13:41:28 +0200 Subject: [PATCH] fix(MenuItem): tooltip should on all item and not only on title (#1914) Co-authored-by: Tal Koren Co-authored-by: Tal Koren --- src/components/Menu/Menu/Menu.module.scss | 1 + .../__tests__/__snapshots__/menu.jest.js.snap | 14 ++++++ .../Menu/Menu/__tests__/menu-interactions.js | 40 +++++++++++---- .../Menu/MenuItem/MenuItem.module.scss | 7 +++ src/components/Menu/MenuItem/MenuItem.tsx | 6 ++- .../Menu/MenuItem/__tests__/MenuItem.jest.js | 4 +- .../menuItem-snapshots.jest.js.snap | 49 +++++++++++++++++++ ...litButtonMenu-snapshot-tests.jest.tsx.snap | 14 ++++++ src/tests/interactions-utils.ts | 14 ++++-- 9 files changed, 130 insertions(+), 19 deletions(-) diff --git a/src/components/Menu/Menu/Menu.module.scss b/src/components/Menu/Menu/Menu.module.scss index d6a7819cfe..311bdec285 100644 --- a/src/components/Menu/Menu/Menu.module.scss +++ b/src/components/Menu/Menu/Menu.module.scss @@ -3,6 +3,7 @@ .menu { margin: unset; padding: unset; + position: relative; } .menu:focus { diff --git a/src/components/Menu/Menu/__tests__/__snapshots__/menu.jest.js.snap b/src/components/Menu/Menu/__tests__/__snapshots__/menu.jest.js.snap index 78e4c18d46..d81bb98f2f 100644 --- a/src/components/Menu/Menu/__tests__/__snapshots__/menu.jest.js.snap +++ b/src/components/Menu/Menu/__tests__/__snapshots__/menu.jest.js.snap @@ -38,6 +38,13 @@ exports[`Snapshots renders correctly with children 1`] = ` > item 1 +
+ item 1 +
item 2
+
+ item 2 +
{ const menuElement = getMenuElement(canvas); - const topMenuItem = getByText(menuElement, TWO_DEPTHS_MENU_TEXTS.TOP_MENU_SUB_MENU_ITEM); + const topMenuItem = getByText(menuElement, TWO_DEPTHS_MENU_TEXTS.TOP_MENU_SUB_MENU_ITEM, { + ignore: HIDDEN_ELEMENT_SELECTOR + }); await userEvent.hover(topMenuItem); - const innerMenuItem = getByText(menuElement, TWO_DEPTHS_MENU_TEXTS.SUB_MENU_ITEM); + const innerMenuItem = getByText(menuElement, TWO_DEPTHS_MENU_TEXTS.SUB_MENU_ITEM, { + ignore: HIDDEN_ELEMENT_SELECTOR + }); await userEvent.hover(innerMenuItem); // validate showing sub sub item const optionToSelect = await waitForElementVisible(() => - within(menuElement).findByText(TWO_DEPTHS_MENU_TEXTS.SUB_SUB_MENU_ITEM) + within(menuElement).findByText(TWO_DEPTHS_MENU_TEXTS.SUB_SUB_MENU_ITEM, { ignore: HIDDEN_ELEMENT_SELECTOR }) ); await clickElement(optionToSelect); expect(document.activeElement).toHaveTextContent(TWO_DEPTHS_MENU_TEXTS.SUB_SUB_MENU_ITEM); //close the sub-menus on hovering the top-level menu - await userEvent.hover(getByText(menuElement, TWO_DEPTHS_MENU_TEXTS.TOP_MENU_NON_SUB_MENU_ITEM)); - expect(canvas.queryByText(TWO_DEPTHS_MENU_TEXTS.SUB_MENU_ITEM)).not.toBeInTheDocument(); - expect(canvas.queryByText(TWO_DEPTHS_MENU_TEXTS.SUB_SUB_MENU_ITEM)).not.toBeInTheDocument(); + await userEvent.hover( + getByText(menuElement, TWO_DEPTHS_MENU_TEXTS.TOP_MENU_NON_SUB_MENU_ITEM, { ignore: HIDDEN_ELEMENT_SELECTOR }) + ); + expect( + canvas.queryByText(TWO_DEPTHS_MENU_TEXTS.SUB_MENU_ITEM, { ignore: HIDDEN_ELEMENT_SELECTOR }) + ).not.toBeInTheDocument(); + expect( + canvas.queryByText(TWO_DEPTHS_MENU_TEXTS.SUB_SUB_MENU_ITEM, { ignore: HIDDEN_ELEMENT_SELECTOR }) + ).not.toBeInTheDocument(); }; const showSubSubMenusWithKeyboard = async canvas => { const menuElement = getMenuElement(canvas); //set the initial focus, to make the keyboard events work - const topMenuItem = getByText(menuElement, TWO_DEPTHS_MENU_TEXTS.TOP_MENU_SUB_MENU_ITEM); + const topMenuItem = getByText(menuElement, TWO_DEPTHS_MENU_TEXTS.TOP_MENU_SUB_MENU_ITEM, { + ignore: HIDDEN_ELEMENT_SELECTOR + }); await userEvent.click(topMenuItem); //open sub menu @@ -53,7 +67,9 @@ const showSubSubMenusWithKeyboard = async canvas => { await pressNavigationKey(NavigationCommand.DOWN_ARROW); await pressNavigationKey(NavigationCommand.RIGHT_ARROW); await waitForElementVisible(() => - within(menuElement).findByText(new RegExp(`^${TWO_DEPTHS_MENU_TEXTS.SUB_MENU_ITEM}$`)) + within(menuElement).findByText(new RegExp(`^${TWO_DEPTHS_MENU_TEXTS.SUB_MENU_ITEM}$`), { + ignore: HIDDEN_ELEMENT_SELECTOR + }) ); expectActiveElementToHavePartialText(TWO_DEPTHS_MENU_TEXTS.SUB_MENU_ITEM); @@ -62,13 +78,17 @@ const showSubSubMenusWithKeyboard = async canvas => { await pressNavigationKey(NavigationCommand.DOWN_ARROW); await pressNavigationKey(NavigationCommand.RIGHT_ARROW); await waitForElementVisible(() => - within(menuElement).findByText(new RegExp(`^${TWO_DEPTHS_MENU_TEXTS.SUB_SUB_MENU_ITEM}$`)) + within(menuElement).findByText(new RegExp(`^${TWO_DEPTHS_MENU_TEXTS.SUB_SUB_MENU_ITEM}$`), { + ignore: HIDDEN_ELEMENT_SELECTOR + }) ); expectActiveElementToHavePartialText(TWO_DEPTHS_MENU_TEXTS.SUB_SUB_MENU_ITEM); //close sub-sub-menu - using left arrow await pressNavigationKey(NavigationCommand.LEFT_ARROW); - expect(canvas.queryByText(menuElement, TWO_DEPTHS_MENU_TEXTS.SUB_SUB_MENU_ITEM)).not.toBeInTheDocument(); + expect( + canvas.queryByText(menuElement, TWO_DEPTHS_MENU_TEXTS.SUB_SUB_MENU_ITEM, { ignore: HIDDEN_ELEMENT_SELECTOR }) + ).not.toBeInTheDocument(); expectActiveElementToHavePartialText(TWO_DEPTHS_MENU_TEXTS.SUB_MENU_ITEM); //close sub-menu - using escape diff --git a/src/components/Menu/MenuItem/MenuItem.module.scss b/src/components/Menu/MenuItem/MenuItem.module.scss index 26ef76e94b..e6735007c1 100644 --- a/src/components/Menu/MenuItem/MenuItem.module.scss +++ b/src/components/Menu/MenuItem/MenuItem.module.scss @@ -103,3 +103,10 @@ flex-grow: 1; padding-block: 1px; } + +.hiddenTitle { + width: 100%; + position: absolute; + left: 0; + opacity: 0; +} diff --git a/src/components/Menu/MenuItem/MenuItem.tsx b/src/components/Menu/MenuItem/MenuItem.tsx index 9ed9f6c8ed..7a653cd22c 100644 --- a/src/components/Menu/MenuItem/MenuItem.tsx +++ b/src/components/Menu/MenuItem/MenuItem.tsx @@ -334,13 +334,15 @@ const MenuItem: VibeComponent & { content={shouldShowTooltip ? finalTooltipContent : null} position={tooltipPosition} showDelay={tooltipShowDelay} - // Tooltip should be on a whole MenuItem, but it's a breaking change - should be fixed in the next major and then this can be removed - moveBy={icon && tooltipPosition === Tooltip.positions.LEFT ? { main: 30 } : undefined} {...tooltipProps} >
{title}
+ {/* Tooltip should be on a whole MenuItem, but it's a breaking change (tooltip adds span) - should be fixed in the next major and then this div be removed */} +
+ {title} +
{label &&
+
+ my item +
my item
+
+ my item +
my item
+
+ my item +
+ +
+
+
+
+
my item
+
+ my item +
my item
+
+ my item +
Test 1
+
+ Test 1 +
Test 2
+
+ Test 2 +
{ return document.getElementsByClassName(className)[0]; }; -export const getByRole = (rootElement: HTMLElement | BoundFunctions, role: string) => { - return getWithin(rootElement).getByRole(role); +export const getByRole = (rootElement: HTMLElement | BoundFunctions, role: string, options = {}) => { + return getWithin(rootElement).getByRole(role, options); }; export const getAllByRole = (rootElement: HTMLElement | BoundFunctions, role: string) => { @@ -161,8 +161,12 @@ export const getAllByLabelText = (rootElement: HTMLElement, text: string) => { return getWithin(rootElement).getAllByLabelText(text); }; -export const getByText = (rootElement: HTMLElement | BoundFunctions, text: string) => { - return getWithin(rootElement).getByText(text); +export const getByText = ( + rootElement: HTMLElement | BoundFunctions, + text: string, + options: SelectorMatcherOptions = {} +) => { + return getWithin(rootElement).getByText(text, options); }; export const getAllByText = (rootElement: HTMLElement | BoundFunctions, text: string) => {