react native - How to test a Saga with an API call? -
i have saga
export function* mysaga(api, action) { const response = yield call(api.service, action); yield put(navactions.goto('page', { success: response.ok })); }
that calls api , return value navigate screen passing api call result (response.ok
).
it('test', () => { // ... const gen = mysaga(api, action); const step = () => gen.next().value; // doesn't run api const response = call(api.service, {}); expect(step()).tomatchobject(response); // ok // error, cannot read property 'ok' of undefined expect(step()).tomatchobject( put(navactions.goto('page', { success: response.ok })) ); });
since it's not running api call response
doesn't defined.
i don't know should test scenario.
how test second step of saga?
by default, yield expression resolves whatever yielded. however, can pass value gen.next method , yield expression resolved passed there.
so should trick (untested):
const gen = rootsaga(api, action); const step = (val) => gen.next(val).value; const mockresponse = { ok: true }; const response = call(api.service, {}); expect(step(mockresponse)).tomatchobject(response); // ok expect(step()).tomatchobject( put(navactions.goto('page', { success: true })) );
Comments
Post a Comment