|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +import { renderMarkdownToHtml } from '@/utils/markdownRendererUtil' |
| 4 | + |
| 5 | +describe('markdownRendererUtil', () => { |
| 6 | + describe('renderMarkdownToHtml', () => { |
| 7 | + it('should render basic markdown to HTML', () => { |
| 8 | + const markdown = '# Hello\n\nThis is a test.' |
| 9 | + const html = renderMarkdownToHtml(markdown) |
| 10 | + |
| 11 | + expect(html).toContain('<h1') |
| 12 | + expect(html).toContain('Hello') |
| 13 | + expect(html).toContain('<p>') |
| 14 | + expect(html).toContain('This is a test.') |
| 15 | + }) |
| 16 | + |
| 17 | + it('should render links with target="_blank" and rel="noopener noreferrer"', () => { |
| 18 | + const markdown = '[Click here](https://example.com)' |
| 19 | + const html = renderMarkdownToHtml(markdown) |
| 20 | + |
| 21 | + expect(html).toContain('target="_blank"') |
| 22 | + expect(html).toContain('rel="noopener noreferrer"') |
| 23 | + expect(html).toContain('href="https://example.com"') |
| 24 | + expect(html).toContain('Click here') |
| 25 | + }) |
| 26 | + |
| 27 | + it('should render multiple links with target="_blank"', () => { |
| 28 | + const markdown = |
| 29 | + '[Link 1](https://example.com) and [Link 2](https://test.com)' |
| 30 | + const html = renderMarkdownToHtml(markdown) |
| 31 | + |
| 32 | + const targetBlankMatches = html.match(/target="_blank"/g) |
| 33 | + expect(targetBlankMatches).toHaveLength(2) |
| 34 | + |
| 35 | + const relMatches = html.match(/rel="noopener noreferrer"/g) |
| 36 | + expect(relMatches).toHaveLength(2) |
| 37 | + }) |
| 38 | + |
| 39 | + it('should handle relative image paths with baseUrl', () => { |
| 40 | + const markdown = '' |
| 41 | + const baseUrl = 'https://cdn.example.com' |
| 42 | + const html = renderMarkdownToHtml(markdown, baseUrl) |
| 43 | + |
| 44 | + expect(html).toContain(`src="${baseUrl}/image.png"`) |
| 45 | + expect(html).toContain('alt="Alt text"') |
| 46 | + }) |
| 47 | + |
| 48 | + it('should not modify absolute image URLs', () => { |
| 49 | + const markdown = '' |
| 50 | + const baseUrl = 'https://cdn.example.com' |
| 51 | + const html = renderMarkdownToHtml(markdown, baseUrl) |
| 52 | + |
| 53 | + expect(html).toContain('src="https://example.com/image.png"') |
| 54 | + expect(html).not.toContain(baseUrl) |
| 55 | + }) |
| 56 | + |
| 57 | + it('should handle empty markdown', () => { |
| 58 | + const html = renderMarkdownToHtml('') |
| 59 | + |
| 60 | + expect(html).toBe('') |
| 61 | + }) |
| 62 | + |
| 63 | + it('should sanitize potentially dangerous HTML', () => { |
| 64 | + const markdown = '<script>alert("xss")</script>' |
| 65 | + const html = renderMarkdownToHtml(markdown) |
| 66 | + |
| 67 | + expect(html).not.toContain('<script>') |
| 68 | + expect(html).not.toContain('alert') |
| 69 | + }) |
| 70 | + |
| 71 | + it('should allow video tags with proper attributes', () => { |
| 72 | + const markdown = |
| 73 | + '<video src="video.mp4" controls autoplay loop muted></video>' |
| 74 | + const html = renderMarkdownToHtml(markdown) |
| 75 | + |
| 76 | + expect(html).toContain('<video') |
| 77 | + expect(html).toContain('src="video.mp4"') |
| 78 | + expect(html).toContain('controls') |
| 79 | + }) |
| 80 | + |
| 81 | + it('should render links with title attribute', () => { |
| 82 | + const markdown = '[Link](https://example.com "This is a title")' |
| 83 | + const html = renderMarkdownToHtml(markdown) |
| 84 | + |
| 85 | + expect(html).toContain('title="This is a title"') |
| 86 | + expect(html).toContain('target="_blank"') |
| 87 | + expect(html).toContain('rel="noopener noreferrer"') |
| 88 | + }) |
| 89 | + |
| 90 | + it('should render complex markdown with links, images, and text', () => { |
| 91 | + const markdown = ` |
| 92 | +# Release Notes |
| 93 | +
|
| 94 | +Check out our [documentation](https://docs.example.com) for more info. |
| 95 | +
|
| 96 | + |
| 97 | +
|
| 98 | +Visit our [homepage](https://example.com) to learn more. |
| 99 | + ` |
| 100 | + const baseUrl = 'https://cdn.example.com' |
| 101 | + const html = renderMarkdownToHtml(markdown, baseUrl) |
| 102 | + |
| 103 | + // Check links have target="_blank" |
| 104 | + const targetBlankMatches = html.match(/target="_blank"/g) |
| 105 | + expect(targetBlankMatches).toHaveLength(2) |
| 106 | + |
| 107 | + // Check image has baseUrl prepended |
| 108 | + expect(html).toContain(`${baseUrl}/screenshot.png`) |
| 109 | + |
| 110 | + // Check heading |
| 111 | + expect(html).toContain('Release Notes') |
| 112 | + }) |
| 113 | + }) |
| 114 | +}) |
0 commit comments