lundi 21 septembre 2020

How to test functions that pass payloads to callback functions

I have a function:

export const refreshAuth = () =>
  new Promise((resolve, reject) =>
    getRefreshToken()
      .then(refreshJWT =>
        axios.post(`${SomeApi.auth}/refresh`, {
          refreshJWT
        })
      )
      .then((res: AxiosResponse<JWTData>) => {
        onSignIn(res.data.accessJWT, res.data.refreshJWT);
        resolve(res.data.accessJWT);
      })
      .catch(err => {
        console.error('failed to refresh the access token', err);
        reject(err);
      })
  );

Callback functions :

export const onSignIn = (access: string, refresh: string) =>
  Promise.all([
    SecureStore.setItemAsync(REFRESH_KEY, refresh),
    SecureStore.setItemAsync(ACCESS_KEY, access)
  ]);

export const getRefreshToken = () => SecureStore.getItemAsync(REFRESH_KEY);

I am at a loss on how to test it using jest, I have done extensive research but cannot find any way how to write a test.

Aucun commentaire:

Enregistrer un commentaire