我有一个登录表单,它一旦提交就会调用一个从钩子返回的函数。为了让我的测试更容易,我收到了我的钩子作为道具,它的默认值是导入的钩子。但在测试中使用模拟钩子和我想测试的方法
我的测试看起来像这样
it('Should correctly send the form data', () => {
const mockSignIn = jest.fn();
const authHookMock = () => ({
signIn: (email: string, password: string) => {
mockSignIn(email, password)
},
resetPassword: (email: string) => { },
isLoggedIn: () => false
})
const { getByText, getByPlaceholderText } = render(<Auth useAuthHook={authHookMock} />);
const emailInput = getByPlaceholderText("Ex: nome@exemplo.com")
const passWordInput = getByPlaceholderText("Ex: 123_teste")
const actionButon = getByText('Entrar')
const mockEmail = 'teste@gmail.com';
const mockPassword = '134343';
fireEvent.change(emailInput, mockEmail);
fireEvent.change(passWordInput, mockPassword);
fireEvent.click(actionButon);
expect(mockSignIn).toHaveBeenCalledWith(mockEmail, mockPassword);
})
预期结果:
实际结果:
Auth route tests › Login › Should correctly send the form data
expect(jest.fn()).toHaveBeenCalledWith(...expected)
Expected: "teste@gmail.com", "134343"
Number of calls: 0