Match-id-89458c22ed2f2dfd010d67ad0d278bf567069cf2

This commit is contained in:
* 2022-03-01 11:57:41 +08:00 committed by *
parent 9ca4b3851b
commit 9ca104278e
6 changed files with 48 additions and 54 deletions

View File

@ -127,7 +127,7 @@ module.exports = {
// runner: "jest-runner", // runner: "jest-runner",
// The paths to modules that run some code to configure or set up the testing environment before each test // The paths to modules that run some code to configure or set up the testing environment before each test
setupFiles: [require.resolve('./scripts/__tests__/jest/setupTests.js')], //setupFiles: [],
// A list of paths to modules that run some code to configure or set up the testing framework before each test // 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/setupEnvironment.js')], setupFilesAfterEnv: [require.resolve('./scripts/__tests__/jest/setupEnvironment.js')],

View File

@ -1,7 +1,6 @@
import * as React from '../../../libs/horizon/src/external/Horizon'; import * as React from '../../../libs/horizon/src/external/Horizon';
import * as HorizonDOM from '../../../libs/horizon/src/dom/DOMExternal'; import * as HorizonDOM from '../../../libs/horizon/src/dom/DOMExternal';
//import { act } from 'react-dom/test-utils'; import * as LogUtils from '../jest/logUtils';
import * as Scheduler from 'scheduler';
describe('useState Hook Test', () => { describe('useState Hook Test', () => {
const { useState, forwardRef, useImperativeHandle, memo } = React; const { useState, forwardRef, useImperativeHandle, memo } = React;
@ -18,11 +17,11 @@ describe('useState Hook Test', () => {
unmountComponentAtNode(container); unmountComponentAtNode(container);
container.remove(); container.remove();
container = null; container = null;
Scheduler.reset(); LogUtils.reset();
}); });
const Text = (props) => { const Text = (props) => {
Scheduler.unstable_yieldValue(props.text); LogUtils.injectValue(props.text);
return <p>{props.text}</p>; return <p>{props.text}</p>;
} }
@ -80,17 +79,17 @@ describe('useState Hook Test', () => {
} }
HorizonDOM.render(<App />, container); HorizonDOM.render(<App />, container);
expect(container.querySelector('p').innerHTML).toBe('0'); expect(container.querySelector('p').innerHTML).toBe('0');
expect(Scheduler).toHaveYielded([0]); expect(LogUtils).toMatchValue([0]);
// useState修改state 时,设置相同的值,函数组件不会重新渲染 // useState修改state 时,设置相同的值,函数组件不会重新渲染
setNum(0); setNum(0);
expect(Scheduler).toHaveYielded([]); expect(LogUtils).toMatchValue([]);
expect(container.querySelector('p').innerHTML).toBe('0'); expect(container.querySelector('p').innerHTML).toBe('0');
}); });
it('useState的惰性初始化', () => { it('useState的惰性初始化', () => {
const App = forwardRef((props, ref) => { const App = forwardRef((props, ref) => {
const [num, setNum] = useState(() => { const [num, setNum] = useState(() => {
Scheduler.unstable_yieldValue(props.initNum); LogUtils.injectValue(props.initNum);
return props.initNum return props.initNum
}); });
useImperativeHandle(ref, () => ({ setNum })) useImperativeHandle(ref, () => ({ setNum }))
@ -99,12 +98,12 @@ describe('useState Hook Test', () => {
}) })
const ref = React.createRef(null); const ref = React.createRef(null);
HorizonDOM.render(<App initNum={1} ref={ref} />, container); HorizonDOM.render(<App initNum={1} ref={ref} />, container);
expect(Scheduler).toHaveYielded([1]); expect(LogUtils).toMatchValue([1]);
expect(container.querySelector('p').innerHTML).toBe('1'); expect(container.querySelector('p').innerHTML).toBe('1');
// 设置num为3 // 设置num为3
ref.current.setNum(3); ref.current.setNum(3);
// 初始化函数只在初始渲染时被调用,所以Scheduler里的dataArray清空后没有新增。 // 初始化函数只在初始渲染时被调用,所以Scheduler里的dataArray清空后没有新增。
expect(Scheduler).toHaveYielded([]); expect(LogUtils).toMatchValue([]);
expect(container.querySelector('p').innerHTML).toBe('3'); expect(container.querySelector('p').innerHTML).toBe('3');
}); });
@ -116,15 +115,15 @@ describe('useState Hook Test', () => {
return <Text text={num} />; return <Text text={num} />;
}) })
HorizonDOM.render(<App />, container); HorizonDOM.render(<App />, container);
expect(Scheduler).toHaveYielded([0]); expect(LogUtils).toMatchValue([0]);
expect(container.querySelector('p').innerHTML).toBe('0'); expect(container.querySelector('p').innerHTML).toBe('0');
// 不会重新渲染 // 不会重新渲染
HorizonDOM.render(<App />, container); HorizonDOM.render(<App />, container);
expect(Scheduler).toHaveYielded([]); expect(LogUtils).toMatchValue([]);
expect(container.querySelector('p').innerHTML).toBe('0'); expect(container.querySelector('p').innerHTML).toBe('0');
// 会重新渲染 // 会重新渲染
setNum(1) setNum(1)
expect(Scheduler).toHaveYielded([1]); expect(LogUtils).toMatchValue([1]);
expect(container.querySelector('p').innerHTML).toBe('1'); expect(container.querySelector('p').innerHTML).toBe('1');
}); });
}); });

View File

@ -1,4 +1,4 @@
function captureAssertion(fn) { function runAssertion(fn) {
try { try {
fn(); fn();
} catch (error) { } catch (error) {
@ -10,13 +10,13 @@ function captureAssertion(fn) {
return {pass: true}; return {pass: true};
} }
function toHaveYielded(Scheduler, expectedYields) { function toMatchValue(LogUtils, expectedValues) {
return captureAssertion(() => { return runAssertion(() => {
const actualYields = Scheduler.unstable_clearYields(); const actualValues = LogUtils.getAndClearValue();
expect(actualYields).toEqual(expectedYields); expect(actualValues).toEqual(expectedValues);
}); });
} }
module.exports = { module.exports = {
toHaveYielded, toMatchValue,
}; };

View File

@ -0,0 +1,30 @@
let dataArray = null;
let isWorking = false;
const injectValue = (value) => {
if (dataArray === null) {
dataArray = [value];
} else {
dataArray.push(value);
}
};
const getAndClearValue = () => {
if (dataArray === null) {
return [];
}
const values = dataArray;
dataArray = null;
return values;
};
const reset = () => {
if (isWorking) {
throw new Error('Cannot reset while already flushing work.');
}
dataArray = null;
}
exports.reset = reset;
exports.injectValue = injectValue;
exports.getAndClearValue = getAndClearValue;

View File

@ -1 +0,0 @@
jest.mock('scheduler', () => require('./testUtil.js'));

View File

@ -1,34 +0,0 @@
if (process.env.NODE_ENV !== 'production') {
(function () {
let dataArray = null;
let isFlushing = false;
const unstable_yieldValue = (value) => {
if (dataArray === null) {
dataArray = [value];
} else {
dataArray.push(value);
}
};
const unstable_clearYields = () => {
if (dataArray === null) {
return [];
}
const values = dataArray;
dataArray = null;
return values;
};
const reset = () => {
if (isFlushing) {
throw new Error('Cannot reset while already flushing work.');
}
dataArray = null;
}
exports.reset = reset;
exports.unstable_yieldValue = unstable_yieldValue;
exports.unstable_clearYields = unstable_clearYields;
})();
}