Skip to content
Source
Sign inSign up
Open History to see the last commit

pullRequestsRouteSearch.test.ts

44 lines1.6 KB

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;
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});