hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { createRoot, createSignal } from "solid-js"; |
| 2 | import { |
| 3 | createUrlEnum, |
| 4 | createUrlString, |
| 5 | decodeSort, |
| 6 | encodePageIndex, |
| 7 | encodeSort, |
| 8 | listParamKeys, |
| 9 | patchSearch, |
| 10 | readEnum, |
| 11 | readPageIndex, |
| 12 | readString, |
| 13 | } from "@/lib/url-state"; |
| 14 | |
| 15 | const router = vi.hoisted(() => ({ |
| 16 | navigate: vi.fn(), |
| 17 | location: null as null | (() => { pathname: string; search: Record<string, unknown>; hash: string }), |
| 18 | })); |
| 19 | |
| 20 | vi.mock("@tanstack/solid-router", () => ({ |
| 21 | useNavigate: () => router.navigate, |
| 22 | useLocation: () => router.location, |
| 23 | })); |
| 24 | |
| 25 | afterEach(() => vi.clearAllMocks()); |
| 26 | |
| 27 | test("patchSearch merges and drops blank values", () => { |
| 28 | expect(patchSearch({ kind: "club", q: "alg", page: 2 }, { q: "", page: undefined, sort: "title.asc" })).toEqual({ |
| 29 | kind: "club", |
| 30 | sort: "title.asc", |
| 31 | }); |
| 32 | }); |
| 33 | |
| 34 | test("readString turns router-parsed scalars back into text", () => { |
| 35 | expect(readString("abc")).toBe("abc"); |
| 36 | expect(readString(12)).toBe("12"); |
| 37 | expect(readString(true)).toBe("true"); |
| 38 | expect(readString({ a: 1 })).toBe(""); |
| 39 | expect(readString(undefined)).toBe(""); |
| 40 | }); |
| 41 | |
| 42 | test("pages are one-based in the URL and zero-based in the table", () => { |
| 43 | expect(readPageIndex(3)).toBe(2); |
| 44 | expect(readPageIndex("2")).toBe(1); |
| 45 | expect(readPageIndex(1)).toBe(0); |
| 46 | expect(readPageIndex(0)).toBe(0); |
| 47 | expect(readPageIndex(-4)).toBe(0); |
| 48 | expect(readPageIndex("x")).toBe(0); |
| 49 | expect(readPageIndex(undefined)).toBe(0); |
| 50 | expect(encodePageIndex(0)).toBeUndefined(); |
| 51 | expect(encodePageIndex(2)).toBe(3); |
| 52 | }); |
| 53 | |
| 54 | test("sort round-trips through one param", () => { |
| 55 | expect(decodeSort("starts_at.desc")).toEqual([{ id: "starts_at", desc: true }]); |
| 56 | expect(decodeSort("title.asc")).toEqual([{ id: "title", desc: false }]); |
| 57 | expect(decodeSort("title")).toEqual([{ id: "title", desc: false }]); |
| 58 | expect(decodeSort("a.b.desc")).toEqual([{ id: "a.b", desc: true }]); |
| 59 | expect(decodeSort("")).toEqual([]); |
| 60 | expect(decodeSort(undefined)).toEqual([]); |
| 61 | expect(encodeSort([{ id: "starts_at", desc: true }])).toBe("starts_at.desc"); |
| 62 | expect(encodeSort([])).toBeUndefined(); |
| 63 | }); |
| 64 | |
| 65 | test("readEnum falls back on anything outside the set", () => { |
| 66 | expect(readEnum("week", ["day", "week", "month"] as const, "month")).toBe("week"); |
| 67 | expect(readEnum("year", ["day", "week", "month"] as const, "month")).toBe("month"); |
| 68 | }); |
| 69 | |
| 70 | test("listParamKeys prefixes keys for a second list on the page", () => { |
| 71 | expect(listParamKeys()).toEqual({ q: "q", page: "page", sort: "sort" }); |
| 72 | expect(listParamKeys("roster")).toEqual({ q: "roster.q", page: "roster.page", sort: "roster.sort" }); |
| 73 | }); |
| 74 | |
| 75 | test("a URL param starts from the URL, writes with replace, and follows outside changes", () => { |
| 76 | const [location, setLocation] = createSignal({ pathname: "/exams", search: { tab: "draft", q: "alg" } as Record<string, unknown>, hash: "" }); |
| 77 | router.location = location; |
| 78 | |
| 79 | // Effects start once the root has been built, as they do in a mounted page. |
| 80 | const { tab, setTab, query, setQuery, dispose } = createRoot((dispose) => { |
| 81 | const [tab, setTab] = createUrlEnum("tab", ["all", "draft"] as const, "all"); |
| 82 | const [query, setQuery] = createUrlString("q"); |
| 83 | return { tab, setTab, query, setQuery, dispose }; |
| 84 | }); |
| 85 | expect(tab()).toBe("draft"); |
| 86 | expect(query()).toBe("alg"); |
| 87 | |
| 88 | setQuery("algebra"); |
| 89 | expect(query()).toBe("algebra"); |
| 90 | const call = router.navigate.mock.calls.at(-1)?.[0]; |
| 91 | expect(call).toMatchObject({ to: "/exams", replace: true, resetScroll: false }); |
| 92 | expect(call.search({ tab: "draft", q: "alg" })).toEqual({ tab: "draft", q: "algebra" }); |
| 93 | |
| 94 | // The default value leaves the URL bare. |
| 95 | setTab("all"); |
| 96 | expect(router.navigate.mock.calls.at(-1)?.[0].search({ tab: "draft" })).toEqual({}); |
| 97 | |
| 98 | // The router catches up with our own writes: nothing is pulled back. |
| 99 | setLocation({ pathname: "/exams", search: { tab: "draft", q: "algebra" }, hash: "" }); |
| 100 | expect(query()).toBe("algebra"); |
| 101 | setLocation({ pathname: "/exams", search: { q: "algebra" }, hash: "" }); |
| 102 | expect(tab()).toBe("all"); |
| 103 | |
| 104 | // A change from elsewhere (a nav link to the bare route) flows back in. |
| 105 | setLocation({ pathname: "/exams", search: {}, hash: "" }); |
| 106 | expect(query()).toBe(""); |
| 107 | dispose(); |
| 108 | }); |