0

I was working on a react native application and I have a redux async action which calls my API when the user tries to sign in. On successful sign in I update the redux store as well my AsyncStorage to keep the user logged in even when the application is restarted.

const fetch = (accessTokenInAsyncStore, navigation) => {
  return (dispatch) => {
    axios({
      method: "post",
      url: CHECK_ACCESS_TOKEN,
      headers: {
        Authorization: accessTokenInAsyncStore
      }
    })
      .then(async ({data}) => {
        const {accessToken, accessTokenExpiresIn} = camelizeKeys(data);
        dispatch(update(accessToken));
        await setAccessTokenDetailsInAsyncStore(accessToken, accessTokenExpiresIn);
      })
      .catch(() => {
        removeAccessTokenDetailsFromAsyncStore();
        const routeName = ROUTE_CONSTANTS.LoginStart;
        const params = {
          resetStoreOnLogout: true
        };
       replaceRoute (navigation, routeName, params); 
    });
  };
};


const setAccessTokenDetailsInAsyncStore = async (accessToken, accessTokenExpiresIn) => {
  const accessTokenExpirationTime = Math.floor(Date.now() / 1000) + accessTokenExpiresIn;
  await AsyncStorage.setItem(accessTokenKey, accessToken);
  await AsyncStorage.setItem(accessTokenExpirationTimeKey, JSON.stringify(accessTokenExpirationTime));
};

const removeAccessTokenDetailsFromAsyncStore = () => {
  AsyncStorage.removeItem(accessTokenKey);
  AsyncStorage.removeItem(accessTokenExpirationTimeKey);
};

I was trying to write a unit test case for redux actions and reducers for this scenario using redux-mock-store but I am not able to understand how to write test cases for thunks which have side effects.

Is this the correct approach or do I need to do modify my code a bit?

Harkirat Saluja
  • 7,768
  • 5
  • 47
  • 73

0 Answers0