#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
import { beforeEach, describe, expect, it, vi } from "vitest";
import { renderHook } from "@testing-library/react";
import { useRef, type ReactNode } from "react";
import { MemoryRouter } from "react-router-dom";
import type { ToolId } from "@editor/types/toolId";
const h = vi.hoisted(() => ({
toolRoute: vi.fn(() => "/compress"),
editorHomeRoute: vi.fn(() => null),
}));
vi.mock("@editor/utils/urlRouting", () => ({
parseToolRoute: () => ({ workbench: "fileEditor", toolId: null }),
toolRoute: h.toolRoute,
editorHomeRoute: h.editorHomeRoute,
}));
vi.mock("@editor/utils/scarfTracking", () => ({ firePixel: vi.fn() }));
vi.mock("@editor/contexts/AppConfigContext", () => ({
useAppConfig: () => ({ config: { premiumEnabled: true } }),
}));
import { useNavigationUrlSync } from "@editor/hooks/useUrlSync";
const registry = {
read: { name: "Read", workbench: "viewer" },
compress: { name: "Compress", workbench: "fileEditor" },
} as never;
/** The hook navigates through the router, so it needs one to render under. */
const wrapper = ({ children }: { children: ReactNode }) => (
<MemoryRouter>{children}</MemoryRouter>
);
/** Drives the hook the way ToolWorkflowContext does, with a startup marker. */
function useHarness(selectedTool: ToolId | null, startupTool: ToolId | null) {
const ref = useRef<ToolId | null>(startupTool);
useNavigationUrlSync(selectedTool, vi.fn(), vi.fn(), registry, true, ref);
return ref;
}
describe("useNavigationUrlSync — startup-view selections", () => {
beforeEach(() => h.toolRoute.mockClear());
// The default-startup-view preference selects a tool to change the *view*.
// Writing it to the address turned every visit to /editor into /read.
it("never writes the URL for the startup-applied tool", () => {
const { rerender } = renderHook(
({ tool }: { tool: ToolId | null }) => useHarness(tool, "read"),
{ initialProps: { tool: null as ToolId | null }, wrapper },
);
rerender({ tool: "read" as ToolId });
expect(h.toolRoute).not.toHaveBeenCalled();
});
// The effect re-runs whenever the registry identity changes, so a marker that
// was consumed on first sight let the second run write /read anyway.
it("survives a re-run for the same tool", () => {
const { rerender } = renderHook(
({ tool }: { tool: ToolId | null }) => useHarness(tool, "read"),
{ initialProps: { tool: null as ToolId | null }, wrapper },
);
rerender({ tool: "read" as ToolId });
rerender({ tool: "read" as ToolId });
rerender({ tool: "read" as ToolId });
expect(h.toolRoute).not.toHaveBeenCalled();
});
it("still writes the URL when the user picks a different tool", () => {
const { rerender } = renderHook(
({ tool }: { tool: ToolId | null }) => useHarness(tool, "read"),
{ initialProps: { tool: null as ToolId | null }, wrapper },
);
rerender({ tool: "read" as ToolId });
rerender({ tool: "compress" as ToolId });
expect(h.toolRoute).toHaveBeenCalledWith(
"compress",
registry,
expect.anything(),
);
});
it("writes the URL for a tool chosen without a startup marker", () => {
const { rerender } = renderHook(
({ tool }: { tool: ToolId | null }) => useHarness(tool, null),
{ initialProps: { tool: null as ToolId | null }, wrapper },
);
rerender({ tool: "read" as ToolId });
expect(h.toolRoute).toHaveBeenCalledWith(
"read",
registry,
expect.anything(),
);
});
});