gpt4 book ai didi

javascript - enzyme 包含具有状态的匹配元素

转载 作者:行者123 更新时间:2023-11-28 20:26:39 26 4
gpt4 key购买 nike

我无法弄清楚为什么我的测试不起作用。假设我有一个这样的组件:

class MyComponent extends Component {
constructor(props) {
super(props);
this.state = { flag: true };
}
render() {
return (
<div>
{
this.state.flag && (
<div>Hello there!</div>
)
}
</div>
)
}
}

这是我的测试文件:

it ('shows Hello There when the flag is set to true', () => {
const component = shallow(<MyComponent />);
component.setState({ flag: true });
expect(
component.containsMatchingElement(<div>Hello There</div>)
).toBe(true)
});

当我运行它时,它说它期望为真,但结果为假。我做错了什么吗?

谢谢!

最佳答案

您在测试用例中遗漏了感叹号。在您的组件中,您有 <div>Hello there!</div>在你的测试用例中你有 <div>Hello There</div> .你的测试用例应该是 <div>Hello there!</div>带有感叹号!

关于javascript - enzyme 包含具有状态的匹配元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53566703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com