diff --git a/jest.config.js b/jest.config.js index 333fc68e..665708c9 100644 --- a/jest.config.js +++ b/jest.config.js @@ -58,15 +58,7 @@ 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: {}, // 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%", @@ -127,7 +119,7 @@ module.exports = { // runner: "jest-runner", // The paths to modules that run some code to configure or set up the testing environment before each test - //setupFiles: [], + setupFiles: [require.resolve('./scripts/__tests__/jest/jestEnvironment.js')], // A list of paths to modules that run some code to configure or set up the testing framework before each test setupFilesAfterEnv: [require.resolve('./scripts/__tests__/jest/jestSetting.js')], 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 63% rename from scripts/__tests__/ComponentTest/UseState.test.js rename to scripts/__tests__/ComponentTest/HookTest/UseState.test.js index ffd1cda0..adccf421 100644 --- a/scripts/__tests__/ComponentTest/UseState.test.js +++ b/scripts/__tests__/ComponentTest/HookTest/UseState.test.js @@ -1,30 +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'; +/* 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}

+