Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 292
Brian Kalski Avatar asked Sep 05 '25 03:09

Brian Kalski


1 Answers

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")
like image 156
Shubham Khatri Avatar answered Sep 08 '25 00:09

Shubham Khatri