Skip to content

Commit

Permalink
add tooltip test
Browse files Browse the repository at this point in the history
  • Loading branch information
bseber committed Oct 25, 2024
1 parent ca8c8ae commit c61d93c
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/main/javascript/components/tooltip/tooltip.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { afterEach, describe, expect, test, vi } from "vitest";
import { Tooltip } from "./tooltip";
import type { Instance } from "@popperjs/core";
import * as popper from "@popperjs/core";

vi.spyOn(popper, "createPopper");

describe("tooltip", () => {
afterEach(() => {
while (document.body.firstChild) {
document.body.firstChild.remove();
}
});

test("sets role attribute", () => {
document.body.innerHTML = '<div id="parent"></div>';

const paragraph = document.createElement("p");
paragraph.textContent = "tooltip text";

const tooltip = new Tooltip();
expect(tooltip.getAttribute("role")).toBe("tooltip");
});

test("initializes tooltip on mount", () => {
document.body.innerHTML = '<div id="parent"></div>';
const tooltip = anyTooltip();

expect(popper.createPopper).not.toHaveBeenCalled();

document.querySelector("#parent").append(tooltip);
expect(popper.createPopper).toHaveBeenCalled();
});

test.each([["mouseenter"], ["focus"]])("shows tooltip on %s", (eventName) => {
const popperInstance = {
update: vi.fn(),
};
vi.mocked(popper.createPopper).mockReturnValue(
popperInstance as unknown as Instance,
);

document.body.innerHTML = '<div id="parent"></div>';
const root = document.querySelector("#parent");
const tooltip = anyTooltip();
root.append(tooltip);

expect(tooltip.dataset.show).not.toBeDefined();

root.dispatchEvent(new Event(eventName));
expect(tooltip.dataset.show).toBeDefined();
expect(popperInstance.update).toHaveBeenCalledOnce();
});

test.each([["mouseleave"], ["blur"]])("hides tooltip on %s", (eventName) => {
document.body.innerHTML = '<div id="parent"></div>';
const root = document.querySelector("#parent");
const tooltip = anyTooltip();
root.append(tooltip);

tooltip.dataset.show = "";

root.dispatchEvent(new Event(eventName));
expect(tooltip.dataset.show).not.toBeDefined();
});
});

function anyTooltip() {
const paragraph = document.createElement("p");
paragraph.textContent = "tooltip text";

const tooltip = new Tooltip();
tooltip.append(paragraph);

return tooltip;
}

0 comments on commit c61d93c

Please sign in to comment.