-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
18f95f0
commit 834ccf1
Showing
3 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |