【发布时间】:2021-06-09 10:55:01
【问题描述】:
我有一个反应组件:
export class Test extends React.Component<TestProps, TestState> {
...
//the constructor sets the state initially
constructor(props: TestProps) {
this.state = {
thisIsAState: 1;
}
}
//this function updates the state given the num param
updateState(num) {
this.setState({ thisIsAState: num });
}
}
所以现在在 jest/enzyme 测试文件中,我有:
/*wrapped in a describe/it...*/
const wrapper = await mount(<Test {..anyPropsHere} />);
wrapper.instance().updateStates(3); //updating the states here via function call
wrapper.update(); //tried this to see if it would update anything
expect(wrapper.state().thisIsAState).toEqual(3);
这不起作用 -> 它将包装器的“状态”变量视为空 - 总是。即使我在这里直接 setState(),它也不起作用 - 仍然为空。
我也尝试过 SetTimeout 函数并使用 await,但单元测试似乎跳过了这些: 它们完成并通过,但如果我说 .toEqual(4) 期待明显的 1 !== 4 错误,它仍然会通过,所以它在某种程度上没有达到这些功能。我也尝试过浅渲染对象。
有没有合适的方法来调用Test的更新函数,让它更新状态,然后检查状态?
我在: “酶”:“^3.11.0”, “酶适配器反应 16”:“^1.15.5”, “反应”:“^16.13.1”,
【问题讨论】:
标签: reactjs typescript async-await jestjs enzyme