Skip to content

Commit c2863f4

Browse files
add tests for highlighting functionality
1 parent 2811a11 commit c2863f4

File tree

6 files changed

+131
-10
lines changed

6 files changed

+131
-10
lines changed

typescript/src/renderer/JsonDocRenderer.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ export const JsonDocRenderer = ({
6666
)}
6767
{/* Page title */}
6868
{page.properties?.title && (
69-
<h1 className="json-doc-page-title" data-page-id={page.id}>
69+
<h1
70+
className="json-doc-page-title"
71+
data-page-id={page.id}
72+
role="heading"
73+
>
7074
{page.properties.title.title?.[0]?.plain_text || "Untitled"}
7175
</h1>
7276
)}

typescript/src/renderer/utils/highlightUtils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export function createHighlightSpan(text: string): HTMLSpanElement {
1717
const highlightSpan = document.createElement("span");
1818
highlightSpan.className = "json-doc-highlight";
1919
highlightSpan.textContent = text;
20+
highlightSpan.role = "note";
2021
return highlightSpan;
2122
}
2223

typescript/tests/BlockRenderer.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe("JsonDocRenderer - All Block Types", () => {
5050
const page = createPageWithBlocks([mockBlocks.heading_1]);
5151
render(<JsonDocRenderer page={page} />);
5252

53-
screen.debug();
53+
// screen.debug();
5454

5555
expect(screen.getByText("Main Heading")).toBeInTheDocument();
5656
});
@@ -204,7 +204,7 @@ describe("JsonDocRenderer - All Block Types", () => {
204204
).toBeInTheDocument();
205205
});
206206

207-
it("renders text annotations correctly", () => {
207+
it("renders text annotations (bold, italic, underline etc.) correctly", () => {
208208
const page = createPageWithBlocks([mockBlocks.paragraph]);
209209
const { container } = render(<JsonDocRenderer page={page} />);
210210

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import "@testing-library/jest-dom";
2+
import { describe, it, expect, vi } from "vitest";
3+
import { render, screen, act, within } from "@testing-library/react";
4+
import React from "react";
5+
6+
import { JsonDocRenderer } from "../src/renderer/JsonDocRenderer";
7+
import { mockPageWithAllBlocks } from "./fixtures/test-blocks";
8+
import { Backref } from "../src/renderer/utils/highlightUtils";
9+
10+
describe("JsonDocRenderer Highlighting", () => {
11+
it("creates highlights when backrefs are provided", async () => {
12+
const paragraphBackref = {
13+
start_idx: 4,
14+
end_idx: 40,
15+
block_id: "para-1",
16+
};
17+
18+
const headingBackref = {
19+
start_idx: 0,
20+
end_idx: 4,
21+
block_id: "h1-1",
22+
};
23+
24+
const backrefs: Backref[] = [paragraphBackref, headingBackref];
25+
26+
const { container } = render(
27+
<JsonDocRenderer page={mockPageWithAllBlocks} backrefs={backrefs} />
28+
);
29+
30+
// Wait for highlights to be processed
31+
await act(async () => {
32+
await new Promise((resolve) => setTimeout(resolve, 50));
33+
});
34+
35+
// // Check that highlight spans are created
36+
const highlights = within(container).getAllByRole("note");
37+
expect(highlights.length).toBeGreaterThan(0);
38+
39+
screen.debug();
40+
41+
const paragraphEl = container.querySelector(
42+
`[data-block-id=${paragraphBackref.block_id}]`
43+
);
44+
expect(paragraphEl).toBeInTheDocument();
45+
46+
const headingEl = container.querySelector(
47+
`[data-block-id=${headingBackref.block_id}]`
48+
);
49+
expect(headingEl).toBeInTheDocument();
50+
51+
const combinedElText =
52+
(paragraphEl?.textContent || "") + " " + (headingEl?.textContent || "");
53+
54+
highlights.forEach((item) => {
55+
expect(item.textContent?.length).toBeGreaterThan(0);
56+
if (item.textContent)
57+
expect(combinedElText.includes(item.textContent)).toBeTruthy();
58+
});
59+
});
60+
61+
it("highlights page title when page_id backref is provided", async () => {
62+
const firstBackref = {
63+
start_idx: 0,
64+
end_idx: 4,
65+
page_id: mockPageWithAllBlocks.id,
66+
};
67+
const backrefs: Backref[] = [firstBackref];
68+
69+
const { container } = render(
70+
<JsonDocRenderer page={mockPageWithAllBlocks} backrefs={backrefs} />
71+
);
72+
73+
// Wait for highlights to be processed
74+
await act(async () => {
75+
await new Promise((resolve) => setTimeout(resolve, 50));
76+
});
77+
78+
// Check that highlight spans are created in the page title
79+
const pageTitle = within(container).getAllByRole("heading")[0];
80+
const highlightsInTitle = within(pageTitle).getAllByRole("note");
81+
82+
expect(highlightsInTitle.length).toBeGreaterThan(0);
83+
84+
const content =
85+
mockPageWithAllBlocks.properties.title.title[0].text.content;
86+
const textToHighlight = content.slice(
87+
firstBackref.start_idx,
88+
firstBackref.end_idx
89+
);
90+
91+
expect(highlightsInTitle[0]).toHaveTextContent(textToHighlight);
92+
});
93+
94+
it("handles invalid backrefs gracefully", async () => {
95+
const backrefs: Backref[] = [
96+
{ start_idx: 0, end_idx: 4, block_id: "non-existent" },
97+
{ start_idx: 99, end_idx: 1, block_id: "para-1" },
98+
];
99+
100+
const { container } = render(
101+
<JsonDocRenderer page={mockPageWithAllBlocks} backrefs={backrefs} />
102+
);
103+
104+
await act(async () => {
105+
await new Promise((resolve) => setTimeout(resolve, 50));
106+
});
107+
108+
const highlights = within(container).queryAllByRole("note");
109+
110+
// No highlights should be created
111+
expect(highlights.length).equals(0);
112+
});
113+
});

typescript/tests/setup.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ global.IntersectionObserver = vi.fn().mockImplementation(() => ({
1111
unobserve: vi.fn(),
1212
takeRecords: vi.fn(() => []),
1313
}));
14+
15+
// Mock scrollIntoView for jsdom
16+
Element.prototype.scrollIntoView = vi.fn();

typescript/tests/test-cases.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
- page title highlighting work
2-
- block highlighting should work
3-
- invalid backref shouldn't trigger highlighting
1+
<!-- - page title highlighting work -->
2+
<!-- - block highlighting should work -->
3+
<!-- - invalid backref shouldn't trigger highlighting -->
44
- incorrect block page number shouldn't break the rendering
5-
- make sure all blocks are rendered
6-
- make sure numbered lists are correctly labeled
7-
- make sure bad page is gracefully handled
8-
- invalid block type should show error block UI
5+
<!-- - make sure all blocks are rendered -->
6+
<!-- - make sure numbered lists are correctly labeled -->
7+
<!-- - make sure bad page is gracefully handled -->
8+
<!-- - invalid block type should show error block UI -->

0 commit comments

Comments
 (0)