【问题标题】:React Testing Library: Do I need a new test with new selectors for each test key?React 测试库:我是否需要为每个测试键使用新的选择器进行新的测试?
【发布时间】:2019-02-10 21:38:18
【问题描述】:

我正在使用 React 测试库构建一个带有 TDD 的 React 应用程序。

TLDR: 选择器(例如toBeInTheDocument())似乎只在render 之后工作一次。对吗?

我使用 CRA 制作了应用程序,这是我的src/setupTests.js

// react-testing-library renders your components to document.body,
// this will ensure they're removed after each test.
import "react-testing-library/cleanup-after-each";
// this adds jest-dom's custom assertions
import "jest-dom/extend-expect";

这是我写的测试:

import { pluck } from "ramda";
import React from "react";
import { render } from "react-testing-library";
import ContactList from "../components/ContactList";
import { contacts } from "../fixtures";

describe("Contact List", () => {
  describe("given no contacts", () => {
    const { getByText } = render(<ContactList />);

    it("displays 'First Name' in the header", () => {
      const actual = getByText(/first name/i);
      expect(actual).toBeInTheDocument();
    });

    it("displays 'Last Name' in the header", () => {
      const actual = getByText(/last name/i);
      expect(actual).toBeInTheDocument();
    });

    it("displays 'No Contacts'", () => {
      const actual = getByText(/no contacts/i);
      expect(actual).toBeInTheDocument();
    });
  });

  describe("given a list of contacts", () => {
    const props = { contacts };

    const { getAllByTestId } = render(<ContactList {...props} />);

    it("displays the first name of all contacts", () => {
      const actual = pluck("textContent")(getAllByTestId("contact-first-name"));
      const expected = pluck("firstName")(contacts);
      expect(actual).toEqual(expected);
    });

    it("displays the last name of all contacts", () => {
      const actual = pluck("textContent")(getAllByTestId("contact-last-name"));
      const expected = pluck("lastName")(contacts);
      expect(actual).toEqual(expected);
    });
  });
});

这是我写的组件:

import React from "react";
import PropTypes from "prop-types";
import Table from "@material-ui/core/Table";
import TableBody from "@material-ui/core/TableBody";
import TableCell from "@material-ui/core/TableCell";
import TableHead from "@material-ui/core/TableHead";
import TableRow from "@material-ui/core/TableRow";
import withStyles from "@material-ui/core/styles/withStyles";
import strings from "../strings";

function ContactList({ contacts }) {
  const renderContact = ({ firstName = "N/A", lastName = "N/A", id }) => (
    <TableRow key={id}>
      <TableCell data-testid="contact-first-name">{firstName}</TableCell>
      <TableCell data-testid="contact-last-name">{lastName}</TableCell>
    </TableRow>
  );
  return (
    <Table>
      <TableHead>
        <TableRow>
          <TableCell>{strings.firstName}</TableCell>
          <TableCell>{strings.lastName}</TableCell>
        </TableRow>
      </TableHead>
      <TableBody>
        {contacts.length > 0 ? (
          contacts.map(renderContact)
        ) : (
          <TableRow>
            <TableCell>{strings.noContacts}</TableCell>
          </TableRow>
        )}
      </TableBody>
    </Table>
  );
}

ContactList.propTypes = {
  classes: PropTypes.object.isRequired,
  contacts: PropTypes.arrayOf(
    PropTypes.shape({
      firstName: PropTypes.string.isRequired,
      lastName: PropTypes.string.isRequired
    })
  ).isRequired
};

ContactList.defaultProps = {
  contacts: []
};

const styles = {};

export default withStyles(styles)(ContactList);

现在,如果我运行这些测试,每个 describe 块只有前两个通过。

例如,如果我给任何it 测试提供.only 它通过。

发生了什么事?我不能为多个断言重用相同的测试设置吗?我必须为每个it 拨打render 吗?

【问题讨论】:

    标签: reactjs tdd jestjs react-testing-library react-test-renderer


    【解决方案1】:

    您正在导入react-testing-library/cleanup-after-each,它会在每次测试后删除渲染的组件。这是正确的方法,但是您应该在每个 it 块中渲染组件。

    【讨论】:

    • 感谢您的帮助。您会建议删除 react-testing-library/cleanup-after-each 还是建议在每个 it 块中渲染组件?
    • 您需要在每个块之后调用cleanup。这是为了确保您的测试是隔离的。每次只需渲染组件。如果渲染逻辑变得太长并且您不想重复它,请将其移动到函数中并在每个测试中调用它
    猜你喜欢
    • 2019-12-31
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 2013-07-29
    • 1970-01-01
    相关资源
    最近更新 更多