Solution: Unit Tests
Here’s the solution of how to write unit tests using the Jest and Vue testing libraries.
We'll cover the following
Solution
Your task was to write tests for the Pagination
component. Here are the changes required:
For the src/components/pagination.spec.js
file:
- The
Paginate forward
test should utilize therenderPagination
helper to mount thePagination
component and destructure thegetByText
method to access the “Next” button. A click event should be fired for the button twice. After each click, the text content of the button should be checked to confirm it matchesPage 2
andPage 3
respectively. - The
Paginate forward
test should use therenderPagination
helper to mount thePagination
component and destructure thegetByText
method. TherenderPagination
method should receive an object as an argument with theinitialPage
property set to3
. ThegetByText
should be used to get access to thePrev
button. A click event should be fired for the button twice. After each click, the text content of the button should be checked to confirm it matchesPage 2
andPage 1
respectively. - The
Can't paginate forward if the last page was reached
test should mount thePagination
component with theinitialPage
set to2
. Then, the “Next” button should be retrieved using thegetByText
method and clicked twice. However, thePagination
component should still be onPage 3
. - The
Can't paginate below page 1
test should mount thePagination
component with theinitialPage
set to1
. Then, the “Prev” button should be retrieved using thegetByText
method and clicked twice. However, thePagination
component should still be onPage 1
.
Let’s run the following code to understand how the solution above works.
Note: The code below may take a while to run. When the server starts, go to the app URL, and see the output.
Get hands-on with 1400+ tech skills courses.