【问题标题】:Functional component that includes a functional component inside it - Test内部包含功能组件的功能组件 - 测试
【发布时间】:2021-12-29 13:40:51
【问题描述】:

我有一个功能组件,我想用 jest 编写测试。但我无法评估结果。我应该如何为这样的组件编写测试? 这是我要测试的组件:

import React from "react";
import { useDispatch } from "react-redux";
import { useNavigate } from "react-router-dom";
import ExpenseForm from "./ExpenseForm";
import { addExpense } from "../store/expenses/actions";

const AddExpensePage = () => {
  const dispatch = useDispatch();
  const navigate = useNavigate();

  function onSubmit(expense) {
    dispatch(addExpense(expense));
    navigate("/");
  }

  return (
    <div>
      <h1>Add Expense</h1>
      <ExpenseForm onSubmit={onSubmit} />
    </div>
  );
};

export default AddExpensePage;

我已经写了这个测试:

import React from "react";
import * as reactRedux from "react-redux";
import { shallow } from "enzyme";
import AddExpensePage from "../AddExpensePage";
import expenses from "../../fixtures/expenses";

// Mock useDispatch hook
const useDispatch = jest.spyOn(reactRedux, "useDispatch");

jest.mock("react-redux", () => ({
  ...jest.requireActual("react-redux"),
  useDispatch: jest.fn(),
}));

const mockedUsedNavigate = jest.fn();

jest.mock("react-router-dom", () => ({
  ...jest.requireActual("react-router-dom"),
  useNavigate: () => mockedUsedNavigate,
}));

beforeEach(() => {
  useDispatch.mockClear();
});

test("should render AddExpensePage correctly", () => {
  const wrapper = shallow(<AddExpensePage />);
  expect(wrapper).toMatchSnapshot();
});

test("should handle onSubmit", () => {
  const dispatch = jest.fn();
  useDispatch.mockReturnValue(dispatch);
  const onSubmitSpy = jest.fn();
  const wrapper = shallow(<AddExpensePage onSubmit={onSubmitSpy} />);
  wrapper.find("ExpenseForm").prop("onSubmit")(expenses[1]);
  expect(dispatch).toHaveBeenCalled();
  expect(mockedUsedNavigate).toHaveBeenLastCalledWith("/");
  // expect(onSubmitSpy).toHaveBeenLastCalledWith(expenses[1]);
});

我不确定这条线是否有效 "wrapper.find("ExpenseForm").prop("onSubmit")(expenses[1]);"

我还想测试这个“expect(onSubmitSpy).toHaveBeenLastCalledWith(expenses[1]);”。但我找不到如何做到这一点。

你有什么想法可以帮助我吗?

请告知我应该如何编写测试这些组件。

提前致谢。

【问题讨论】:

    标签: javascript reactjs redux react-hooks jestjs


    【解决方案1】:

    建议测试组件的行为,而不是它们的实现。因此,您应该找到调用它的按钮并模拟点击,而不是以编程方式调用您的函数。接受测试函数处理的数据的文本输入(我想)也是如此。

    此外,如果您的组件不接受这样的道具,您不能通过将函数作为道具传递来简单地模拟函数。

    test("should handle onSubmit", () => {
      const dispatch = jest.fn();
      const navigate = jest.fn();
      useDispatch.mockReturnValue(dispatch);
      useNavigate.mockReturnValue(navigate);
    
      const wrapper = shallow(<AddExpensePage />);
      const submitBtn = wrapper.find('button[type="submit"]');
      submitBtn.simulate('change', { target: { value: 'myMock' } } );
      
      expect(dispatch).toHaveBeenCalled();
      expect(navigate).toHaveBeenCalledWith("/");
    });
    

    【讨论】:

    • 其实这不是我要找的。我实际上在 ExpenseForm 页面测试中测试了提交表单。但我又试了一次,我知道我不需要这个“const onSubmitSpy = jest.fn();”。但这行有效“wrapper.find("ExpenseForm").prop("onSubmit")(expenses[1]);"
    • 我尝试这样做:“expect(dispatch).toHaveBeenLastCalledWith(addExpense(expenses[1]));”这是正确的方法吗?它实际上有效,只是我收到错误,因为在 addExpense 操作中有费用 id 的 uuid。现在我正在努力解决这个问题。
    • 非常感谢您的回复,它帮助我理解了这个概念
    猜你喜欢
    • 2021-08-30
    • 2020-12-10
    • 2019-08-06
    • 1970-01-01
    • 2020-12-18
    • 2021-08-06
    • 2021-04-13
    • 2021-08-18
    • 2020-09-21
    相关资源
    最近更新 更多