Skip to content

Commit 92a4730

Browse files
committed
added unit test to ensure event arguments are passed correctly to ALL hooks.
1 parent d73d918 commit 92a4730

3 files changed

Lines changed: 59 additions & 5 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ along with the following members:
7777
Hooks
7878
=====
7979

80+
>> _NOTE: I'm using the word 'hook' to avoid overloading the word 'event'._
81+
8082
4 hooks are available if your object has methods using the following naming conventions:
8183

8284
* onbefore**event** - fired before an event

state-machine.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ StateMachine = {
6868
var changeState = function(from, to, args) {
6969
var func = this['onchangestate'];
7070
if (func)
71-
func.call(this, from, to);
71+
func.apply(this, [from,to].concat(args));
7272
};
7373

7474
var afterEvent = function(name, args) {
@@ -80,7 +80,7 @@ StateMachine = {
8080
var transition = function(from, to, args) {
8181
this.current = to;
8282
enterState.call(this, to, args);
83-
changeState.call(this, from, to);
83+
changeState.call(this, from, to, args);
8484
};
8585

8686
return function() {
@@ -91,14 +91,15 @@ StateMachine = {
9191
var from = this.current;
9292
var to = map[from].to;
9393
var async = map[from].async;
94+
var self = this;
95+
var args = Array.prototype.slice.call(arguments);
9496

95-
if (beforeEvent.call(this, name) === false)
97+
if (beforeEvent.call(this, name, args) === false)
9698
return;
9799

98100
if (this.current != to) {
99101

100-
var self = this;
101-
this.transition = function() { transition.call(self, from, to, arguments); self.transition = null; };
102+
this.transition = function() { transition.call(self, from, to, args); self.transition = null; };
102103

103104
exitState.call(this, this.current, arguments);
104105

test/test_basics.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,55 @@ test("hooks are called when appropriate", function() {
224224

225225
//-----------------------------------------------------------------------------
226226

227+
test("arguments are passed correctly to all hooks", function() {
228+
229+
var fsm = StateMachine.create({
230+
initial: 'green',
231+
events: [
232+
{ name: 'warn', from: 'green', to: 'yellow' },
233+
{ name: 'panic', from: 'yellow', to: 'red' },
234+
{ name: 'calm', from: 'red', to: 'yellow' },
235+
{ name: 'clear', from: 'yellow', to: 'green' },
236+
]});
237+
238+
var called = [];
239+
240+
// generic state hook
241+
fsm.onchangestate = function(from,to,a,b,c) { called.push('onchange from ' + from + ' to ' + to + ' ' + (a+b+c)); };
242+
243+
// state hooks
244+
fsm.onentergreen = function(a,b,c) { called.push('onentergreen ' + (a+b+c)); };
245+
fsm.onleavegreen = function(a,b,c) { called.push('onleavegreen ' + (a+b+c)); };
246+
fsm.onenteryellow = function(a,b,c) { called.push('onenteryellow ' + (a+b+c)); };
247+
fsm.onleaveyellow = function(a,b,c) { called.push('onleaveyellow ' + (a+b+c)); };
248+
fsm.onenterred = function(a,b,c) { called.push('onenterred ' + (a+b+c)); };
249+
fsm.onleavered = function(a,b,c) { called.push('onleavered ' + (a+b+c)); };
250+
251+
// event hooks
252+
fsm.onbeforewarn = function(a,b,c) { called.push('onbeforewarn ' + (a+b+c)); };
253+
fsm.onafterwarn = function(a,b,c) { called.push('onafterwarn ' + (a+b+c)); };
254+
fsm.onbeforepanic = function(a,b,c) { called.push('onbeforepanic ' + (a+b+c)); };
255+
fsm.onafterpanic = function(a,b,c) { called.push('onafterpanic ' + (a+b+c)); };
256+
fsm.onbeforecalm = function(a,b,c) { called.push('onbeforecalm ' + (a+b+c)); };
257+
fsm.onaftercalm = function(a,b,c) { called.push('onaftercalm ' + (a+b+c)); };
258+
fsm.onbeforeclear = function(a,b,c) { called.push('onbeforeclear ' + (a+b+c)); };
259+
fsm.onafterclear = function(a,b,c) { called.push('onafterclear ' + (a+b+c)); };
260+
261+
called = [];
262+
fsm.warn(1,2,3);
263+
deepEqual(called, ['onbeforewarn 6', 'onleavegreen 6', 'onenteryellow 6', 'onchange from green to yellow 6', 'onafterwarn 6']);
264+
265+
called = [];
266+
fsm.panic(4,5,6);
267+
deepEqual(called, ['onbeforepanic 15', 'onleaveyellow 15', 'onenterred 15', 'onchange from yellow to red 15', 'onafterpanic 15']);
268+
269+
called = [];
270+
fsm.calm(0,0,1);
271+
deepEqual(called, ['onbeforecalm 1', 'onleavered 1', 'onenteryellow 1', 'onchange from red to yellow 1', 'onaftercalm 1']);
272+
273+
called = [];
274+
fsm.clear("a", "b", "c");
275+
deepEqual(called, ['onbeforeclear abc', 'onleaveyellow abc', 'onentergreen abc', 'onchange from yellow to green abc', 'onafterclear abc']);
276+
277+
});
227278

0 commit comments

Comments
 (0)