|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { render } from 'svelte/server' |
| 3 | +import { parse } from 'comark' |
| 4 | +import Markdown from '../src/components/Markdown.svelte' |
| 5 | +import MarkdownParsed from '../src/components/MarkdownParsed.svelte' |
| 6 | +import MarkdownAsync from '../src/async/MarkdownAsync.svelte' |
| 7 | + |
| 8 | +/** Strip Svelte SSR hydration comments from rendered HTML */ |
| 9 | +function html(body: string): string { |
| 10 | + return body.replace(/<!--[[\]\-\d!]*-->/g, '').replace(/<!---->/g, '') |
| 11 | +} |
| 12 | + |
| 13 | +describe('Markdown value as MarkdownTree', () => { |
| 14 | + it('renders a pre-parsed tree the same as MarkdownParsed', async () => { |
| 15 | + const tree = await parse('# Hello **World**') |
| 16 | + const fromMarkdown = html(render(Markdown, { props: { value: tree } }).body) |
| 17 | + const fromParsed = html(render(MarkdownParsed, { props: { value: tree } }).body) |
| 18 | + |
| 19 | + expect(fromMarkdown).toContain('<h1') |
| 20 | + expect(fromMarkdown).toContain('Hello <strong>World</strong>') |
| 21 | + expect(fromMarkdown).toBe(fromParsed) |
| 22 | + }) |
| 23 | + |
| 24 | + it('still renders markdown strings after parse settles', async () => { |
| 25 | + // Server render of Markdown only emits once parse has completed in $effect — |
| 26 | + // string path is empty on first SSR tick. MarkdownAsync covers the string path. |
| 27 | + const { body } = await render(MarkdownAsync, { |
| 28 | + props: { value: 'Hello **world**' }, |
| 29 | + }) |
| 30 | + const output = html(body) |
| 31 | + expect(output).toContain('<p>') |
| 32 | + expect(output).toContain('<strong>world</strong>') |
| 33 | + }) |
| 34 | + |
| 35 | + it('renders an empty tree without crashing', () => { |
| 36 | + const { body } = render(Markdown, { |
| 37 | + props: { value: { nodes: [], frontmatter: {}, meta: {} } }, |
| 38 | + }) |
| 39 | + expect(html(body)).toBe('<div class="comark-content "></div>') |
| 40 | + }) |
| 41 | +}) |
| 42 | + |
| 43 | +describe('MarkdownAsync value as MarkdownTree', () => { |
| 44 | + it('renders a pre-parsed tree without parsing', async () => { |
| 45 | + const tree = await parse('# Hello **World**') |
| 46 | + const { body } = await render(MarkdownAsync, { props: { value: tree } }) |
| 47 | + const output = html(body) |
| 48 | + expect(output).toContain('<h1') |
| 49 | + expect(output).toContain('Hello <strong>World</strong>') |
| 50 | + }) |
| 51 | + |
| 52 | + it('renders a tree the same as MarkdownParsed', async () => { |
| 53 | + const tree = await parse('A paragraph with **bold**') |
| 54 | + const fromAsync = html((await render(MarkdownAsync, { props: { value: tree } })).body) |
| 55 | + const fromParsed = html(render(MarkdownParsed, { props: { value: tree } }).body) |
| 56 | + expect(fromAsync).toBe(fromParsed) |
| 57 | + }) |
| 58 | +}) |
0 commit comments