test(e4): readSelectedId ?pl guard, useUrlMirror dep-keying, usePref flush
The remaining E4 deep-review deferred gaps, in one sprint: - readSelectedId split into playlistsInit.ts (react-refresh-clean, testable) — ?pl accepted only as a positive integer; empty/0/negative/fractional/non-numeric fall back to the stored selection; null when neither is present. - useUrlMirror: mounts write the param, a same-serialized rerender does NOT re-fire (stable dep), a null value clears the key, other params preserved. - usePref: a pending debounced write is FLUSHED on unmount (not dropped); an unmount with nothing pending stays silent. 20 new test cases; full FE gate green (tsc/eslint/prettier/knip, 309 vitest).
This commit is contained in:
@@ -155,6 +155,29 @@ describe("usePref", () => {
|
||||
expect(savePrefs).toHaveBeenCalledTimes(1);
|
||||
expect(savePrefs).toHaveBeenCalledWith({ vol: 30 });
|
||||
});
|
||||
|
||||
it("flushes a pending debounced write on unmount instead of dropping it", () => {
|
||||
const qc = makeClient();
|
||||
seedMe(qc, {});
|
||||
const { result, unmount } = renderHook(() => usePref<number>("vol", 0, { debounceMs: 300 }), {
|
||||
wrapper: wrapperFor(qc),
|
||||
});
|
||||
act(() => result.current[1](42));
|
||||
expect(savePrefs).not.toHaveBeenCalled(); // still inside the debounce window
|
||||
unmount(); // cleanup must flush the pending write, not lose it
|
||||
expect(savePrefs).toHaveBeenCalledTimes(1);
|
||||
expect(savePrefs).toHaveBeenCalledWith({ vol: 42 });
|
||||
});
|
||||
|
||||
it("unmount with no pending write does not call savePrefs", () => {
|
||||
const qc = makeClient();
|
||||
seedMe(qc, {});
|
||||
const { unmount } = renderHook(() => usePref<number>("vol", 0, { debounceMs: 300 }), {
|
||||
wrapper: wrapperFor(qc),
|
||||
});
|
||||
unmount();
|
||||
expect(savePrefs).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
// @vitest-environment jsdom
|
||||
//
|
||||
// useUrlMirror: mirror a set of nav params onto the CURRENT history entry (replaceState) whenever they
|
||||
// change. Backfills the E4 deep-review's deferred coverage — the effect keys on the SERIALIZED updates
|
||||
// so an inline object literal (new identity every render) only fires when a param actually changes; a
|
||||
// null value clears a key; and every other param on the URL is preserved.
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
||||
import { renderHook } from "@testing-library/react";
|
||||
import { useUrlMirror } from "./useUrlMirror";
|
||||
|
||||
const setUrl = (search: string) => window.history.replaceState(null, "", search || "/");
|
||||
let spy: ReturnType<typeof vi.spyOn>;
|
||||
|
||||
beforeEach(() => {
|
||||
setUrl("/");
|
||||
spy = vi.spyOn(window.history, "replaceState");
|
||||
});
|
||||
afterEach(() => spy.mockRestore());
|
||||
|
||||
describe("useUrlMirror", () => {
|
||||
it("writes the param onto the current entry on mount", () => {
|
||||
renderHook(() => useUrlMirror({ pl: "5" }));
|
||||
expect(window.location.search).toBe("?pl=5");
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("does not re-fire when the serialized updates are unchanged (stable dep)", () => {
|
||||
const { rerender } = renderHook((p: Record<string, string | null>) => useUrlMirror(p), {
|
||||
initialProps: { pl: "5" } as Record<string, string | null>,
|
||||
});
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
rerender({ pl: "5" }); // new object identity, same serialized value
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("clears a key when its value turns null", () => {
|
||||
const { rerender } = renderHook((p: Record<string, string | null>) => useUrlMirror(p), {
|
||||
initialProps: { pl: "5" } as Record<string, string | null>,
|
||||
});
|
||||
expect(window.location.search).toBe("?pl=5");
|
||||
rerender({ pl: null });
|
||||
expect(window.location.search).toBe("");
|
||||
expect(spy).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("preserves other params already on the URL", () => {
|
||||
setUrl("?page=playlists");
|
||||
renderHook(() => useUrlMirror({ pl: "5" }));
|
||||
expect(window.location.search).toBe("?page=playlists&pl=5");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user