Skip to content

Commit f2886b6

Browse files
update ctx.js and add ctx.test.js
1 parent 93e7d19 commit f2886b6

File tree

2 files changed

+106
-9
lines changed

2 files changed

+106
-9
lines changed

src/ctx.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ class Ctx {
22
constructor() {
33
this._temp = '';
44
this._parentTemp = `"use strict";return @temp;`;
5-
this._defaultCom = null;
65
this._com = null;
76
if (!(window.hasOwnProperty('Babel') && typeof window.Babel === 'object')) {
8-
throw new Error(`
9-
string-to-react-component package needs @babel/standalone for working correctly.
10-
you should load @babel/standalone in the browser.
11-
`);
7+
throw new Error(`string-to-react-component package needs @babel/standalone for working correctly.
8+
you should load @babel/standalone in the browser.`);
129
}
1310
this._b = window.Babel;
1411
this._babelpresets = ['react'];
@@ -19,19 +16,24 @@ class Ctx {
1916
}).code;
2017
}
2118
_generateCom() {
22-
this._com = this._temp ? Function(this._parentTemp.replace('@temp', this._transpile()))() : this._defaultCom;
19+
this._com = Function(this._parentTemp.replace('@temp', this._transpile()))();
20+
this._validateCodeInsideTheTemp();
21+
}
22+
_validateCodeInsideTheTemp() {
23+
if (typeof this._com !== 'function') {
24+
throw new Error(`code inside the passed string into string-to-react-component, should be a function`);
25+
}
2326
}
2427
_validateTemplate(temp) {
2528
if (typeof temp !== 'string') {
26-
throw `passed child into string-to-react-component element should b a string`;
29+
throw new Error(`passed child into string-to-react-component element should b a string`);
2730
}
2831
if (temp === '') {
29-
throw `passed string into string-to-react-component element can not be empty`;
32+
throw new Error(`passed string into string-to-react-component element can not be empty`);
3033
}
3134
}
3235
updateTemplate(template) {
3336
this._validateTemplate(template);
34-
template = template || '';
3537
if (template !== this._temp) {
3638
this._temp = template;
3739
this._generateCom();

src/ctx.test.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import Ctx from './ctx.js';
2+
import {TestScheduler} from 'jest';
3+
beforeEach(() => {
4+
window.Babel = {};
5+
});
6+
afterEach(() => {
7+
delete window.Babel;
8+
});
9+
describe('constructor :', () => {
10+
test('it should work correctly without errors', () => {
11+
new Ctx();
12+
expect(1).toBe(1);
13+
});
14+
test('it should throw an error when Babel global variable is not existed', () => {
15+
delete window.Babel;
16+
expect.assertions(1);
17+
try {
18+
new Ctx();
19+
} catch (er) {
20+
expect(er.message).toBe(`string-to-react-component package needs @babel/standalone for working correctly.
21+
you should load @babel/standalone in the browser.`);
22+
}
23+
});
24+
test('check _parentTemp property', () => {
25+
const ins = new Ctx();
26+
expect(ins._parentTemp).toBe(`"use strict";return @temp;`);
27+
});
28+
test('check _babelpresets property', () => {
29+
const ins = new Ctx();
30+
expect(ins._babelpresets).toEqual(['react']);
31+
});
32+
});
33+
describe('methods : ', () => {
34+
test('_validateTemplate method ', () => {
35+
expect.assertions(3);
36+
const ins = new Ctx();
37+
try {
38+
ins._validateTemplate({});
39+
} catch (er) {
40+
expect(er.message).toBe('passed child into string-to-react-component element should b a string');
41+
}
42+
try {
43+
ins._validateTemplate();
44+
} catch (er) {
45+
expect(er.message).toBe('passed child into string-to-react-component element should b a string');
46+
}
47+
try {
48+
ins._validateTemplate('');
49+
} catch (er) {
50+
expect(er.message).toBe('passed string into string-to-react-component element can not be empty');
51+
}
52+
});
53+
test('_validateCodeInsideTheTemp method', () => {
54+
expect.assertions(3);
55+
const ins = new Ctx();
56+
try {
57+
ins._com = () => {};
58+
ins._validateCodeInsideTheTemp();
59+
expect(1).toBe(1);
60+
} catch (er) {}
61+
try {
62+
class c {}
63+
ins._com = c;
64+
ins._validateCodeInsideTheTemp();
65+
expect(1).toBe(1);
66+
} catch (er) {}
67+
try {
68+
ins._com = {};
69+
ins._validateCodeInsideTheTemp();
70+
} catch (er) {
71+
expect(er.message).toBe('code inside the passed string into string-to-react-component, should be a function');
72+
}
73+
});
74+
test('_generateCom method', () => {
75+
const ins = new Ctx();
76+
ins._transpile = () => '() => {}';
77+
ins._validateCodeInsideTheTemp = jest.fn(() => {});
78+
ins._generateCom();
79+
expect(ins._validateCodeInsideTheTemp.mock.calls.length).toBe(1);
80+
});
81+
test('updateTemplate method', () => {
82+
const ins = new Ctx();
83+
ins._validateTemplate = jest.fn();
84+
ins._generateCom = jest.fn();
85+
let temp = '()=>3';
86+
ins._temp = '()=>3';
87+
ins.updateTemplate(temp);
88+
expect(ins._validateTemplate.mock.calls.length).toBe(1);
89+
expect(ins._validateTemplate.mock.calls[0][0]).toBe(temp);
90+
expect(ins._generateCom.mock.calls.length).toBe(0);
91+
ins._temp = '';
92+
ins.updateTemplate(temp);
93+
expect(ins._generateCom.mock.calls.length).toBe(1);
94+
});
95+
});

0 commit comments

Comments
 (0)