Testing React components
In this section, we are going to implement tests on the Page
, Question
, and HomePage
components. React component tests can be challenging because they have dependencies, such as the browser's DOM and sometimes HTTP requests. Due to this, we are going to leverage the React Testing Library and Jest's mocking functionality to help us implement our tests.
Testing the Page component
Carry out the following steps to test that the Page
component renders correctly:
- Create a file for the tests called
Page.test.tsx
with the following content:import React from 'react'; import { render, cleanup } from '@testing-library/react'; import { Page } from './Page'; test('When the Page component is rendered, it should contain the correct title and content', () => { });
We imported React with our
Page
component, along...