我有一个 jest/enzyme 测试,它在组件周围创建一个 ShallowWrapper,找到一个指定的语义-ui-react 按钮(通过 id),模拟按钮上的点击,然后查看点击是否切换了某些内容。
示例 JSX:
<Popup
trigger={<Button onClick={this.toggleShowThing} id="special-btn">a button</Button>}
content="Popup Words"
/>
{this.state.showThing &&
<div className="special-thing">The Thing's Words</div>
}
样品测试:
it('shows the thing when the button is clicked', () => {
const wrapper = shallow(<MyComponent />);
wrapper.find('#special-btn').simulate('click', { preventDefault() {} });
expect(wrapper.find('.special-thing').exists()).toBe(true);
});
当我刚刚拥有 Button 时,此测试有效。当我添加 Popup 并将 Button 放入 trigger 道具时,我收到一个错误,因为找不到#special-btn。
错误:方法“props”仅用于在单个节点上运行。找到了 0 个。
该组件的酶快照显示弹出窗口如下所示:
<Popup
content="Popup Words"
on="hover"
position="top left"
trigger={
<Button
id="special-btn"
onClick={[Function]}
>
a button
</Button>
}
/>
我需要我的测试才能再次工作。如何在测试中再次获得对 #special-btn 的访问权限,以便可以调用 .simulate('click') ?