Testing the Event
Recall how we implemented the userHasRegisteredEvent
.
File: src/events/userHasRegisteredEvent.js
Now for this test, we actually don't care that much about the mailer
utility and whether or not we actually sent an email. What we are really interested in is whether or not the mailer.sendEmailToUser
was called or not. For this particular test, it would be sufficient to just mock up the mailer
and the sendEmailToUser
method to check if they've been called.
The Test
The mocking part for this test is a lot tricky, but notice how we mock both the mailer
and the sendEmailToToUser
below.
File: src/events/__tests__/userHasRegisteredEvent.test.js
This might look a little strange at first, but one aspect to consider is that you should not be sending an actual email when running the test. Imagine a scenario in which we did not mock the mailer
utility and its sendEmailToUser
method, and we actually stubbed in real inputs into the function, then we would be making real life calls that would send real emails.
Last updated