我是 react-testing-library 的新手,长期以来一直在尝试测试一个功能。例如,我想检查当单击按钮时是否调用了给定的函数并抛出错误。所以任何帮助都将受到高度赞赏,如果可能,请与我分享任何有用的资源。
登录.js
export default class SignIn extends Component {
constructor(props) {
super(props);
this.state = {
};
}
handleClose = (event, reason) => { };
validate = () => { };
change = (e) => { };
onSubmit = (e) => { };
render() {
return (<div>...</div>);
}
}
完整:https : //github.com/blaise82/react-testing-library-try/blob/master/src/views/SignIn.js
这是我的测试
it('should submit form', async () => {
const { getByLabelText, getByText, container, debug } = render(<SignIn />);
const change = jest.fn();
const onSubmit = jest.fn();
const email = getByLabelText('email');
const password = getByLabelText('password');
const submit = getByLabelText('submit');
userEvent.type(email, 'octopusbn@gmail.com');
expect(email.value).toBe('octopusbn@gmail.com');
expect(password.value).toBe('');
expect(change).toHaveBeenCalled();
console.log(password)
await userEvent.click(submit);
expect(onSubmit).toHaveBeenCalled();
});
完整:https : //github.com/blaise82/react-testing-library-try/blob/master/src/test/signin.test.js
结果
> Expected number of calls: >= 1
> Received number of calls: 0
请让我知道我做错了什么。
GitHub 上的完整代码:https : //github.com/blaise82/react-testing-library-try