Skip to content

Commit da89740

Browse files
committed
Babel: add tests for the babel plugin
1 parent 9d02c23 commit da89740

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"license": "Apache-2.0",
4444
"homepage": "https://github.com/developit/htm",
4545
"devDependencies": {
46+
"@babel/core": "^7.0.0-rc.0",
4647
"eslint": "^5.2.0",
4748
"eslint-config-developit": "^1.1.1",
4849
"jest": "^23.4.2",

test/babel.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
5+
const { transform } = require('@babel/core');
6+
const htmBabelPlugin = require('../babel');
7+
8+
describe('htm/babel', () => {
9+
test('basic transformation', () => {
10+
expect(
11+
transform('html`<div id=hello>hello</div>`;', {
12+
babelrc: false,
13+
compact: true,
14+
plugins: [
15+
htmBabelPlugin
16+
]
17+
}).code
18+
).toBe(`h("div",{id:"hello"},["hello"]);`);
19+
});
20+
21+
test('basic transformation with variable', () => {
22+
expect(
23+
transform('var name="world";html`<div id=hello>hello, ${name}</div>`;', {
24+
babelrc: false,
25+
compact: true,
26+
plugins: [
27+
htmBabelPlugin
28+
]
29+
}).code
30+
).toBe(`var name="world";h("div",{id:"hello"},["hello, ",name]);`);
31+
});
32+
33+
test('inline vnode transformation: (pragma:false)', () => {
34+
expect(
35+
transform('var name="world",vnode=html`<div id=hello>hello, ${name}</div>`;', {
36+
babelrc: false,
37+
compact: true,
38+
plugins: [
39+
[htmBabelPlugin, {
40+
pragma: false
41+
}]
42+
]
43+
}).code
44+
).toBe(`var name="world",vnode={tag:"div",props:{id:"hello"},children:["hello, ",name]};`);
45+
});
46+
47+
test('monomorphic transformation', () => {
48+
expect(
49+
transform('var name="world",vnode=html`<div id=hello>hello, ${name}</div>`;', {
50+
babelrc: false,
51+
compact: true,
52+
plugins: [
53+
[htmBabelPlugin, {
54+
monomorphic: true
55+
}]
56+
]
57+
}).code
58+
).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};`);
59+
});
60+
});

0 commit comments

Comments
 (0)