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:
@@ -1,8 +1,9 @@
|
||||
import { createContext, useCallback, useContext, useMemo, useState, type ReactNode } from "react";
|
||||
import { getAccountRaw, LS, setAccountRaw } from "../lib/storage";
|
||||
import { LS, setAccountRaw } from "../lib/storage";
|
||||
import { useAccountRehydrate } from "../lib/useAccountRehydrate";
|
||||
import { useUrlMirror } from "../lib/useUrlMirror";
|
||||
import { useNavigation } from "./NavigationProvider";
|
||||
import { readSelectedId } from "./playlistsInit";
|
||||
|
||||
// Owns the Playlists module's shared local state — the rail's name filter and the selected playlist
|
||||
// — lifted out of the App god component so the header search box, the PlaylistsRail, and the
|
||||
@@ -38,17 +39,6 @@ export function usePlaylistsActions(): PlaylistsActions {
|
||||
return useContext(ActionsContext);
|
||||
}
|
||||
|
||||
function readSelectedId(): number | null {
|
||||
// A ?pl bookmark wins over the stored selection so a pasted link opens that playlist. Accept only a
|
||||
// positive integer id — a bare "?pl=" gives Number("")===0 (finite!) and "?pl=1.5" a fractional id,
|
||||
// both of which would select a non-existent playlist.
|
||||
const fromUrl = new URLSearchParams(window.location.search).get("pl");
|
||||
const n = fromUrl ? Number(fromUrl) : NaN;
|
||||
if (Number.isInteger(n) && n > 0) return n;
|
||||
const s = getAccountRaw(LS.playlist);
|
||||
return s ? Number(s) : null;
|
||||
}
|
||||
|
||||
export function PlaylistsProvider({ children }: { children: ReactNode }) {
|
||||
const [search, setSearchState] = useState("");
|
||||
const [selectedId, setSelectedIdState] = useState<number | null>(readSelectedId);
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
// @vitest-environment jsdom
|
||||
//
|
||||
// readSelectedId: the Playlists selected-id seed. Backfills the E4 deep-review's deferred ?pl guard
|
||||
// coverage — a ?pl bookmark wins over the stored selection but only when it's a positive integer;
|
||||
// anything else (empty / zero / negative / fractional / non-numeric) falls back to the stored id.
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
|
||||
const { stored } = vi.hoisted(() => ({ stored: { v: null as string | null } }));
|
||||
vi.mock("../lib/storage", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("../lib/storage")>();
|
||||
return { ...actual, getAccountRaw: () => stored.v };
|
||||
});
|
||||
|
||||
import { readSelectedId } from "./playlistsInit";
|
||||
|
||||
const setUrl = (search: string) => window.history.replaceState(null, "", search || "/");
|
||||
|
||||
beforeEach(() => {
|
||||
setUrl("/");
|
||||
stored.v = null;
|
||||
});
|
||||
|
||||
describe("readSelectedId ?pl guard", () => {
|
||||
it("accepts a positive-integer ?pl", () => {
|
||||
setUrl("?pl=5");
|
||||
expect(readSelectedId()).toBe(5);
|
||||
});
|
||||
|
||||
it("a ?pl bookmark wins over the stored selection", () => {
|
||||
setUrl("?pl=5");
|
||||
stored.v = "7";
|
||||
expect(readSelectedId()).toBe(5);
|
||||
});
|
||||
|
||||
it.each(["", "0", "-2", "1.5", "abc"])("rejects ?pl=%s and falls back to stored", (raw) => {
|
||||
setUrl(`?pl=${raw}`);
|
||||
stored.v = "7";
|
||||
expect(readSelectedId()).toBe(7);
|
||||
});
|
||||
|
||||
it("rejects a bad ?pl and returns null when there is no stored selection", () => {
|
||||
setUrl("?pl=0");
|
||||
expect(readSelectedId()).toBeNull();
|
||||
});
|
||||
|
||||
it("uses the stored selection when there is no ?pl", () => {
|
||||
stored.v = "9";
|
||||
expect(readSelectedId()).toBe(9);
|
||||
});
|
||||
|
||||
it("returns null when neither ?pl nor a stored selection is present", () => {
|
||||
expect(readSelectedId()).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
import { getAccountRaw, LS } from "../lib/storage";
|
||||
|
||||
// The Playlists module's selected-id seed, split out of PlaylistsProvider so its ?pl guard is
|
||||
// unit-testable and the provider stays react-refresh-clean (component-only exports).
|
||||
export function readSelectedId(): number | null {
|
||||
// A ?pl bookmark wins over the stored selection so a pasted link opens that playlist. Accept only a
|
||||
// positive integer id — a bare "?pl=" gives Number("")===0 (finite!) and "?pl=1.5" a fractional id,
|
||||
// both of which would select a non-existent playlist.
|
||||
const fromUrl = new URLSearchParams(window.location.search).get("pl");
|
||||
const n = fromUrl ? Number(fromUrl) : NaN;
|
||||
if (Number.isInteger(n) && n > 0) return n;
|
||||
const s = getAccountRaw(LS.playlist);
|
||||
return s ? Number(s) : null;
|
||||
}
|
||||
Reference in New Issue
Block a user