Test if a component is rendered with the right props with react-testing-library

前端 未结 2 718
耶瑟儿~
耶瑟儿~ 2021-02-01 23:39

I have some components that are rendering another component (FetchNextPageButton) that is already tested in isolation, like these ones:

const News = () => (
          


        
2条回答
  •  遥遥无期
    2021-02-02 00:23

    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) => 
    ); .... const { getByTestId } = render(); 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

提交回复
热议问题