Error: This method is only meant to be run on single node. 0 found instead
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.
If you have more than one HTML elements like
<button className = "age_up" onClick = {() => dispatch(onAgeUpAction())}> Age UP </button> <button className = "age_down" onClick = {() => dispatch(onAgeDownAction())}> Age Down </button> <button type = "button"onClick = {handleClick}> Fetch Post </button>
and are calling it by a generic query like
wrapper.find('button').simulate('click');
it will return all the three nodes for you.So call it by unique ID's or ClassNames.