import { describe, expect, it } from 'vitest' import { addSourceToJsx } from './inject-source' const removeEmptySpace = (str: string) => { return str.replace(/\s/g, '').trim() } describe('inject source', () => { it("shouldn't augment react fragments", () => { const output = addSourceToJsx( ` export const Route = createFileRoute("/test")({ component: function() { return <>Hello World }, }) `, 'test.jsx', ) expect(output).toBe(undefined) }) it("shouldn't augment react fragments if they start with Fragment ", () => { const output = addSourceToJsx( ` export const Route = createFileRoute("/test")({ component: function() { return Hello World }, }) `, 'test.jsx', ) expect(output).toBe(undefined) }) it("shouldn't augment react fragments if they start with React.Fragment ", () => { const output = addSourceToJsx( ` export const Route = createFileRoute("/test")({ component: function() { return Hello World }, }) `, 'test.jsx', ) expect(output).toBe(undefined) }) describe('FunctionExpression', () => { it('should work with deeply nested custom JSX syntax', () => { const output = removeEmptySpace( addSourceToJsx( ` export const Route = createFileRoute("/test")({ component: function() { return
Hello World
}, }) `, 'test.jsx', )!.code, ) expect(output).toBe( removeEmptySpace(` export const Route = createFileRoute("/test")({ component: function() { return
Hello World
}, }) `), ) }) it('should work with props not destructured and spread', () => { const output = addSourceToJsx( ` export const Route = createFileRoute("/test")({ component: function(props) { return
Hello World
}, }) `, 'test.jsx', ) expect(output).toBe(undefined) }) it('should work with props destructured and spread', () => { const output = addSourceToJsx( ` export const Route = createFileRoute("/test")({ component: function({...props}) { return
Hello World
}, }) `, 'test.jsx', ) expect(output).toBe(undefined) }) it('should work with props destructured and spread with a different name', () => { const output = addSourceToJsx( ` export const Route = createFileRoute("/test")({ component: function({...rest}) { return
Hello World
}, }) `, 'test.jsx', ) expect(output).toBe(undefined) }) it('should work with props spread and other normal elements', () => { const output = removeEmptySpace( addSourceToJsx( ` export const Route = createFileRoute("/test")({ component: function({...rest}) { return
Hello World
} }) `, 'test.jsx', )!.code, ) expect(output).toBe( removeEmptySpace(` export const Route = createFileRoute("/test")({ component: function({...rest}) { return
Hello World
} }) `), ) }) }) describe('ArrowFunctionExpression', () => { it('should work with deeply nested custom JSX syntax', () => { const output = removeEmptySpace( addSourceToJsx( ` export const Route = createFileRoute("/test")({ component: () =>
Hello World
, }) `, 'test.jsx', )!.code, ) expect(output).toBe( removeEmptySpace(` export const Route = createFileRoute("/test")({ component: () =>
Hello World
, }) `), ) }) it('should work with props not destructured and spread', () => { const output = addSourceToJsx( ` export const Route = createFileRoute("/test")({ component: (props) =>
Hello World
, }) `, 'test.jsx', ) expect(output).toBe(undefined) }) it('should work with props destructured and spread', () => { const output = addSourceToJsx( ` export const Route = createFileRoute("/test")({ component: ({...props}) =>
Hello World
, }) `, 'test.jsx', ) expect(output).toBe(undefined) }) it('should work with props destructured and spread with a different name', () => { const output = addSourceToJsx( ` export const Route = createFileRoute("/test")({ component: ({...rest}) =>
Hello World
, }) `, 'test.jsx', ) expect(output).toBe(undefined) }) it('should work with props spread and other normal elements', () => { const output = removeEmptySpace( addSourceToJsx( ` export const Route = createFileRoute("/test")({ component: ({...rest}) =>
Hello World
, }) `, 'test.jsx', )!.code, ) expect(output).toBe( removeEmptySpace(` export const Route = createFileRoute("/test")({ component: ({...rest}) =>
Hello World
, }) `), ) }) }) describe('function declarations', () => { it('should not duplicate the same property if there are nested functions', () => { const output = removeEmptySpace( addSourceToJsx( ` function Parent({ ...props }) { function Child({ ...props }) { return
} return } `, 'test.jsx', )!.code, ) expect(output).toBe( removeEmptySpace(` function Parent({ ...props }) { function Child({ ...props }) { return
} return } `), ) }) it('should apply data-tsd-source from parent props if an external import', () => { const output = removeEmptySpace( addSourceToJsx( ` import Custom from "external"; function test({...props }) { return } `, 'test.tsx', )!.code, ) expect(output).toBe( removeEmptySpace(` import Custom from "external"; function test({...props }) { return } `), ) }) it(' props not destructured', () => { const output = removeEmptySpace( addSourceToJsx( ` function test(props){ return
) } `, 'test.jsx', )!.code, ) expect(output).toBe( removeEmptySpace(` function test(props) { return (
) } `), ) }) it("doesn't transform when props are spread across the element but applies to other elements without any props even when props are destructured", () => { const output = removeEmptySpace( addSourceToJsx( ` function test({...props}) { return (
) } `, 'test.jsx', )!.code, ) expect(output).toBe( removeEmptySpace(` function test({...props}) { return (
) } `), ) }) it("doesn't transform when props are spread across the element but applies to other elements without any props even when props are destructured and name is different", () => { const output = removeEmptySpace( addSourceToJsx( ` function test({...rest}) { return (
) } `, 'test.jsx', )!.code, ) expect(output).toBe( removeEmptySpace(` function test({...rest}) { return (
) } `), ) }) it(' props destructured and collected with a different name', () => { const output = addSourceToJsx( ` function test({ children, ...rest }) { return
) } `, 'test.jsx', )!.code, ) expect(output).toBe( removeEmptySpace(` const ButtonWithProps = function test(props) { return (
) } `), ) }) it("doesn't transform when props are spread across the element but applies to other elements without any props even when props are destructured", () => { const output = removeEmptySpace( addSourceToJsx( ` const ButtonWithProps = function test({...props}) { return (
) } `, 'test.jsx', )!.code, ) expect(output).toBe( removeEmptySpace(` const ButtonWithProps = function test({...props}) { return (
) } `), ) }) it("doesn't transform when props are spread across the element but applies to other elements without any props even when props are destructured and name is different", () => { const output = removeEmptySpace( addSourceToJsx( ` const ButtonWithProps = function test({...rest}) { return (
) } `, 'test.jsx', )!.code, ) expect(output).toBe( removeEmptySpace(` const ButtonWithProps = function test({...rest}) { return (
) } `), ) }) it(' props destructured and collected with a different name', () => { const output = addSourceToJsx( ` const ButtonWithProps = function test({ children, ...rest }) { return