【发布时间】:2020-04-09 06:45:08
【问题描述】:
说明:
我正在尝试测试当用户按下“Enter”键时表单是否提交。当按下Submit 按钮时,我通过了测试,但我还想确保使用键盘提交表单(方便和 a11y)。
代码:
test("should submit when pressing enter", () => {
const handleSubmit = jest.fn();
const { getByLabelText } = render(<App handleSubmit={handleSubmit} />);
const input = getByLabelText("Name:");
fireEvent.change(input, { target: { value: "abc" } });
fireEvent.keyPress(input, { key: "Enter", code: 13, charCode: 13 });
expect(handleSubmit).toHaveBeenCalled();
});
这是一个CodeSandbox,所需的代码量最少。
【问题讨论】:
标签: reactjs forms events jestjs react-testing-library