Skip to content

Commit a7188ba

Browse files
committed
feat: add comprehensive tests for Props detection with 'as' property
1 parent 44ff5fd commit a7188ba

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

packages/compiler/test/tsx/props.ts

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,150 @@ export default function __AstroComponent_(_props: Record<string, any>): any {}\n
289289
assert.snapshot(code, output, 'expected code to match snapshot');
290290
});
291291

292+
test('props interface with as property', async () => {
293+
const input = `---
294+
interface Props {
295+
as?: string;
296+
href?: string;
297+
}
298+
---
299+
300+
<div></div>
301+
`;
302+
const output = `${TSXPrefix}
303+
interface Props {
304+
as?: string;
305+
href?: string;
306+
}
307+
308+
{};<Fragment>
309+
<div></div>
310+
311+
</Fragment>
312+
export default function __AstroComponent_(_props: Props): any {}
313+
${PREFIX()}`;
314+
const { code } = await convertToTSX(input, { sourcemap: 'external' });
315+
assert.snapshot(code, output, 'expected code to match snapshot');
316+
});
317+
318+
test('props with destructured as property', async () => {
319+
const input = `---
320+
interface Props {
321+
as?: string;
322+
className?: string;
323+
}
324+
325+
const { as, className } = Astro.props;
326+
---
327+
328+
<div class={className}>{as}</div>
329+
`;
330+
const output = `${TSXPrefix}
331+
interface Props {
332+
as?: string;
333+
className?: string;
334+
}
335+
336+
const { as, className } = Astro.props;
337+
338+
<Fragment>
339+
<div class={className}>{as}</div>
340+
341+
</Fragment>
342+
export default function __AstroComponent_(_props: Props): any {}
343+
${PREFIX()}`;
344+
const { code } = await convertToTSX(input, { sourcemap: 'external' });
345+
assert.snapshot(code, output, 'expected code to match snapshot');
346+
});
347+
348+
test('props with renamed as property in destructuring', async () => {
349+
const input = `---
350+
interface Props {
351+
as?: string;
352+
}
353+
354+
const { as: element } = Astro.props;
355+
---
356+
357+
<div>{element}</div>
358+
`;
359+
const output = `${TSXPrefix}
360+
interface Props {
361+
as?: string;
362+
}
363+
364+
const { as: element } = Astro.props;
365+
366+
<Fragment>
367+
<div>{element}</div>
368+
369+
</Fragment>
370+
export default function __AstroComponent_(_props: Props): any {}
371+
${PREFIX()}`;
372+
const { code } = await convertToTSX(input, { sourcemap: 'external' });
373+
assert.snapshot(code, output, 'expected code to match snapshot');
374+
});
375+
376+
test('props interface with as and other properties', async () => {
377+
const input = `---
378+
interface Props extends HTMLAttributes<'div'> {
379+
as?: keyof HTMLElementTagNameMap;
380+
variant?: 'primary' | 'secondary';
381+
size?: 'sm' | 'md' | 'lg';
382+
}
383+
384+
const { as = 'div', variant = 'primary', size = 'md', ...rest } = Astro.props;
385+
---
386+
387+
<div data-variant={variant} data-size={size}></div>
388+
`;
389+
const output = `${TSXPrefix}
390+
interface Props extends HTMLAttributes<'div'> {
391+
as?: keyof HTMLElementTagNameMap;
392+
variant?: 'primary' | 'secondary';
393+
size?: 'sm' | 'md' | 'lg';
394+
}
395+
396+
const { as = 'div', variant = 'primary', size = 'md', ...rest } = Astro.props;
397+
398+
<Fragment>
399+
<div data-variant={variant} data-size={size}></div>
400+
401+
</Fragment>
402+
export default function __AstroComponent_(_props: Props): any {}
403+
${PREFIX()}`;
404+
const { code } = await convertToTSX(input, { sourcemap: 'external' });
405+
assert.snapshot(code, output, 'expected code to match snapshot');
406+
});
407+
408+
test('props type alias with as property', async () => {
409+
const input = `---
410+
type Props = {
411+
as?: string;
412+
children?: any;
413+
}
414+
415+
const props = Astro.props as Props;
416+
---
417+
418+
<div>{props.children}</div>
419+
`;
420+
const output = `${TSXPrefix}
421+
type Props = {
422+
as?: string;
423+
children?: any;
424+
}
425+
426+
const props = Astro.props as Props;
427+
428+
<Fragment>
429+
<div>{props.children}</div>
430+
431+
</Fragment>
432+
export default function __AstroComponent_(_props: Props): any {}
433+
${PREFIX()}`;
434+
const { code } = await convertToTSX(input, { sourcemap: 'external' });
435+
assert.snapshot(code, output, 'expected code to match snapshot');
436+
});
437+
292438
test.run();

0 commit comments

Comments
 (0)