Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/components/app/RouteContentTransition.test.tsx
T

137 lines
4.7 KiB
TypeScript

import "@testing-library/jest-dom";
import { fireEvent, render, screen } from "@testing-library/react";
import { Link, MemoryRouter, Route, Routes } from "react-router";
import { afterEach, describe, expect, it, vi } from "vitest";
import { RouteContentTransition } from "./RouteContentTransition";
const originalAnimate = HTMLElement.prototype.animate;
const originalMatchMedia = window.matchMedia;
function renderTransition() {
render(
<MemoryRouter initialEntries={["/first"]}>
<Link to="/second">Next</Link>
<Routes>
<Route
path="*"
element={
<RouteContentTransition>
<span>Content</span>
</RouteContentTransition>
}
/>
</Routes>
</MemoryRouter>
);
}
describe("RouteContentTransition", () => {
afterEach(() => {
HTMLElement.prototype.animate = originalAnimate;
window.matchMedia = originalMatchMedia;
vi.restoreAllMocks();
});
it("animates the existing content wrapper only after a pathname change", () => {
const cancel = vi.fn();
const animate = vi.fn(() => ({ cancel }) as unknown as Animation);
HTMLElement.prototype.animate = animate;
window.matchMedia = vi.fn(() => ({ matches: false }) as MediaQueryList);
renderTransition();
expect(animate).not.toHaveBeenCalled();
const wrapper = document.querySelector('[data-route-content-transition="true"]');
fireEvent.click(screen.getByRole("link", { name: "Next" }));
expect(document.querySelector('[data-route-content-transition="true"]')).toBe(wrapper);
expect(animate).toHaveBeenCalledWith(
[
{ opacity: 0.15, filter: "blur(5px)" },
{ opacity: 1, filter: "blur(0px)" },
],
expect.objectContaining({ duration: 420, easing: "cubic-bezier(0.22, 1, 0.36, 1)" })
);
});
it("does not animate when reduced motion is requested", () => {
const animate = vi.fn(() => ({ cancel: vi.fn() }) as unknown as Animation);
HTMLElement.prototype.animate = animate;
window.matchMedia = vi.fn(() => ({ matches: true }) as MediaQueryList);
renderTransition();
fireEvent.click(screen.getByRole("link", { name: "Next" }));
expect(animate).not.toHaveBeenCalled();
});
it("does not animate filter-only navigation", () => {
const animate = vi.fn(() => ({ cancel: vi.fn() }) as unknown as Animation);
HTMLElement.prototype.animate = animate;
window.matchMedia = vi.fn(() => ({ matches: false }) as MediaQueryList);
render(
<MemoryRouter initialEntries={["/first"]}>
<Link to="/first?q=compact">Change filter</Link>
<RouteContentTransition>
<span>Content</span>
</RouteContentTransition>
</MemoryRouter>
);
fireEvent.click(screen.getByRole("link", { name: "Change filter" }));
expect(animate).not.toHaveBeenCalled();
});
it("animates structural tab and view changes on the same pathname", () => {
const animate = vi.fn(() => ({ cancel: vi.fn() }) as unknown as Animation);
HTMLElement.prototype.animate = animate;
window.matchMedia = vi.fn(() => ({ matches: false }) as MediaQueryList);
render(
<MemoryRouter initialEntries={["/first?tab=overview"]}>
<Link to="/first?tab=details">Open details tab</Link>
<RouteContentTransition>
<span>Content</span>
</RouteContentTransition>
</MemoryRouter>
);
fireEvent.click(screen.getByRole("link", { name: "Open details tab" }));
expect(animate).toHaveBeenCalledOnce();
});
it("animates marked page content without touching a stable inner sidebar", () => {
const animate = vi.fn(() => ({ cancel: vi.fn() }) as unknown as Animation);
HTMLElement.prototype.animate = animate;
window.matchMedia = vi.fn(() => ({ matches: false }) as MediaQueryList);
render(
<MemoryRouter initialEntries={["/first"]}>
<Link to="/second">Next inner page</Link>
<Routes>
<Route
path="*"
element={
<RouteContentTransition>
<aside data-testid="inner-sidebar">Inner sidebar</aside>
<section data-testid="page-content" data-route-transition-content="true">
Page content
</section>
</RouteContentTransition>
}
/>
</Routes>
</MemoryRouter>
);
const wrapper = document.querySelector<HTMLElement>('[data-route-content-transition="true"]');
const pageContent = screen.getByTestId("page-content");
fireEvent.click(screen.getByRole("link", { name: "Next inner page" }));
expect(animate).toHaveBeenCalledOnce();
expect(animate.mock.contexts[0]).toBe(pageContent);
expect(animate.mock.contexts[0]).not.toBe(wrapper);
});
});