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

FileSearchResults.browser.test.tsx

71 lines2.7 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { render } from "../../../test/render";
4import { FileSearchResults } from "./FileSearchResults";
5​
6function marks(container: Element) {
7 return Array.from(container.querySelectorAll("mark"), (mark) => mark.textContent);
8}
9​
10describe("FileSearchResults", () => {
11 it("groups matches by file and highlights the pattern", async () => {
12 await using screen = await render(
13 <FileSearchResults
14 pattern="export const \w+"
15 workspaceRoot={undefined}
16 results={[
17 { fileName: "src/one.ts", line: 1, preview: 'export const heading = "intro";' },
18 { fileName: "src/two.ts", line: 4, preview: "export const second = 2;" },
19 { fileName: "src/one.ts", line: 3, preview: ' export const footer = "outro";' },
20 ]}
21 />,
22 { router: false },
23 );
24 const sections = Array.from(screen.container.querySelectorAll("section"));
25 expect(sections.map((section) => section.textContent)).toEqual([
26 'src/one.ts2 matches1export const heading = "intro";3export const footer = "outro";',
27 "src/two.ts4export const second = 2;",
28 ]);
29 expect(marks(screen.container)).toEqual([
30 "export const heading",
31 "export const footer",
32 "export const second",
33 ]);
34 });
35​
36 it("falls back to a case-insensitive highlight and skips patterns JavaScript can't run safely", async () => {
37 await using screen = await render(
38 <div>
39 <FileSearchResults
40 pattern="needle"
41 workspaceRoot={undefined}
42 results={[{ fileName: "a.ts", line: 1, preview: "const NEEDLE = 1;" }]}
43 />
44 <FileSearchResults
45 pattern="(?i)needle"
46 workspaceRoot={undefined}
47 results={[{ fileName: "b.ts", line: 2, preview: "const needle = 2;" }]}
48 />
49 <FileSearchResults
50 pattern="(a+)+$|needle"
51 workspaceRoot={undefined}
52 results={[{ fileName: "c.ts", line: 3, preview: `${"a".repeat(100)}!needle` }]}
53 />
54 <FileSearchResults
55 pattern="a+a+a+a+a+a+$|needle"
56 workspaceRoot={undefined}
57 results={[{ fileName: "d.ts", line: 4, preview: `${"a".repeat(1000)}!needle` }]}
58 />
59 <FileSearchResults
60 pattern={`^${"(a|aa)".repeat(40)}b$|needle`}
61 workspaceRoot={undefined}
62 results={[{ fileName: "e.ts", line: 5, preview: `${"a".repeat(60)}!needle` }]}
63 />
64 </div>,
65 { router: false },
66 );
67 expect(marks(screen.container)).toEqual(["NEEDLE"]);
68 expect(screen.container.textContent).toContain("const needle = 2;");
69 expect(screen.container.textContent).toContain(`${"a".repeat(100)}!needle`);
70 });
71});