|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { parse } from '../src/index' |
| 3 | +import type { ComarkNode } from 'comark' |
| 4 | + |
| 5 | +// Helper to check if a node is an element with a specific tag |
| 6 | +function isElement(node: ComarkNode, tag: string): boolean { |
| 7 | + return Array.isArray(node) && node[0] === tag |
| 8 | +} |
| 9 | + |
| 10 | +// Helper to get the attrs object of an element |
| 11 | +function getAttrs(node: ComarkNode): Record<string, unknown> { |
| 12 | + return Array.isArray(node) ? ((node[1] as Record<string, unknown>) ?? {}) : {} |
| 13 | +} |
| 14 | + |
| 15 | +describe('empty component props block (#319)', () => { |
| 16 | + it('parses an empty props block without throwing', async () => { |
| 17 | + const result = await parse('::page-section\n---\n---\n#title\nHello\n::') |
| 18 | + const section = result.nodes[0] as ComarkNode |
| 19 | + |
| 20 | + expect(isElement(section, 'page-section')).toBe(true) |
| 21 | + // No YAML-derived keys should be present on the props object |
| 22 | + const attrs = getAttrs(section) |
| 23 | + const yamlKeys = Object.keys(attrs).filter((key) => key !== '$') |
| 24 | + expect(yamlKeys).toHaveLength(0) |
| 25 | + }) |
| 26 | + |
| 27 | + it('preserves slot content when the props block is empty', async () => { |
| 28 | + const result = await parse('::page-section\n---\n---\n#title\nHello\n::') |
| 29 | + const md = JSON.stringify(result.nodes) |
| 30 | + expect(md).toContain('Hello') |
| 31 | + }) |
| 32 | + |
| 33 | + it('parses a whitespace-only props block without throwing', async () => { |
| 34 | + const result = await parse('::hero\n---\n \n---\ncontent\n::') |
| 35 | + const hero = result.nodes[0] as ComarkNode |
| 36 | + |
| 37 | + expect(isElement(hero, 'hero')).toBe(true) |
| 38 | + expect(JSON.stringify(result.nodes)).toContain('content') |
| 39 | + }) |
| 40 | + |
| 41 | + it('parses a comment-only props block without throwing', async () => { |
| 42 | + const result = await parse('::hero\n---\n# todo\n---\ncontent\n::') |
| 43 | + const hero = result.nodes[0] as ComarkNode |
| 44 | + |
| 45 | + expect(isElement(hero, 'hero')).toBe(true) |
| 46 | + const attrs = getAttrs(hero) |
| 47 | + const yamlKeys = Object.keys(attrs).filter((key) => key !== '$') |
| 48 | + expect(yamlKeys).toHaveLength(0) |
| 49 | + }) |
| 50 | + |
| 51 | + it('still applies props from a non-empty props block', async () => { |
| 52 | + const result = await parse('::hero\n---\ntitle: x\n---\n#title\nHi\n::') |
| 53 | + const hero = result.nodes[0] as ComarkNode |
| 54 | + |
| 55 | + expect(isElement(hero, 'hero')).toBe(true) |
| 56 | + expect(getAttrs(hero).title).toBe('x') |
| 57 | + }) |
| 58 | + |
| 59 | + it('round-trips a multi-key, typed props block', async () => { |
| 60 | + const result = await parse('::hero\n---\ncount: 3\nlabel: hello\n---\ncontent\n::') |
| 61 | + const hero = result.nodes[0] as ComarkNode |
| 62 | + |
| 63 | + expect(isElement(hero, 'hero')).toBe(true) |
| 64 | + // Non-string attribute values are JSON-stringified onto the element (see syntax.ts) |
| 65 | + expect(getAttrs(hero).count).toBe('3') |
| 66 | + expect(getAttrs(hero).label).toBe('hello') |
| 67 | + }) |
| 68 | + |
| 69 | + it('parses document-level comment-only frontmatter without throwing', async () => { |
| 70 | + const result = await parse('---\n# comment only\n---\n\n# Heading') |
| 71 | + |
| 72 | + const hasHeading = result.nodes.some((node) => isElement(node, 'h1')) |
| 73 | + expect(hasHeading).toBe(true) |
| 74 | + }) |
| 75 | + |
| 76 | + it('rejects a malformed (non-empty, invalid) props block', async () => { |
| 77 | + await expect(parse('::hero\n---\nfoo: [1,2\n---\ncontent\n::')).rejects.toThrow() |
| 78 | + }) |
| 79 | +}) |
0 commit comments