|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest' |
| 2 | +import { rmSync } from 'node:fs' |
| 3 | +import { render } from '../../render/index.ts' |
| 4 | +import { createTempProject } from './_helpers.ts' |
| 5 | + |
| 6 | +describe('render props', () => { |
| 7 | + let tempDir: string |
| 8 | + const originalCwd = process.cwd() |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + tempDir = createTempProject() |
| 12 | + process.chdir(tempDir) |
| 13 | + }) |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + process.chdir(originalCwd) |
| 17 | + rmSync(tempDir, { recursive: true, force: true }) |
| 18 | + }) |
| 19 | + |
| 20 | + it('passes props to the template via defineProps', async () => { |
| 21 | + const result = await render(` |
| 22 | + <script setup> |
| 23 | + defineProps(['name']) |
| 24 | + </script> |
| 25 | + <template> |
| 26 | + <div>Hello {{ name }}</div> |
| 27 | + </template> |
| 28 | + `, { |
| 29 | + props: { name: 'Ava' }, |
| 30 | + }) |
| 31 | + |
| 32 | + expect(result.html).toContain('Hello Ava') |
| 33 | + }) |
| 34 | + |
| 35 | + it('does not leak props into useConfig() or the returned config', async () => { |
| 36 | + const result = await render(` |
| 37 | + <script setup> |
| 38 | + const config = useConfig() |
| 39 | + </script> |
| 40 | + <template> |
| 41 | + <div>{{ config.props === undefined ? 'clean' : 'leaked' }}</div> |
| 42 | + </template> |
| 43 | + `, { |
| 44 | + props: { name: 'Ava' }, |
| 45 | + }) |
| 46 | + |
| 47 | + expect(result.html).toContain('clean') |
| 48 | + expect(result.config.props).toBeUndefined() |
| 49 | + }) |
| 50 | + |
| 51 | + it('does not render a declared prop as a fallthrough attribute', async () => { |
| 52 | + const result = await render(` |
| 53 | + <script setup> |
| 54 | + defineProps(['name']) |
| 55 | + </script> |
| 56 | + <template> |
| 57 | + <div>{{ name }}</div> |
| 58 | + </template> |
| 59 | + `, { |
| 60 | + props: { name: 'Ava' }, |
| 61 | + }) |
| 62 | + |
| 63 | + expect(result.html).not.toContain('name="Ava"') |
| 64 | + }) |
| 65 | +}) |
0 commit comments