简体   繁体   English

如何测试用 div(react-testing-library)渲染的组件数量?

[英]How to test the number of components that are rendered with a div (react-testing-library)?

I have this test file我有这个测试文件

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

describe("ParentComment", () => {
  const comment = {
    id: "4b2d74e6-7d1a-4ba3-9e95-0f52ee8ebc6e",
    author: "Reed Fisher",
    body: "Sint in in sunt amet.",
    postedAt: 1550488214207,
    replies_count: 3,
    replies: [
      {
        id: "116dbd01-d5f3-4dfb-afeb-f822a9264a5e",
        comment_id: "4b2d74e6-7d1a-4ba3-9e95-0f52ee8ebc6e",
        author: "Kathleen Nikolaus",
        body:
          "Officia suscipit sint sint impedit nemo. Labore aut et quia quasi ut. Eos voluptatibus quidem eius delectus beatae excepturi.",
        postedAt: 1550419941546,
      },
      {
        id: "116dbd01-d643-4dfb-afeb-f822a9264a5e",
        comment_id: "4b2d74e6-7d1a-4ba3-9e95-0f52ee8ebc6e",
        author: "Kathleen Nikolaus",
        body:
          "Offici sint sint impedit nemo. Labore aut et quia quasi ut. Eos voluptatibus quidem eius delectus beatae excepturi.",
        postedAt: 1550419941546,
      },
    ],
  };
  let component;
  beforeEach(() => {
    component = render(<ParentComment comment={comment} />);
  });
  it("has a class parent-comment", () => {
    expect(
      component.container.querySelector(".parent-comment")
    ).toBeInTheDocument();
  });
  it("renders replies for comment", () => {
    let comments = component.getAllByTestId("comment");
    expect(comments.length).toBe(comment.replies.length + 1);
  });
});

And following matching component:以及以下匹配组件:

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

const ParentComment = (props) => {
  const replies = props.comment.replies.map((reply) => (
    <Comment key={reply.id} comment={reply} />
  ));
  const handleShowMoreReplies = (e) => {
    e.preventDefault();
    props.onShowMoreReplies(props.comment.id);
  };
  return (
    <div className="parent-comment">
      <Comment comment={props.comment} />
      <div className="replies">
        {replies}
        {props.comment.replies_count !== replies.length ? (
          <a href="#" className="show_more" onClick={handleShowMoreReplies}>
            Show More Replies ({props.comment.replies_count - 1})
          </a>
        ) : null}
      </div>
    </div>
  );
};

export default ParentComment;

What I want to test is the number of comments inside of the replies div, so, I did comment.replies.length + 1 as there is only 1 instance of Comment component outside of the replies div, but this is not really a good way, as I could add easily break my test if I add another Comment component outside of this div.我想测试的是回复 div 内的评论数量,所以,我做了comment.replies.length + 1因为replies div 之外只有 1 个 Comment 组件实例,但这并不是一个好方法,因为如果我在此 div 之外添加另一个Comment组件,我可以很容易地破坏我的测试。

I suppose there is a better way?我想有更好的方法吗?

I've just realized that there is a within helper.我刚刚意识到有一个within帮手。

  it("renders replies for comment", () => {
    const { getAllByTestId } = within(
      component.container.querySelector(".replies")
    );
    let comments = getAllByTestId("comment");
    expect(comments.length).toBe(comment.replies.length);
  });

The above test passes now.上面的测试现在通过了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 react-testing-library 测试由其他组件组成的组件? - How to test a component composed of other components with react-testing-library? 如何使用 React-testing-library 测试 styled-components 属性? - How to test styled-components attribute with React-testing-library? Jest &amp; react-testing-library - 为有条件渲染的孩子添加测试 - Jest & react-testing-library - add test for conditionally rendered children 使用 react-testing-library 时如何测试组件是否使用正确的道具呈现? - How to test if a component is rendered with the right props when using react-testing-library? 如何在 react-testing-library 中测试第三方组件库 - how to test third-party components library in react-testing-library 如何使用带有 msw 和 react-testing-library 的 react-query 测试组件? - How to test components using react-query with msw and react-testing-library? 使用 react-testing-library 测试样式化的组件 - Testing styled components with react-testing-library 如何使用react-testing-library测试withStyles中包含的样式化的Material-UI组件? - How to test styled Material-UI components wrapped in withStyles using react-testing-library? 如何使用 Jest 和 react-testing-library 测试 react-dropzone? - How to test react-dropzone with Jest and react-testing-library? 如何使用 react-testing-library 测试 react-select - how to test react-select with react-testing-library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM