2

I'm starting with AWS-Cognito and the reactJs app, the service is working very well. I want to test this function using react-testing-library and Jest.

I don't know how to mock the response Promise received from Auth.signUp(email, password) API

const Handelsregister = async (event) => {
        event.preventDefault();
        try {
            const user = await Auth.signUp(email, password);
            console.log("Registred user info", user);
        } catch (e) {
            console.log(e)
        }
}

My test file looks like this :

it('should render sign up component', async function () {
    const handleRegisterSpy = jest.fn();
    const user = {
        email: 'test@PWDil.com',
        pwd: 'SignUp@2022'
    };
        const {getByTestId} = render(<Authentication screen={1} setScreen={setScreenSpy()} handleRegister={handleRegisterSpy()}/>);

        const email = getByTestId("email");
        const passwd = getByTestId("pwd");
        const signUp = getByTestId("btnSignUpField");
        expect(email).toBeInTheDocument();
        expect(passwd).toBeInTheDocument();
        expect(signUp).toBeInTheDocument();

        fireEvent.change(email, {target: {value: user.emailValue}});
        fireEvent.change(passwd, {target: {value: user.pwdValue}});

        expect(email).toHaveAttribute("type", "email");
        expect(email.value).toEqual("test@gmail.com");
        expect(passwd).toHaveAttribute("type", "password");
        expect(passwd.value).toEqual("SignUp@2022");

        fireEvent.click(signUp);
        expect(handleRegisterSpy).toHaveBeenCalled();
    });

My question is, how can I mock the Auth.SignUp behavior to test both responses mockResolvedValue and mockRejectedValue

Tarek
  • 51
  • 3

0 Answers0