|
| 1 | +import { |
| 2 | + createMemoryHistory, |
| 3 | + createRootRoute, |
| 4 | + createRoute, |
| 5 | + createRouter, |
| 6 | +} from "@tanstack/react-router"; |
| 7 | +import { describe, expect, it } from "vite-plus/test"; |
| 8 | + |
| 9 | +import { Route as PullRequestsRoute } from "../../routes/_chat.pull-requests"; |
| 10 | + |
| 11 | +/** Matches a URL in a router shaped like the app's: a layout without its own search rules. */ |
| 12 | +function matchedSearch(url: string) { |
| 13 | + const root = createRootRoute(); |
| 14 | + const chat = createRoute({ getParentRoute: () => root, id: "_chat" }); |
| 15 | + const pullRequests = createRoute({ |
| 16 | + getParentRoute: () => chat, |
| 17 | + path: "pull-requests", |
| 18 | + validateSearch: PullRequestsRoute.options.validateSearch, |
| 19 | + }); |
| 20 | + const router = createRouter({ |
| 21 | + routeTree: root.addChildren([chat.addChildren([pullRequests])]), |
| 22 | + history: createMemoryHistory({ initialEntries: [url] }), |
| 23 | + }); |
| 24 | + // The search a component reads through `useSearch` is the matched route's, not the URL's. |
| 25 | + return router.matchRoutes(router.latestLocation).at(-1)?.search as |
| 26 | + | Record<string, unknown> |
| 27 | + | undefined; |
| 28 | +} |
| 29 | + |
| 30 | +describe("pull requests route search", () => { |
| 31 | + it("drops a q the URL parser turned into a number", () => { |
| 32 | + expect(matchedSearch("/pull-requests?q=4089")?.q).toBeUndefined(); |
| 33 | + expect(matchedSearch("/pull-requests?q=true")?.q).toBeUndefined(); |
| 34 | + }); |
| 35 | + |
| 36 | + it("keeps a q that is text, including quoted digits", () => { |
| 37 | + expect(matchedSearch("/pull-requests?q=fix")?.q).toBe("fix"); |
| 38 | + expect(matchedSearch("/pull-requests?q=%224089%22")?.q).toBe("4089"); |
| 39 | + }); |
| 40 | +}); |
0 commit comments