|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +import { ImportedPrompt } from '../testdata/imported-prompt' |
| 4 | +import { renderComponent, renderElement } from './index' |
| 5 | + |
| 6 | +describe('renderComponent from render-node', () => { |
| 7 | + it('should type-check props requirements', () => { |
| 8 | + function Prompt() { |
| 9 | + return <div>Hello Velin</div> |
| 10 | + } |
| 11 | + |
| 12 | + function RequiredPrompt({ name }: { name: string }) { |
| 13 | + return <div>{`Hello ${name}`}</div> |
| 14 | + } |
| 15 | + |
| 16 | + void renderComponent(Prompt) |
| 17 | + // @ts-expect-error required props must be provided |
| 18 | + void renderComponent(RequiredPrompt) |
| 19 | + void renderComponent(RequiredPrompt, { name: 'React' }) |
| 20 | + }) |
| 21 | + |
| 22 | + it('should render function component output to Markdown', async () => { |
| 23 | + function Prompt() { |
| 24 | + return <div>Hello Velin</div> |
| 25 | + } |
| 26 | + |
| 27 | + await expect(renderComponent(Prompt)).resolves.toBe('Hello Velin\n') |
| 28 | + }) |
| 29 | + |
| 30 | + it('should pass props to the component', async () => { |
| 31 | + function Prompt({ name }: { name: string }) { |
| 32 | + return <div>{`Hello ${name}`}</div> |
| 33 | + } |
| 34 | + |
| 35 | + await expect(renderComponent(Prompt, { name: 'React' })).resolves.toBe('Hello React\n') |
| 36 | + }) |
| 37 | + |
| 38 | + it('should convert nested HTML to Markdown', async () => { |
| 39 | + function Prompt() { |
| 40 | + return ( |
| 41 | + <section> |
| 42 | + <h1>Plan</h1> |
| 43 | + <ul> |
| 44 | + <li>Render</li> |
| 45 | + <li>Convert</li> |
| 46 | + </ul> |
| 47 | + </section> |
| 48 | + ) |
| 49 | + } |
| 50 | + |
| 51 | + const result = await renderComponent(Prompt) |
| 52 | + |
| 53 | + expect(result).toContain('# Plan') |
| 54 | + expect(result).toContain('- Render') |
| 55 | + expect(result).toContain('- Convert') |
| 56 | + }) |
| 57 | + |
| 58 | + it('should render null output to empty Markdown', async () => { |
| 59 | + function Prompt() { |
| 60 | + return null |
| 61 | + } |
| 62 | + |
| 63 | + await expect(renderComponent(Prompt)).resolves.toBe('') |
| 64 | + }) |
| 65 | + |
| 66 | + it('should render an imported component with React imports', async () => { |
| 67 | + await expect(renderComponent(ImportedPrompt, { name: 'node import' })).resolves.toBe( |
| 68 | + '# Imported\n\n**NODE IMPORT**\n', |
| 69 | + ) |
| 70 | + }) |
| 71 | +}) |
| 72 | + |
| 73 | +describe('renderElement from render-node', () => { |
| 74 | + it('should render a React element to Markdown', async () => { |
| 75 | + await expect(renderElement(<span>Element prompt</span>)).resolves.toBe('Element prompt\n') |
| 76 | + }) |
| 77 | +}) |
0 commit comments