(upbeat music)
- [Instructor] Here's the solution to the challenge
about mocking a successful reservation creation with Jest.
If you haven't completed it yet,
please take a moment to practice your skills.
Switch over to VS code.
In the Explorer, navigate to lib reservations.test.js.
Scroll to describe create if it's not already visible.
While we previously required reservations every time,
we're going to be manipulating it prior to requiring it.
So we're going to take the reservations require,
paste those into the two test specs,
and remove the before all.
With that preparation out of the way,
we can write our test spec for creating a reservation
with no validation problems.
At the end of the group,
we're going to start with an it should create a reservation
if there are no validation problems
and this is an async function.
Next, we're going to prepare to require.
We'll start with something that we expect.
So const expected insert ID
and we'll just arbitrarily choose one.
Then we'll create a mock insert.
So const mockInsert equals a jest function
with a mockResolvedValue,
which returns an array containing the expected insert ID.
So just expected insert ID.
We did something similar with a save test.
With that setup, we'll use jest.mock
on the connects module with a relative path.
So slash connects same as in reservations.js.
Same as before, two arrow function expressions,
then we're going to use parenthesis
and return an object with just one property,
insert with the value mockInsert, a trailing comma,
and then a semi-colon.
Now that we've prepared, we can require reservations.
So reservations equals require the relative path
to reservations, add a trailing semi-colon.
Now, let's mock the validation.
So const mock validation equals just.spyOn reservations
with method validate.
We can mock the implementation with something simple.
Whatever value we give it,
we can resolve to that same value,
which is what it would do if it passed validation.
So mockvalidation.mockImplementation, and then value,
which will just go to Promise.resolve with the value.
We'll prepare our test data with a reservation
with an arbitrary structure, which we'll use
for both creation and to check later.
So a const reservation equals just an object foo
and then a string literal bar.
Finally, let's execute and check.
So we're going to await expect and then reservations.create
with the reservation, singular.
This should .resolves, plural, .toStrictEqual
the expected insert ID not an array,
add a trailing semi-colon.
We can do a couple of additional checks,
such as expect reservations.validate
toHaveBeenCalledTimes, one.
We can also expect that mock validation
and then we'll use the matcher .toBeCalledWith
a reservation, singular.
And then we expect mockvalidation.toBeCalledTimes
and just one.
Finally, clean up and restore.
First, the mockvalidation.mockRestore
and then unmock connects itself.
So jest.unmock and then the relative path of ./connects.
Add a trailing semi-colon, save the file and we're all done.
Let's open up a terminal.
Let's run our new test suite, type npm test and press enter.
Fantastic!
All tests passed and we're up to two test suites
and 12 tests.
Let's clean up.
Exit out of the terminal and close the test file.
The completed version can be found
in solutions/04_08/reservations.test.js.
So a practical question, is mocking validation worth it?
In my experience, it can be,
in that you can focus your tests on just one thing at a time
rather than having to refactor a number of different tests
if your validation methodology changes.
Of course, use your best judgment
and try things out in your project.
Thanks for following along.