|
1 | | -import React from "react"; |
2 | | -import { describe, test, expect, jest } from "@jest/globals"; |
| 1 | +import { describe, test, expect } from "@jest/globals"; |
3 | 2 | import { act } from "react-dom/test-utils"; |
4 | 3 | import "./"; |
5 | 4 | import type { MarkdownDisplay } from "./index.js"; |
6 | 5 |
|
7 | | -jest.mock("@next-shared/markdown", () => ({ |
8 | | - MarkdownComponent: jest.fn((props: { content?: string }) => ( |
9 | | - <div>{props.content}</div> |
10 | | - )), |
11 | | -})); |
12 | 6 | jest.mock("@next-core/theme", () => ({})); |
13 | 7 |
|
| 8 | +async function waitForContent( |
| 9 | + element: MarkdownDisplay, |
| 10 | + selector: string, |
| 11 | + timeout = 5000 |
| 12 | +): Promise<Element> { |
| 13 | + const startTime = Date.now(); |
| 14 | + while (Date.now() - startTime < timeout) { |
| 15 | + await act(async () => { |
| 16 | + await new Promise((resolve) => setTimeout(resolve, 50)); |
| 17 | + }); |
| 18 | + const found = element.shadowRoot?.querySelector(selector); |
| 19 | + if (found) { |
| 20 | + return found; |
| 21 | + } |
| 22 | + } |
| 23 | + throw new Error(`Timeout waiting for selector: ${selector}`); |
| 24 | +} |
| 25 | + |
14 | 26 | describe("eo-markdown-display", () => { |
15 | | - test("basic usage", async () => { |
| 27 | + test("should render with content", async () => { |
16 | 28 | const element = document.createElement( |
17 | 29 | "eo-markdown-display" |
18 | 30 | ) as MarkdownDisplay; |
| 31 | + element.content = "# Hello World"; |
| 32 | + |
| 33 | + act(() => { |
| 34 | + document.body.appendChild(element); |
| 35 | + }); |
| 36 | + |
| 37 | + const h1 = await waitForContent(element, "h1"); |
| 38 | + expect(h1?.textContent).toBe("Hello World"); |
| 39 | + |
| 40 | + act(() => { |
| 41 | + document.body.removeChild(element); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + test("should render external links with target blank and icon", async () => { |
| 46 | + const element = document.createElement( |
| 47 | + "eo-markdown-display" |
| 48 | + ) as MarkdownDisplay; |
| 49 | + element.content = "[External Link](https://example.com)"; |
| 50 | + |
| 51 | + act(() => { |
| 52 | + document.body.appendChild(element); |
| 53 | + }); |
| 54 | + |
| 55 | + const link = await waitForContent(element, "a"); |
| 56 | + expect(link.getAttribute("target")).toBe("_blank"); |
| 57 | + expect(link.getAttribute("rel")).toBe("nofollow noopener noreferrer"); |
19 | 58 |
|
20 | | - expect(element.shadowRoot).toBeFalsy(); |
| 59 | + // Should have external link icon |
| 60 | + const icon = link.querySelector("eo-icon"); |
| 61 | + expect(icon?.getAttribute("lib")).toBe("lucide"); |
| 62 | + expect(icon?.getAttribute("icon")).toBe("external-link"); |
| 63 | + |
| 64 | + act(() => { |
| 65 | + document.body.removeChild(element); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + test("should render internal links without target blank", async () => { |
| 70 | + const element = document.createElement( |
| 71 | + "eo-markdown-display" |
| 72 | + ) as MarkdownDisplay; |
| 73 | + element.content = "[Internal Link](/some/path)"; |
21 | 74 |
|
22 | 75 | act(() => { |
23 | 76 | document.body.appendChild(element); |
24 | 77 | }); |
25 | | - expect(element.shadowRoot?.childNodes.length).toBeGreaterThan(1); |
| 78 | + |
| 79 | + const link = await waitForContent(element, "a"); |
| 80 | + expect(link.getAttribute("target")).toBeNull(); |
| 81 | + expect(link.getAttribute("rel")).toBeNull(); |
| 82 | + |
| 83 | + // Should not have external link icon |
| 84 | + const icon = link.querySelector("eo-icon"); |
| 85 | + expect(icon).toBeNull(); |
| 86 | + |
| 87 | + act(() => { |
| 88 | + document.body.removeChild(element); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + test("should not add icon to external links with images", async () => { |
| 93 | + const element = document.createElement( |
| 94 | + "eo-markdown-display" |
| 95 | + ) as MarkdownDisplay; |
| 96 | + element.content = |
| 97 | + "[](https://example.com)"; |
| 98 | + |
| 99 | + act(() => { |
| 100 | + document.body.appendChild(element); |
| 101 | + }); |
| 102 | + |
| 103 | + const link = await waitForContent(element, "a"); |
| 104 | + expect(link.getAttribute("target")).toBe("_blank"); |
| 105 | + |
| 106 | + // Should not have external link icon since it contains an image |
| 107 | + const icon = link.querySelector("eo-icon"); |
| 108 | + expect(icon).toBeNull(); |
| 109 | + |
| 110 | + act(() => { |
| 111 | + document.body.removeChild(element); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + test("should render code blocks with syntax highlighting", async () => { |
| 116 | + const element = document.createElement( |
| 117 | + "eo-markdown-display" |
| 118 | + ) as MarkdownDisplay; |
| 119 | + element.content = "```js\nconst a = 1;\n```"; |
| 120 | + |
| 121 | + act(() => { |
| 122 | + document.body.appendChild(element); |
| 123 | + }); |
| 124 | + |
| 125 | + // Should use presentational.code-wrapper for code blocks |
| 126 | + const codeWrapper = await waitForContent( |
| 127 | + element, |
| 128 | + "presentational\\.code-wrapper" |
| 129 | + ); |
| 130 | + expect(codeWrapper).toBeTruthy(); |
| 131 | + |
| 132 | + act(() => { |
| 133 | + document.body.removeChild(element); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + test("should pass themeVariant to code wrapper", async () => { |
| 138 | + const element = document.createElement( |
| 139 | + "eo-markdown-display" |
| 140 | + ) as MarkdownDisplay; |
| 141 | + element.content = "```js\nconst a = 1;\n```"; |
| 142 | + element.themeVariant = "elevo"; |
| 143 | + |
| 144 | + act(() => { |
| 145 | + document.body.appendChild(element); |
| 146 | + }); |
| 147 | + |
| 148 | + const codeWrapper = await waitForContent( |
| 149 | + element, |
| 150 | + "presentational\\.code-wrapper" |
| 151 | + ); |
| 152 | + expect(codeWrapper.getAttribute("themeVariant")).toBe("elevo"); |
26 | 153 |
|
27 | 154 | act(() => { |
28 | 155 | document.body.removeChild(element); |
29 | 156 | }); |
30 | | - expect(element.shadowRoot?.childNodes.length).toBe(0); |
31 | 157 | }); |
32 | 158 | }); |
0 commit comments