Skip to content

Commit

Permalink
add tests and setup npm
Browse files Browse the repository at this point in the history
  • Loading branch information
rt2zz committed Mar 22, 2016
1 parent e8a7ac8 commit 6a667b1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "ava"
},
"author": "rt2zz <[email protected]>",
"license": "MIT"
"license": "MIT",
"devDependencies": {
"ava": "^0.13.0",
"redux": "^3.3.1"
}
}
49 changes: 49 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
@@ -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()
})

0 comments on commit 6a667b1

Please sign in to comment.