@@ -46,7 +46,7 @@ describe('htm/babel', () => {
4646 ]
4747 } ) . code
4848 ) . toBe ( `h("a",Object.assign({b:2},{c:3}),"d: ",4);` ) ;
49-
49+
5050 expect (
5151 transform ( 'html`<a b=${2} ...${{ c: 3 }}>d: ${4}</a>`;' , {
5252 ...options ,
@@ -68,7 +68,7 @@ describe('htm/babel', () => {
6868 ]
6969 } ) . code
7070 ) . toBe ( `h("a",foo);` ) ;
71-
71+
7272 expect (
7373 transform ( 'html`<a ...${foo}></a>`;' , {
7474 ...options ,
@@ -92,7 +92,7 @@ describe('htm/babel', () => {
9292 ]
9393 } ) . code
9494 ) . toBe ( `h("a",Object.assign({},foo,bar));` ) ;
95-
95+
9696 expect (
9797 transform ( 'html`<a ...${foo} ...${bar}></a>`;' , {
9898 ...options ,
@@ -116,7 +116,7 @@ describe('htm/babel', () => {
116116 ]
117117 } ) . code
118118 ) . toBe ( `h("a",Object.assign({b:"1"},foo));` ) ;
119-
119+
120120 expect (
121121 transform ( 'html`<a b="1" ...${foo}></a>`;' , {
122122 ...options ,
@@ -140,7 +140,7 @@ describe('htm/babel', () => {
140140 ]
141141 } ) . code
142142 ) . toBe ( `h("a",Object.assign({},foo,{b:"1"}));` ) ;
143-
143+
144144 expect (
145145 transform ( 'html`<a ...${foo} b="1"></a>`;' , {
146146 ...options ,
@@ -164,7 +164,7 @@ describe('htm/babel', () => {
164164 ]
165165 } ) . code
166166 ) . toBe ( `h("a",Object.assign({b:"1"},foo,{c:2},{d:3}));` ) ;
167-
167+
168168 expect (
169169 transform ( 'html`<a b="1" ...${foo} c=${2} ...${{d:3}}></a>`;' , {
170170 ...options ,
@@ -307,6 +307,125 @@ describe('htm/babel', () => {
307307 } ) ;
308308 } ) ;
309309
310+ describe ( '{import:"preact"}' , ( ) => {
311+ test ( 'should do nothing when pragma=false' , ( ) => {
312+ expect (
313+ transform ( 'var name="world",vnode=html`<div id=hello>hello, ${name}</div>`;' , {
314+ ...options ,
315+ plugins : [
316+ [ htmBabelPlugin , {
317+ pragma : false ,
318+ import : 'preact'
319+ } ]
320+ ]
321+ } ) . code
322+ ) . toBe ( `var name="world",vnode={tag:"div",props:{id:"hello"},children:["hello, ",name]};` ) ;
323+ } ) ;
324+ test ( 'should do nothing when tag is not used' , ( ) => {
325+ expect (
326+ transform ( 'console.log("hi");' , {
327+ ...options ,
328+ plugins : [
329+ [ htmBabelPlugin , {
330+ import : 'preact'
331+ } ]
332+ ]
333+ } ) . code
334+ ) . toBe ( `console.log("hi");` ) ;
335+ } ) ;
336+ test ( 'should add import' , ( ) => {
337+ expect (
338+ transform ( 'html`<div id=hello>hello</div>`;' , {
339+ ...options ,
340+ plugins : [
341+ [ htmBabelPlugin , {
342+ import : 'preact'
343+ } ]
344+ ]
345+ } ) . code
346+ ) . toBe ( `import{h}from"preact";h("div",{id:"hello"},"hello");` ) ;
347+ } ) ;
348+ test ( 'should add import for pragma' , ( ) => {
349+ expect (
350+ transform ( 'html`<div id=hello>hello</div>`;' , {
351+ ...options ,
352+ plugins : [
353+ [ htmBabelPlugin , {
354+ pragma : 'createElement' ,
355+ import : 'react'
356+ } ]
357+ ]
358+ } ) . code
359+ ) . toBe ( `import{createElement}from"react";createElement("div",{id:"hello"},"hello");` ) ;
360+ } ) ;
361+ } ) ;
362+
363+ describe ( '{import:Object}' , ( ) => {
364+ test ( 'should add import' , ( ) => {
365+ expect (
366+ transform ( 'html`<div id=hello>hello</div>`;' , {
367+ ...options ,
368+ plugins : [
369+ [ htmBabelPlugin , {
370+ import : {
371+ module : 'preact' ,
372+ export : 'h'
373+ }
374+ } ]
375+ ]
376+ } ) . code
377+ ) . toBe ( `import{h}from"preact";h("div",{id:"hello"},"hello");` ) ;
378+ } ) ;
379+ test ( 'should add import as pragma' , ( ) => {
380+ expect (
381+ transform ( 'html`<div id=hello>hello</div>`;' , {
382+ ...options ,
383+ plugins : [
384+ [ htmBabelPlugin , {
385+ pragma : 'hh' ,
386+ import : {
387+ module : 'preact' ,
388+ export : 'h'
389+ }
390+ } ]
391+ ]
392+ } ) . code
393+ ) . toBe ( `import{h as hh}from"preact";hh("div",{id:"hello"},"hello");` ) ;
394+ } ) ;
395+ test ( 'should add import default' , ( ) => {
396+ expect (
397+ transform ( 'html`<div id=hello>hello</div>`;' , {
398+ ...options ,
399+ plugins : [
400+ [ htmBabelPlugin , {
401+ pragma : 'React.createElement' ,
402+ import : {
403+ module : 'react' ,
404+ export : 'default'
405+ }
406+ } ]
407+ ]
408+ } ) . code
409+ ) . toBe ( `import React from"react";React.createElement("div",{id:"hello"},"hello");` ) ;
410+ } ) ;
411+ test ( 'should add import *' , ( ) => {
412+ expect (
413+ transform ( 'html`<div id=hello>hello</div>`;' , {
414+ ...options ,
415+ plugins : [
416+ [ htmBabelPlugin , {
417+ pragma : 'Preact.h' ,
418+ import : {
419+ module : 'preact' ,
420+ export : '*'
421+ }
422+ } ]
423+ ]
424+ } ) . code
425+ ) . toBe ( `import*as Preact from"preact";Preact.h("div",{id:"hello"},"hello");` ) ;
426+ } ) ;
427+ } ) ;
428+
310429 describe ( 'main test suite' , ( ) => {
311430 // Run all of the main tests against the Babel plugin:
312431 const mod = require ( 'fs' ) . readFileSync ( require ( 'path' ) . resolve ( __dirname , 'index.test.mjs' ) , 'utf8' ) . replace ( / \\ 0 / g, '\0' ) ;
0 commit comments