diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..9daeafb --- /dev/null +++ b/.npmignore @@ -0,0 +1 @@ +test diff --git a/index.js b/index.js index d74a468..1923c7d 100644 --- a/index.js +++ b/index.js @@ -11,7 +11,8 @@ module.exports = function bufferActions (breaker, cb) { } } - return next => action => { + return store => next => action => { + // console.log('next', next, action) if (!active) return next(action) if (breaker(action)) { active = false diff --git a/package.json b/package.json index 9f0d116..5a096e1 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,12 @@ "description": "", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "ava" }, "author": "rt2zz ", - "license": "MIT" + "license": "MIT", + "devDependencies": { + "ava": "^0.13.0", + "redux": "^3.3.1" + } } diff --git a/test/tests.js b/test/tests.js new file mode 100644 index 0000000..3d1e823 --- /dev/null +++ b/test/tests.js @@ -0,0 +1,49 @@ +import test from 'ava' + +import { createStore, applyMiddleware } from 'redux' +import actionBuffer from '../index' + +const BREAKER = 'BREAKER' + +// @TODO split into multiple atomic tests +test('buffers actions', t => { + var actionHistory = [] + + function breakCallback(err, {results, queue}) { + // result is an object with results and queue arrays + t.is(results.length, 2) + t.is(queue.length, 2) + } + + let store = createStore( + (state, action) => { + if (action.type.indexOf('@@') !== 0) actionHistory.push(action) + return {} + }, + null, + applyMiddleware(actionBuffer(BREAKER, breakCallback)) + ) + + let action1 = {type: 'ACTION1'} + let action2 = {type: 'ACTION2'} + let action3 = {type: 'ACTION3'} + let breaker = {type: 'BREAKER'} + + let r1 = store.dispatch(action1) + let r2 = store.dispatch(action2) + let rB = store.dispatch(breaker) + let r3 = store.dispatch(action3) + + // buffered actions return strings, other actions return themselves + t.ok(typeof r1 === 'string') + t.ok(typeof r2 === 'string') + t.same(rB, breaker) + t.same(r3, action3) + + // history is re-ordered as expected + t.is(actionHistory.indexOf(breaker), 0) + t.is(actionHistory.indexOf(action1), 1) + t.is(actionHistory.indexOf(action2), 2) + t.is(actionHistory.indexOf(action3), 3) + t.pass() +})