import { transform } from '@babel/core';
import JSX, { Opts } from '../src';
interface Test {
name: string;
from: string;
}
const transpile = (
source: string, options: Opts = {},
) => new Promise((resolve, reject) => transform(
source,
{
filename: '',
presets: null,
plugins: [[JSX, options]],
configFile: false,
}, (error, result) => {
if (error) {
return reject(error);
}
resolve(result?.code);
},
));
const tests: Test[] = [
{
name: 'input[type="checkbox"]',
from: '',
},
{
name: 'input[type="radio"]',
from: `
<>
>
`,
},
{
name: 'select',
from: `
`,
},
{
name: 'textarea',
from: '',
},
{
name: 'input[type="text"]',
from: '',
},
{
name: 'dynamic type in input',
from: '',
},
{
name: 'v-show',
from: '
vShow
',
},
{
name: 'input[type="text"] .lazy modifier',
from: `
`,
},
{
name: 'custom directive',
from: '',
},
{
name: 'vHtml',
from: '',
},
{
name: 'vText',
from: '',
},
{
name: 'Without props',
from: 'a',
},
{
name: 'MereProps Order',
from: '',
},
{
name: 'Merge class/ style attributes into array',
from: '',
},
{
name: 'single no need for a mergeProps call',
from: 'single
',
},
{
name: 'should keep `import * as Vue from "vue"`',
from: `
import * as Vue from 'vue';
Vue
`,
},
{
name: 'specifiers should be merged into a single importDeclaration',
from: `
import { createVNode, Fragment as _Fragment } from 'vue';
import { vShow } from 'vue'
<_Fragment />
`,
},
{
name: 'Without JSX should work',
from: `
import { createVNode } from 'vue';
createVNode('div', null, ['Without JSX should work']);
`,
},
{
name: 'reassign variable as component',
from: `
import { defineComponent } from 'vue';
let a = 1;
const A = defineComponent({
setup(_, { slots }) {
return () => {slots.default()};
},
});
const _a2 = 2;
a = _a2;
a = {a};
`,
},
{
name: 'vModels',
from: '',
},
{
name: 'use "model" as the prop name',
from: '',
},
];
tests.forEach((
{ name, from },
) => {
test(
name,
async () => {
expect(await transpile(from, { optimize: true, enableObjectSlots: true })).toMatchSnapshot(name);
},
);
});
const overridePropsTests: Test[] = [{
name: 'single',
from: '',
}, {
name: 'multiple',
from: '',
}];
overridePropsTests.forEach((
{ name, from },
) => {
test(
`override props ${name}`,
async () => {
expect(await transpile(from, { mergeProps: false })).toMatchSnapshot(name);
},
);
});
const slotsTests: Test[] = [
{
name: 'multiple expressions',
from: '{foo}{bar}',
},
{
name: 'single expression, function expression',
from: `
{() => "foo"}
`,
},
{
name: 'single expression, non-literal value: runtime check',
from: `
const foo = () => 1;
{foo()};
`,
},
];
slotsTests.forEach(({
name, from,
}) => {
test(
`passing object slots via JSX children ${name}`,
async () => {
expect(await transpile(from, { optimize: true, enableObjectSlots: true })).toMatchSnapshot(name);
},
);
});
const objectSlotsTests = [
{
name: 'defaultSlot',
from: '{slots.default()}',
},
];
objectSlotsTests.forEach(({
name, from,
}) => {
test(
`disable object slot syntax with ${name}`,
async () => {
expect(await transpile(from, { optimize: true, enableObjectSlots: false }))
.toMatchSnapshot(name);
},
);
});