11import Ctx from './ctx' ;
2- beforeEach ( ( ) => {
3- window . Babel = { } ;
4- } ) ;
5- afterEach ( ( ) => {
6- delete window . Babel ;
7- } ) ;
2+ import React from 'react' ;
3+ import * as Babel from '@babel/standalone' ;
4+ beforeEach ( ( ) => { } ) ;
5+ afterEach ( ( ) => { } ) ;
86describe ( 'constructor :' , ( ) => {
97 test ( 'it should work correctly without errors' , ( ) => {
10- new Ctx ( ) ;
8+ new Ctx ( React , Babel ) ;
119 expect ( 1 ) . toBe ( 1 ) ;
1210 } ) ;
13- test ( 'it should throw an error when Babel global variable is not existed' , ( ) => {
14- delete window . Babel ;
11+ test ( 'it should throw an error when Babel value is not passed in to it' , ( ) => {
1512 expect . assertions ( 1 ) ;
1613 try {
17- new Ctx ( ) ;
14+ new Ctx ( React , undefined ) ;
1815 } catch ( er ) {
19- expect ( er . message ) . toBe ( `string-to-react-component package needs @babel/standalone for working correctly.
20- you should load @babel/standalone in the browser.` ) ;
16+ expect ( er . message ) . toBe (
17+ `Package "string-to-react-component" has a missing peer dependency of "@babel/standalone" ( requires "^7.23.10" )` ,
18+ ) ;
2119 }
2220 } ) ;
2321 test ( 'check _parentTemp property' , ( ) => {
24- const ins = new Ctx ( ) ;
22+ const ins = new Ctx ( React , Babel ) ;
2523 expect ( ins . _parentTemp ) . toBe ( `"use strict";\nreturn @temp;` ) ;
2624 } ) ;
25+ test ( 'it should set React global variable if it is not existed' , ( ) => {
26+ window . React = undefined ;
27+ new Ctx ( React , Babel ) ;
28+ expect ( window . React ) . toEqual ( React ) ;
29+ window . React = undefined ;
30+ const _React = { } ;
31+ new Ctx ( _React , Babel ) ;
32+ expect ( window . React ) . toEqual ( _React ) ;
33+ new Ctx ( React , Babel ) ;
34+ expect ( window . React ) . toEqual ( _React ) ;
35+ window . React = undefined ;
36+ } ) ;
37+ test ( 'the initial value of _com prop should be a function which returns null' , ( ) => {
38+ const ins = new Ctx ( React , Babel ) ;
39+ expect ( typeof ins . _com ) . toBe ( 'function' ) ;
40+ expect ( ins . _com ( ) ) . toBe ( null ) ;
41+ } ) ;
2742} ) ;
2843describe ( 'methods : ' , ( ) => {
2944 test ( '_validateTemplate method ' , ( ) => {
3045 expect . assertions ( 3 ) ;
31- const ins = new Ctx ( ) ;
46+ const ins = new Ctx ( React , Babel ) ;
3247 try {
3348 ins . _validateTemplate ( { } ) ;
3449 } catch ( er ) {
@@ -47,7 +62,7 @@ describe('methods : ', () => {
4762 } ) ;
4863 test ( '_validateCodeInsideTheTemp method' , ( ) => {
4964 expect . assertions ( 3 ) ;
50- const ins = new Ctx ( ) ;
65+ const ins = new Ctx ( React , Babel ) ;
5166 {
5267 ins . _com = ( ) => { } ;
5368 ins . _validateCodeInsideTheTemp ( ) ;
@@ -69,14 +84,14 @@ describe('methods : ', () => {
6984 }
7085 } ) ;
7186 test ( '_generateCom method' , ( ) => {
72- const ins = new Ctx ( ) ;
87+ const ins = new Ctx ( React , Babel ) ;
7388 ins . _transpile = ( ) => '() => {}' ;
7489 ins . _validateCodeInsideTheTemp = jest . fn ( ( ) => { } ) ;
7590 ins . _generateCom ( ) ;
7691 expect ( ins . _validateCodeInsideTheTemp . mock . calls . length ) . toBe ( 1 ) ;
7792 } ) ;
7893 test ( 'updateTemplate method' , ( ) => {
79- const ins = new Ctx ( ) ;
94+ const ins = new Ctx ( React , Babel ) ;
8095 ins . _validateTemplate = jest . fn ( ) ;
8196 ins . _generateCom = jest . fn ( ) ;
8297 let temp = '()=>3' ;
@@ -89,4 +104,36 @@ describe('methods : ', () => {
89104 ins . updateTemplate ( temp ) ;
90105 expect ( ins . _generateCom . mock . calls . length ) . toBe ( 1 ) ;
91106 } ) ;
107+ test ( '_checkBabelOptions method should set react preset and throw an error with invalid parameter' , ( ) => {
108+ expect . assertions ( 4 ) ;
109+ const ins = new Ctx ( React , Babel ) ;
110+ try {
111+ ins . _checkBabelOptions ( [ ] ) ;
112+ } catch ( e ) {
113+ expect ( e . message ) . toBe ( `babelOptions prop of string-to-react-component element should be an object.` ) ;
114+ }
115+ try {
116+ ins . _checkBabelOptions ( { presets : { } } ) ;
117+ } catch ( e ) {
118+ expect ( e . message ) . toBe (
119+ `string-to-react-component Error : presets property of babelOptions prop should be an array` ,
120+ ) ;
121+ }
122+ let babelOp = { } ;
123+ ins . _checkBabelOptions ( babelOp ) ;
124+ expect ( babelOp . presets . indexOf ( 'react' ) >= 0 ) . toBe ( true ) ;
125+ babelOp = { presets : [ ] } ;
126+ ins . _checkBabelOptions ( babelOp ) ;
127+ expect ( babelOp . presets . indexOf ( 'react' ) >= 0 ) . toBe ( true ) ;
128+ } ) ;
129+ test ( '_transpile method should return "null" when _temp is an empty string' , ( ) => {
130+ const ins = new Ctx ( React , Babel ) ;
131+ expect ( ins . _transpile ( { } ) ) . toBe ( 'null' ) ;
132+ } ) ;
133+ test ( '_transpile method should return the transpiled code' , ( ) => {
134+ const ins = new Ctx ( React , Babel ) ;
135+ ins . _temp = `()=><div>2</div>` ;
136+ const code = ins . _transpile ( { filename : 'counter.ts' } ) ;
137+ expect ( code ) . toBe ( '() => /*#__PURE__*/React.createElement("div", null, "2");\n//# sourceURL=counter.ts' ) ;
138+ } ) ;
92139} ) ;
0 commit comments