|
| 1 | +import React from "react"; |
| 2 | +import { fireEvent, render, screen, waitFor } from "@testing-library/react"; |
| 3 | + |
| 4 | +import "@testing-library/jest-dom"; |
| 5 | + |
| 6 | +import { CLASSPREFIX as eccgui } from "../../configuration/constants"; |
| 7 | + |
| 8 | +import Tooltip from "./Tooltip"; |
| 9 | +import { Default as TooltipStory } from "./Tooltip.stories"; |
| 10 | + |
| 11 | +const checkForPlaceholderClass = (container: HTMLElement, tobe: number) => { |
| 12 | + expect(container.getElementsByClassName(`${eccgui}-tooltip__wrapper--placeholder`).length).toBe(tobe); |
| 13 | +}; |
| 14 | + |
| 15 | +describe("Tooltip", () => { |
| 16 | + it("should render placeholder automatically for text tooltip", () => { |
| 17 | + const { container } = render(<Tooltip {...TooltipStory.args} content="this is a simple text tooltip" />); |
| 18 | + checkForPlaceholderClass(container, 1); |
| 19 | + }); |
| 20 | + it("should render no placeholder automatically for html tooltip", () => { |
| 21 | + const { container } = render( |
| 22 | + <Tooltip {...TooltipStory.args} content={<div>this is a simple text tooltip</div>} /> |
| 23 | + ); |
| 24 | + checkForPlaceholderClass(container, 0); |
| 25 | + }); |
| 26 | + it("should render placeholder when `usePlaceholder===true`", () => { |
| 27 | + const { container } = render( |
| 28 | + <Tooltip {...TooltipStory.args} content={<div>this is a simple text tooltip</div>} usePlaceholder={true} /> |
| 29 | + ); |
| 30 | + checkForPlaceholderClass(container, 1); |
| 31 | + }); |
| 32 | + it("should render no placeholder when `usePlaceholder===false`", () => { |
| 33 | + const { container } = render( |
| 34 | + <Tooltip {...TooltipStory.args} content="this is a simple text tooltip" usePlaceholder={false} /> |
| 35 | + ); |
| 36 | + checkForPlaceholderClass(container, 0); |
| 37 | + }); |
| 38 | + it("should be displayed on first mouse hover when no placeholder is used", async () => { |
| 39 | + const { container } = render(<Tooltip {...TooltipStory.args} usePlaceholder={false} />); |
| 40 | + fireEvent.mouseEnter(container.getElementsByClassName(`${eccgui}-tooltip__wrapper`)[0]); |
| 41 | + expect(await screen.findByText(TooltipStory.args.content)).toBeVisible(); |
| 42 | + }); |
| 43 | + it("should not be displayed on first mouse hover when placeholder is used but placeholder markup is swapped", async () => { |
| 44 | + const { container } = render(<Tooltip {...TooltipStory.args} usePlaceholder={true} />); |
| 45 | + fireEvent.mouseEnter(container.getElementsByClassName(`${eccgui}-tooltip__wrapper--placeholder`)[0]); |
| 46 | + checkForPlaceholderClass(container, 1); |
| 47 | + await waitFor(() => { |
| 48 | + expect(screen.queryAllByText(TooltipStory.args.content)).toHaveLength(0); |
| 49 | + checkForPlaceholderClass(container, 0); |
| 50 | + }); |
| 51 | + }); |
| 52 | + it("should be displayed on two continues mouse hover when placeholder is used", async () => { |
| 53 | + const { container } = render(<Tooltip {...TooltipStory.args} usePlaceholder={true} />); |
| 54 | + fireEvent.mouseEnter(container.getElementsByClassName(`${eccgui}-tooltip__wrapper`)[0]); |
| 55 | + checkForPlaceholderClass(container, 1); |
| 56 | + await waitFor(async () => { |
| 57 | + expect(screen.queryAllByText(TooltipStory.args.content)).toHaveLength(0); |
| 58 | + checkForPlaceholderClass(container, 0); |
| 59 | + fireEvent.mouseOver(container.getElementsByClassName(`${eccgui}-tooltip__wrapper`)[0]); |
| 60 | + expect(await screen.findByText(TooltipStory.args.content)).toBeVisible(); |
| 61 | + }); |
| 62 | + }); |
| 63 | +}); |
0 commit comments