Skip to content

Commit

Permalink
Add a DispatcherRecorder util
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Feb 25, 2015
1 parent 18f95f0 commit 834ccf1
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
"build": "npm run build-alt; npm run build-alt-runtime",
"build-alt": "babel ./src/alt.js > dist/alt.js",
"build-alt-runtime": "babel --external-helpers src/alt.js > dist/alt-with-runtime.js",
"coverage": "istanbul cover _mocha -- -u exports -R list --compilers js:babel/register test/index.js",
"coverage": "istanbul cover _mocha -- -u exports -R list --compilers js:babel/register test",
"prepublish": "npm test",
"test": "npm run build; npm run tests-all",
"tests-all": "mocha -u exports -R list --compilers js:babel/register test/index.js"
"tests-all": "mocha -u exports -R list --compilers js:babel/register test"
},
"keywords": [
"alt",
Expand Down
68 changes: 68 additions & 0 deletions test/recorder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import 'babel/external-helpers'

import Alt from '../dist/alt-with-runtime'
import assert from 'assert'

import DispatcherRecorder from '../utils/DispatcherRecorder'

let alt = new Alt()
let recorder = new DispatcherRecorder(alt)

function Actions() { this.generateActions('a', 'b', 'c') }

let actions = alt.createActions(Actions)

class Store {
constructor() {
this.bindActions(actions)
this.a = 0
this.b = 0
this.c = 0
}

a(value) { this.a = value }
b(value) { this.b = value }
c(value) { this.c = value }
}

let store = alt.createStore(Store)

export default {
'the dispatcher recorder util'() {
let recording = recorder.record()

assert.equal(recording, true, 'we are now recording')

actions.a('hello')
actions.b('world')
actions.c('it works')

recording = recorder.record()

assert.equal(recording, false, 'we are already recording')

recorder.stop()

assert.equal(store.getState().a, 'hello', 'store state is set')
assert.equal(store.getState().b, 'world', 'store state is set')
assert.equal(store.getState().c, 'it works', 'store state is set')

alt.recycle()

assert.equal(store.getState().a, 0, 'store state is cleared')
assert.equal(store.getState().b, 0, 'store state is cleared')
assert.equal(store.getState().c, 0, 'store state is cleared')

recorder.replay()

assert.equal(store.getState().a, 'hello', 'store state is set')
assert.equal(store.getState().b, 'world', 'store state is set')
assert.equal(store.getState().c, 'it works', 'store state is set')

assert.equal(recorder.events.length, 3, 'there are 3 events stored')

recorder.clear()

assert.equal(recorder.events.length, 0, 'recorder was cleared')
}
}
34 changes: 34 additions & 0 deletions utils/DispatcherRecorder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module.exports = DispatcherRecorder

function DispatcherRecorder(alt) {
this.alt = alt
this.events = []
this.dispatchToken = null
}

DispatcherRecorder.prototype.record = function () {
if (this.dispatchToken) {
return false
}

this.dispatchToken = this.alt.dispatcher.register(function (payload) {
this.events.push(payload)
}.bind(this))

return true
}

DispatcherRecorder.prototype.stop = function () {
this.alt.dispatcher.unregister(this.dispatchToken)
this.dispatchToken = null
}

DispatcherRecorder.prototype.clear = function () {
this.events = []
}

DispatcherRecorder.prototype.replay = function () {
this.events.forEach(function (payload) {
this.alt.dispatch(payload.action, payload.data)
}.bind(this))
}

0 comments on commit 834ccf1

Please sign in to comment.