samedi 4 août 2018

Testing redux saga with dynamic payloads from requests

I'm trying to understand tests with redux-saga-test-plan, but I'm kinda stuck with dynamic payloads.

Here's my saga:

function* getCheapCars() {
    try {
        const response = yield call(fetch, '/api/home')
        const cars = yield call([response, response.json])
        yield put({ type: types.FETCH_DATA_SUCCESS, cars })
    } catch (error) {
        yield put({ type: types.FETCH_DATA_ERROR, error })
    }
}

And here is the test:

it('just works!', () => {
    return expectSaga(saga)
        .take('FETCH_DATA_SUCCESS')
        .put({ type: 'FETCH_DATA_SUCCESS', cars })
        .dispatch({ type: 'FETCH_DATA_SUCCESS', cars })
        .run()
})

I'm probably wrong (I'm completely new to saga), but how to get dynamic payloads from request - in this case it's cars. Cars represent an array of multiple "posts" with title, slug, id and many other fields. Should I just create a fake object with exact same fields?

And I would need a little help with testing withReducer, basically here's the same saga and this test pass but it shouldn't. Because I don't expect an empty array of items, but array with length of 4 (cars).

it('just works!', async () => {
   const { storeState } = await expectSaga(saga)
     .withReducer(reducer)
     .run()

   expect(storeState).toEqual({
     loading: false,
     error: null,
     items: []
   })
})

Is there something I just don't get? Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire