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

threadFindHighlights.browser.test.tsx

47 lines1.6 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2​
3import { render } from "../../../test/render";
4​
5import { collectThreadFindRanges, useThreadFindHighlights } from "./threadFindHighlights";
6​
7function row(html: string) {
8 const container = document.createElement("div");
9 container.innerHTML = `<div data-timeline-row-id="row"><div data-thread-find-text>${html}</div></div>`;
10 return container;
12​
13describe("collectThreadFindRanges", () => {
14 it("finds matches split across inline token spans, in order", () => {
15 const container = row(
16 "<pre><code><span>con</span><span>st</span> a = <span>c</span>onst; const</code></pre>",
17 );
18 const ranges = collectThreadFindRanges(container, "const");
19 expect(ranges.map((match) => [match.occurrence, match.range.toString()])).toEqual([
20 [0, "const"],
21 [1, "const"],
22 [2, "const"],
23 ]);
24 });
25});
26​
27function Highlights(props: Parameters<typeof useThreadFindHighlights>[0]) {
28 useThreadFindHighlights(props);
29 return null;
31​
32it("paints every match when one row has more matches than a call can take as arguments", async () => {
33 const count = 200_000;
34 const onActiveRange = vi.fn<(range: Range | null) => void>();
35 await using _screen = await render(
36 <Highlights
37 container={row("e x".repeat(count))}
38 query="e"
39 activeRowId="row"
40 activeOccurrence={count - 1}
41 onActiveRange={onActiveRange}
42 />,
43 );
44 expect(CSS.highlights.get("t3-thread-find")?.size).toBe(count - 1);
45 expect(CSS.highlights.get("t3-thread-find-active")?.size).toBe(1);
46 expect(onActiveRange.mock.lastCall?.[0]?.startOffset).toBe((count - 1) * 3);
47});