Error: This method is only meant to be run on single node. 0 found instead

南楼画角 提交于 2019-12-05 00:01:26

That error happens when, as it says, you run it with any number of nodes other than 1.

Similar to jQuery, your find call will return some number of nodes (really it's a single wrapper that knows how many nodes your find selector has found). And you can't call simulate against 0 nodes! Or multiple.

The solution then is to figure out why your selector (the styles.container in wrapper.find(styles.container)) is returning 0 nodes, and make sure it returns exactly 1, and then simulate will work as you expect.

const container = wrapper.find(styles.container)
expect(container.length).to.equal(1)
container.simulate('keyup', {keyCode: 27});
expect(store.getActions()[0]).to.deep.equal(expectedAction);

Enzyme's debug method is really useful here. You could do console.log(container.debug()), or also console.log(container.html()) to make sure your component is rendering as expected during the test.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!