|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { renderHook } from "@testing-library/react"; |
| 3 | +import { useRef } from "react"; |
| 4 | +import type { ToolId } from "@app/types/toolId"; |
| 5 | + |
| 6 | +const h = vi.hoisted(() => ({ |
| 7 | + updateToolRoute: vi.fn(), |
| 8 | + clearToolRoute: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("@app/utils/urlRouting", () => ({ |
| 12 | + parseToolRoute: () => ({ workbench: "fileEditor", toolId: null }), |
| 13 | + updateToolRoute: h.updateToolRoute, |
| 14 | + clearToolRoute: h.clearToolRoute, |
| 15 | +})); |
| 16 | +vi.mock("@app/utils/scarfTracking", () => ({ firePixel: vi.fn() })); |
| 17 | +vi.mock("@app/contexts/AppConfigContext", () => ({ |
| 18 | + useAppConfig: () => ({ config: { premiumEnabled: true } }), |
| 19 | +})); |
| 20 | + |
| 21 | +import { useNavigationUrlSync } from "@app/hooks/useUrlSync"; |
| 22 | + |
| 23 | +const registry = { |
| 24 | + read: { name: "Read", workbench: "viewer" }, |
| 25 | + compress: { name: "Compress", workbench: "fileEditor" }, |
| 26 | +} as never; |
| 27 | + |
| 28 | +/** Drives the hook the way ToolWorkflowContext does, with a startup marker. */ |
| 29 | +function useHarness(selectedTool: ToolId | null, startupTool: ToolId | null) { |
| 30 | + const ref = useRef<ToolId | null>(startupTool); |
| 31 | + useNavigationUrlSync(selectedTool, vi.fn(), vi.fn(), registry, true, ref); |
| 32 | + return ref; |
| 33 | +} |
| 34 | + |
| 35 | +describe("useNavigationUrlSync — startup-view selections", () => { |
| 36 | + beforeEach(() => h.updateToolRoute.mockClear()); |
| 37 | + |
| 38 | + // The default-startup-view preference selects a tool to change the *view*. |
| 39 | + // Writing it to the address turned every visit to /editor into /read. |
| 40 | + it("never writes the URL for the startup-applied tool", () => { |
| 41 | + const { rerender } = renderHook( |
| 42 | + ({ tool }: { tool: ToolId | null }) => useHarness(tool, "read" as ToolId), |
| 43 | + { initialProps: { tool: null as ToolId | null } }, |
| 44 | + ); |
| 45 | + rerender({ tool: "read" as ToolId }); |
| 46 | + expect(h.updateToolRoute).not.toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + // The effect re-runs whenever the registry identity changes, so a marker that |
| 50 | + // was consumed on first sight let the second run write /read anyway. |
| 51 | + it("survives a re-run for the same tool", () => { |
| 52 | + const { rerender } = renderHook( |
| 53 | + ({ tool }: { tool: ToolId | null }) => useHarness(tool, "read" as ToolId), |
| 54 | + { initialProps: { tool: null as ToolId | null } }, |
| 55 | + ); |
| 56 | + rerender({ tool: "read" as ToolId }); |
| 57 | + rerender({ tool: "read" as ToolId }); |
| 58 | + rerender({ tool: "read" as ToolId }); |
| 59 | + expect(h.updateToolRoute).not.toHaveBeenCalled(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("still writes the URL when the user picks a different tool", () => { |
| 63 | + const { rerender } = renderHook( |
| 64 | + ({ tool }: { tool: ToolId | null }) => useHarness(tool, "read" as ToolId), |
| 65 | + { initialProps: { tool: null as ToolId | null } }, |
| 66 | + ); |
| 67 | + rerender({ tool: "read" as ToolId }); |
| 68 | + rerender({ tool: "compress" as ToolId }); |
| 69 | + expect(h.updateToolRoute).toHaveBeenCalledWith("compress", registry, false); |
| 70 | + }); |
| 71 | + |
| 72 | + it("writes the URL for a tool chosen without a startup marker", () => { |
| 73 | + const { rerender } = renderHook( |
| 74 | + ({ tool }: { tool: ToolId | null }) => useHarness(tool, null), |
| 75 | + { initialProps: { tool: null as ToolId | null } }, |
| 76 | + ); |
| 77 | + rerender({ tool: "read" as ToolId }); |
| 78 | + expect(h.updateToolRoute).toHaveBeenCalledWith("read", registry, false); |
| 79 | + }); |
| 80 | +}); |
0 commit comments