fix(webui): keep file preview tab switching responsive

This commit is contained in:
Xubin Ren committed 2026-09-24 15:17:05 +08:00
1 parent ebb29759c0
commit 8b02d56e6c
6 files changed
+308 -12

No files matched your search

+49
View File
@@ -66,6 +66,55 @@ describe("CodeBlock", () => {
expect(screen.getAllByRole("button")).toHaveLength(1);
});
it.each([
["many lines", "x\n".repeat(60_000)],
["many short lines", "\n".repeat(2_000)],
["a minified line", "x".repeat(2_001) + "😀tail"],
])("keeps an oversized file preview (%s) complete without a huge token/line DOM", (_name, code) => {
const { container } = render(<CodeBlock code={code} language="html" chrome="none"
showLineNumbers wrapLongLines={false} viewportHighlight />);
expect(screen.queryByTestId("highlighted-code")).not.toBeInTheDocument();
expect(screen.getByTestId("plain-code-fallback").querySelector("code > span > span:last-child")?.textContent).toBe(code);
expect(container.querySelectorAll("*").length).toBeLessThan(10);
});
it("paints large preview text before enhancing it, and cancels enhancement on unmount", async () => {
const callbacks = new Map<number, FrameRequestCallback>();
let next = 0;
const raf = vi.spyOn(window, "requestAnimationFrame").mockImplementation(callback => {
callbacks.set(++next, callback);
return next;
});
const cancel = vi.spyOn(window, "cancelAnimationFrame").mockImplementation(id => { callbacks.delete(id); });
const code = "const value = 1;\n".repeat(600);
try {
const { unmount } = render(<CodeBlock code={code} language="ts" chrome="none"
showLineNumbers wrapLongLines={false} viewportHighlight />);
expect(screen.getByTestId("plain-code-fallback")).toBeInTheDocument();
expect(screen.queryByTestId("highlighted-code")).not.toBeInTheDocument();
await act(async () => { callbacks.get(1)?.(0); });
expect(screen.getByTestId("plain-code-fallback")).toBeInTheDocument();
const selection = document.getSelection()!;
const range = document.createRange();
range.selectNodeContents(screen.getByTestId("plain-code-fallback"));
act(() => selection.addRange(range));
await act(async () => { callbacks.get(2)?.(16); });
expect(screen.getByTestId("plain-code-fallback")).toBeInTheDocument();
await act(async () => { selection.removeAllRanges(); document.dispatchEvent(new Event("selectionchange")); });
expect(await screen.findByTestId("highlighted-code")).toBeInTheDocument();
unmount();
expect(cancel).toHaveBeenCalledWith(2);
const second = render(<CodeBlock code={code} language="ts" chrome="none"
showLineNumbers wrapLongLines={false} viewportHighlight />);
const pending = next;
second.unmount();
expect(callbacks.has(pending)).toBe(false);
} finally {
raf.mockRestore();
cancel.mockRestore();
}
});
it("renders plain code without mounting the highlighter when highlighting is disabled", () => {
render(
<ThemeProvider theme="dark">