Open History to see the last commit
1import {
2 createMemoryHistory,
3 createRootRoute,
4 createRoute,
5 createRouter,
6} from "@tanstack/react-router";
7import { describe, expect, it } from "vite-plus/test";
8
9import { Route as PullRequestsRoute } from "../../routes/_chat.pull-requests";
10
11function matchedSearch(url: string) {
12 const root = createRootRoute();
13 const chat = createRoute({ getParentRoute: () => root, id: "_chat" });
14 const pullRequests = createRoute({
15 getParentRoute: () => chat,
16 path: "pull-requests",
17 validateSearch: PullRequestsRoute.options.validateSearch,
18 });
19 const router = createRouter({
20 routeTree: root.addChildren([chat.addChildren([pullRequests])]),
21 history: createMemoryHistory({ initialEntries: [url] }),
22 });
23 // useSearch reads matched-route search, which can differ from URL search.
24 return router.matchRoutes(router.latestLocation).at(-1)?.search as
25 | Record<string, unknown>
26 | undefined;
27}
28
29describe("pull requests route search", () => {
30 it("drops a q the URL parser turned into a number", () => {
31 expect(matchedSearch("/pull-requests?q=4089")?.q).toBeUndefined();
32 expect(matchedSearch("/pull-requests?q=true")?.q).toBeUndefined();
33 });
34
35 it("keeps a q that is text, including quoted digits", () => {
36 expect(matchedSearch("/pull-requests?q=fix")?.q).toBe("fix");
37 expect(matchedSearch("/pull-requests?q=%224089%22")?.q).toBe("4089");
38 });
39
40 it("drops an author or labels the URL parser turned into a number", () => {
41 expect(matchedSearch("/pull-requests?author=4089")?.author).toBeUndefined();
42 expect(matchedSearch("/pull-requests?labels=4089")?.labels).toBeUndefined();
43 });
44
45 it("drops a repository, host or selectedHost the URL parser turned into a non-string", () => {
46 expect(matchedSearch("/pull-requests?repository=123")?.repository).toBeUndefined();
47 expect(matchedSearch("/pull-requests?repository=null")?.repository).toBeUndefined();
48 expect(matchedSearch("/pull-requests?repository=a%2Fb&host=8080&selectedHost=1")).toMatchObject(
49 {
50 repository: "a/b",
51 host: undefined,
52 selectedHost: undefined,
53 },
54 );
55 });
56
57 it.each([
58 ["surrogate pair", 199, "😀"],
59 ["flag", 198, "🇺🇸"],
60 ["joined emoji", 198, "👨👩👧👦"],
61 ])(
62 "cuts a long search, host, author, and label before a %s crossing the limit",
63 (_, prefixLength, cluster) => {
64 const q = "a".repeat(prefixLength);
65 const host = "h".repeat(prefixLength);
66 const author = "b".repeat(prefixLength);
67 const label = "c".repeat(prefixLength);
68 expect(
69 matchedSearch(
70 `/pull-requests?${new URLSearchParams({
71 q: `${q}${cluster} rest`,
72 host: `${host}${cluster}`,
73 author: `${author}${cluster}`,
74 labels: `${label}${cluster}`,
75 })}`,
76 ),
77 ).toMatchObject({ q, host, author, labels: [label] });
78 },
79 );
80
81 it("drops invalid filter, sort, number, selectedUrl, and id params instead of leaking raw search values", () => {
82 const matched = matchedSearch(
83 "/pull-requests?draft=bogus&review=bogus&checks=bogus&sort=bogus&number=abc&selectedUrl=123&host=%20&projectId=123&environmentId=true&selectedProjectId=%20&selectedEnvironmentId=456",
84 );
85 expect(matched).toMatchObject({
86 involvement: "all",
87 state: "open",
88 draft: undefined,
89 review: undefined,
90 checks: undefined,
91 sort: undefined,
92 number: undefined,
93 selectedUrl: undefined,
94 host: undefined,
95 projectId: undefined,
96 environmentId: undefined,
97 selectedProjectId: undefined,
98 selectedEnvironmentId: undefined,
99 });
100 });
101
102 it("keeps valid filters, sort, number, and ids", () => {
103 expect(
104 matchedSearch(
105 "/pull-requests?draft=only&review=approved&checks=passing&sort=newest&number=42&selectedUrl=https%3A%2F%2Fgithub.com%2Fo%2Fr%2Fpull%2F42&projectId=proj-1&environmentId=env-1&selectedProjectId=proj-2&selectedEnvironmentId=env-2",
106 ),
107 ).toMatchObject({
108 draft: "only",
109 review: "approved",
110 checks: "passing",
111 sort: "newest",
112 number: 42,
113 selectedUrl: "https://github.com/o/r/pull/42",
114 projectId: "proj-1",
115 environmentId: "env-1",
116 selectedProjectId: "proj-2",
117 selectedEnvironmentId: "env-2",
118 });
119 });
120});