你需要return await axios.get(API_URL)。
例如
rootSaga.js:
import { call, put, takeEvery } from 'redux-saga/effects';
import { getBlogsSaga } from './getBlogSaga';
const BLOGS = {
LOAD: 'BLOGS_LOAD',
};
function setBlogs(payload) {
return {
type: 'SET_BLOGS',
payload,
};
}
function* displayBlogs() {
const data = yield call(getBlogsSaga);
console.log(data);
yield put(setBlogs(data));
}
function* rootSaga() {
yield takeEvery(BLOGS.LOAD, displayBlogs);
}
export { rootSaga, displayBlogs };
getBlogSaga.ts:
const getBlogsSaga = async () => {
return await Promise.resolve().then(() => {
return [1, 2, 3];
});
};
export { getBlogsSaga };
rootSaga.test.ts:
import { displayBlogs } from './rootSaga';
import { runSaga } from 'redux-saga';
describe('63000691', () => {
it('should pass', async () => {
const dispatched: any[] = [];
await runSaga(
{
dispatch: (action) => dispatched.push(action),
getState: () => ({}),
},
displayBlogs,
).toPromise();
});
});
测试结果:
PASS src/stackoverflow/63000691/rootSaga.test.ts
63000691
✓ should pass (16 ms)
console.log
[ 1, 2, 3 ]
at src/stackoverflow/63000691/rootSaga.ts:17:11
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 2.235 s, estimated 3 s