diff --git a/jest.config.js b/jest.config.js index 333fc68e..1c30d15d 100644 --- a/jest.config.js +++ b/jest.config.js @@ -58,15 +58,15 @@ module.exports = { // globalTeardown: undefined, // A set of global variables that need to be available in all test environments - // globals: { - // 'isDev': process.env.NODE_ENV === 'development', - // 'MessageChannel': function MessageChannel() { - // this.port1 = {}; - // this.port2 = { - // postMessage() {} - // }; - // } - // }, + globals: { + //'isDev': process.env.NODE_ENV === 'development', + 'MessageChannel': function MessageChannel() { + this.port1 = {}; + this.port2 = { + postMessage() {} + }; + } + }, // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers. // maxWorkers: "50%", diff --git a/scripts/__tests__/ComponentTest/HookTest/UseCallback.test.js b/scripts/__tests__/ComponentTest/HookTest/UseCallback.test.js new file mode 100644 index 00000000..071c4512 --- /dev/null +++ b/scripts/__tests__/ComponentTest/HookTest/UseCallback.test.js @@ -0,0 +1,36 @@ +/* eslint-disable no-undef */ +import * as React from '../../../../libs/horizon/src/external/Horizon'; +import * as HorizonDOM from '../../../../libs/horizon/src/dom/DOMExternal'; + +describe('useCallback Hook Test', () => { + const { useState, useCallback } = React; + + it('测试useCallback', () => { + const App = (props) => { + const [num, setNum] = useState(0); + const NumUseCallback = useCallback(() => { + setNum(num + props.text) + }, [props]); + return ( + <> +

{num}

+ + ; + + ) + + } + HorizonDOM.render(, container); + expect(LogUtils.getAndClear()).toEqual([1]); + expect(container.querySelector('p').innerHTML).toBe('1'); + // 点击按钮触发ref.current加1 + container.querySelector('button').click(); + // ref.current改变不会触发重新渲染 + expect(LogUtils.getAndClear()).toEqual([]); + expect(container.querySelector('p').innerHTML).toBe('1'); + }); +}); diff --git a/scripts/__tests__/ComponentTest/UseState.test.js b/scripts/__tests__/ComponentTest/HookTest/UseState.test.js similarity index 68% rename from scripts/__tests__/ComponentTest/UseState.test.js rename to scripts/__tests__/ComponentTest/HookTest/UseState.test.js index 02c3996f..adccf421 100644 --- a/scripts/__tests__/ComponentTest/UseState.test.js +++ b/scripts/__tests__/ComponentTest/HookTest/UseState.test.js @@ -1,31 +1,35 @@ -import * as React from '../../../libs/horizon/src/external/Horizon'; -import * as HorizonDOM from '../../../libs/horizon/src/dom/DOMExternal'; -import * as LogUtils from '../jest/logUtils'; -import { act } from '../jest/customMatcher'; +/* eslint-disable no-undef */ +import * as React from '../../../../libs/horizon/src/external/Horizon'; +import * as HorizonDOM from '../../../../libs/horizon/src/dom/DOMExternal'; +import * as LogUtils from '../../jest/logUtils'; +import { act } from '../../jest/customMatcher'; +import Text from '../../jest/Text'; describe('useState Hook Test', () => { - const { useState, forwardRef, useImperativeHandle, memo } = React; - const { unmountComponentAtNode } = HorizonDOM; - let container = null; - beforeEach(() => { - // 创建一个 DOM 元素作为渲染目标 - container = document.createElement('div'); - document.body.appendChild(container); - }); + const { + useState, + forwardRef, + useImperativeHandle, + memo + } = React; - afterEach(() => { - // 退出时进行清理 - unmountComponentAtNode(container); - container.remove(); - container = null; - LogUtils.clear(); + it('简单使用useState', () => { + const App = () => { + const [num, setNum] = useState(0); + return ( + <> +

{num}

+ - ; - - ) - - } - HorizonDOM.render(, container); - expect(LogUtils.getAndClear()).toEqual([1]); - expect(container.querySelector('p').innerHTML).toBe('1'); - // 点击按钮触发ref.current加1 - container.querySelector('button').click(); - // ref.current改变不会触发重新渲染 - expect(LogUtils.getAndClear()).toEqual([]); - expect(container.querySelector('p').innerHTML).toBe('1'); - }); - }) - - describe('useImperativeHandle Test', () => { - it('useImperativeHandle没有配置dep时自动更新', () => { - - let App = (props, ref) => { - const [num, setNum] = useState(0); - useImperativeHandle(ref, () => ({ num, setNum })); - return ; - } - let App1 = (props, ref) => { - const [num1, setNum1] = useState(0); - useImperativeHandle(ref, () => ({ num1, setNum1 }), []); - return ; - } - - App = forwardRef(App); - App1 = forwardRef(App1); - const counter = React.createRef(null); - const counter1 = React.createRef(null); - HorizonDOM.render(, container); - expect(LogUtils.getAndClear()).toEqual([0]); - expect(counter.current.num).toBe(0); - act(() => { - counter.current.setNum(1); - }); - expect(LogUtils.getAndClear()).toEqual([1]); - // useImperativeHandle没有配置的dep,所以会自动更新 - expect(counter.current.num).toBe(1); - // 清空container - unmountComponentAtNode(container); - - HorizonDOM.render(, container); - expect(LogUtils.getAndClear()).toEqual([0]); - expect(counter1.current.num1).toBe(0); - act(() => { - counter1.current.setNum1(1); - }); - expect(LogUtils.getAndClear()).toEqual([1]); - // useImperativeHandle的dep为[],所以不会变 - expect(counter1.current.num1).toBe(0); - }); - }) -}) diff --git a/scripts/__tests__/ComponentTest/SimpleUseHook.test.js b/scripts/__tests__/ComponentTest/SimpleUseHook.test.js deleted file mode 100644 index 0d99f8ed..00000000 --- a/scripts/__tests__/ComponentTest/SimpleUseHook.test.js +++ /dev/null @@ -1,312 +0,0 @@ -import * as React from '../../../libs/horizon/src/external/Horizon'; -import * as HorizonDOM from '../../../libs/horizon/src/dom/DOMExternal'; -import { act } from '../jest/customMatcher'; - - -describe('Hook Test', () => { - const { - useState, - useReducer, - useEffect, - useLayoutEffect, - useContext, - useMemo, - useCallback, - useRef, - useImperativeHandle, - forwardRef - } = React; - const { unmountComponentAtNode } = HorizonDOM; - let container = null; - beforeEach(() => { - // 创建一个 DOM 元素作为渲染目标 - container = document.createElement('div'); - document.body.appendChild(container); - }); - - afterEach(() => { - // 退出时进行清理 - unmountComponentAtNode(container); - container.remove(); - container = null; - }); - - - it('简单使用useState', () => { - const App = () => { - const [num, setNum] = useState(0); - return ( - <> -

{num}

-