所以,我似乎无法找到正确的方法。只想测试点击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
});
有关如何正确执行此操作的任何想法?
谢谢!
答案 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();
});