|
| 1 | +import React from "react"; |
| 2 | +import { render } from "@testing-library/react"; |
| 3 | + |
| 4 | +import { truncateMarkdownDisplay } from "../../common/utils/truncateMarkdownDisplay"; |
| 5 | + |
| 6 | +import { Markdown } from "./Markdown"; |
| 7 | + |
| 8 | +describe("Markdown", () => { |
| 9 | + it("keeps markdown links valid when cutOff is calculated from rendered link labels", () => { |
| 10 | + const linkHeavy = Array.from({ length: 20 }, (_, i) => `[click](https://example.com/${i})`).join(" "); |
| 11 | + const { container } = render(<Markdown cutOff={60}>{linkHeavy}</Markdown>); |
| 12 | + |
| 13 | + expect(container.querySelectorAll("a").length).toBeGreaterThan(0); |
| 14 | + expect(container.textContent).toContain("click"); |
| 15 | + expect(container.textContent).not.toContain("https://example.com"); |
| 16 | + }); |
| 17 | + |
| 18 | + it("backs up before a fenced code block when cutOff falls inside it after a paragraph boundary", () => { |
| 19 | + const content = [ |
| 20 | + "Intro.", |
| 21 | + "", |
| 22 | + "```", |
| 23 | + "const first = 1;", |
| 24 | + "const second = 2;", |
| 25 | + "const third = 3;", |
| 26 | + "```", |
| 27 | + "", |
| 28 | + "Outro.", |
| 29 | + ].join("\n"); |
| 30 | + |
| 31 | + const { container } = render(<Markdown cutOff={35}>{content}</Markdown>); |
| 32 | + |
| 33 | + expect(container.querySelector("pre")).toBeFalsy(); |
| 34 | + expect(container.textContent).toContain("Intro."); |
| 35 | + expect(container.textContent).not.toContain("const first"); |
| 36 | + expect(container.textContent).not.toContain("Outro"); |
| 37 | + }); |
| 38 | + |
| 39 | + it("backs up before a fenced code block when cutOff falls inside it after preceding text without a paragraph boundary", () => { |
| 40 | + const content = [ |
| 41 | + "A short paragraph before the code block.", |
| 42 | + "Here is an important code example:", |
| 43 | + "```json", |
| 44 | + "{", |
| 45 | + ' "host": "localhost"', |
| 46 | + "}", |
| 47 | + "```", |
| 48 | + "", |
| 49 | + "After fence.", |
| 50 | + ].join("\n"); |
| 51 | + |
| 52 | + const { container } = render(<Markdown cutOff={content.indexOf("localhost")}>{content}</Markdown>); |
| 53 | + |
| 54 | + expect(container.querySelector("pre")).toBeFalsy(); |
| 55 | + expect(container.textContent).toContain("Here is an important code example:"); |
| 56 | + expect(container.textContent).not.toContain("localhost"); |
| 57 | + expect(container.textContent).not.toContain("After fence"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("renders a valid table when cutOff falls inside a markdown table", () => { |
| 61 | + const content = [ |
| 62 | + "| Name | Value |", |
| 63 | + "| --- | --- |", |
| 64 | + "| alpha | one |", |
| 65 | + "| beta | two |", |
| 66 | + "| gamma | three |", |
| 67 | + "", |
| 68 | + "After table.", |
| 69 | + ].join("\n"); |
| 70 | + |
| 71 | + const { container } = render(<Markdown cutOff={55}>{content}</Markdown>); |
| 72 | + |
| 73 | + expect(container.querySelector("table")).toBeTruthy(); |
| 74 | + expect(container.textContent).toContain("Name"); |
| 75 | + expect(container.textContent).toContain("alpha"); |
| 76 | + expect(container.textContent).not.toContain("After table"); |
| 77 | + }); |
| 78 | + |
| 79 | + it("keeps a complete list when cutOff falls after it without a paragraph boundary", () => { |
| 80 | + const content = `This component renders Markdown content safely. It supports **GitHub Flavoured Markdown**, syntax highlighting for code blocks, and definition lists. |
| 81 | +
|
| 82 | +You can: |
| 83 | + * configure _link targets_ |
| 84 | + * add custom __rehype__ plugins |
| 85 | + * and filter content through an allowed elements list |
| 86 | +A third paragraph that will not appear once the cutOff limit is reached.`; |
| 87 | + |
| 88 | + const { container } = render(<Markdown cutOff={300}>{content}</Markdown>); |
| 89 | + |
| 90 | + expect(container.textContent).toContain("You can:"); |
| 91 | + expect(container.textContent).toContain("configure link targets"); |
| 92 | + expect(container.textContent).toContain("add custom rehype plugins"); |
| 93 | + expect(container.textContent).toContain("and filter content through an allowed elements list"); |
| 94 | + expect(container.textContent).not.toContain("A third paragraph"); |
| 95 | + }); |
| 96 | + |
| 97 | + it("keeps complete fenced blocks before a following link with display cutOff", () => { |
| 98 | + const content = [ |
| 99 | + "A short paragraph before the code block.", |
| 100 | + "", |
| 101 | + "```ts", |
| 102 | + 'const status = "ready";', |
| 103 | + 'const nextStep = "open details";', |
| 104 | + "```", |
| 105 | + "", |
| 106 | + "~~~ts", |
| 107 | + "some code here", |
| 108 | + "~~~", |
| 109 | + "Continue with the [detailed implementation guide](https://example.com/docs/implementation/very/long/path) after the code block.", |
| 110 | + ].join("\n"); |
| 111 | + |
| 112 | + for (const cutOff of [152, 153]) { |
| 113 | + const { container } = render(truncateMarkdownDisplay(<Markdown cutOff={cutOff}>{content}</Markdown>)); |
| 114 | + |
| 115 | + expect(container.querySelectorAll("pre")).toHaveLength(2); |
| 116 | + expect(container.textContent).toContain('const status = "ready";'); |
| 117 | + expect(container.textContent).toContain("some code here"); |
| 118 | + expect(container.textContent).toContain("Continue with the"); |
| 119 | + expect(container.textContent).not.toContain("https://example.com"); |
| 120 | + } |
| 121 | + }); |
| 122 | + |
| 123 | + it("renders a link at the end of long content when cutOff is absent", () => { |
| 124 | + const content = `${Array.from({ length: 40 }, (_, index) => `Long visible paragraph part ${index + 1}.`).join( |
| 125 | + " ", |
| 126 | + )} [final reference](https://example.com/final-reference)`; |
| 127 | + |
| 128 | + const { container } = render(<Markdown>{content}</Markdown>); |
| 129 | + |
| 130 | + expect(container.textContent).toContain("Long visible paragraph part 1."); |
| 131 | + expect(container.textContent).toContain("final reference"); |
| 132 | + expect(container.querySelector('a[href="https://example.com/final-reference"]')).toBeTruthy(); |
| 133 | + }); |
| 134 | +}); |
0 commit comments