I have some components that are rendering another component (FetchNextPageButton) that is already tested in isolation, like these ones:
const News = () => ( <div> <h1>News</h1> ... <FetchNextPageButton query={NEWS_QUERY} path="viewer.news" /> </div> ) const Jobs = () => ( <div> <h1>Jobs</h1> ... <FetchNextPageButton query={JOBS_QUERY} path="viewer.jobs" /> </div> ) const Posts = () => ( <div> <h1>Posts</h1> ... <FetchNextPageButton query={POSTS_QUERY} path="viewer.posts" /> </div> )
The thing is that I'd not like having to add tests on each of these components for a functionality that is already tested somewhere else, so I think that should be enough just to test that the component is rendered and that I'm passing the right props to it.
I'd have been able to test this easily with Enzyme with something like this:
expect(wrapper.find('FetchNextPageButton').props()).toMatchObject({ query: NEWS_QUERY, path: "viewer.news" })
So I'm wondering what's the best approach to test it by using React testing library instead.
Using React DevTools to highlight what components rerendered To enable it, go to "Profiler" >> click the "Cog wheel" on the right side of the top bar >> "General" tab >> Check the "Highlight updates when components render." checkbox.
There are a few ways to test React components. Broadly, they divide into two categories: Rendering component trees in a simplified test environment and asserting on their output. Running a complete app in a realistic browser environment (also known as “end-to-end” tests).
By default, React Testing Library will create a div and append that div to the document. body and this is where your React component will be rendered.
This is the approach that Kent C. Dodds (the creator of RTL) shared with me after discussing it with him:
import FetchNextPageButton from 'FetchNextPageButton' jest.mock('FetchNextPageButton', () => { return jest.fn(() => null) }) // ... in your test expect(FetchNextPageButton).toHaveBeenCalledWith(props, context)
Don't believe it's possible. RTL looks like focusing on validating against DOM not React's components tree.
The only workaround I see is to mock FetchNextPageButton
to make it rendering all props into attributes.
jest.mock("../../../FetchNextPageButton.js", () => (props) => <div data-test-id="FetchNextPageButton" {...props} />); .... const { getByTestId } = render(<YourComponent />); expect(getByTestId("FetchNextPageButton")).toHaveAttribute("query", NEWS_QUERY); expect(getByTestId("FetchNextPageButton")).toHaveAttribute("path", "viewer.news");
Sure, this is smoothly only for primitive values in props, but validating something like object or function would be harder.
Think, it's not RTL-way, but I agree it would be massive work to check that in scope of each container(and completely ignoring that would be rather a risk).
PS toHaveAttribute
is from jest-dom
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With