在酵素和玩笑中有id的wrapper.find找不到元素 [英] wrapper.find with id in enzyme and jest doesn't find element

查看:384
本文介绍了在酵素和玩笑中有id的wrapper.find找不到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Jest和Enzyme测试的新手,我想知道为什么find函数无法使用id。

I'm new to Jest and Enzyme testing, and I would like to know why the find function is not working with id.

// html来自react,只是ID增量在哪里的代码

//html from react, just the code where is the id increment

<div className="App-body">
  <div className="changePage">
    <button className="Selections" onClick={this.decrementPage}>Previous</button>
    <p className="changePageNumber">{this.state.page}</p>
    <button className="Selections" id="increment" onClick={this.incrementPage}>Next</button>
  </div>
</div>

// test

it('next page', () => {
  const wrapper = shallow(<Home />)
  const incrementPage = wrapper.find('#increment')
  incrementPage.simulate('click')
  const countState = wrapper.state().page
  expect(countState).toEqual(2)
})

Method "simulate" is meant to be run on 1 node. 0 found instead.

25 |   //const text = wrapper.find('p').text()
 26 |   const incrementPage = wrapper.find('#increment')
> 27|   incrementPage.simulate('click')
    |                 ^                   

推荐答案

尝试使用 mount ,而不是 shallow 不会超出元素的第一级。在您的情况下,仅显示className为 App-Body的div

Try using mount instead of shallow. shallow does not render beyond the first level of elements. In your case, only the div with className 'App-Body' is rendered

这篇关于在酵素和玩笑中有id的wrapper.find找不到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆