Bru*_*oLM 1 jestjs react-native redux-saga
我有一个传奇
export function* mysaga(api, action) {
const response = yield call(api.service, action);
yield put(NavActions.goTo('Page', { success: response.ok }));
}
Run Code Online (Sandbox Code Playgroud)
会调用API,并返回值,并通过API调用结果(response.ok)导航到另一个屏幕。
it('test', () => {
// ...
const gen = mysaga(api, action);
const step = () => gen.next().value;
// doesn't actually run the 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 }))
);
});
Run Code Online (Sandbox Code Playgroud)
由于实际上未运行,response因此未定义API调用。
我不知道应该怎么做才能测试这种情况。
如何测试我的传奇故事的第二步?
默认情况下,yield表达式解析为它产生的任何内容。但是,您可以将另一个值传递给gen.next方法,然后yield表达式将解析为您在此处传递的值。
因此,这应该可以解决问题(未测试):
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 }))
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
450 次 |
| 最近记录: |