76 lines
3.7 KiB
TypeScript
76 lines
3.7 KiB
TypeScript
import { act, createElement } from "react";
|
|
import { createRoot } from "react-dom/client";
|
|
import { renderToStaticMarkup } from "react-dom/server";
|
|
import { expect, it, vi } from "vitest";
|
|
import { Btn } from "../src/client/components/living/primitives";
|
|
import { Tooltip } from "../src/client/components/living/Tooltip";
|
|
|
|
it("a tooltip around a control named by text carries that text, never an object", () => {
|
|
const markup = renderToStaticMarkup(createElement(Tooltip, { overflow: { text: "Friends", selector: ".lr-cap" },
|
|
children: createElement(Btn, { icon: "chevron", title: { text: "Site settings" }, cap: { text: "Friends" }, disabled: true }) }));
|
|
expect(markup).not.toContain("[object Object]");
|
|
expect(markup).toContain('aria-label="Site settings"');
|
|
});
|
|
|
|
it("anchors inside a declared row and repositions an open neighbouring hint as the row comes and goes", async () => {
|
|
const trigger = { left: 180, right: 200, top: 200, bottom: 220, width: 20, height: 20 };
|
|
const bounds = { left: 0, right: 400, top: 240, bottom: 320 };
|
|
let inside = false, present = false, top = 0;
|
|
class Row {
|
|
dataset = { tooltipControls: "true" };
|
|
matches(selector: string) { return selector.split(",").some(part => part.trim() === "[data-tooltip-controls]"); }
|
|
querySelector() { return null; }
|
|
contains() { return inside; }
|
|
getBoundingClientRect() { return bounds; }
|
|
getClientRects() { return [bounds]; }
|
|
}
|
|
const row = new Row();
|
|
const observer = class { observe() {} disconnect() {} };
|
|
const view = Object.assign(new EventTarget(), {
|
|
innerWidth: 400, innerHeight: 600, Element: Row, HTMLIFrameElement: class {},
|
|
ResizeObserver: observer, IntersectionObserver: observer,
|
|
requestAnimationFrame: (pick: () => void) => { pick(); return 1; }, cancelAnimationFrame: vi.fn(),
|
|
});
|
|
const page = Object.assign(new EventTarget(), { nodeType: 9, defaultView: view, documentElement: { dataset: {} }, body: {},
|
|
querySelectorAll: (selector: string) => present && row.matches(selector) ? [row] : [],
|
|
});
|
|
const wrap = { ownerDocument: page, firstElementChild: { getBoundingClientRect: () => trigger },
|
|
closest: (selector: string) => inside && row.matches(selector) ? row : null };
|
|
let notify!: (records: { addedNodes: Row[]; removedNodes: Row[] }[]) => void;
|
|
vi.stubGlobal("MutationObserver", class extends observer {
|
|
constructor(private readonly callback: typeof notify) { super(); }
|
|
observe(_target?: unknown, options?: MutationObserverInit) { if (options?.childList) notify = this.callback; }
|
|
});
|
|
vi.stubGlobal("window", Object.assign(view, { document: page }));
|
|
vi.stubGlobal("document", page);
|
|
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
|
|
const container = Object.assign(new EventTarget(), { nodeType: 1, tagName: "DIV", ownerDocument: page });
|
|
const root = createRoot(container as unknown as HTMLElement);
|
|
function Hint() {
|
|
const hint = Tooltip({ text: "Hint", autoOpen: true, place: "below", children: createElement("button") });
|
|
hint.props.ref.current = wrap;
|
|
top = hint.props.children[1].props.style.top;
|
|
return null;
|
|
}
|
|
const showRow = (show: boolean) => act(() => {
|
|
present = show;
|
|
notify([{ addedNodes: show ? [row] : [], removedNodes: show ? [] : [row] }]);
|
|
});
|
|
try {
|
|
await act(() => root.render(createElement(Hint)));
|
|
const gap = top - trigger.bottom;
|
|
expect(gap).toBeGreaterThan(0);
|
|
await showRow(true);
|
|
expect(top).toBeLessThan(trigger.top);
|
|
await showRow(false);
|
|
expect(top).toBe(trigger.bottom + gap);
|
|
inside = true;
|
|
Object.assign(trigger, { top: 260, bottom: 280 });
|
|
await showRow(true);
|
|
expect(top).toBe(bounds.bottom + gap);
|
|
} finally {
|
|
await act(() => root.unmount());
|
|
vi.unstubAllGlobals();
|
|
}
|
|
});
|