enzymejs / enzyme

JavaScript Testing utilities for React
https://enzymejs.github.io/enzyme/
MIT License
19.95k stars 2.01k forks source link

Facing error - Method “simulate” is meant to be run on 1 node. 0 found instead for simulating select onchange #2490

Closed bhavnahari27 closed 3 years ago

bhavnahari27 commented 3 years ago

Facing error - Method “simulate” is meant to be run on 1 node. 0 found instead for simulating select onchange . Below is my test case and the select of the component. Please help me and i stuck with this issue .

test case.js

it('test handleChange() for 24h search criteria', () => {
    const wrapper = shallow(
        <Provider store={store}>
           <MyComponent />
       </Provider>
    );
    const ourEvent = {
      target: {
        value: '24h'
      }
    };
    const searchCriteriaVal = wrapper.find('select').find('option').at(1).simulate('change', {
      target: {
        value: '24h'
      }
    });
    expect(searchCriteriaVal).toEqual(ourEvent.target.value);
  });
bhavnahari27 commented 3 years ago

component.js

ljharb commented 3 years ago

Please don’t delete the issue template - it’s there for a reason.

this is because shallow rendering doesn’t dig into MyComponent. Use the wrappingComponent option instead of directly wrapping it with Provider, and then look at wrapper.debug() if you’re still having trouble. dive() is your friend.

bhavnahari27 commented 3 years ago

Thanks ljharb.Please help with sample code on how to implement in my case.

ljharb commented 3 years ago

Please first read the docs - about the wrappingComponent option and about dive.

bhavnahari27 commented 3 years ago

I am using redux in my react project. Also i see that the basic MatchSnapShot function is not working for me . even though the snapshot file is created and testcase is passing,i dont see the html contents in that. Below is my component code.

It( 'matches snapshot', () => { expect( shallow(

)

).toMatchSnapshot(); });

Please help , is there something i missing?

ljharb commented 3 years ago

@bhavnahari27 please fill out the issue template in #2491 and I’ll be happy to reopen it.