mardi 16 juin 2020

Testing a post saga

I'm trying totest the following saga but I'm not having any luck:

    params: typeSafeAction<typeof getRefreshTokenRequest>
) {
    const requestURL = '/.netlify/functions/refreshToken'

    try {
        const response = yield call(request, requestURL, {
            method: 'POST',
            body: serialize({
                userId: params.payload
            })
        })

        authToken.token = response.authToken

        yield put(getRefreshTokenSuccess(response.user))
    } catch (error) {
            yield put(getRefreshTokenError(error))
        }
    }
}

I'm trying to properly test it but I'm running into a lot of issues. My test looks like this::

    let refreshTokenGenerator: Generator

    const params = {
        type: ActionTypes.GET_REFRESH_TOKEN_REQUEST,
        payload: ''
    }

    beforeEach(() => {
        refreshTokenGenerator = refreshTokenSaga(
            params as typeSafeAction<typeof getRefreshTokenRequest>
        )

        const callDescriptor = refreshTokenGenerator.next().value
        expect(callDescriptor).toMatchSnapshot()
    })

    it('should dispatch the getRefreshTokenSuccess action if call was successfull', () => {
        const response = { id: 0, name: '' }
        const putDescriptor = refreshTokenGenerator.next(response).value

        expect(putDescriptor).toEqual(put(getRefreshTokenSuccess(response)))
    })
}

It's giving errors like:

- Expected
+ Received
-       "payload": Object {
-         "id": 0,
-         "name": "",
-       },
+       "payload": undefined,

Or it's calling the error function for some reason. does anybody have any idea? any help would be appreciated!

Aucun commentaire:

Enregistrer un commentaire