|
| 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 | +}); |
0 commit comments