Brian Kalski
Brian Kalski

Reputation: 957

How to test redux action by onClick with enzyme

I have the following function I am trying to test with enzyme.

renderLoginLink(caption) {
return (
  <a href="#" id="lnkAuthenticateWithGoogle" onClick={() => this.props.actions.authenticateWithGoogle()}>
    {caption}
  </a>
);
}

I'm using this to simulate the click.

let container, store
const mockStore = configureMockStore();
store = mockStore(initialState);
container = mount(<Provider store={store}><Login {...initialState} /></Provider>)
container.find("a#lnkAuthenticateWithGoogle").simulate("click")

I am getting an error that this.props.actions is undefined. I'm not sure what I am missing.

Upvotes: 4

Views: 936

Answers (1)

Shubham Khatri
Shubham Khatri

Reputation: 281734

While mounting the component, you would need to provide the actions as props to it like

let container, store
const mockStore = configureMockStore();
store = mockStore(initialState);
const actions = {
     authenticateWithGoogle: jest.fn()
}
container = mount(<Provider store={store}><Login {...initialState} actions={actions}/></Provider>)
container.find("a#lnkAuthenticateWithGoogle").simulate("click")

Upvotes: 5

Related Questions