|
1 | 1 | import React from "react"; |
2 | 2 | import { fireEvent, render, screen, waitFor } from "@testing-library/react"; |
| 3 | +import userEvent from "@testing-library/user-event"; |
3 | 4 |
|
4 | 5 | import "@testing-library/jest-dom"; |
5 | 6 |
|
@@ -45,19 +46,53 @@ describe("Tooltip", () => { |
45 | 46 | fireEvent.mouseEnter(container.getElementsByClassName(`${eccgui}-tooltip__wrapper--placeholder`)[0]); |
46 | 47 | checkForPlaceholderClass(container, 1); |
47 | 48 | await waitFor(() => { |
48 | | - expect(screen.queryAllByText(TooltipStory.args.content)).toHaveLength(0); |
49 | 49 | checkForPlaceholderClass(container, 0); |
50 | 50 | }); |
| 51 | + expect(screen.queryAllByText(TooltipStory.args.content as string)).toHaveLength(0); |
51 | 52 | }); |
52 | 53 | it("should be displayed on two continues mouse hover when placeholder is used", async () => { |
53 | 54 | const { container } = render(<Tooltip {...TooltipStory.args} usePlaceholder={true} />); |
54 | 55 | fireEvent.mouseEnter(container.getElementsByClassName(`${eccgui}-tooltip__wrapper`)[0]); |
55 | 56 | checkForPlaceholderClass(container, 1); |
56 | | - await waitFor(async () => { |
57 | | - expect(screen.queryAllByText(TooltipStory.args.content)).toHaveLength(0); |
| 57 | + await waitFor(() => { |
| 58 | + checkForPlaceholderClass(container, 0); |
| 59 | + }); |
| 60 | + expect(screen.queryAllByText(TooltipStory.args.content as string)).toHaveLength(0); |
| 61 | + fireEvent.mouseEnter(container.getElementsByClassName(`${eccgui}-tooltip__wrapper`)[0]); |
| 62 | + expect(await screen.findByText(TooltipStory.args.content as string)).toBeVisible(); |
| 63 | + }); |
| 64 | + it("should be displayed on focus when no placeholder is used", async () => { |
| 65 | + // Blueprint ignores focus events with null relatedTarget (page-refocus guard), so we tab |
| 66 | + // from a preceding element to produce a non-null relatedTarget. |
| 67 | + render( |
| 68 | + <> |
| 69 | + <button>previous element</button> |
| 70 | + <Tooltip {...TooltipStory.args} usePlaceholder={false} /> |
| 71 | + </> |
| 72 | + ); |
| 73 | + const user = userEvent.setup(); |
| 74 | + await user.tab(); // focuses "previous element" |
| 75 | + await user.tab(); // focuses tooltip target, relatedTarget is non-null → Blueprint opens |
| 76 | + expect(await screen.findByText(TooltipStory.args.content as string)).toBeVisible(); |
| 77 | + }); |
| 78 | + it("should be displayed after keyboard focus when placeholder is used", async () => { |
| 79 | + // Use a focusable button child so refocus() can call .focus() on it after the swap. |
| 80 | + // Tab from a preceding element so relatedTarget is non-null when Blueprint handles focus. |
| 81 | + const { container } = render( |
| 82 | + <> |
| 83 | + <button>previous element</button> |
| 84 | + <Tooltip {...TooltipStory.args} usePlaceholder={true}> |
| 85 | + <button>tooltip target</button> |
| 86 | + </Tooltip> |
| 87 | + </> |
| 88 | + ); |
| 89 | + const user = userEvent.setup(); |
| 90 | + await user.tab(); // focuses "previous element" |
| 91 | + await user.tab(); // focuses placeholder inner button, triggers focusin swap |
| 92 | + checkForPlaceholderClass(container, 1); |
| 93 | + await waitFor(() => { |
58 | 94 | checkForPlaceholderClass(container, 0); |
59 | | - fireEvent.mouseOver(container.getElementsByClassName(`${eccgui}-tooltip__wrapper`)[0]); |
60 | | - expect(await screen.findByText(TooltipStory.args.content)).toBeVisible(); |
61 | 95 | }); |
| 96 | + expect(await screen.findByText(TooltipStory.args.content as string)).toBeVisible(); |
62 | 97 | }); |
63 | 98 | }); |
0 commit comments