Slide 15
Slide 15 text
import { call, put } from 'react-saga/effects';
import { fetchSuccess } from '../actions';
import { fetchSaga } from '../sagas';
function* fetchSaga(httpGet, action) {
const { url, options } = action;
try {
let data = yield call(httpGet, url, options);
yield put(fetchSuccess(data));
} catch(err) {
yield put(fetchFailure(err));
}
}
function* watchFetch(httpGet) {
yield* takeEvery(HTTP_FETCH, fetchSaga, httpGet);
}
import { call, put } from 'react-saga/effects';
import { fetchSuccess } from '../actions';
import { fetchSaga } from '../sagas';
describe('fetchSaga', () => {
describe('should call httpGet with url extracted from action', () => {
const httpGet = () => {};
const action = {
type: 'HTTP_FETCH',
url: 'https://twitter.com/ryyppy',
options: {},
};
let ret;
const iter = fetchSaga(httpGet, action);
// Returns the first yield value and injects the data returned by this call
// (that is what the saga-middleware does in the real implementation)
ret = iter.next({ data: 'https://twitter.com/ryyppy});
// iter.throw('Some error') // if you wanna test the catch() condition
expect(ret.value).to.deep.equal(call(httpGet, 'https://twitter.com/ryyppy', {}))
// Next yield should emit the put(...) effect
ret = iter.next();
expect(ret.value).to.deep.equal(
put(fetchSuccess({ data: 'https://twitter.com/ryyppy}))
);
});
});
IMPLEMENTATION TEST