Check React Component Props are Passed to Child in Jest Unit Test

Last Updated:
Small dog doing React component unit testing

This short article aims to explain how to unit test a parent components logic, and check that a child component is passed the correct props. A quick How To on testing if a component is called with expected props in Jest.

The mindset behind this is that the child components have already been unit tested themselves, and do not need to be included in this particular test. Breaking it down to its simplest form, the only purpose of the test is to check that the correct arguments are passed to the child function. Not only does this simplify the testing, but it also helps reduce the appeal to nest the tests as the functionality is more effectively split up.

BTW, I appreciateĀ the image banner is not particularly relevant, but finding interesting ‘react unit test passing props check’ images is a long and daunting task…

Example

This example is purely to show how to verify that a React components props are passed in a Jest unit test. There are two components, a ParentComponent and a ChildComponent. The ParentComponent can take a prop of open, and data. When open is set to true the child component is shown, and the data is passed into it.

For this example imagine that the ChildComponent is stored in the same folder as the ParentComponent.

ParentComponent

import React from "react";
import ChildComponent from "./ChildComponent";

const ParentComponent = ({ open, data }) => (
  <>
    <p>Some content to render</p>
    {open && <ChildComponent open={open} data={data} />}
  </>
);

export default ParentComponent;

The Complete Test

Scroll down for a broken down explaination.

import React from "react";
import { render } from "@testing-library/react";
import ParentComponent from "./ParentComponent";

const mockChildComponent = jest.fn();
jest.mock("./ChildComponent", () => (props) => {
  mockChildComponent(props);
  return <mock-childComponent />;
});

test("If ParentComponent is passed open and has data, ChildComponent is called with prop open and data", () => {
  render(<ParentComponent open data="some data" />);
  expect(mockChildComponent).toHaveBeenCalledWith(
    expect.objectContaining({
      open: true,
      data: "some data",
    })
  );
});

test("If ParentComponent is not passed open, ChildComponent is not called", () => {
  render(<ParentComponent />);
  expect(mockChildComponent).not.toHaveBeenCalled();
});

Break Down

Setting Up the Mocked ChildComponent

The first thing that must happen is the component that is to be tested needs to be mocked. This happens outside of any test or it blocks.

// Set up a Jest mock function to check any props.
const mockChildComponent = jest.fn();

// Mock the child component file, and grab all props passed to it
// and pass them to the Jest mock function so it can listen.
jest.mock("./ChildComponent", () => (props) => {
  mockChildComponent(props);

  // Return something for React to render. I like creating
  // an element to pass through as it
  // helps with visually debugging if needed.
  return <mock-childComponent />;
});

Testing The ParentComponent is Setting Props Correctly

Then the test block can be set up and the ParentComponent rendered with React Testing Library. The props of the ParentComponent should be set (or not, depending on the test) and then an expect is put in place for checking the passed props.

test("If ParentComponent is passed open and has data, ChildComponent is called with prop open and data", () => {
  // Render the ParentComponent with the props wanting to
  // be tested.
  render(<ParentComponent open data="some data" />);

  // Check that the Jest mock function is called with an object.
  // Use 'expect.objectContaining' to make sure any other default
  // React props are ignored.
  expect(mockChildComponent).toHaveBeenCalledWith(
    expect.objectContaining({
      open: true,
      data: "some data",
    })
  );
});

This way of checking if the correct props have been set has massively simplified my unit testing. I quite often need to Jest Test that React component has passed props correctly, and this layout is quick and clean.

 

Related Posts