Created
November 3, 2022 11:40
-
-
Save LeighCiechanowski/d53925daeb93711e9605f274e75d09ce to your computer and use it in GitHub Desktop.
Example Testing Child Props are Passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
----------------------------------------- | |
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(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment