Skip to content

Commit 2cd944b

Browse files
add test for wrongly ordered page nums
1 parent c2863f4 commit 2cd944b

File tree

6 files changed

+85
-75
lines changed

6 files changed

+85
-75
lines changed

typescript/tests/BlockRenderer.test.tsx

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,7 @@ import userEvent from "@testing-library/user-event";
66
import { describe, it, expect } from "vitest";
77
import { JsonDocRenderer } from "../src/renderer/JsonDocRenderer";
88
import { mockBlocks, mockPageWithAllBlocks } from "./fixtures/test-blocks";
9-
10-
// Helper to create a page with specific blocks
11-
const createPageWithBlocks = (blocks: any[]) => ({
12-
object: "page",
13-
id: "test-page",
14-
properties: {
15-
title: {
16-
type: "title",
17-
title: [
18-
{
19-
href: null,
20-
type: "text",
21-
text: { link: null, content: "Test Page" },
22-
annotations: {},
23-
plain_text: "Test Page",
24-
},
25-
],
26-
},
27-
},
28-
children: blocks,
29-
});
9+
import { createPageWithBlocks } from "./utils/helpers";
3010

3111
describe("JsonDocRenderer - All Block Types", () => {
3212
it("renders page title correctly", () => {
@@ -208,9 +188,6 @@ describe("JsonDocRenderer - All Block Types", () => {
208188
const page = createPageWithBlocks([mockBlocks.paragraph]);
209189
const { container } = render(<JsonDocRenderer page={page} />);
210190

211-
// Debug: Print the HTML structure
212-
// screen.debug();
213-
214191
// Test bold annotation
215192
const boldText = screen.getByText("bold text");
216193
expect(boldText).toBeInTheDocument();
@@ -246,4 +223,56 @@ describe("JsonDocRenderer - All Block Types", () => {
246223
expect(screen.getByText("Header 1")).toBeInTheDocument();
247224
expect(screen.getByText("Content in first column")).toBeInTheDocument();
248225
});
226+
227+
it("incorrect block page number shouldn't break the rendering", () => {
228+
// Test misordered page numbers (3, 1, 2)
229+
const blocksWithMisorderedPages = [
230+
{
231+
...mockBlocks.paragraph,
232+
metadata: {
233+
origin: {
234+
file_id: "test-file",
235+
page_num: 3,
236+
},
237+
},
238+
},
239+
{
240+
...mockBlocks.heading_1,
241+
metadata: {
242+
origin: {
243+
file_id: "test-file",
244+
page_num: 1,
245+
},
246+
},
247+
},
248+
{
249+
...mockBlocks.heading_2,
250+
metadata: {
251+
origin: {
252+
file_id: "test-file",
253+
page_num: 2,
254+
},
255+
},
256+
},
257+
];
258+
259+
const pageWithMisorderedBlocks = createPageWithBlocks(
260+
blocksWithMisorderedPages
261+
);
262+
263+
// This should not throw an error or break rendering
264+
expect(() => {
265+
render(<JsonDocRenderer page={pageWithMisorderedBlocks} />);
266+
}).not.toThrow();
267+
268+
// Verify that content still renders despite misordered page numbers
269+
expect(screen.getByText(/This is a paragraph with/)).toBeInTheDocument();
270+
expect(screen.getByText(/Main Heading/)).toBeInTheDocument();
271+
expect(screen.getByText(/Subheading/)).toBeInTheDocument();
272+
273+
// Verify page delimiters are rendered in the order they appear
274+
expect(screen.getByText(/Page 3/)).toBeInTheDocument();
275+
expect(screen.getByText(/Page 1/)).toBeInTheDocument();
276+
expect(screen.getByText(/Page 2/)).toBeInTheDocument();
277+
});
249278
});

typescript/tests/ThemeSwitching.test.tsx

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,12 @@ import { render, screen } from "@testing-library/react";
55
import { describe, it, expect } from "vitest";
66
import { JsonDocRenderer } from "../src/renderer/JsonDocRenderer";
77
import { mockBlocks, mockPageWithAllBlocks } from "./fixtures/test-blocks";
8-
9-
// Helper to create a simple page for theme testing
10-
const createSimplePage = () => ({
11-
object: "page",
12-
id: "theme-test-page",
13-
properties: {
14-
title: {
15-
type: "title",
16-
title: [
17-
{
18-
href: null,
19-
type: "text",
20-
text: { link: null, content: "Theme Test Page" },
21-
annotations: {},
22-
plain_text: "Theme Test Page",
23-
},
24-
],
25-
},
26-
},
27-
children: [mockBlocks.paragraph],
28-
});
8+
import { createPageWithBlocks } from "./utils/helpers";
299

3010
const JSON_DOC_ROOT_TEST_ID = "jsondoc-renderer-root";
3111

3212
describe("JsonDocRenderer - Theme Switching", () => {
33-
const simplePage = createSimplePage();
13+
const simplePage = createPageWithBlocks([mockBlocks.paragraph]);
3414

3515
it("renders with light theme by default", () => {
3616
const { container } = render(<JsonDocRenderer page={simplePage} />);

typescript/tests/UnsupportedBlockHandling.test.tsx

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,7 @@ import { render, screen } from "@testing-library/react";
33
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
44
import { JsonDocRenderer } from "../src/renderer/JsonDocRenderer";
55
import { mockBlocks } from "./fixtures/test-blocks";
6-
7-
// Helper to create a page with specific blocks
8-
const createPageWithBlocks = (blocks: any[]) => ({
9-
object: "page",
10-
id: "test-page",
11-
properties: {
12-
title: {
13-
type: "title",
14-
title: [
15-
{
16-
href: null,
17-
type: "text",
18-
text: { link: null, content: "Test Page" },
19-
annotations: {},
20-
plain_text: "Test Page",
21-
},
22-
],
23-
},
24-
},
25-
children: blocks,
26-
});
6+
import { createPageWithBlocks } from "./utils/helpers";
277

288
describe("Unsupported Block Handling", () => {
299
// Mock console.warn to test logging behavior

typescript/tests/fixtures/test-blocks.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ import {
1818
Page,
1919
} from "@/models/generated";
2020

21-
// Mock data for all block types to test comprehensive rendering
22-
export const mockBlocks: Record<
23-
string,
24-
| Block
21+
type AllBlocks =
2522
| ParagraphBlock
2623
| Heading1Block
2724
| Heading2Block
@@ -36,8 +33,12 @@ export const mockBlocks: Record<
3633
| ToggleBlock
3734
| TableBlock
3835
| ColumnListBlock
39-
| EquationBlock
40-
> = {
36+
| EquationBlock;
37+
38+
type BlockType = Block["type"];
39+
40+
// Mock data for all block types to test comprehensive rendering
41+
export const mockBlocks: Record<BlockType, AllBlocks> = {
4142
paragraph: {
4243
object: "block",
4344
id: "para-1",
@@ -428,6 +429,7 @@ export const mockBlocks: Record<
428429
type: "paragraph",
429430
created_time: "2025-06-24T09:44:12.014249Z",
430431
has_children: false,
432+
// @ts-expect-error
431433
paragraph: {
432434
rich_text: [
433435
{
@@ -470,7 +472,6 @@ export const mockBlocks: Record<
470472
type: "ai_block" as any,
471473
created_time: "2025-06-24T09:44:12.014249Z",
472474
has_children: false,
473-
// @ts-expect-error
474475
ai_block: {
475476
prompt: "Generate something amazing",
476477
},

typescript/tests/test-cases.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!-- - page title highlighting work -->
22
<!-- - block highlighting should work -->
33
<!-- - invalid backref shouldn't trigger highlighting -->
4-
- incorrect block page number shouldn't break the rendering
4+
<!-- - incorrect block page number shouldn't break the rendering -->
55
<!-- - make sure all blocks are rendered -->
66
<!-- - make sure numbered lists are correctly labeled -->
77
<!-- - make sure bad page is gracefully handled -->

typescript/tests/utils/helpers.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Helper to create a page with specific blocks
2+
export const createPageWithBlocks = (blocks: any[]) => ({
3+
object: "page",
4+
id: "test-page",
5+
properties: {
6+
title: {
7+
type: "title",
8+
title: [
9+
{
10+
href: null,
11+
type: "text",
12+
text: { link: null, content: "Test Page" },
13+
annotations: {},
14+
plain_text: "Test Page",
15+
},
16+
],
17+
},
18+
},
19+
children: blocks,
20+
});

0 commit comments

Comments
 (0)