jfairbank / redux-saga-test-plan

Test Redux Saga with an easy plan.
http://redux-saga-test-plan.jeremyfairbank.com
MIT License
1.25k stars 127 forks source link

Tests are being cancelled #374

Closed LeoRedin closed 2 years ago

LeoRedin commented 3 years ago

Howdy everyone! I may be doing things the wrong way but I wanted to ask anyway in case anyone else got the same problem.

Heres my test:

it('should create the access token', () => {
      const refreshToken = 'random-token';

      return expectSaga(createAccessToken, refreshToken)
        .provide([
          [
            matchers.call(HeliosApi.createAccessToken, overrideHeliosAuthentication, refreshToken),
            {
              data: {
                data: {
                  access_token: 'some-token',
                },
              },
            },
          ],
        ])
        .run();
    });

and here is the saga:

function* createAccessToken(refreshToken?: string) {
  yield put(setAuthenticationStepAction(AuthenticationSteps.CreatingAccessToken));
  try {
    if (!refreshToken) {
      throw new Error('Tried to createAccessToken without a refresh token');
    }
    // the code is OK until here and also runs inside the call
    const response = yield* call(() =>
      HeliosApi.createAccessToken(overrideHeliosAuthentication(refreshToken)),
    );
    // from here we get nothing back into the response and the saga is terminated by timeout
    const access_token = response.data.data?.access_token;
    const creation_token = response.data.data?.creation_token;
    if (access_token) {
      setHeliosAccessToken(access_token);
      yield put(refreshRiderAction());
    } else if (creation_token) {
      yield put(setCreationTokenAction(creation_token));
    } else {
      throw new Error('createAccessToken failed to return any tokens');
    }
  } catch (error) {
    yield createErrorReport('createAccessToken failed', { error });
    const status = error.response?.status;
    if (status === 404 || status === 401) {
      // The refresh token we have is invalid, the user is effectively logged out so do it
      yield put(logoutAction());
    } else {
      yield put(setAuthenticationStepAction(AuthenticationSteps.LoginFailed));
    }
  }
}

I wanted to know how should I properly call the matchers to actually meet what my api is doing. I think I am not providing it the correct way. I wanted to have those last 2 params inside the call to be nested params. Any ideas? This happens a lot on my sagas and I wanted to test them :)

Thanks!

LeoRedin commented 2 years ago

In case anyone wants to know the answer you cannot have anonymous functions on your calls. That was the error.