Skip to content

Build your personal knowledge base with Trilium Notes

17.7 KB
Raw Blame
import { render } from "preact";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import Popup, { closeOpenPopups, placeFloating, type PopupProps } from "./Popup";

describe("Popup", () => {
    const host = document.createElement("div");
    document.body.append(host);
    let anchor: HTMLButtonElement;

    beforeEach(() => {
        anchor = document.createElement("button");
        document.body.append(anchor);
        // Floating UI reads the viewport from the root element's client size, and the popup's own
        // size from its offset size; happy-dom lays out nothing.
        vi.spyOn(document.documentElement, "clientWidth", "get").mockReturnValue(1000);
        vi.spyOn(document.documentElement, "clientHeight", "get").mockReturnValue(800);
        vi.spyOn(HTMLElement.prototype, "offsetWidth", "get").mockReturnValue(200);
        vi.spyOn(HTMLElement.prototype, "offsetHeight", "get").mockReturnValue(100);
    });

    afterEach(() => {
        render(null, host);
        anchor.remove();
        vi.restoreAllMocks();
        Object.defineProperty(document, "fullscreenElement", { value: null, configurable: true });
    });

    function anchorAt(x: number, y: number) {
        vi.spyOn(anchor, "getBoundingClientRect").mockReturnValue(DOMRect.fromRect({ x, y, width: 80, height: 30 }));
    }

    /** Renders the popup and waits for it to be placed, which is when it shows. */
    async function open(props: Partial<PopupProps> = {}) {
        render(<Popup anchor={anchor} onDismiss={() => {}} {...props}><span>content</span></Popup>, host);
        const popup = document.querySelector<HTMLElement>(".tn-popup");
        if (!popup) throw new Error("expected the popup to render");
        await vi.waitFor(() => expect(popup.style.visibility).toBe("visible"));
        return popup;
    }

    it("stands in the page's body, below its anchor and lined up with its start", async () => {
        anchorAt(100, 50);
        const popup = await open({ className: "dropdown-menu", role: "menu" });

        expect(popup.parentElement).toBe(document.body);
        expect([ ...popup.classList ]).toEqual([ "tn-popup", "dropdown-menu" ]);
        expect(popup.getAttribute("role")).toBe("menu");
        expect(popup.textContent).toBe("content");
        expect([ popup.style.left, popup.style.top ]).toEqual([ "100px", "80px" ]);
    });

    it("keeps the gap it is given from its anchor", async () => {
        anchorAt(100, 50);
        const popup = await open({ offset: 2 });

        expect(popup.style.top).toBe("82px");
    });

    it("offsets an arrow along the edge facing the anchor, clear of the corners", async () => {
        const card = document.createElement("div");
        const pointer = document.createElement("div");
        Object.defineProperty(pointer, "offsetWidth", { value: 12 });
        Object.defineProperty(pointer, "offsetHeight", { value: 12 });
        card.append(pointer);
        document.body.append(card);
        const arrow = { element: pointer, padding: 10 };

        // Right of the anchor: the arrow's offset is along the card's left edge, and the
        // stylesheet sets the other axis.
        anchorAt(100, 350);
        expect(await placeFloating(card, anchor, { placement: "right-start", offset: 10, arrow })).toBe("right-start");
        expect(card.style.left).toBe("190px");
        const top = parseFloat(pointer.style.top);
        expect(top).toBeGreaterThanOrEqual(10);
        expect(top).toBeLessThanOrEqual(100 - 12 - 10);
        expect(pointer.style.left).toBe("");

        // Below the anchor: along the top edge, and the stylesheet sets the other axis.
        expect(await placeFloating(card, anchor, { placement: "bottom-start", offset: 10, arrow })).toBe("bottom-start");
        expect(pointer.style.left).not.toBe("");
        expect(pointer.style.top).toBe("");
        card.remove();
    });

    it("flips above its anchor where there is no room below, and caps its height to the room it has", async () => {
        anchorAt(100, 740);
        const popup = await open();
        // 740 - 100: it ends where the anchor starts.
        expect(popup.style.top).toBe("640px");

        render(null, host);
        vi.spyOn(HTMLElement.prototype, "offsetHeight", "get").mockReturnValue(1000);
        anchorAt(100, 300);
        const tall = await open();
        // The side with the most room, less the gap it keeps from the viewport's edges.
        expect(tall.style.maxHeight).toBe(`${800 - 330 - 5}px`);
    });

    it("keeps a top it is pinned to, whatever its height, and caps its height to the room below", async () => {
        const layer = document.createElement("div");
        document.body.append(layer);
        anchorAt(100, 600);
        vi.spyOn(HTMLElement.prototype, "offsetHeight", "get").mockReturnValue(400);
        // Unpinned, too tall for the room beside its anchor, it moves up to fit.
        await placeFloating(layer, anchor, { placement: "right-start", capHeight: true });
        expect(parseFloat(layer.style.top)).toBeLessThan(600);

        await placeFloating(layer, anchor, { placement: "right-start", capHeight: true, pinTop: 250 });
        expect(layer.style.top).toBe("250px");
        expect(layer.style.maxHeight).toBe(`${800 - 250 - 5}px`);
        // Its side still follows the room it has.
        expect(layer.style.left).toBe("180px");
        layer.remove();
    });

    it("keeps to one side of a caret, never over the line typed, however long it is", async () => {
        vi.spyOn(HTMLElement.prototype, "offsetHeight", "get").mockReturnValue(1000);
        const caret = { getBoundingClientRect: () => DOMRect.fromRect({ x: 100, y: 300, width: 1, height: 16 }) };
        const popup = await open({ anchor: caret });

        // Below the caret, with the room down to the viewport's edge less its gap.
        expect(popup.style.top).toBe("316px");
        expect(popup.style.maxHeight).toBe(`${800 - 316 - 5}px`);
    });

    it("keeps clear of the screen's safe areas, such as a notch or a gesture bar", async () => {
        // The insets a device reports, as the probe reads them from the stylesheet.
        const measure = window.getComputedStyle.bind(window);
        vi.spyOn(window, "getComputedStyle").mockImplementation((element, pseudo) => {
            if (!element.classList.contains("tn-safe-area-probe")) return measure(element, pseudo);
            const insets = { paddingTop: "20px", paddingRight: "0px", paddingBottom: "30px", paddingLeft: "0px" };
            return insets as CSSStyleDeclaration;
        });
        vi.spyOn(HTMLElement.prototype, "offsetHeight", "get").mockReturnValue(1000);

        // Below the anchor, short of the bottom inset as well as the gap from the edge.
        anchorAt(100, 300);
        const below = await open();
        expect(below.style.maxHeight).toBe(`${800 - 330 - 5 - 30}px`);
        render(null, host);

        // Above it, short of the top inset.
        anchorAt(100, 740);
        const above = await open();
        expect(above.style.maxHeight).toBe(`${740 - 5 - 20}px`);
    });

    it("grows past the viewport without its height cap, stands in a wrapper it is given, and says when it is placed", async () => {
        vi.spyOn(HTMLElement.prototype, "offsetHeight", "get").mockReturnValue(1000);
        anchorAt(100, 300);
        const onPlaced = vi.fn();
        const popup = await open({ capHeight: false, portalClassName: "tn-dropdown-portal note-actions", onPlaced });

        expect(popup.style.maxHeight).toBe("");
        expect(popup.parentElement?.className).toBe("tn-dropdown-portal note-actions");
        expect(popup.parentElement?.parentElement).toBe(document.body);
        expect(onPlaced).toHaveBeenCalledTimes(1);
    });

    it("names the side it reveals itself from once placed, when it is asked to reveal itself", async () => {
        anchorAt(100, 50);
        expect((await open({ reveal: true })).dataset.reveal).toBe("bottom-start");

        render(null, host);
        anchorAt(100, 740);
        expect((await open({ reveal: true })).dataset.reveal).toBe("top-start");

        render(null, host);
        expect((await open({ reveal: true, placedByStylesheet: true })).dataset.reveal).toBe("stylesheet");

        render(null, host);
        expect((await open()).dataset.reveal).toBeUndefined();
    });

    it("leaves an inert copy in its wrapper to play its closing animation, gone once that ends", async () => {
        anchorAt(100, 50);
        const popup = await open({ reveal: true, portalClassName: "tn-dropdown-portal global-menu", id: "the-menu" });
        popup.scrollTop = 40;
        let finish = () => {};
        const finished = new Promise<void>((resolve) => {
            finish = resolve;
        });
        vi.spyOn(HTMLElement.prototype, "getAnimations").mockImplementation(function (this: HTMLElement) {
            return this.dataset.closing !== undefined ? [ { finished } as unknown as Animation ] : [];
        });

        render(null, host);
        const copy = document.querySelector<HTMLElement>(".tn-popup");
        expect(copy).not.toBeNull();
        expect(copy).not.toBe(popup);
        expect(copy?.dataset.closing).toBe("");
        expect(copy?.dataset.reveal).toBe("bottom-start");
        expect([ copy?.hasAttribute("inert"), copy?.getAttribute("aria-hidden"), copy?.id ]).toEqual([ true, "true", "" ]);
        expect(copy?.scrollTop).toBe(40);
        expect(copy?.parentElement?.className).toBe("tn-dropdown-portal global-menu");
        expect(copy?.parentElement?.parentElement).toBe(document.body);

        finish();
        await vi.waitFor(() => expect(document.querySelector(".tn-dropdown-portal")).toBeNull());
    });

    it("leaves no copy behind without a reveal, or with nothing to play", async () => {
        anchorAt(100, 50);
        await open();
        render(null, host);
        expect(document.querySelector(".tn-popup")).toBeNull();

        // As with motion turned off, which leaves no animation.
        await open({ reveal: true });
        render(null, host);
        expect(document.querySelector(".tn-popup")).toBeNull();
    });

    it("says nothing of a placement that settles once it has closed", async () => {
        anchorAt(100, 50);
        const onPlaced = vi.fn();
        render(<Popup anchor={anchor} onPlaced={onPlaced}>content</Popup>, host);
        // Closed before the placement it started has settled, as a row picked at once closes a menu.
        render(null, host);

        await new Promise((resolve) => setTimeout(resolve, 20));
        expect(onPlaced).not.toHaveBeenCalled();
    });

    it("opens at a point, as a menu opened where a right-click landed does", async () => {
        const popup = await open({ anchor: { x: 300, y: 200 }, placement: "right-start" });

        expect([ popup.style.left, popup.style.top ]).toEqual([ "300px", "200px" ]);
    });

    it("stands inside whatever has the screen, which is all a browser paints then", async () => {
        const fullscreen = document.createElement("div");
        document.body.append(fullscreen);
        Object.defineProperty(document, "fullscreenElement", { value: fullscreen, configurable: true });
        anchorAt(100, 50);

        const popup = await open();
        expect(popup.parentElement).toBe(fullscreen);
        fullscreen.remove();
    });

    it("stays open for a press in what `keepOpenSelector` names, elsewhere in the page", async () => {
        anchorAt(100, 50);
        const onDismiss = vi.fn();
        await open({ onDismiss, keepOpenSelector: ".hints-pane" });
        const pane = document.createElement("div");
        pane.className = "hints-pane";
        pane.innerHTML = "<span>Ctrl+Space</span>";
        document.body.append(pane);

        pane.firstElementChild?.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true }));
        expect(onDismiss).not.toHaveBeenCalled();

        document.body.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true }));
        expect(onDismiss).toHaveBeenCalledWith("outside");
        pane.remove();
    });

    it("is dismissed by a press outside it and its anchor, and by Escape, which goes no further", async () => {
        anchorAt(100, 50);
        const onDismiss = vi.fn();
        const popup = await open({ onDismiss });
        const pressOn = (target: Element) => target.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true }));
        // As the note tree stops its own presses.
        const outside = document.createElement("div");
        outside.addEventListener("pointerdown", (e) => e.stopPropagation());
        document.body.append(outside);

        pressOn(popup.firstElementChild ?? popup);
        // The anchor is a dropdown's toggle, which opens and closes the popup itself.
        pressOn(anchor);
        expect(onDismiss).not.toHaveBeenCalled();

        pressOn(outside);
        expect(onDismiss).toHaveBeenCalledWith("outside");

        const dialogHeard = vi.fn();
        document.addEventListener("keydown", dialogHeard);
        popup.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
        expect(onDismiss).toHaveBeenLastCalledWith("escape");
        expect(dialogHeard).not.toHaveBeenCalled();
        document.removeEventListener("keydown", dialogHeard);
        outside.remove();

        // As for a keyboard shortcut that opens a dialog.
        onDismiss.mockClear();
        closeOpenPopups();
        expect(onDismiss).toHaveBeenCalledExactlyOnceWith("escape");
        render(null, host);
        closeOpenPopups();
        expect(onDismiss).toHaveBeenCalledTimes(1);
    });

    it("drops the click after a dismissing press unless it lands on what was pressed", async () => {
        anchorAt(100, 50);
        const popup = await open({ onDismiss: vi.fn() });
        const press = (target: Element) => target.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true }));
        const click = (target: Element) => target.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true }));
        // A phone's cover, hidden with the popup before the tap's click, and an icon under it.
        const cover = document.createElement("div");
        const icon = document.createElement("button");
        const iconClicked = vi.fn();
        icon.addEventListener("click", iconClicked);
        document.body.append(cover, icon);

        press(cover);
        click(icon);
        expect(iconClicked).not.toHaveBeenCalled();
        // Only the first click is dropped.
        click(icon);
        expect(iconClicked).toHaveBeenCalledTimes(1);

        press(icon);
        click(icon);
        expect(iconClicked).toHaveBeenCalledTimes(2);

        // A press inside the popup does not dismiss it, so its click is kept.
        press(popup);
        click(icon);
        expect(iconClicked).toHaveBeenCalledTimes(3);

        // A tap that starts on the button's glyph and ends on its padding clicks the button.
        const glyph = document.createElement("span");
        icon.append(glyph);
        press(glyph);
        click(icon);
        expect(iconClicked).toHaveBeenCalledTimes(4);

        // WebKit takes a tap close to the one before as a double tap, and clicks where that one
        // landed instead of where this one did.
        const other = document.createElement("button");
        const otherClicked = vi.fn();
        other.addEventListener("click", otherClicked);
        document.body.append(other);
        press(glyph);
        other.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, detail: 2 }));
        expect(otherClicked).not.toHaveBeenCalled();
        expect(iconClicked).toHaveBeenCalledTimes(5);
        other.remove();
        cover.remove();
        icon.remove();
    });

    it("counts a popup opened inside it as inside, and leaves Escape to that popup", async () => {
        anchorAt(100, 50);
        const onOuterDismiss = vi.fn();
        const onInnerDismiss = vi.fn();
        const pressOn = (target: Element) => target.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true }));
        // As the calendar's month list opens from inside the calendar's panel.
        render((
            <Popup anchor={anchor} className="outer" onDismiss={onOuterDismiss}>
                <Popup anchor={{ x: 300, y: 200 }} className="inner" onDismiss={onInnerDismiss}>month</Popup>
            </Popup>
        ), host);
        // A popup of another tree, which is not the outer one's.
        const unrelatedHost = document.createElement("div");
        document.body.append(unrelatedHost);
        render(<Popup anchor={{ x: 0, y: 0 }} className="unrelated">other</Popup>, unrelatedHost);
        const inner = document.querySelector(".tn-popup.inner");
        const unrelated = document.querySelector(".tn-popup.unrelated");
        if (!inner || !unrelated) throw new Error("expected both popups to render");
        // Portaled to the body, not into the outer popup.
        expect(document.querySelector(".tn-popup.outer")?.contains(inner)).toBe(false);

        pressOn(inner);
        expect(onOuterDismiss).not.toHaveBeenCalled();
        expect(onInnerDismiss).not.toHaveBeenCalled();

        inner.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
        expect(onInnerDismiss).toHaveBeenCalledWith("escape");
        expect(onOuterDismiss).not.toHaveBeenCalled();

        pressOn(unrelated);
        expect(onOuterDismiss).toHaveBeenCalledWith("outside");

        // Once the inner one is gone, Escape reaches the outer one again.
        render((
            <Popup anchor={anchor} className="outer" onDismiss={onOuterDismiss}>content</Popup>
        ), host);
        document.body.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
        expect(onOuterDismiss).toHaveBeenLastCalledWith("escape");

        render(null, unrelatedHost);
        unrelatedHost.remove();
    });
});