/**
* @jest-environment node
*/
import { transform } from '@babel/core';
import htmBabelPlugin from 'babel-plugin-htm';
describe('htm/babel', () => {
test('basic transformation', () => {
expect(
transform('html`
hello
`;', {
babelrc: false,
compact: true,
plugins: [
htmBabelPlugin
]
}).code
).toBe(`h("div",{id:"hello"},"hello");`);
});
test('basic transformation with variable', () => {
expect(
transform('var name="world";html`hello, ${name}
`;', {
babelrc: false,
compact: true,
plugins: [
htmBabelPlugin
]
}).code
).toBe(`var name="world";h("div",{id:"hello"},"hello, ",name);`);
});
test('basic nested transformation', () => {
expect(
transform('html`d: ${4}`;', {
babelrc: false,
compact: true,
plugins: [
[htmBabelPlugin, {
useBuiltIns: true
}]
]
}).code
).toBe(`h("a",Object.assign({b:2},{c:3}),"d: ",4);`);
});
test('spread a single variable', () => {
expect(
transform('html``;', {
babelrc: false,
compact: true,
plugins: [
htmBabelPlugin
]
}).code
).toBe(`h("a",foo);`);
});
test('spread two variables', () => {
expect(
transform('html``;', {
babelrc: false,
compact: true,
plugins: [
[htmBabelPlugin, {
useBuiltIns: true
}]
]
}).code
).toBe(`h("a",Object.assign({},foo,bar));`);
});
test('property followed by a spread', () => {
expect(
transform('html``;', {
babelrc: false,
compact: true,
plugins: [
[htmBabelPlugin, {
useBuiltIns: true
}]
]
}).code
).toBe(`h("a",Object.assign({b:"1"},foo));`);
});
test('spread followed by a property', () => {
expect(
transform('html``;', {
babelrc: false,
compact: true,
plugins: [
[htmBabelPlugin, {
useBuiltIns: true
}]
]
}).code
).toBe(`h("a",Object.assign({},foo,{b:"1"}));`);
});
test('mix-and-match spreads', () => {
expect(
transform('html``;', {
babelrc: false,
compact: true,
plugins: [
[htmBabelPlugin, {
useBuiltIns: true
}]
]
}).code
).toBe(`h("a",Object.assign({b:"1"},foo,{c:2},{d:3}));`);
});
describe('{variableArity:false}', () => {
test('should pass no children as an empty Array', () => {
expect(
transform('html``;', {
babelrc: false,
compact: true,
plugins: [
[htmBabelPlugin, {
variableArity: false
}]
]
}).code
).toBe(`h("div",null,[]);`);
});
test('should pass children as an Array', () => {
expect(
transform('html`hello
`;', {
babelrc: false,
compact: true,
plugins: [
[htmBabelPlugin, {
variableArity: false
}]
]
}).code
).toBe(`h("div",{id:"hello"},["hello"]);`);
});
});
describe('{pragma:false}', () => {
test('should transform to inline vnodes', () => {
expect(
transform('var name="world",vnode=html`hello, ${name}
`;', {
babelrc: false,
compact: true,
plugins: [
[htmBabelPlugin, {
pragma: false
}]
]
}).code
).toBe(`var name="world",vnode={tag:"div",props:{id:"hello"},children:["hello, ",name]};`);
});
});
describe('{monomorphic:true}', () => {
test('should transform to monomorphic inline vnodes', () => {
expect(
transform('var name="world",vnode=html`hello, ${name}
`;', {
babelrc: false,
compact: true,
plugins: [
[htmBabelPlugin, {
monomorphic: true
}]
]
}).code
).toBe(`var name="world",vnode={type:1,tag:"div",props:{id:"hello"},children:[{type:3,tag:null,props:null,children:null,text:"hello, "},name],text:null};`);
});
});
describe('main test suite', () => {
// Run all of the main tests against the Babel plugin:
const mod = require('fs').readFileSync(require('path').resolve(__dirname, 'index.test.mjs'), 'utf8').replace(/\\0/g, '\0');
const { code } = transform(mod.replace(/^\s*import\s*.+?\s*from\s+(['"]).*?\1[\s;]*$/im, 'const htm = function(){};'), {
babelrc: false,
plugins: [htmBabelPlugin]
});
eval(code);
});
});