diff --git a/app/containers/HomePage/sagas.js b/app/containers/HomePage/sagas.js index 7e368760e3..076a4888b1 100644 --- a/app/containers/HomePage/sagas.js +++ b/app/containers/HomePage/sagas.js @@ -19,13 +19,12 @@ export function* getRepos() { const username = yield select(selectUsername()); const requestURL = `https://api.github.com/users/${username}/repos?type=all&sort=updated`; - // Call our request helper (see 'utils/request') - const repos = yield call(request, requestURL); - - if (!repos.err) { - yield put(reposLoaded(repos.data, username)); - } else { - yield put(repoLoadingError(repos.err)); + try { + // Call our request helper (see 'utils/request') + const repos = yield call(request, requestURL); + yield put(reposLoaded(repos, username)); + } catch (err) { + yield put(repoLoadingError(err)); } } diff --git a/app/containers/HomePage/tests/sagas.test.js b/app/containers/HomePage/tests/sagas.test.js index 2ecbb014ab..fe4d88faea 100644 --- a/app/containers/HomePage/tests/sagas.test.js +++ b/app/containers/HomePage/tests/sagas.test.js @@ -34,23 +34,19 @@ describe('getRepos Saga', () => { }); it('should dispatch the reposLoaded action if it requests the data successfully', () => { - const response = { - data: [{ - name: 'First repo', - }, { - name: 'Second repo', - }], - }; + const response = [{ + name: 'First repo', + }, { + name: 'Second repo', + }]; const putDescriptor = getReposGenerator.next(response).value; - expect(putDescriptor).toEqual(put(reposLoaded(response.data, username))); + expect(putDescriptor).toEqual(put(reposLoaded(response, username))); }); it('should call the repoLoadingError action if the response errors', () => { - const response = { - err: 'Some error', - }; - const putDescriptor = getReposGenerator.next(response).value; - expect(putDescriptor).toEqual(put(repoLoadingError(response.err))); + const response = new Error('Some error'); + const putDescriptor = getReposGenerator.throw(response).value; + expect(putDescriptor).toEqual(put(repoLoadingError(response))); }); }); diff --git a/app/utils/request.js b/app/utils/request.js index b42743a508..1ea906cb83 100644 --- a/app/utils/request.js +++ b/app/utils/request.js @@ -34,12 +34,10 @@ function checkStatus(response) { * @param {string} url The URL we want to request * @param {object} [options] The options we want to pass to "fetch" * - * @return {object} An object containing either "data" or "err" + * @return {object} The response data */ export default function request(url, options) { return fetch(url, options) .then(checkStatus) - .then(parseJSON) - .then((data) => ({ data })) - .catch((err) => ({ err })); + .then(parseJSON); } diff --git a/app/utils/tests/request.test.js b/app/utils/tests/request.test.js index 63741e6302..30907832b6 100644 --- a/app/utils/tests/request.test.js +++ b/app/utils/tests/request.test.js @@ -34,7 +34,7 @@ describe('request', () => { request('/thisurliscorrect') .catch(done) .then((json) => { - expect(json.data.hello).toEqual('world'); + expect(json.hello).toEqual('world'); done(); }); }); @@ -56,9 +56,9 @@ describe('request', () => { it('should catch errors', (done) => { request('/thisdoesntexist') - .then((json) => { - expect(json.err.response.status).toEqual(404); - expect(json.err.response.statusText).toEqual('Not Found'); + .catch((err) => { + expect(err.response.status).toEqual(404); + expect(err.response.statusText).toEqual('Not Found'); done(); }); });