day 259 — code 365

Mon 29 Aug 2016

Enzyme

Spent some time trying to figure out how to write a test for a button that called e.preventDefault(), but was causing an error because e was not defined.

From my reading around, I think this is because event propagation doesn’t happen (…?) but I found this solution worked well:

wrapper.find('.btn').at(1).simulate('click', { preventDefault() {} });

So we pass in the preventDefault function, as the 2nd argument to the simulate function, and define it as an empty function.


React

When we import a function,

import { sendEmail } from '../redux/actions/users';

We are assigning that function to a constant.

However, when we want Redux to dispatch that, we need to use connect() to connect that component, and then we map that to the component’s props.

Therefore, it is the props.sendEmail which holds that new connected function, and that is what we need to call, and NOT the constant sendEmail…

That is:

sendEmail()

Is different to:

props.sendEmail();
Show your support

Clapping shows how much you appreciated Ampers Smith’s story.