Skip to content

Commit

Permalink
Add test for takex
Browse files Browse the repository at this point in the history
  • Loading branch information
kuy committed Sep 24, 2016
1 parent 821e3a6 commit 21f8973
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"homepage": "https://github.com/kuy/redux-saga-examples",
"scripts": {
"start": "./node_modules/.bin/webpack-dev-server --progress --color --content-base public/",
"build": "./node_modules/.bin/webpack"
"build": "./node_modules/.bin/webpack",
"test": "./node_modules/.bin/mocha --compilers js:babel-register */test/*.js"
},
"authors": [
"Yuki Kodama <endflow.net@gmail.com>"
Expand Down
6 changes: 3 additions & 3 deletions takex/sagas.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { call, fork, take } from 'redux-saga/effects';

function* takexSaga(pattern) {
export function* takexSaga(pattern) {
let action;
while (true) {
action = yield take('*');
Expand All @@ -14,11 +14,11 @@ function* takexSaga(pattern) {
return action;
}

function takex(pattern) {
export function takex(pattern) {
return call(takexSaga, pattern);
}

function* handleActions() {
export function* handleActions() {
while (true) {
const action = yield takex(/^FETCH_/);
console.log('handle', action.type);
Expand Down
41 changes: 41 additions & 0 deletions takex/test/sagas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'babel-polyfill';
import assert from 'assert';
import { handleActions, takex, takexSaga } from '../sagas';
import { fork, take } from 'redux-saga/effects';

describe('sagas', () => {
describe('.handleActions', () => {
it('takes filtered actions', () => {
const saga = handleActions();
let ret = saga.next();
assert.deepStrictEqual(ret.value, takex(/^FETCH_/));
assert(!ret.done);
});
});

describe('.takexSaga', () => {
it('takes an action matching given regex', () => {
const saga = takexSaga(/^FETCH_/);
let ret = saga.next();
assert.deepStrictEqual(ret.value, take('*'));
assert(!ret.done);

const action = { type: 'FETCH_FOO' };
ret = saga.next(action);
assert.deepStrictEqual(ret.value, action);
assert(ret.done);
});

it('ignores an action', () => {
const saga = takexSaga(/^FETCH_/);
let ret = saga.next();
assert.deepStrictEqual(ret.value, take('*'));
assert(!ret.done);

const action = { type: 'SUCCESS_FOO' };
ret = saga.next(action);
assert.deepStrictEqual(ret.value, take('*'));
assert(!ret.done);
});
});
});

0 comments on commit 21f8973

Please sign in to comment.