88 lines
3.1 KiB
TypeScript
88 lines
3.1 KiB
TypeScript
// @vitest-environment jsdom
|
|
import "@testing-library/jest-dom/vitest";
|
|
import { act, fireEvent, render, screen } from "@testing-library/react";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { SidebarWithSections } from ".";
|
|
|
|
describe("SidebarWithSections navigation motion", () => {
|
|
it("uses caller-provided translated names for logo navigation", () => {
|
|
const onLogoClick = vi.fn();
|
|
const { rerender } = render(
|
|
<SidebarWithSections logoHref="/dashboard" logoLabel="Übersicht" />,
|
|
);
|
|
|
|
expect(screen.getByRole("link", { name: "Übersicht" })).toHaveAttribute("href", "/dashboard");
|
|
|
|
rerender(
|
|
<SidebarWithSections logoLabel="Zur Übersicht" onLogoClick={onLogoClick} />,
|
|
);
|
|
fireEvent.click(screen.getByRole("button", { name: "Zur Übersicht" }));
|
|
expect(onLogoClick).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("smoothly transitions the full item scale in both directions", () => {
|
|
const { container } = render(<SidebarWithSections.NavItem>Dashboard</SidebarWithSections.NavItem>);
|
|
const itemSurface = container.querySelector('[data-slot="app-sidebar-nav-item-surface"]');
|
|
|
|
expect(itemSurface).toHaveClass(
|
|
"pointer-events-none",
|
|
"scale-100",
|
|
"transition-[background-color,scale]",
|
|
"duration-150",
|
|
"ease-[cubic-bezier(0.4,0,0.2,1)]",
|
|
"motion-reduce:transition-none",
|
|
"group-hover/2713e17b:scale-[1.065]",
|
|
"data-[sidebar-sweep-hover=true]:scale-[1.065]",
|
|
);
|
|
});
|
|
|
|
it("pulses items crossed between fast pointer samples", () => {
|
|
vi.useFakeTimers();
|
|
try {
|
|
const { container } = render(
|
|
<SidebarWithSections>
|
|
<SidebarWithSections.NavItem>CRM</SidebarWithSections.NavItem>
|
|
<SidebarWithSections.NavItem>Leads</SidebarWithSections.NavItem>
|
|
<SidebarWithSections.NavItem>Boards</SidebarWithSections.NavItem>
|
|
</SidebarWithSections>,
|
|
);
|
|
const sidebar = container.querySelector("nav");
|
|
const items = container.querySelectorAll<HTMLElement>('[data-sidebar-hover-item="true"]');
|
|
const surfaces = container.querySelectorAll<HTMLElement>('[data-sidebar-hover-surface="true"]');
|
|
|
|
expect(sidebar).not.toBeNull();
|
|
expect(items).toHaveLength(3);
|
|
items.forEach((item, index) => {
|
|
const top = index * 40;
|
|
vi.spyOn(item, "getBoundingClientRect").mockReturnValue({
|
|
top,
|
|
bottom: top + 40,
|
|
left: 0,
|
|
right: 200,
|
|
width: 200,
|
|
height: 40,
|
|
x: 0,
|
|
y: top,
|
|
toJSON: () => ({}),
|
|
});
|
|
});
|
|
|
|
const movePointerTo = (clientY: number) => {
|
|
const event = new Event("pointermove", { bubbles: true });
|
|
Object.defineProperty(event, "clientY", { value: clientY });
|
|
fireEvent(sidebar!, event);
|
|
};
|
|
|
|
movePointerTo(20);
|
|
movePointerTo(100);
|
|
|
|
expect(surfaces[1]).toHaveAttribute("data-sidebar-sweep-hover", "true");
|
|
act(() => vi.advanceTimersByTime(150));
|
|
expect(surfaces[1]).not.toHaveAttribute("data-sidebar-sweep-hover");
|
|
} finally {
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
});
|