|
| 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