From the course: Testing React Applications with Jest and React Testing Library

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Solution: Unit and integration tests: CreatePost

Solution: Unit and integration tests: CreatePost

(bright music) - [Instructor] Let's walk through the solution to create post-testing challenge together. If you gave it a shot, well done. These types of tests are complex and require thinking through several layers, such as validation, states management, network behavior and routing. Now we'll write tests that simulate different user scenarios, mock global functions, and assert both UI states and side effects. In the create post test file, you will see how we mocked both use navigate, use odds, and the create post service. This allows us to control the authentication states, navigation behavior in our test, and the actual request to create a post. So let's start with the simplest behavior in the components. If the user is not logged in, we want to immediately redirect them to the login page. To simulate that, we mock the use auth hook to return the user as null, and we've already mocked use navigate so we can track where the user is sent. We also check that the component renders…

Contents