import { useMutation } from "@tanstack/react-query"; import { act, render, screen, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { afterEach, describe, expect, it, vi } from "vitest"; import { readFileSync } from "node:fs"; import path from "node:path"; import { Providers } from "./providers"; vi.mock("@/lib/auth/auth-context", () => ({ AuthProvider: ({ children }: { children: React.ReactNode }) => <>{children}, })); function Probe({ mutationFn }: { mutationFn: () => Promise }) { const mutation = useMutation({ mutationFn }); return ; } describe("Providers", () => { afterEach(() => { vi.restoreAllMocks(); // TanStack's onlineManager tracks the browser events, so restore it too. act(() => { window.dispatchEvent(new Event("online")); }); }); it("runs mutations while navigator.onLine is false", async () => { vi.spyOn(window.navigator, "onLine", "get").mockReturnValue(false); const mutationFn = vi.fn().mockResolvedValue("ok"); render( , ); // onlineManager (listening once mounted) only learns about airplane mode through the window event. act(() => { window.dispatchEvent(new Event("offline")); }); await userEvent.click(screen.getByRole("button", { name: "Speichern" })); await waitFor(() => expect(mutationFn).toHaveBeenCalledTimes(1)); }); it("does not apply the motion level itself any more (the inline script does)", () => { document.documentElement.removeAttribute("data-motion"); window.localStorage.setItem("apicore.motion", "off"); render(

x

, ); expect(document.documentElement).not.toHaveAttribute("data-motion"); window.localStorage.clear(); }); }); describe("theme handling", () => { it("the toaster follows resolvedTheme and next-themes is not imported", () => { const toaster = readFileSync(path.resolve(__dirname, "../components/ui/sonner.tsx"), "utf8"); expect(toaster).toContain("resolvedTheme"); expect(toaster).not.toContain("next-themes"); const pkg = JSON.parse(readFileSync(path.resolve(__dirname, "../../package.json"), "utf8")); expect(pkg.dependencies["next-themes"]).toBeUndefined(); }); });