我正在尝试用状态true测试此组件:
<DashboardBreadcrumbButtons canShare={stateHooks.state.canShare} />
但是在我的测试中,总是是错误的。
const setStates = ({state, setState}, newState) =>
setState(Object.assign({}, state, newState));
const [state, setState] = useState({
isFullscreen: false,
confirmDelete: false,
icon: props.icon,
iconUpload: null,
title: props.title,
description: props.description,
canShare: false,
canDefineHome: false,
canEditPortal: false,
canCopyPortal: false,
canViewAudit: false,
});
useEffect(() => {
debugger;
// Enter here in the tests and assign
setStates(stateHooks, {
canShare: true,
canDefineHome: true,
canEditPortal: true,
canCopyPortal: true,
canViewAudit: true,
});
}, []);
useEffect(() => {
// Here, always is false in test, setStates not working
console.log(stateHooks.state.canShare);
debugger;
}, [stateHooks.state.canShare]);
在我的应用程序中正在工作应用程序图像
setstate是一种异步操作,因此很难预测何时设置它。您可以使用下面的代码来更新主张操作上方的包装组件。这对我有用。
componentWrapper.update();