Mocking child components
In this section, we’re going to use the jest.mock
test helper to replace the child component with a dummy implementation. Then, we’ll write expectations that check whether we passed the right props to the child component and that it is correctly rendered on the screen.
But first, let’s take a detailed look at how mocked components work.
How to mock components, and why?
The component we’re going to build in this chapter has the following shape:
export const AppointmentsDayViewLoader = ({ today }) => { const [appointments, setAppointments] = useState([]); useEffect(() => { // fetch data from the server const result = await global.fetch(...); // populate the appointments array: setAppointments(await result.json()); }, [today]); return ( <AppointmentsDayView...