- [Instructor] The page for reservations has a form,
so let's functionally test that next.
Switch back to VS Code
and make sure that route reservations.test.js is open.
Navigate to after the GET and describe a POST group.
The first test we can do
is see what happens if a reservation request is invalid.
It should reject an invalid reservation request,
then an async arrow function expression.
Similar to GET, we'll create a response.
So const response,
then await app.post, the http verb that we're going to use
to the /reservations endpoint.
On the next line,
let's specify the type of request explicitly.
This is a form.
Other options include JSON.
This time, we're going to be sending information
in the form of an object,
which will be automatically encoded to a form submission.
We're going to set the date to 2017/06/10,
the time to 06:02 am,
the party, we're going to set this
to something invalid, so bananas,
then the name, family,
and the email, username@example.com,
and with a semicolon.
We have no assertions this time.
With the response, we can expect response.text,
then the matcher .toContain,
the text: Sorry, there was a problem
with your booking request,
which is the generic response for a validation problem,
and a period and a semicolon.
Let's also check the response code,
which previously we did with SuperTest.
So we're going to expect response.status
and then use the matcher toBe 400,
and semicolon.
Let's add trailing semicolons and save the file.
Switch back to the terminal.
Run the test to make sure it passes, npm test.
Great, no problems.
Let's do a more complicated test,
a valid reservation request.
We're going to want to mock up the database interaction.
Above, click in the reservations.test.js file
and scroll to the top.
Just below mockMorgan, let's create a const mockInsert
and assign it to a jest function,
then mockResolvedValue
to an array containing a single integer, 1349.
This mirrors what connects with return
upon a successful insert,
an array containing the inserted ID.
Add a trailing semicolon, and the mockInsert is complete.
Within the beforeAll,
let's add a line after the jest mock for Morgan,
and we're going to use jest.mock
to mock the relative directory to /lib/knex.
This is relative to the file, not to its inclusion.
For the implementation,
we'll do an arrow function expression, a second one,
then the shorthand for executing and returning
an object containing one property, insert,
which we'll assign mockInsert to,
trailing comma and then a semicolon.
Fantastic, almost there. Scroll down to the bottom.
Within the describe group,
we're going to make a new test spec.
It should accept a valid reservation request
with an async anonymous function expression.
Set the const response to await app.post
to the /reservations endpoint.
As before, don't forget to specify the type of form.
Let's send an object containing the same values as before,
so I'll just copy and paste here,
but this time with a party of four.
No trailing semicolon this time
because we're going to use SuperTest
to expect that we'll get back a 200 OK response.
Semicolon now,
and then finally, we expect that the response.text,
and then we'll use that .toContain matcher to be the string:
Thanks, your booking request number 1349,
and save the file.
Switch back to the terminal, maximize the screen,
and then rerun the test with npm test,
then we're going to do it verbosely this time,
so space dash dash, then space --verbose.
Press Enter.
What a relief. Everything is passing.
These functional tests are definitely slower
than the rest of them
so that could be an area for future optimization.
We're going to restore the panel size, destroy the terminal,
and close up any open files.
Want to parse the HTML properly
instead of just searching for text?
Check out cheerio.js.org
for a server-side implementation of core jQuery
without the browser cruft.
Speaking of that 100% coverage, should we attempt it?
What's the worst that can happen?