Match-id-094caf9a8c8fd7bbc214a6ea1eee473a5bb4f8bf
This commit is contained in:
parent
639a797ef1
commit
e8a1e5951a
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
import {asyncUpdates} from '../renderer/TreeBuilder';
|
import {asyncUpdates} from '../renderer/TreeBuilder';
|
||||||
import {callRenderQueueImmediate} from '../renderer/taskExecutor/RenderQueue';
|
import {callRenderQueueImmediate} from '../renderer/taskExecutor/RenderQueue';
|
||||||
import {runAsyncEffects} from '../renderer/submit/HookEffectHandler';
|
import {hasAsyncEffects, runAsyncEffects} from '../renderer/submit/HookEffectHandler';
|
||||||
import {isPromise} from '../renderer/ErrorHandler';
|
import {isPromise} from '../renderer/ErrorHandler';
|
||||||
|
|
||||||
interface Thenable {
|
interface Thenable {
|
||||||
|
@ -62,12 +62,19 @@ function act(fun: () => void | Thenable): Thenable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 防止死循环
|
||||||
|
const LOOPING_LIMIT = 50;
|
||||||
|
let loopingCount = 0;
|
||||||
function callRenderQueue() {
|
function callRenderQueue() {
|
||||||
callRenderQueueImmediate();
|
callRenderQueueImmediate();
|
||||||
|
|
||||||
|
while (hasAsyncEffects() && loopingCount < LOOPING_LIMIT) {
|
||||||
|
loopingCount++;
|
||||||
runAsyncEffects();
|
runAsyncEffects();
|
||||||
// effects可能产生刷新任务,这里再执行一次
|
// effects可能产生刷新任务,这里再执行一次
|
||||||
callRenderQueueImmediate();
|
callRenderQueueImmediate();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
act
|
act
|
||||||
|
|
|
@ -25,6 +25,11 @@ import { EffectConstant } from '../hooks/EffectConstant';
|
||||||
|
|
||||||
let hookEffects: Array<HookEffect | VNode> = [];
|
let hookEffects: Array<HookEffect | VNode> = [];
|
||||||
let hookRemoveEffects: Array<HookEffect | VNode> = [];
|
let hookRemoveEffects: Array<HookEffect | VNode> = [];
|
||||||
|
|
||||||
|
export function hasAsyncEffects() {
|
||||||
|
return hookEffects.length > 0 || hookRemoveEffects.length > 0;
|
||||||
|
}
|
||||||
|
|
||||||
// 是否正在异步调度effects
|
// 是否正在异步调度effects
|
||||||
let isScheduling = false;
|
let isScheduling = false;
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020 Huawei Technologies Co.,Ltd.
|
||||||
|
*
|
||||||
|
* openGauss is licensed under Mulan PSL v2.
|
||||||
|
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||||
|
* You may obtain a copy of Mulan PSL v2 at:
|
||||||
|
*
|
||||||
|
* http://license.coscl.org.cn/MulanPSL2
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||||
|
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||||
|
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||||
|
* See the Mulan PSL v2 for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import Horizon, { render, useState, act, useEffect } from '@cloudsop/horizon/index.ts';
|
||||||
|
|
||||||
|
describe('Horizon.act function Test', () => {
|
||||||
|
it('Parent can get Child instance by refs', function () {
|
||||||
|
const Parent = props => {
|
||||||
|
const [buttonOptions, setBtn] = useState([]);
|
||||||
|
const [checkedRows, setCheckedRows] = useState([]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setBtn([1, 2, 3]);
|
||||||
|
}, [checkedRows.length]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<Child buttonOptions={buttonOptions}></Child>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const Child = props => {
|
||||||
|
const { buttonOptions } = props;
|
||||||
|
const [btnList, setBtnList] = useState(0);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setBtnList(buttonOptions.length);
|
||||||
|
}, [buttonOptions]);
|
||||||
|
|
||||||
|
return <div id="childDiv">{btnList}</div>;
|
||||||
|
};
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
render(<Parent />, container);
|
||||||
|
});
|
||||||
|
|
||||||
|
// act能够等待useEffect触发的update完成
|
||||||
|
expect(container.querySelector('#childDiv').innerHTML).toEqual('3');
|
||||||
|
});
|
||||||
|
});
|
|
@ -0,0 +1,51 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020 Huawei Technologies Co.,Ltd.
|
||||||
|
*
|
||||||
|
* openGauss is licensed under Mulan PSL v2.
|
||||||
|
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||||
|
* You may obtain a copy of Mulan PSL v2 at:
|
||||||
|
*
|
||||||
|
* http://license.coscl.org.cn/MulanPSL2
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||||
|
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||||
|
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||||
|
* See the Mulan PSL v2 for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import * as Horizon from '@cloudsop/horizon/index.ts';
|
||||||
|
|
||||||
|
describe('Class refs Test', () => {
|
||||||
|
it('Parent can get Child instance by refs', function () {
|
||||||
|
let pInst;
|
||||||
|
|
||||||
|
class Parent extends Horizon.Component {
|
||||||
|
componentDidMount() {
|
||||||
|
pInst = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<Child ref="child">
|
||||||
|
<div ref="childDiv">childDiv</div>
|
||||||
|
</Child>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Child extends Horizon.Component {
|
||||||
|
state = { y: 0 };
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return <div>{this.props.children}</div>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Horizon.render(<Parent />, container);
|
||||||
|
|
||||||
|
expect(pInst.refs['child'].state.y).toEqual(0);
|
||||||
|
expect(pInst.refs['childDiv'].innerHTML).toEqual('childDiv');
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue