【发布时间】:2020-09-24 17:06:31
【问题描述】:
这就是我的测试用例的样子:
const renderCard = ({
onSubmit = jest.fn(),
}: RenderCardParams = {}) => {
return render(
<Card title={title} onSubmit={onSubmit}>
<Button type="submit">Save</Button>
</Card>,
);
};
it("should invoke onSubmit when form is submitted", async () => {
const onSubmit = jest.fn();
window.HTMLFormElement.prototype.submit = () => {};
const { getByText } = renderCard({ onSubmit });
const button = getByText("Save").closest("button");
if (button) {
fireEvent.click(button);
}
await wait(() => expect(onSubmit).toHaveBeenCalled());
});
我收到“错误:未实现:HTMLFormElement.prototype.submit”。我尝试了这里提到的解决方案 https://github.com/jsdom/jsdom/issues/1937 ,但没有奏效。我不想消除错误,但要正确实施测试。谢谢你。
【问题讨论】:
标签: reactjs typescript forms jestjs react-testing-library