Ajay Gaur
Ajay Gaur

Reputation: 5270

How to find props of a component in jest when it's connected component has updated?

I'm using jest and enzyme for testing my react app. I've a connected component which has 1 hoc over it (connectWithActions) and connect from react-redux. With connectWithActions, I provide setState, dispatch and getState. I use this HOC as :

compose(
    connectActions(Handlers),
    connect(mapStateToProps, mapDispatchToProps),
)(MyComponent)

My handlers look like:

const Handlers = {
    Handler1: (action, {getState, setState, dispatch}) => {
        setState({newStateValue: action.payload.value})
    }
}

this setState is the setState which react provides to the component. (through HOC)

Now MyComponent has updated props with newStateValue in it.

So, In jest: I've mounted my composedComponent (connected) and I'm simulating the action which would trigger Handler1 and will set the state as :

test('test...', () => {
    const node = Component.find('.className');
    node.simulate('click');

    //how do I check that state is updated??
    console.log(Component.find('MyComponent').props());
  });

but I'm not able to figure out how do I test this. I tried checking props but I don't have any newStateValue.

Upvotes: 1

Views: 1006

Answers (1)

Ajay Gaur
Ajay Gaur

Reputation: 5270

The following piece of code worked for me:

test('test...', () => {
    const node = Component.find('.className');
    node.simulate('click');

    Component.update();

    expect(Component.find(MyComponent).props(). newStateValue).toEqual(desiredValue);
  });

Upvotes: 1

Related Questions