1

I'm using jest in my expo project for unit testing. I am new to jest I don't know how to use mock functions to test my redux actions.Following is my action for SignIn.

authActions.ts

..
import Api, {
  SubscriptionPreference,
} from "@myPackage/api";
...
    export const signIn =
      ({ email, phoneNumber, password }) =>
      async (dispatch) => {
        console.log("DATA")
        dispatch({ type: types.STATE_LOADING });
      try {
           const result = (
            await Api.accounts.accountsLogin({
              email,
              phoneNumber,
              password,
            })
          ).data;    
     
          if (!result.success) {
            throw new Error(i18n.t("signin_incorrectUsername"));
          }
    
          await SecureStore.setItemAsync("authtoken", result.token);
          dispatch(loadAuth());
        } catch (e) {
          const errorMessage = e.error?.message || e.message;
          Alert.alert(errorMessage);
          dispatch({
            type: types.AUTH_ERROR,
            payload: errorMessage,
          });
        }
      };

The Api is being called from my custom package.So I am not sure how to write a test suite for my signIn action. I have gone through several documentation but couldn't find a proper one for my case. If anybody knows the answer please help. Any help would be really appreciable. Thank you

Lineu Pastorelli
  • 502
  • 1
  • 8
  • 20
Linu Sherin
  • 1,712
  • 7
  • 38
  • 88

0 Answers0