1
1
import 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 ( ( ) => { } ) ;
8
6
describe ( 'constructor :' , ( ) => {
9
7
test ( 'it should work correctly without errors' , ( ) => {
10
- new Ctx ( ) ;
8
+ new Ctx ( React , Babel ) ;
11
9
expect ( 1 ) . toBe ( 1 ) ;
12
10
} ) ;
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' , ( ) => {
15
12
expect . assertions ( 1 ) ;
16
13
try {
17
- new Ctx ( ) ;
14
+ new Ctx ( React , undefined ) ;
18
15
} 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
+ ) ;
21
19
}
22
20
} ) ;
23
21
test ( 'check _parentTemp property' , ( ) => {
24
- const ins = new Ctx ( ) ;
22
+ const ins = new Ctx ( React , Babel ) ;
25
23
expect ( ins . _parentTemp ) . toBe ( `"use strict";\nreturn @temp;` ) ;
26
24
} ) ;
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
+ } ) ;
27
42
} ) ;
28
43
describe ( 'methods : ' , ( ) => {
29
44
test ( '_validateTemplate method ' , ( ) => {
30
45
expect . assertions ( 3 ) ;
31
- const ins = new Ctx ( ) ;
46
+ const ins = new Ctx ( React , Babel ) ;
32
47
try {
33
48
ins . _validateTemplate ( { } ) ;
34
49
} catch ( er ) {
@@ -47,7 +62,7 @@ describe('methods : ', () => {
47
62
} ) ;
48
63
test ( '_validateCodeInsideTheTemp method' , ( ) => {
49
64
expect . assertions ( 3 ) ;
50
- const ins = new Ctx ( ) ;
65
+ const ins = new Ctx ( React , Babel ) ;
51
66
{
52
67
ins . _com = ( ) => { } ;
53
68
ins . _validateCodeInsideTheTemp ( ) ;
@@ -69,14 +84,14 @@ describe('methods : ', () => {
69
84
}
70
85
} ) ;
71
86
test ( '_generateCom method' , ( ) => {
72
- const ins = new Ctx ( ) ;
87
+ const ins = new Ctx ( React , Babel ) ;
73
88
ins . _transpile = ( ) => '() => {}' ;
74
89
ins . _validateCodeInsideTheTemp = jest . fn ( ( ) => { } ) ;
75
90
ins . _generateCom ( ) ;
76
91
expect ( ins . _validateCodeInsideTheTemp . mock . calls . length ) . toBe ( 1 ) ;
77
92
} ) ;
78
93
test ( 'updateTemplate method' , ( ) => {
79
- const ins = new Ctx ( ) ;
94
+ const ins = new Ctx ( React , Babel ) ;
80
95
ins . _validateTemplate = jest . fn ( ) ;
81
96
ins . _generateCom = jest . fn ( ) ;
82
97
let temp = '()=>3' ;
@@ -89,4 +104,36 @@ describe('methods : ', () => {
89
104
ins . updateTemplate ( temp ) ;
90
105
expect ( ins . _generateCom . mock . calls . length ) . toBe ( 1 ) ;
91
106
} ) ;
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
+ } ) ;
92
139
} ) ;
0 commit comments