Skip to content

Commit

Permalink
Use race to cancel on LOCATION_CHANGE
Browse files Browse the repository at this point in the history
  • Loading branch information
justingreenberg committed May 26, 2016
1 parent 4080286 commit ed4792c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
38 changes: 24 additions & 14 deletions app/containers/HomePage/sagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

/* eslint-disable no-constant-condition */

import { take, call, put, select } from 'redux-saga/effects';
import { take, call, put, select, race } from 'redux-saga/effects';

import { LOCATION_CHANGE } from 'react-router-redux';

import { LOAD_REPOS } from 'containers/App/constants';
import { reposLoaded, repoLoadingError } from 'containers/App/actions';
Expand All @@ -19,18 +21,26 @@ export default [

// Individual exports for testing
export function* getGithubData() {
yield take(LOAD_REPOS);
const username = yield select(selectUsername());
const requestURL = `https://api.github.com/users/${username}/repos?type=all&sort=updated`;

// Use call from redux-saga for easier testing
const repos = yield call(request, requestURL);

// We return an object in a specific format, see utils/request.js for more information
if (repos.err === undefined || repos.err === null) {
yield put(reposLoaded(repos.data, username));
} else {
console.log(repos.err.response); // eslint-disable-line no-console
yield put(repoLoadingError(repos.err));
while (true) {
const watcher = yield race({
loadRepos: take(LOAD_REPOS),
stop: take(LOCATION_CHANGE), // stop watching if user leaves page
});

if (watcher.stop) break;

const username = yield select(selectUsername());
const requestURL = `https://api.github.com/users/${username}/repos?type=all&sort=updated`;

// Use call from redux-saga for easier testing
const repos = yield call(request, requestURL);

// We return an object in a specific format, see utils/request.js for more information
if (repos.err === undefined || repos.err === null) {
yield put(reposLoaded(repos.data, username));
} else {
console.log(repos.err.response); // eslint-disable-line no-console
yield put(repoLoadingError(repos.err));
}
}
}
18 changes: 10 additions & 8 deletions app/containers/HomePage/tests/sagas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
*/

import expect from 'expect';
import { take, call, put, select } from 'redux-saga/effects';
import { take, call, put, select, race } from 'redux-saga/effects';
import { LOCATION_CHANGE } from 'react-router-redux';

import { getGithubData } from '../sagas';

Expand All @@ -13,17 +14,18 @@ import { reposLoaded, repoLoadingError } from 'containers/App/actions';
import request from 'utils/request';
import { selectUsername } from 'containers/HomePage/selectors';

describe('getGithubData Saga', () => {
const username = 'mxstbr';
let generator;
const generator = getGithubData();
const username = 'mxstbr';

describe('getGithubData Saga', () => {
// We have to test twice, once for a successful load and once for an unsuccessful one
// so we do all the stuff that happens beforehand automatically in the beforeEach
beforeEach(() => {
generator = getGithubData();

expect(generator.next().value).toEqual(take(LOAD_REPOS));
expect(generator.next().value).toEqual(select(selectUsername()));
expect(generator.next().value).toEqual(race({
loadRepos: take(LOAD_REPOS),
stop: take(LOCATION_CHANGE),
}));
expect(generator.next(take(LOAD_REPOS)).value).toEqual(select(selectUsername()));
const requestURL = `https://api.github.com/users/${username}/repos?type=all&sort=updated`;
expect(generator.next(username).value).toEqual(call(request, requestURL));
});
Expand Down

0 comments on commit ed4792c

Please sign in to comment.