import { describe, it, expect } from 'vitest';
import { transform } from '../transform';
describe('vite (server, non-hydratable)', () => {
describe('FunctionDeclaration', () => {
it('should transform FunctionDeclaration with valid Component name and params', async () => {
expect(
await transform(
`
function Foo(props) {
return
Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
expect(
await transform(
`
function Foo() {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
expect(
await transform(
`
const example = 'Foo';
function Foo() {
return {example}
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
expect(
await transform(
`
const Example = createContext();
function Foo() {
return Foo;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
expect(
await transform(
`
function Bar() {
return bar
;
}
function Foo() {
return (
<>
foo
>
);
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should skip FunctionDeclaration with valid Component name and >1 params', async () => {
expect(
await transform(
`
function Foo(a, b) {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should skip FunctionDeclaration with invalid Component name', async () => {
expect(
await transform(
`
function foo() {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should skip FunctionDeclaration with @refresh skip', async () => {
expect(
await transform(
`
// @refresh skip
function Foo() {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should skip FunctionDeclaration with @refresh reload', async () => {
expect(
await transform(
`
// @refresh reload
function Foo() {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
});
describe('VariableDeclarator', () => {
describe('FunctionExpression', () => {
it('should transform VariableDeclarator w/ FunctionExpression with valid Component name and params', async () => {
expect(
await transform(
`
const Foo = function (props) {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
expect(
await transform(
`
const Foo = function() {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
expect(
await transform(
`
const example = 'Foo';
const Foo = function() {
return {example}
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
expect(
await transform(
`
const Example = createContext();
const Foo = function() {
return Foo;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
expect(
await transform(
`
const Bar = function() {
return bar
;
};
const Foo = function() {
return (
<>
foo
>
);
};
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should skip VariableDeclarator w/ FunctionExpression with valid Component name and >1 params', async () => {
expect(
await transform(
`
const Foo = function (a, b) {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should skip VariableDeclarator w/ FunctionExpression with invalid Component name', async () => {
expect(
await transform(
`
const foo = function () {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should skip VariableDeclarator w/ FunctionExpression with @refresh skip', async () => {
expect(
await transform(
`
// @refresh skip
const Foo = function() {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should skip VariableDeclarator w/ FunctionExpression with @refresh reload', async () => {
expect(
await transform(
`
// @refresh reload
const Foo = function() {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
});
describe('ArrowFunctionExpression', () => {
it('should transform VariableDeclarator w/ ArrowFunctionExpression with valid Component name and params', async () => {
expect(
await transform(
`
const Foo = (props) => {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
expect(
await transform(
`
const Foo = () => {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
expect(
await transform(
`
const example = 'Foo';
const Foo = () => {
return {example}
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
expect(
await transform(
`
const Example = createContext();
const Foo = () => {
return Foo;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
expect(
await transform(
`
const Bar = () => {
return bar
;
}
const Foo = () => {
return (
<>
foo
>
);
};
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should skip VariableDeclarator w/ ArrowFunctionExpression with valid Component name and >1 params', async () => {
expect(
await transform(
`
const Foo = (a, b) => {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should skip VariableDeclarator w/ ArrowFunctionExpression with invalid Component name', async () => {
expect(
await transform(
`
const foo = () => {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should skip VariableDeclarator w/ ArrowFunctionExpression with @refresh skip', async () => {
expect(
await transform(
`
// @refresh skip
const Foo = () => {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should skip VariableDeclarator w/ ArrowFunctionExpression with @refresh reload', async () => {
expect(
await transform(
`
// @refresh reload
const Foo = () => {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
});
});
describe('ExportNamedDeclaration w/ FunctionExpression', () => {
it('should transform ExportNamedDeclaration w/ FunctionExpression with valid Component name and params', async () => {
expect(
await transform(
`
export function Foo(props) {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
expect(
await transform(
`
export function Foo() {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
expect(
await transform(
`
const example = 'Foo';
export function Foo() {
return {example}
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
expect(
await transform(
`
const Example = createContext();
export function Foo() {
return Foo;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
expect(
await transform(
`
export function Bar() {
return bar
;
}
export function Foo() {
return (
<>
foo
>
);
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should skip ExportNamedDeclaration w/ FunctionExpression with valid Component name and >1 params', async () => {
expect(
await transform(
`
export function Foo(a, b) {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should skip ExportNamedDeclaration w/ FunctionExpression with invalid Component name', async () => {
expect(
await transform(
`
export function Foo() {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should skip ExportNamedDeclaration w/ FunctionExpression with @refresh skip', async () => {
expect(
await transform(
`
// @refresh skip
export function Foo() {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should skip ExportNamedDeclaration w/ FunctionExpression with @refresh reload', async () => {
expect(
await transform(
`
// @refresh reload
export function Foo() {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
});
describe('ExportDefaultDeclaration w/ FunctionExpression', () => {
it('should transform ExportDefaultDeclaration w/ FunctionExpression with valid Component name and params', async () => {
expect(
await transform(
`
export default function Foo(props) {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
expect(
await transform(
`
export default function Foo() {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
expect(
await transform(
`
const example = 'Foo';
export default function Foo() {
return {example}
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
expect(
await transform(
`
const Example = createContext();
export default function Foo() {
return Foo;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
expect(
await transform(
`
function Bar() {
return bar
;
}
export default function Foo() {
return (
<>
foo
>
);
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should skip ExportDefaultDeclaration w/ FunctionExpression with valid Component name and >1 params', async () => {
expect(
await transform(
`
export default function Foo(a, b) {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should skip ExportDefaultDeclaration w/ FunctionExpression with invalid Component name', async () => {
expect(
await transform(
`
export default function foo() {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should skip ExportDefaultDeclaration w/ FunctionExpression with @refresh skip', async () => {
expect(
await transform(
`
// @refresh skip
export default function Foo() {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should skip ExportDefaultDeclaration w/ FunctionExpression with @refresh reload', async () => {
expect(
await transform(
`
// @refresh reload
export default function Foo() {
return Foo
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
});
describe('Context API', () => {
it('should support top-level VariableDeclaration', async () => {
expect(
await transform(
`
import { createContext } from 'solid-js';
const Example = createContext();
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should support ExportNamedDeclaration', async () => {
expect(
await transform(
`
import { createContext } from 'solid-js';
export const Example = createContext();
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should not support VariableDeclaration that is not top-level', async () => {
expect(
await transform(
`
import { createContext } from 'solid-js';
if (someCond) {
const Example = createContext();
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
});
describe('fix render', () => {
describe('import specifiers', () => {
it('should work with ImportSpecifier + Identifier', async () => {
expect(
await transform(
`
import { render } from 'solid-js/web';
render(() => , root);
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should work with ImportSpecifier + aliased Identifier', async () => {
expect(
await transform(
`
import { render as Render } from 'solid-js/web';
Render(() => , root);
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should work with ImportSpecifier + aliased Identifier from StringLiteral', async () => {
expect(
await transform(
`
import { 'render' as Render } from 'solid-js/web';
Render(() => , root);
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should work with ImportNamespaceSpecifier', async () => {
expect(
await transform(
`
import * as solidWeb from 'solid-js/web';
solidWeb.render(() => , root);
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
});
describe('top-level statements', () => {
it('should work with IfStatement', async () => {
expect(
await transform(
`
import { render } from 'solid-js/web';
if (root) {
render(() => , root);
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
});
describe('@refresh reload', () => {
it('should work', async () => {
expect(
await transform(
`
// @refresh reload
import { render } from 'solid-js/web';
if (root) {
render(() => , root);
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
});
});
describe('fix build', () => {
describe('refs', () => {
it('should work with a mutable variable', async () => {
expect(
await transform(
`
const Comp = () => {
let el;
return Comp
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
it('should work with a function', async () => {
expect(
await transform(
`
const Comp = () => {
let el;
return el = _el}>Comp
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
});
describe('signals as refs', () => {
it('should work', async () => {
expect(
await transform(
`
import { createSignal } from 'solid-js';
const Comp = () => {
const [el, setEl] = createSignal();
return Comp
;
}
`,
'vite',
'server',
false,
),
).toMatchSnapshot();
});
});
});
});