酶:如何测试包裹形式?

时间:2018-04-18 02:11:42

标签: reactjs enzyme

所以,我似乎无法找到正确的方法。只想测试点击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
});

有关如何正确执行此操作的任何想法?

谢谢!

1 个答案:

答案 0 :(得分:1)

要使上述代码起作用,我必须:

  • 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();
});