1

I have been asked to write a test suite on a series of async functions in React Native. One function for example reads as follows...

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);
      })
  );

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

How would you properly test this function using Jest?

diedu
  • 19,277
  • 4
  • 32
  • 49
Jacob
  • 151
  • 2
  • 12
  • You should use https://jestjs.io/docs/en/tutorial-async for docs on Promises, and something like https://www.npmjs.com/package/jest-mock-axios or https://stackoverflow.com/questions/45016033/how-do-i-test-axios-in-jest to mock the server requests. – Elias Schablowski Sep 11 '20 at 23:41
  • How did you `import` `SecureStore` module? Please provide the code – Lin Du Sep 14 '20 at 07:35
  • Just standard import from expo-secure-store. – Jacob Sep 14 '20 at 17:24

0 Answers0