Skip to content

Commit 5cf5132

Browse files
update tests
1 parent 1153644 commit 5cf5132

File tree

5 files changed

+1363
-1171
lines changed

5 files changed

+1363
-1171
lines changed

package-lock.json

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"deploy": "gh-pages -d demo"
3434
},
3535
"peerDependencies": {
36-
"@babel/standalone": "^7.23.10",
36+
"@babel/standalone": "^7.24.3",
3737
"react": ">=16.8.0",
3838
"react-dom": ">=16.8.0"
3939
},
@@ -70,7 +70,8 @@
7070
"rollup": "^2.41.0",
7171
"rollup-plugin-terser": "7.0.2",
7272
"webpack": "^4.44.1",
73-
"webpack-cli": "^3.3.12"
73+
"webpack-cli": "^3.3.12",
74+
"@babel/standalone": "^7.24.3"
7475
},
7576
"files": [
7677
"plugins",

src/ctx.test.js

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,49 @@
11
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(() => {});
86
describe('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
});
2843
describe('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
});

src/stringToReact.test.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import React from 'react';
2+
import * as Babel from '@babel/standalone';
23
import {render, unmountComponentAtNode} from 'react-dom';
34
import {act} from 'react-dom/test-utils';
4-
import StrintToReact from './strintToReact.js';
5-
import Ctx from './ctx.js';
5+
import StrintToReact from './strintToReact';
6+
import Ctx from './ctx';
7+
const react = React;
68
let container = document.createElement('div');
79
const str = `()=><p id='someText'>some text</p>`;
810
const str2 = `()=><p id='someText2'>some text2</p>`;
@@ -11,8 +13,6 @@ beforeAll(() => {
1113
document.body.appendChild(container);
1214
});
1315
beforeEach(() => {
14-
window.Babel = window.Babel || {};
15-
window.React = window.React || React;
1616
renderApp = (temp, deps, rerender, temp2) => {
1717
let secondRender = false;
1818
const StrintToReactCom = StrintToReact.bind(undefined, deps);
@@ -32,7 +32,6 @@ beforeEach(() => {
3232
};
3333
});
3434
afterEach(() => {
35-
delete window.Babel;
3635
unmountComponentAtNode(container);
3736
container.innerHTML = '';
3837
renderApp = null;
@@ -45,7 +44,7 @@ describe('rendering : ', () => {
4544
test('generated component from string should be updated when props.children is changed', () => {
4645
let _ctx, _ctx2;
4746
const getCtx = function () {
48-
_ctx = new Ctx(React);
47+
_ctx = new Ctx(React, Babel);
4948
_ctx.getComponent = jest.fn(() => _ctx._com);
5049
_ctx._transpile = jest.fn(
5150
() => `() => /*#__PURE__*/React.createElement("p", {
@@ -55,7 +54,7 @@ describe('rendering : ', () => {
5554
return _ctx;
5655
},
5756
getCtx2 = function () {
58-
_ctx2 = new Ctx(React);
57+
_ctx2 = new Ctx(React, Babel);
5958
_ctx2.getComponent = jest.fn(() => _ctx2._com);
6059
_ctx2._transpile = jest.fn(
6160
() => `() => /*#__PURE__*/React.createElement("p", {
@@ -64,17 +63,17 @@ describe('rendering : ', () => {
6463
);
6564
return _ctx2;
6665
};
67-
renderApp(str, {getCtx}, true);
66+
renderApp(str, {getCtx, react, Babel}, true);
6867
expect(_ctx.getComponent.mock.calls.length).toBe(2);
6968
expect(_ctx._transpile.mock.calls.length).toBe(1);
70-
renderApp(str, {getCtx: getCtx2}, true, str2);
69+
renderApp(str, {getCtx: getCtx2, react, Babel}, true, str2);
7170
expect(_ctx2.getComponent.mock.calls.length).toBe(2);
7271
expect(_ctx2._transpile.mock.calls.length).toBe(2);
7372
});
7473
test('it should call updateTemplate method with props.children as a parameter', () => {
7574
let _ctx;
7675
const getCtx = function () {
77-
_ctx = new Ctx(React);
76+
_ctx = new Ctx(React, Babel);
7877
const updateTemplate = _ctx.updateTemplate;
7978
_ctx.updateTemplate = jest.fn((temp) => updateTemplate.call(_ctx, temp));
8079
_ctx._transpile = jest.fn(
@@ -84,14 +83,14 @@ describe('rendering : ', () => {
8483
);
8584
return _ctx;
8685
};
87-
renderApp(str, {getCtx});
86+
renderApp(str, {getCtx, react, Babel});
8887
expect(_ctx.updateTemplate.mock.calls[0][0]).toBe(str);
8988
});
9089
});
9190
describe('React global variable', () => {
9291
test('The constructor should set the React global variable', () => {
9392
window.React = undefined;
94-
new Ctx(React);
93+
new Ctx(React, Babel);
9594
expect(window.React).toEqual(React);
9695
window.React = React;
9796
});

0 commit comments

Comments
 (0)