|
| 1 | +import React from 'react'; |
| 2 | +import {render, unmountComponentAtNode} from 'react-dom'; |
| 3 | +import {act} from 'react-dom/test-utils'; |
| 4 | +import StrintToReact from './strintToReact.js'; |
| 5 | +import Ctx from './ctx.js'; |
| 6 | +let container = document.createElement('div'); |
| 7 | +const str = `()=><p id='someText'>some text</p>`; |
| 8 | +const str2 = `()=><p id='someText2'>some text2</p>`; |
| 9 | +let renderApp; |
| 10 | +beforeAll(() => { |
| 11 | + document.body.appendChild(container); |
| 12 | +}); |
| 13 | +beforeEach(() => { |
| 14 | + renderApp = (temp, deps, rerender, temp2) => { |
| 15 | + let secondRender = false; |
| 16 | + const StrintToReactCom = StrintToReact.bind(undefined, deps); |
| 17 | + const App = function () { |
| 18 | + const template = secondRender ? temp2 || str : temp || str; |
| 19 | + return <StrintToReactCom>{template}</StrintToReactCom>; |
| 20 | + }; |
| 21 | + act(() => { |
| 22 | + render(<App></App>, container); |
| 23 | + }); |
| 24 | + if (rerender) { |
| 25 | + secondRender = true; |
| 26 | + act(() => { |
| 27 | + render(<App></App>, container); |
| 28 | + }); |
| 29 | + } |
| 30 | + }; |
| 31 | +}); |
| 32 | +afterEach(() => { |
| 33 | + unmountComponentAtNode(container); |
| 34 | + container.innerHTML = ''; |
| 35 | + renderApp = null; |
| 36 | +}); |
| 37 | +afterAll(() => { |
| 38 | + document.body.removeChild(container); |
| 39 | + container = null; |
| 40 | +}); |
| 41 | +describe('rendering : ', () => { |
| 42 | + test('generated component from string should be updated when props.children is changed', () => { |
| 43 | + let _ctx, _ctx2; |
| 44 | + const getCtx = function () { |
| 45 | + _ctx = new Ctx(); |
| 46 | + _ctx.getComponent = jest.fn(() => _ctx._com); |
| 47 | + _ctx._transpile = jest.fn( |
| 48 | + () => `() => /*#__PURE__*/React.createElement("p", { |
| 49 | + id: "someText" |
| 50 | + }, "some text");`, |
| 51 | + ); |
| 52 | + return _ctx; |
| 53 | + }, |
| 54 | + getCtx2 = function () { |
| 55 | + _ctx2 = new Ctx(); |
| 56 | + _ctx2.getComponent = jest.fn(() => _ctx2._com); |
| 57 | + _ctx2._transpile = jest.fn( |
| 58 | + () => `() => /*#__PURE__*/React.createElement("p", { |
| 59 | + id: "someText2" |
| 60 | + }, "some text2");`, |
| 61 | + ); |
| 62 | + return _ctx2; |
| 63 | + }; |
| 64 | + renderApp(str, {getCtx}, true); |
| 65 | + expect(_ctx.getComponent.mock.calls.length).toBe(2); |
| 66 | + expect(_ctx._transpile.mock.calls.length).toBe(1); |
| 67 | + renderApp(str, {getCtx: getCtx2}, true, str2); |
| 68 | + expect(_ctx2.getComponent.mock.calls.length).toBe(2); |
| 69 | + expect(_ctx2._transpile.mock.calls.length).toBe(2); |
| 70 | + }); |
| 71 | + test('it should call updateTemplate method with props.children as a parameter', () => { |
| 72 | + let _ctx; |
| 73 | + const getCtx = function () { |
| 74 | + _ctx = new Ctx(); |
| 75 | + const updateTemplate = _ctx.updateTemplate; |
| 76 | + _ctx.updateTemplate = jest.fn((temp) => updateTemplate.call(_ctx, temp)); |
| 77 | + _ctx._transpile = jest.fn( |
| 78 | + () => `() => /*#__PURE__*/React.createElement("p", { |
| 79 | + id: "someText" |
| 80 | + }, "some text");`, |
| 81 | + ); |
| 82 | + return _ctx; |
| 83 | + }; |
| 84 | + renderApp(str, {getCtx}); |
| 85 | + expect(_ctx.updateTemplate.mock.calls[0][0]).toBe(str); |
| 86 | + }); |
| 87 | +}); |
| 88 | +describe('React global variable', () => { |
| 89 | + test('this package should set the React global variable', () => { |
| 90 | + expect(window.React).toEqual(React); |
| 91 | + }); |
| 92 | +}); |
0 commit comments