在酶中使用 id 的 wrapper.find 和 jest 找不到元素

Jon*_*han 9 reactjs jestjs enzyme

我是 Jest 和 Enzyme 测试的新手,我想知道为什么 find 函数不适用于 id。

//来自react的html,只是id增量所在的代码

<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>
Run Code Online (Sandbox Code Playgroud)

//测试

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

Method “simulate” is meant to be run on 1 node. 0 found instead.
Run Code Online (Sandbox Code Playgroud)

Method “simulate” is meant to be run on 1 node. 0 found instead.
Run Code Online (Sandbox Code Playgroud)

Muk*_*oni 13

尝试使用mount代替shallow. shallow不渲染超出第一级元素。在您的情况下,仅呈现 className 'App-Body' 的 div

  • 我有同样的问题,谢谢,现在我知道 shallow 和 mount 之间的区别 (2认同)