-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMarkdown.test.tsx
More file actions
481 lines (417 loc) · 18.7 KB
/
Markdown.test.tsx
File metadata and controls
481 lines (417 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
import { render } from '@testing-library/react'
import { describe, expect, it } from 'vitest'
import Markdown from './Markdown.js'
describe('Markdown', () => {
it('renders plain text as a paragraph', () => {
const { getByText } = render(<Markdown text="Hello, world!" />)
expect(getByText('Hello, world!')).toBeDefined()
})
it('renders bold text', () => {
const { getByText } = render(<Markdown text="This is **bold** text." />)
const boldText = getByText('bold')
expect(boldText).toBeDefined()
expect(boldText.tagName).toBe('STRONG')
})
it('renders italic text', () => {
const { getByText } = render(<Markdown text="This is *italic* text." />)
const italicText = getByText('italic')
expect(italicText).toBeDefined()
expect(italicText.tagName).toBe('EM')
})
it('does not treat asterisks used for multiplication as italic', () => {
const text = 'Make tool calls to compute 123123 * 3423542 and 235666 * 233333.'
const { container, getByText } = render(<Markdown text={text} />)
expect(getByText(text)).toBeDefined()
expect(container.querySelector('em')).toBeNull()
})
it('does italicize numbers without spaces', () => {
const text = 'Four should be italic: 3*4*5.'
const { container, getByText } = render(<Markdown text={text} />)
expect(getByText('4')).toBeDefined()
expect(container.querySelector('em')).toBeDefined()
})
it('does not italicize snake case', () => {
const text = 'Variables snake_post_ and mid_snake_case and _init_snake should not be italicized.'
const { container, getByText } = render(<Markdown text={text} />)
expect(container.innerHTML).not.toContain('<em>')
expect(container.innerHTML).toContain('mid_snake_case')
expect(getByText(text)).toBeDefined()
})
it('does italicize surrounding underscores', () => {
const text = '_this_one_tho_'
const { container, getByText } = render(<Markdown text={text} />)
expect(getByText('this_one_tho')).toBeDefined()
expect(container.querySelector('em')).toBeDefined()
})
it('renders single asterisks for italic', () => {
const text = '*single asterisks*'
const { getByText } = render(<Markdown text={text} />)
const italicText = getByText('single asterisks')
expect(italicText).toBeDefined()
expect(italicText.tagName).toBe('EM')
})
it('renders headers', () => {
const text = '# Heading 1\n## Heading 2\n### Heading 3'
const { getByText } = render(<Markdown text={text} />)
expect(getByText('Heading 1')).toBeDefined()
expect(getByText('Heading 2')).toBeDefined()
expect(getByText('Heading 3')).toBeDefined()
})
it('renders an image', () => {
const text = ''
const { getByAltText } = render(<Markdown text={text} />)
expect(getByAltText('Hyperparam logo')).toBeDefined()
})
it('renders a link', () => {
const text = 'Check out [Hyp](https://hyperparam.app).'
const { getByRole, getByText } = render(<Markdown text={text} />)
expect(getByText('Hyp')).toBeDefined()
expect(getByRole('link').getAttribute('href')).toBe('https://hyperparam.app')
})
it('renders a partial link', () => {
const text = 'Check out [Hyp](https://hyper'
const { getByText, queryByText } = render(<Markdown text={text} />)
expect(getByText('Hyp')).toBeDefined()
expect(queryByText('hyper')).toBeNull()
expect(getByText('Hyp').tagName).toBe('A')
})
it('multiple links in one line', () => {
const text = 'Check out [Hyp](https://hyperparam.app) on [GitHub](https://github.com/hyparam).'
const { getAllByRole, getByText } = render(<Markdown text={text} />)
expect(getByText('Hyp')).toBeDefined()
expect(getByText('GitHub')).toBeDefined()
const links = getAllByRole('link')
expect(links[0]?.getAttribute('href')).toBe('https://hyperparam.app')
expect(links[1]?.getAttribute('href')).toBe('https://github.com/hyparam')
})
it('renders blockquote', () => {
const text = '> This is a blockquote.'
const { getByText } = render(<Markdown text={text} />)
expect(getByText('This is a blockquote.')).toBeDefined()
})
})
describe('Markdown horizontal rules', () => {
it('renders a horizontal rule', () => {
const text = 'First paragraph\n---\nSecond paragraph'
const { container, getByText, queryByRole } = render(<Markdown text={text} />)
expect(container.querySelector('hr')).toBeDefined()
expect(queryByRole('separator')).toBeDefined()
expect(getByText('First paragraph')).toBeDefined()
expect(getByText('Second paragraph')).toBeDefined()
})
it('horizontal rule must be entire line', () => {
const text = 'First paragraph\n\n--- dashes\n\nSecond paragraph'
const { container, getByText, queryByRole } = render(<Markdown text={text} />)
expect(container.querySelector('hr')).toBeNull()
expect(queryByRole('separator')).toBeNull()
expect(getByText('First paragraph')).toBeDefined()
expect(getByText('--- dashes')).toBeDefined()
expect(getByText('Second paragraph')).toBeDefined()
})
})
describe('Markdown code blocks', () => {
it('renders a code block', () => {
const text = '```js\nconsole.log(\'Hello, world!\')\n```'
const { container } = render(<Markdown text={text} />)
const code = container.querySelector('pre')
expect(code).toBeDefined()
expect(code?.textContent).toBe('console.log(\'Hello, world!\')')
})
it('unterminated code block', () => {
const text = '```js\nconsole.log(\'Hello, world!\')'
const { container } = render(<Markdown text={text} />)
const code = container.querySelector('pre')
expect(code).toBeDefined()
expect(code?.textContent).toBe('console.log(\'Hello, world!\')')
})
it('bold code block', () => {
const text = '**`Math.pow(base, exponent)`**'
const { container, getByText } = render(<Markdown text={text} />)
expect(getByText('Math.pow(base, exponent)')).toBeDefined()
expect(container.innerHTML).not.toContain('**')
})
it('markdown inside a code block as literal text', () => {
const text = `\`\`\`
**This should not be bold**
*nor italic*
\`\`\``
const { container } = render(<Markdown text={text} />)
const codeBlock = container.querySelector('pre')
expect(codeBlock).toBeDefined()
expect(codeBlock?.textContent).toContain('**This should not be bold**')
expect(codeBlock?.textContent).toContain('*nor italic*')
// Ensure markdown inside code block is not parsed
expect(container.innerHTML).not.toContain('<strong>')
expect(container.innerHTML).not.toContain('<em>')
})
})
describe('Markdown lists', () => {
it('renders a list', () => {
const text = '- Item 1\n- Item 2\n- Item 3\n\n'
const { container, getByText } = render(<Markdown text={text} />)
expect(getByText('Item 1')).toBeDefined()
expect(getByText('Item 2')).toBeDefined()
expect(getByText('Item 3')).toBeDefined()
// Should have no <p> tags for simple lists
expect(container.querySelectorAll('p').length).toBe(0)
})
it('list with bold', () => {
const text = '- **Item 1**\n- Item 2\n- Item 3\n\n'
const { getByText } = render(<Markdown text={text} />)
expect(getByText('Item 1')).toBeDefined()
expect(getByText('Item 2')).toBeDefined()
expect(getByText('Item 3')).toBeDefined()
})
it('list with indented sub-paragraphs', () => {
const text = '- Item 1\n Paragraph 1\n Paragraph 2\n- Item 2\n\n'
const { container, getByText } = render(<Markdown text={text} />)
expect(getByText('Item 1')).toBeDefined()
expect(getByText('Paragraph 1')).toBeDefined()
expect(getByText('Paragraph 2')).toBeDefined()
expect(getByText('Item 2')).toBeDefined()
// Should have <p> tags for paragraphs
expect(container.querySelectorAll('p').length).toBe(2)
})
it('unterminated list', () => {
const text = '- Item 1'
const { getByText } = render(<Markdown text={text} />)
expect(getByText('Item 1')).toBeDefined()
})
it('nested unordered list with sub-items', () => {
const text = `- Parent item
- Child item
- Grandchild item`
const { getByText } = render(<Markdown text={text} />)
expect(getByText('Parent item')).toBeDefined()
expect(getByText('Child item')).toBeDefined()
expect(getByText('Grandchild item')).toBeDefined()
})
it('nested ordered list', () => {
const text = `1. First item
1. Nested item
2. Another nested item
2. Second item`
const { getByText } = render(<Markdown text={text} />)
expect(getByText('First item')).toBeDefined()
expect(getByText('Nested item')).toBeDefined()
expect(getByText('Another nested item')).toBeDefined()
expect(getByText('Second item')).toBeDefined()
})
it('blank lines in lists', () => {
const text = '- Item 1\n\n- Item 2\n\n- Item 3'
const { container, getByText } = render(<Markdown text={text} />)
expect(getByText('Item 1')).toBeDefined()
expect(getByText('Item 2')).toBeDefined()
expect(getByText('Item 3')).toBeDefined()
expect(container.querySelectorAll('ul').length).toBe(1)
expect(container.querySelectorAll('li').length).toBe(3)
})
it('inline code inside a nested list with mixed formatting', () => {
const text = `- Item with inline code: \`const x = 10;\`
- Child item with **bold** text and \`inline code\``
const { getByText, container } = render(<Markdown text={text} />)
// Check for the plain text parts
expect(getByText('Item with inline code:')).toBeDefined()
expect(getByText('const x = 10;')).toBeDefined()
expect(getByText('bold')).toBeDefined()
expect(getByText('inline code')).toBeDefined()
// Verify that inline code elements exist
const inlineCodes = container.querySelectorAll('code')
expect(inlineCodes.length).toBe(2)
})
it('nested code block within a list item', () => {
const text = `- List item with code:
\`\`\`js
console.log("Nested code")
\`\`\``
const { container, getByText } = render(<Markdown text={text} />)
expect(getByText('List item with code:')).toBeDefined()
expect(getByText('console.log("Nested code")')).toBeDefined()
const codeBlock = container.querySelector('pre')
expect(codeBlock).toBeDefined()
expect(codeBlock?.textContent).toContain('console.log("Nested code")')
})
it('list with unicode dash –', () => {
const text = '– Item 1\n– Item 2\n– Item 3\n\n'
const { getByText } = render(<Markdown text={text} />)
expect(getByText('Item 1')).toBeDefined()
expect(getByText('Item 2')).toBeDefined()
expect(getByText('Item 3')).toBeDefined()
})
it('list with unicode dot •', () => {
const text = '• Item 1\n• Item 2\n• Item 3\n\n'
const { getByText } = render(<Markdown text={text} />)
expect(getByText('Item 1')).toBeDefined()
expect(getByText('Item 2')).toBeDefined()
expect(getByText('Item 3')).toBeDefined()
})
})
describe('Markdown images', () => {
it('image inside a link', () => {
const text = '[](https://opensource.org/licenses/MIT)'
const { container } = render(<Markdown text={text} />)
// Check that we have an image
const img = container.querySelector('img')
expect(img).toBeDefined()
expect(img?.getAttribute('alt')).toBe('mit license')
expect(img?.getAttribute('src')).toBe('https://img.shields.io/badge/License-MIT-orange.svg')
// Check that the image is inside a link
const link = container.querySelector('a')
expect(link).toBeDefined()
expect(link?.getAttribute('href')).toBe('https://opensource.org/licenses/MIT')
expect(link?.contains(img)).toBe(true)
})
it('multiple images inside links in one paragraph', () => {
const text = 'Check [](https://opensource.org/licenses/MIT) and [](https://www.npmjs.com/package)'
const { container } = render(<Markdown text={text} />)
const links = container.querySelectorAll('a')
expect(links.length).toBe(2)
const images = container.querySelectorAll('img')
expect(images.length).toBe(2)
if (!(0 in images && 1 in images)) {
throw new Error('should not occur')
}
// First link contains first image
expect(links[0]?.getAttribute('href')).toBe('https://opensource.org/licenses/MIT')
expect(links[0]?.contains(images[0])).toBe(true)
expect(images[0].getAttribute('alt')).toBe('license')
// Second link contains second image
expect(links[1]?.getAttribute('href')).toBe('https://www.npmjs.com/package')
expect(links[1]?.contains(images[1])).toBe(true)
expect(images[1].getAttribute('alt')).toBe('npm')
})
it('images and text inside links', () => {
const text = '[Click here  for more info](https://example.com)'
const { container } = render(<Markdown text={text} />)
const link = container.querySelector('a')
expect(link).toBeDefined()
expect(link?.getAttribute('href')).toBe('https://example.com')
// Check that the link contains both text fragments
const linkText = link?.textContent ?? ''
expect(linkText.includes('Click here')).toBe(true)
expect(linkText.includes('for more info')).toBe(true)
// Image should be inside the link
const img = container.querySelector('img')
expect(img).toBeDefined()
expect(img?.getAttribute('src')).toBe('https://example.com/icon.png')
expect(link?.contains(img)).toBe(true)
})
it('incomplete image syntax without closing bracket', () => {
const text = '
expect(container.textContent).toBe('
expect(container.querySelector('img')).toBeNull()
})
describe('image alt text', () => {
it('keeps inline code', () => {
const md = ''
const { container } = render(<Markdown text={md} />)
expect(container.querySelector('img')?.alt).toBe('Press Ctrl+C')
})
})
})
describe('Markdown with nested elements', () => {
it('renders nested formatting with bold, italic, and inline code', () => {
const text = 'This is **bold text with *italic and `code` inside* text**.'
const { container } = render(<Markdown text={text} />)
// Check for inline code element
const codeElem = container.querySelector('code')
expect(codeElem).toBeDefined()
// Check for italic element that should contain the inline code
const italicElem = container.querySelector('em')
expect(italicElem).toBeDefined()
expect(italicElem?.textContent).toContain('code')
// The bold text should wrap the entire content
const boldElem = container.querySelector('strong')
expect(boldElem).toBeDefined()
expect(boldElem?.textContent).toContain('bold text with')
})
it('handles incomplete link syntax without closing bracket', () => {
const text = '[link'
const { container } = render(<Markdown text={text} />)
// The parser treats this as a text token with '['
expect(container.textContent).toBe('[')
expect(container.querySelector('a')).toBeNull()
})
it('handles unclosed image link', () => {
const text = '![alt]'
const { container } = render(<Markdown text={text} />)
expect(container.textContent).toBe('![alt]')
expect(container.querySelector('img')).toBeNull()
})
it('handles unclosed link', () => {
const text = '[link]'
const { container } = render(<Markdown text={text} />)
expect(container.textContent).toBe('[link]')
expect(container.querySelector('a')).toBeNull()
})
it('renders a list immediately after a paragraph', () => {
const text = 'List:\n - First\n - Second'
const { getByText } = render(<Markdown text={text} />)
expect(getByText('First')).toBeDefined()
expect(getByText('Second')).toBeDefined()
expect(getByText('List:')).toBeDefined()
expect(getByText('List:').tagName).toBe('P')
expect(getByText('First').tagName).toBe('LI')
expect(getByText('Second').tagName).toBe('LI')
})
})
describe('Markdown with tables', () => {
it('renders a simple table', () => {
const text = '| Header 1 | Header 2 |\n|----------|----------|\n| Row 1 | Data 1 |\n| Row 2 | Data 2 |'
const { getByText } = render(<Markdown text={text} />)
expect(getByText('Header 1')).toBeDefined()
expect(getByText('Header 2')).toBeDefined()
expect(getByText('Row 1')).toBeDefined()
expect(getByText('Data 1')).toBeDefined()
expect(getByText('Row 2')).toBeDefined()
expect(getByText('Data 2')).toBeDefined()
})
it('table that omits the outer pipes', () => {
const text = 'Header A | Header B\n---|---\nCell 1 | Cell 2'
const { getByText, getByRole } = render(<Markdown text={text} />)
expect(getByRole('table')).toBeDefined()
expect(getByText('Header A')).toBeDefined()
expect(getByText('Cell 2')).toBeDefined()
})
it('inline formatting inside cells', () => {
const text = '| **Bold** | Link |\n|-----------|------|\n| `code` | [x](#) |'
const { getByText, getByRole } = render(<Markdown text={text} />)
expect(getByRole('table')).toBeDefined()
expect(getByText('Bold').tagName).toBe('STRONG')
expect(getByRole('link', { name: 'x' })).toBeDefined()
})
it('keeps surrounding paragraphs intact', () => {
const text = 'Above\n\n| H1 | H2 |\n|----|----|\n| a | b |\n\nBelow'
const { getByText, getByRole } = render(<Markdown text={text} />)
expect(getByText('Above').tagName).toBe('P')
expect(getByText('Below').tagName).toBe('P')
expect(getByRole('table')).toBeDefined()
})
it('ignores pipe‑separated text that lacks a separator line', () => {
const bogus = 'not | a | table'
const { queryByRole, getByText } = render(<Markdown text={bogus} />)
expect(queryByRole('table')).toBeNull() // no table
expect(getByText('not | a | table')).toBeDefined()
})
it('single column table', () => {
const text = '| Only Header |\n|-------------|\n| Single cell |'
const { getByText, getByRole } = render(<Markdown text={text} />)
expect(getByRole('table')).toBeDefined()
expect(getByText('Only Header')).toBeDefined()
expect(getByText('Single cell')).toBeDefined()
})
it('table with no leading or trailing pipes', () => {
const text = 'Header1 | Header2\n------- | -------\nData1 | Data2'
const { getByText, getByRole } = render(<Markdown text={text} />)
expect(getByRole('table')).toBeDefined()
expect(getByText('Header1')).toBeDefined()
expect(getByText('Data2')).toBeDefined()
})
})