mirror of
https://github.com/gtsteffaniak/filebrowser.git
synced 2026-11-26 08:29:41 -05:00
+5








8f2e8add9a
Co-authored-by: Kevin B. <kurami32_kbm@proton.me> Co-authored-by: spatiumstas <79056064+spatiumstas@users.noreply.github.com> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: Andreas Fehn <fehnomenal@fehn.systems> Co-authored-by: Matt Van Horn <mvanhorn@users.noreply.github.com> Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Co-authored-by: Takahiro Shimizu <anatofuz@gmail.com> Co-authored-by: Graham Steffaniak <gtsteffaniak@users.noreply.github.com> Co-authored-by: Joel Town Road <13539685+hippi345@users.noreply.github.com> Co-authored-by: beemines <3083452174@qq.com> Co-authored-by: Bee mines <Beemines82@gmail.com> Co-authored-by: dweebzxx <134675545+dweebzxx@users.noreply.github.com> Co-authored-by: Stephan Paternotte <Stephan-P@users.noreply.github.com>
300 lines
9.5 KiB
JavaScript
300 lines
9.5 KiB
JavaScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const { storeMock } = vi.hoisted(() => ({
|
|
storeMock: {
|
|
state: {
|
|
tooltip: {
|
|
show: false,
|
|
content: "",
|
|
component: null,
|
|
pointerEvents: false,
|
|
},
|
|
},
|
|
getters: {
|
|
isMobile: vi.fn(() => false),
|
|
},
|
|
mutations: {
|
|
showTooltip: vi.fn(),
|
|
hideTooltip: vi.fn(),
|
|
},
|
|
},
|
|
}));
|
|
|
|
vi.mock("@/store", () => storeMock);
|
|
|
|
import {
|
|
hideInteractiveTooltip,
|
|
isTooltipContentVisible,
|
|
onTooltipHelpClick,
|
|
onTooltipHelpMouseEnter,
|
|
onTooltipHelpMouseLeave,
|
|
onTooltipHelpTouchEnd,
|
|
shouldIgnoreOutsideTap,
|
|
showHoverTooltip,
|
|
tooltipEventCoords,
|
|
} from "./tooltipHelp.js";
|
|
|
|
function makePointerEvent(coords = {}) {
|
|
return {
|
|
clientX: 0,
|
|
clientY: 0,
|
|
preventDefault: vi.fn(),
|
|
stopPropagation: vi.fn(),
|
|
...coords,
|
|
};
|
|
}
|
|
|
|
describe("tooltipHelp", () => {
|
|
beforeEach(() => {
|
|
storeMock.getters.isMobile.mockReturnValue(false);
|
|
storeMock.state.tooltip.show = false;
|
|
storeMock.state.tooltip.content = "";
|
|
storeMock.state.tooltip.component = null;
|
|
storeMock.state.tooltip.pointerEvents = false;
|
|
storeMock.mutations.showTooltip.mockClear();
|
|
storeMock.mutations.hideTooltip.mockClear();
|
|
hideInteractiveTooltip(true);
|
|
storeMock.mutations.showTooltip.mockClear();
|
|
storeMock.mutations.hideTooltip.mockClear();
|
|
vi.stubGlobal("matchMedia", vi.fn(() => ({ matches: false })));
|
|
});
|
|
|
|
afterEach(() => {
|
|
hideInteractiveTooltip(true);
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
it("uses touch coordinates from touchend events", () => {
|
|
const coords = tooltipEventCoords({
|
|
changedTouches: [{ clientX: 42, clientY: 84 }],
|
|
});
|
|
expect(coords).toEqual({ x: 42, y: 84 });
|
|
});
|
|
|
|
it("prefers touch coordinates when clientX is undefined", () => {
|
|
const coords = tooltipEventCoords({
|
|
clientX: undefined,
|
|
clientY: undefined,
|
|
changedTouches: [{ clientX: 10, clientY: 20 }],
|
|
});
|
|
expect(coords).toEqual({ x: 10, y: 20 });
|
|
});
|
|
|
|
it("uses mouse coordinates for hover events", () => {
|
|
const coords = tooltipEventCoords({ clientX: 15, clientY: 25 });
|
|
expect(coords).toEqual({ x: 15, y: 25 });
|
|
});
|
|
|
|
it("shows tooltip on desktop hover", () => {
|
|
const event = { clientX: 10, clientY: 20 };
|
|
onTooltipHelpMouseEnter(event, "help text");
|
|
expect(storeMock.mutations.showTooltip).toHaveBeenCalledWith({
|
|
content: "help text",
|
|
x: 10,
|
|
y: 20,
|
|
pointerEvents: false,
|
|
});
|
|
});
|
|
|
|
it("shows hover tooltip even when tap mode is enabled", () => {
|
|
vi.stubGlobal("matchMedia", vi.fn(() => ({ matches: true })));
|
|
const event = { clientX: 10, clientY: 20 };
|
|
onTooltipHelpMouseEnter(event, "help text");
|
|
expect(storeMock.mutations.showTooltip).toHaveBeenCalledWith({
|
|
content: "help text",
|
|
x: 10,
|
|
y: 20,
|
|
pointerEvents: false,
|
|
});
|
|
});
|
|
|
|
it("hides tooltip on desktop mouse leave", () => {
|
|
onTooltipHelpMouseLeave();
|
|
expect(storeMock.mutations.hideTooltip).toHaveBeenCalled();
|
|
});
|
|
|
|
it("shows tooltip on mobile tap and keeps it open on repeat tap", () => {
|
|
storeMock.getters.isMobile.mockReturnValue(true);
|
|
const event = makePointerEvent({ clientX: 5, clientY: 6 });
|
|
|
|
onTooltipHelpTouchEnd(event, "mobile help");
|
|
expect(event.preventDefault).toHaveBeenCalled();
|
|
expect(event.stopPropagation).toHaveBeenCalled();
|
|
expect(storeMock.mutations.showTooltip).toHaveBeenCalledWith({
|
|
content: "mobile help",
|
|
x: 5,
|
|
y: 6,
|
|
pointerEvents: true,
|
|
});
|
|
|
|
storeMock.state.tooltip.show = true;
|
|
storeMock.state.tooltip.content = "mobile help";
|
|
storeMock.state.tooltip.pointerEvents = true;
|
|
storeMock.mutations.hideTooltip.mockClear();
|
|
storeMock.mutations.showTooltip.mockClear();
|
|
|
|
onTooltipHelpTouchEnd(event, "mobile help");
|
|
expect(storeMock.mutations.hideTooltip).not.toHaveBeenCalled();
|
|
expect(storeMock.mutations.showTooltip).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("ignores the synthetic click after a mobile tap", () => {
|
|
storeMock.getters.isMobile.mockReturnValue(true);
|
|
const touchEvent = makePointerEvent({ clientX: 8, clientY: 9 });
|
|
const clickEvent = makePointerEvent({ clientX: 8, clientY: 9 });
|
|
|
|
onTooltipHelpTouchEnd(touchEvent, "touch help");
|
|
onTooltipHelpClick(clickEvent, "touch help");
|
|
|
|
expect(touchEvent.preventDefault).toHaveBeenCalled();
|
|
expect(clickEvent.preventDefault).toHaveBeenCalled();
|
|
expect(storeMock.mutations.showTooltip).toHaveBeenCalledTimes(1);
|
|
expect(shouldIgnoreOutsideTap()).toBe(true);
|
|
});
|
|
|
|
it("prevents default on desktop click", () => {
|
|
const event = makePointerEvent({ clientX: 12, clientY: 14 });
|
|
|
|
onTooltipHelpClick(event, "desktop help");
|
|
|
|
expect(event.preventDefault).toHaveBeenCalled();
|
|
expect(event.stopPropagation).toHaveBeenCalled();
|
|
expect(storeMock.mutations.showTooltip).toHaveBeenCalledWith({
|
|
content: "desktop help",
|
|
x: 12,
|
|
y: 14,
|
|
pointerEvents: false,
|
|
});
|
|
});
|
|
|
|
it("tracks visible tooltip content", () => {
|
|
storeMock.state.tooltip.show = true;
|
|
storeMock.state.tooltip.content = "visible";
|
|
expect(isTooltipContentVisible("visible")).toBe(true);
|
|
expect(isTooltipContentVisible("other")).toBe(false);
|
|
});
|
|
|
|
it("ignores hover-driven hide while a tap tooltip is open", () => {
|
|
storeMock.getters.isMobile.mockReturnValue(true);
|
|
onTooltipHelpTouchEnd(
|
|
makePointerEvent({ clientX: 1, clientY: 2 }),
|
|
"help",
|
|
);
|
|
storeMock.state.tooltip.show = true;
|
|
storeMock.state.tooltip.pointerEvents = true;
|
|
storeMock.mutations.hideTooltip.mockClear();
|
|
|
|
hideInteractiveTooltip();
|
|
expect(storeMock.mutations.hideTooltip).not.toHaveBeenCalled();
|
|
|
|
hideInteractiveTooltip(true);
|
|
expect(storeMock.mutations.hideTooltip).toHaveBeenCalled();
|
|
});
|
|
|
|
it("hides tooltip on scroll", () => {
|
|
storeMock.state.tooltip.show = true;
|
|
document.dispatchEvent(new Event("scroll"));
|
|
expect(storeMock.mutations.hideTooltip).toHaveBeenCalled();
|
|
});
|
|
|
|
it("hides tooltip on browser navigation", () => {
|
|
storeMock.state.tooltip.show = true;
|
|
window.dispatchEvent(new Event("popstate"));
|
|
expect(storeMock.mutations.hideTooltip).toHaveBeenCalled();
|
|
});
|
|
|
|
it("ignores synthetic mouseleave while a tap tooltip is open", () => {
|
|
storeMock.getters.isMobile.mockReturnValue(true);
|
|
onTooltipHelpTouchEnd(
|
|
makePointerEvent({ clientX: 1, clientY: 2 }),
|
|
"tap help",
|
|
);
|
|
storeMock.state.tooltip.show = true;
|
|
storeMock.state.tooltip.pointerEvents = true;
|
|
storeMock.mutations.hideTooltip.mockClear();
|
|
|
|
onTooltipHelpMouseLeave();
|
|
expect(storeMock.mutations.hideTooltip).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does not replace a tap tooltip with a hover tooltip on mouseenter", () => {
|
|
storeMock.getters.isMobile.mockReturnValue(true);
|
|
onTooltipHelpTouchEnd(
|
|
makePointerEvent({ clientX: 1, clientY: 2 }),
|
|
"tap help",
|
|
);
|
|
storeMock.state.tooltip.show = true;
|
|
storeMock.state.tooltip.pointerEvents = true;
|
|
storeMock.mutations.showTooltip.mockClear();
|
|
|
|
onTooltipHelpMouseEnter({ clientX: 3, clientY: 4 }, "hover help");
|
|
expect(storeMock.mutations.showTooltip).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("switches tap tooltip when tapping another icon", () => {
|
|
storeMock.getters.isMobile.mockReturnValue(true);
|
|
const firstEvent = makePointerEvent({ clientX: 5, clientY: 6 });
|
|
const secondEvent = makePointerEvent({ clientX: 20, clientY: 30 });
|
|
|
|
onTooltipHelpTouchEnd(firstEvent, "first help");
|
|
storeMock.state.tooltip.show = true;
|
|
storeMock.state.tooltip.content = "first help";
|
|
storeMock.state.tooltip.pointerEvents = true;
|
|
onTooltipHelpMouseLeave();
|
|
|
|
storeMock.mutations.showTooltip.mockClear();
|
|
storeMock.mutations.hideTooltip.mockClear();
|
|
|
|
onTooltipHelpTouchEnd(secondEvent, "second help");
|
|
storeMock.state.tooltip.show = true;
|
|
storeMock.state.tooltip.pointerEvents = true;
|
|
onTooltipHelpMouseLeave();
|
|
|
|
expect(firstEvent.preventDefault).toHaveBeenCalled();
|
|
expect(secondEvent.preventDefault).toHaveBeenCalled();
|
|
expect(storeMock.mutations.hideTooltip).not.toHaveBeenCalled();
|
|
expect(storeMock.mutations.showTooltip).toHaveBeenCalledWith({
|
|
content: "second help",
|
|
x: 20,
|
|
y: 30,
|
|
pointerEvents: true,
|
|
});
|
|
});
|
|
|
|
it("switches tooltip when tapping a different help icon", () => {
|
|
storeMock.getters.isMobile.mockReturnValue(true);
|
|
const firstEvent = makePointerEvent({ clientX: 5, clientY: 6 });
|
|
const secondEvent = makePointerEvent({ clientX: 20, clientY: 30 });
|
|
|
|
onTooltipHelpTouchEnd(firstEvent, "first help");
|
|
storeMock.state.tooltip.show = true;
|
|
storeMock.state.tooltip.content = "first help";
|
|
storeMock.state.tooltip.pointerEvents = true;
|
|
storeMock.mutations.showTooltip.mockClear();
|
|
|
|
onTooltipHelpTouchEnd(secondEvent, "second help");
|
|
storeMock.state.tooltip.show = true;
|
|
storeMock.state.tooltip.pointerEvents = true;
|
|
storeMock.mutations.hideTooltip.mockClear();
|
|
hideInteractiveTooltip();
|
|
expect(storeMock.mutations.hideTooltip).not.toHaveBeenCalled();
|
|
expect(storeMock.mutations.showTooltip).toHaveBeenCalledWith({
|
|
content: "second help",
|
|
x: 20,
|
|
y: 30,
|
|
pointerEvents: true,
|
|
});
|
|
});
|
|
|
|
it("showHoverTooltip always uses hover mode", () => {
|
|
storeMock.getters.isMobile.mockReturnValue(true);
|
|
showHoverTooltip("hover help", { clientX: 3, clientY: 4 });
|
|
expect(storeMock.mutations.showTooltip).toHaveBeenCalledWith({
|
|
content: "hover help",
|
|
x: 3,
|
|
y: 4,
|
|
pointerEvents: false,
|
|
});
|
|
});
|
|
});
|