|
1 | 1 | "use client" |
2 | 2 |
|
3 | | -import { render } from "@testing-library/react" |
| 3 | +import { fireEvent, render, waitFor } from "@testing-library/react" |
4 | 4 | import "@testing-library/jest-dom" |
5 | 5 |
|
| 6 | +import { parseHighlightedCode } from "../highlightParser" |
6 | 7 | import CodeBlock from "../index" |
| 8 | +import { replaceBrTagsWithNewlines } from "../utils" |
7 | 9 |
|
8 | 10 | // Helper function for rendering CodeBlock with default content |
9 | 11 | const renderCodeBlock = (content = 'console.log("Hello, World!")') => |
@@ -68,4 +70,201 @@ describe("CodeBlock", () => { |
68 | 70 | // The code block should decode the encoded ampersands |
69 | 71 | expect(codeElement?.textContent).toBe("apt-get update && apt-get install -y curl python3") |
70 | 72 | }) |
| 73 | + |
| 74 | + describe("line highlighting", () => { |
| 75 | + it("should remove highlight markers from displayed code", async () => { |
| 76 | + const contentWithMarkers = "const x = 1 // HIGHLIGHT LINE\nfoo()\nbar()" |
| 77 | + const { container } = renderCodeBlock(contentWithMarkers) |
| 78 | + await waitFor(() => { |
| 79 | + const codeElement = container.querySelector("code") |
| 80 | + expect(codeElement?.textContent).not.toContain("// HIGHLIGHT LINE") |
| 81 | + expect(codeElement?.textContent).toContain("const x = 1") |
| 82 | + expect(codeElement?.textContent).toContain("foo()") |
| 83 | + }) |
| 84 | + }) |
| 85 | + |
| 86 | + it("should apply highlighted-line class to marked lines", async () => { |
| 87 | + const contentWithMarkers = "line1 // HIGHLIGHT LINE\nline2\nline3" |
| 88 | + const { container } = renderCodeBlock(contentWithMarkers) |
| 89 | + await waitFor( |
| 90 | + () => { |
| 91 | + const highlighted = container.querySelectorAll(".highlighted-line") |
| 92 | + expect(highlighted.length).toBeGreaterThanOrEqual(1) |
| 93 | + expect(highlighted[0].textContent).toContain("line1") |
| 94 | + }, |
| 95 | + { timeout: 2000 }, |
| 96 | + ) |
| 97 | + }) |
| 98 | + |
| 99 | + it("should wrap lines in code-line spans when highlighting is used", async () => { |
| 100 | + const contentWithMarkers = "a // HIGHLIGHT LINE\nb\nc" |
| 101 | + const { container } = renderCodeBlock(contentWithMarkers) |
| 102 | + await waitFor( |
| 103 | + () => { |
| 104 | + const codeLines = container.querySelectorAll(".code-line") |
| 105 | + expect(codeLines.length).toBeGreaterThanOrEqual(2) |
| 106 | + }, |
| 107 | + { timeout: 2000 }, |
| 108 | + ) |
| 109 | + }) |
| 110 | + |
| 111 | + it("should copy clean code without markers when copy button is used", async () => { |
| 112 | + const writeText = jest.fn(() => Promise.resolve()) |
| 113 | + Object.defineProperty(navigator, "clipboard", { |
| 114 | + value: { writeText }, |
| 115 | + configurable: true, |
| 116 | + }) |
| 117 | + const consoleSpy = jest.spyOn(console, "info").mockImplementation() |
| 118 | + const contentWithMarkers = |
| 119 | + "const url = process.env.URI // HIGHLIGHT LINE\nmongoose.connect(url)" |
| 120 | + const { container } = renderCodeBlock(contentWithMarkers) |
| 121 | + const copyButton = container.querySelector('button[aria-label="copy-to-clipboard"]') |
| 122 | + expect(copyButton).toBeInTheDocument() |
| 123 | + fireEvent.click(copyButton!) |
| 124 | + await waitFor(() => { |
| 125 | + expect(writeText).toHaveBeenCalled() |
| 126 | + }) |
| 127 | + expect(writeText).toHaveBeenCalledWith("const url = process.env.URI\nmongoose.connect(url)") |
| 128 | + consoleSpy.mockRestore() |
| 129 | + }) |
| 130 | + |
| 131 | + it("copied text matches cleanCode when content has <br> and highlight markers (no extra blank lines from markers)", async () => { |
| 132 | + const writeText = jest.fn(() => Promise.resolve()) |
| 133 | + Object.defineProperty(navigator, "clipboard", { |
| 134 | + value: { writeText }, |
| 135 | + configurable: true, |
| 136 | + }) |
| 137 | + const contentWithBrAndMarkers = |
| 138 | + "const url = process.env.MONGODB_URI // HIGHLIGHT LINE<br><br>mongoose.connect(url)<br>// BEGIN HIGHLIGHT<br>.then(result => {<br> console.log('connected to MongoDB')<br>})<br>.catch(error => {<br> console.log('error connecting to MongoDB:', error.message)<br>})<br>// END HIGHLIGHT<br><br>module.exports = mongoose.model('Note', noteSchema) // HIGHLIGHT LINE" |
| 139 | + const { container } = renderCodeBlock(contentWithBrAndMarkers) |
| 140 | + await waitFor(() => { |
| 141 | + const codeElement = container.querySelector("code") |
| 142 | + expect(codeElement?.textContent).not.toContain("// HIGHLIGHT") |
| 143 | + }) |
| 144 | + const copyButton = container.querySelector('button[aria-label="copy-to-clipboard"]') |
| 145 | + fireEvent.click(copyButton!) |
| 146 | + await waitFor(() => { |
| 147 | + expect(writeText).toHaveBeenCalled() |
| 148 | + }) |
| 149 | + const copied = (writeText.mock.calls[0] as unknown as [string] | undefined)?.[0] |
| 150 | + expect(copied).toBeDefined() |
| 151 | + const processed = replaceBrTagsWithNewlines(contentWithBrAndMarkers) |
| 152 | + const { cleanCode: expected } = parseHighlightedCode(processed ?? "") |
| 153 | + expect(String(copied).replace(/\r\n/g, "\n")).toBe(expected) |
| 154 | + }) |
| 155 | + |
| 156 | + it("should highlight range between BEGIN HIGHLIGHT and END HIGHLIGHT", async () => { |
| 157 | + const contentWithRange = "before\n// BEGIN HIGHLIGHT\nmid1\nmid2\n// END HIGHLIGHT\nafter" |
| 158 | + const { container } = renderCodeBlock(contentWithRange) |
| 159 | + await waitFor( |
| 160 | + () => { |
| 161 | + const codeElement = container.querySelector("code") |
| 162 | + expect(codeElement?.textContent).not.toContain("// BEGIN HIGHLIGHT") |
| 163 | + expect(codeElement?.textContent).not.toContain("// END HIGHLIGHT") |
| 164 | + const highlighted = container.querySelectorAll(".highlighted-line") |
| 165 | + expect(highlighted.length).toBeGreaterThanOrEqual(2) |
| 166 | + }, |
| 167 | + { timeout: 2000 }, |
| 168 | + ) |
| 169 | + }) |
| 170 | + |
| 171 | + it("should recognize highlight markers when content has <br> tags", async () => { |
| 172 | + const contentWithBrAndMarkers = |
| 173 | + "const x = 1 // HIGHLIGHT LINE<br/>const y = 2<br/>// BEGIN HIGHLIGHT<br/>const z = 3<br/>// END HIGHLIGHT" |
| 174 | + const { container } = renderCodeBlock(contentWithBrAndMarkers) |
| 175 | + await waitFor( |
| 176 | + () => { |
| 177 | + const codeElement = container.querySelector("code") |
| 178 | + expect(codeElement?.textContent).not.toContain("// HIGHLIGHT LINE") |
| 179 | + expect(codeElement?.textContent).not.toContain("// BEGIN HIGHLIGHT") |
| 180 | + expect(codeElement?.textContent).not.toContain("// END HIGHLIGHT") |
| 181 | + const highlighted = container.querySelectorAll(".highlighted-line") |
| 182 | + expect(highlighted.length).toBeGreaterThanOrEqual(2) |
| 183 | + expect(highlighted[0].textContent).toContain("const x = 1") |
| 184 | + }, |
| 185 | + { timeout: 2000 }, |
| 186 | + ) |
| 187 | + }) |
| 188 | + |
| 189 | + it("correctly highlights lines in code with blank lines", async () => { |
| 190 | + const contentWithBlanks = "first\n\nthird // HIGHLIGHT LINE" |
| 191 | + const { container } = renderCodeBlock(contentWithBlanks) |
| 192 | + await waitFor( |
| 193 | + () => { |
| 194 | + const lines = container.querySelectorAll(".code-line") |
| 195 | + expect(lines.length).toBe(3) |
| 196 | + expect(lines[2].classList.contains("highlighted-line")).toBe(true) |
| 197 | + }, |
| 198 | + { timeout: 2000 }, |
| 199 | + ) |
| 200 | + }) |
| 201 | + |
| 202 | + it("should correctly highlight lines inside multi-line block comments", async () => { |
| 203 | + const content = "// BEGIN HIGHLIGHT\n/*\n * block comment\n */\n// END HIGHLIGHT\ncode" |
| 204 | + const { container } = renderCodeBlock(content) |
| 205 | + await waitFor( |
| 206 | + () => { |
| 207 | + const highlighted = container.querySelectorAll(".highlighted-line") |
| 208 | + expect(highlighted.length).toBeGreaterThanOrEqual(3) |
| 209 | + }, |
| 210 | + { timeout: 2000 }, |
| 211 | + ) |
| 212 | + }) |
| 213 | + |
| 214 | + it("should not double-wrap lines on re-render", async () => { |
| 215 | + const content = "a // HIGHLIGHT LINE\nb" |
| 216 | + const data = { |
| 217 | + attributes: { content }, |
| 218 | + name: "core/code" as const, |
| 219 | + isValid: true, |
| 220 | + clientId: "test-id", |
| 221 | + innerBlocks: [], |
| 222 | + } |
| 223 | + const { container, rerender } = render(<CodeBlock data={data} id="test-id" isExam={false} />) |
| 224 | + await waitFor(() => { |
| 225 | + expect(container.querySelectorAll(".code-line").length).toBe(2) |
| 226 | + }) |
| 227 | + rerender(<CodeBlock data={data} id="test-id" isExam={false} />) |
| 228 | + await waitFor(() => { |
| 229 | + expect(container.querySelectorAll(".code-line").length).toBe(2) |
| 230 | + }) |
| 231 | + }) |
| 232 | + |
| 233 | + it("should highlight lines when language is auto-detected", async () => { |
| 234 | + const content = "const x = 1 // HIGHLIGHT LINE\nconst y = 2" |
| 235 | + const { container } = renderCodeBlock(content) |
| 236 | + await waitFor( |
| 237 | + () => { |
| 238 | + const highlighted = container.querySelectorAll(".highlighted-line") |
| 239 | + expect(highlighted.length).toBe(1) |
| 240 | + expect(highlighted[0].textContent).toContain("const x = 1") |
| 241 | + }, |
| 242 | + { timeout: 2000 }, |
| 243 | + ) |
| 244 | + }) |
| 245 | + |
| 246 | + it("should strip # HIGHLIGHT LINE and highlight regardless of language", async () => { |
| 247 | + const content = "x = 1 # HIGHLIGHT LINE\ny = 2" |
| 248 | + const { container } = renderCodeBlock(content) |
| 249 | + await waitFor( |
| 250 | + () => { |
| 251 | + const codeEl = container.querySelector("code") |
| 252 | + expect(codeEl?.textContent).not.toContain("# HIGHLIGHT LINE") |
| 253 | + const highlighted = container.querySelectorAll(".highlighted-line") |
| 254 | + expect(highlighted.length).toBe(1) |
| 255 | + expect(highlighted[0].textContent?.trim()).toBe("x = 1") |
| 256 | + }, |
| 257 | + { timeout: 2000 }, |
| 258 | + ) |
| 259 | + }) |
| 260 | + |
| 261 | + it("announces highlighted lines to screen readers when highlights are present", () => { |
| 262 | + const content = "line1 // HIGHLIGHT LINE\nline2\nline3 // HIGHLIGHT LINE" |
| 263 | + const { container } = renderCodeBlock(content) |
| 264 | + const preParent = container.querySelector("pre")?.parentElement |
| 265 | + const srOnlySpan = preParent?.querySelector(":scope > span") |
| 266 | + expect(srOnlySpan).toBeInTheDocument() |
| 267 | + expect(srOnlySpan?.textContent?.length).toBeGreaterThan(0) |
| 268 | + }) |
| 269 | + }) |
71 | 270 | }) |
0 commit comments