Testing Post Components
Follow step-by-step instructions to write test cases for the post components.
We'll cover the following
Now that we’ve had a solid introduction to testing in React, let’s continue with writing tests for the Post
components.
The functionalities to create, read, update, and delete posts are core features of the Postagram application, so it’s important to make sure that they work as expected. Let’s start with a simple test for the Post
component.
Mocking the localStorage
object
Before writing a test for the Post
component, it’s important to understand how the Post components work. Basically, it takes a prop called post
and makes a call to localStorage
to retrieve information about the user. Unfortunately, localStorage
can’t be mocked by Jest. There are a lot of workarounds to allow our tests to work with localStorage
and to make it simple and have less boilerplate, we’ll use the jest-localstorage-mock
JavaScript package. The package can be used with Jest to run frontend tests that rely on localStorage
. To add the package, add the following line to the file:
Get hands-on with 1400+ tech skills courses.