使用本机和测试库测试函数调用时出现的问题

时间:2021-01-11 14:21:24

标签: react-native react-native-testing-library

我有一个登录表单,它一旦提交就会调用一个从钩子返回的函数。为了让我的测试更容易,我收到了我的钩子作为道具,它的默认值是导入的钩子。但在测试中使用模拟钩子和我想测试的方法

我的测试看起来像这样

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

0 个答案:

没有答案