Jestjs 在jest和Ezyme中从react语义ui弹出窗口获取触发器元素
我正在尝试编写一些测试,但还没有找到如何从react语义ui的弹出窗口中获取触发器元素。有人知道吗 这是弹出窗口;我想测试图标Jestjs 在jest和Ezyme中从react语义ui弹出窗口获取触发器元素,jestjs,enzyme,semantic-ui-react,Jestjs,Enzyme,Semantic Ui React,我正在尝试编写一些测试,但还没有找到如何从react语义ui的弹出窗口中获取触发器元素。有人知道吗 这是弹出窗口;我想测试图标 <Popup trigger={<Icon name='plus' color='green' onClick={this.followItem.bind(this)} />} content='Follow this item' /> 当前试验方法: describe('Follow Icon', () => { it
<Popup trigger={<Icon name='plus' color='green' onClick={this.followItem.bind(this)} />}
content='Follow this item' />
当前试验方法:
describe('Follow Icon', () => {
it('should create a userItem when the follow icon is clicked', (done) => {
const wrapper = mount(<MemoryRouter><ItemDetails match={{ params: { id: 1 } }} /></MemoryRouter>);
const instance = wrapper.find(ItemDetails).instance();
instance.getItem().then(() => {
wrapper.update();
}).then(() => {
expect(wrapper.find(Popup)).toHaveLength(1);
const follow = shallow(wrapper.find(Popup).prop('trigger')());
// wrapper.find(...).prop(...) is not a function
expect(follow.props().name).toBe('plus');
done();
});
});
});
description('跟随图标',()=>{
它('单击下面的图标时应创建一个userItem',(完成)=>{
const wrapper=mount();
const instance=wrapper.find(ItemDetails.instance();
instance.getItem()。然后(()=>{
wrapper.update();
}).然后(()=>{
expect(wrapper.find(Popup)).toHaveLength(1);
const follow=shallow(wrapper.find(Popup.prop('trigger')());
//wrapper.find(…).prop(…)不是函数
expect(follow.props().name).toBe('plus');
完成();
});
});
});
最简单的方法是调用函数并呈现结果。假设弹出窗口中呈现了浅层,只需调用触发器
函数并将结果传递给浅层
:
const icon = shallow(popup.prop('trigger')())
我找到了一个有效的解决方案,但我不知道这是否是最佳实践
<Popup trigger={<Icon id='follow'/>
const follow = wrapper.find(Popup).find('#follow').first();
我遇到了类似的问题,我测试弹出组件的解决方案(使用您的示例)是:
我得到弹出。prop(…)不是一个函数如何生成popup
mount().find(popup)
并找到弹出,因为它看起来不像。当我测试时,期望(wrapper.find(popup)).toHaveLength(1)代码>它可以工作
<Popup
trigger={<Icon
name='plus'
color='green'
onClick={this.followItem.bind(this)} />}
content={<div className="content">Follow this item</div>}
/>
// after setting the wrapper
// to test the trigger property, as this is always rendered we just need to:
expect(wrapper.find(Icon).prop().name).toBe('plus');
// to test the component property, as this gets rendered in the document.body,
// there is no way to assert on it using just one mount
const popupContentWrapper = mount(wrapper.find(Popup).instance().props.content);
expect(popupContentWrapper.find('.content').hostNodes()).toBeDefined()
// not sure if you notice the use of hostNodes here, but that's because find is
// returning both React components instances and DOM nodes, that match your query selector. hostNodes() solves the issue, reference: https://github.com/airbnb/enzyme/issues/836#issuecomment-401260477