酶:如何测试包装形式?

Enzyme: How to test a wrapped form?

所以,我似乎找不到正确执行此操作的方法。只想测试点击 antd 表单内的按钮:

import React...
import { shallow }...
import { SignInForm }...
import { Form } from "antd"

test("should call newFunction on button click", () => {
  const newFunction = jest.fn();
  const WrappedForm = Form.create()(SignInForm);
  const wrapper = shallow(<WrappedForm newFunction={newFunction} />);
  wrapper.find("#button-id").simulate("click");
  expect(newFunction).toHaveBeenCalled();

  // TypeError: Cannot read property 'getFieldDecorator' of undefined
});

对如何正确执行此操作有任何想法吗?

谢谢!

为了让上面的代码工作,我必须:

  • yarn add jsdom jsdom-global --dev
  • import "jsdom-global/register";添加到测试文件的顶部...上面import React...
  • shallow()更改为mount()
  • 从组件文件中导出 <WrappedForm /> 而不是 const WrappedForm = Form.create()(SignInForm); 虽然两者都有效

工作代码:

test("should call newFunction on Sign In with Button click", () => {
  const newFunction = jest.fn();
  const wrapper = mount(
    <WrappedSignInForm newFunction={newFunction} />
  );
 wrapper
 .find("#button-id")
 .at(0)
 .simulate("click");
  expect(newFunction).toHaveBeenCalled();
});