Skip to content

Commit 69808b3

Browse files
committed
Allow no-op transition event if 'to' parameter is not specified (github issue jakesgordon#5)
jakesgordon#5
1 parent 25b9d49 commit 69808b3

4 files changed

Lines changed: 73 additions & 3 deletions

File tree

state-machine.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ StateMachine = {
2626
var from = (e.from instanceof Array) ? e.from : [e.from];
2727
map[e.name] = map[e.name] || {};
2828
for (var n = 0 ; n < from.length ; n++)
29-
map[e.name][from[n]] = e.to;
29+
map[e.name][from[n]] = e.to || from[n]; // allow no-op transition if 'to' is not specified
3030
};
3131

3232
if (initial) {

state-machine.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test_advanced.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test("multiple 'from' states for the same event", function() {
1818
equals(fsm.current, 'green', "initial state should be green");
1919

2020
ok(fsm.can('warn'), "should be able to warn from green state")
21-
ok(fsm.can('panic'), "should NOT be able to panic from green state")
21+
ok(fsm.can('panic'), "should be able to panic from green state")
2222
ok(fsm.cannot('calm'), "should NOT be able to calm from green state")
2323
ok(fsm.cannot('clear'), "should NOT be able to clear from green state")
2424

@@ -66,6 +66,48 @@ test("multiple 'to' states for the same event", function() {
6666

6767
//-----------------------------------------------------------------------------
6868

69+
test("no-op transitions (github issue #5) with multiple from states", function() {
70+
71+
var fsm = StateMachine.create({
72+
initial: 'green',
73+
events: [
74+
{ name: 'warn', from: 'green', to: 'yellow' },
75+
{ name: 'panic', from: ['green', 'yellow'], to: 'red' },
76+
{ name: 'noop', from: ['green', 'yellow'] }, // NOTE: 'to' not specified
77+
{ name: 'calm', from: 'red', to: 'yellow' },
78+
{ name: 'clear', from: ['yellow', 'red'], to: 'green' },
79+
]});
80+
81+
equals(fsm.current, 'green', "initial state should be green");
82+
83+
ok(fsm.can('warn'), "should be able to warn from green state")
84+
ok(fsm.can('panic'), "should be able to panic from green state")
85+
ok(fsm.can('noop'), "should be able to noop from green state")
86+
ok(fsm.cannot('calm'), "should NOT be able to calm from green state")
87+
ok(fsm.cannot('clear'), "should NOT be able to clear from green state")
88+
89+
fsm.noop(); equals(fsm.current, 'green', "noop event should not transition");
90+
fsm.warn(); equals(fsm.current, 'yellow', "warn event should transition from green to yellow");
91+
92+
ok(fsm.cannot('warn'), "should NOT be able to warn from yellow state")
93+
ok(fsm.can('panic'), "should be able to panic from yellow state")
94+
ok(fsm.can('noop'), "should be able to noop from yellow state")
95+
ok(fsm.cannot('calm'), "should NOT be able to calm from yellow state")
96+
ok(fsm.can('clear'), "should be able to clear from yellow state")
97+
98+
fsm.noop(); equals(fsm.current, 'yellow', "noop event should not transition");
99+
fsm.panic(); equals(fsm.current, 'red', "panic event should transition from yellow to red");
100+
101+
ok(fsm.cannot('warn'), "should NOT be able to warn from red state")
102+
ok(fsm.cannot('panic'), "should NOT be able to panic from red state")
103+
ok(fsm.cannot('noop'), "should NOT be able to noop from red state")
104+
ok(fsm.can('calm'), "should be able to calm from red state")
105+
ok(fsm.can('clear'), "should be able to clear from red state")
106+
107+
});
108+
109+
//-----------------------------------------------------------------------------
110+
69111
test("callbacks are called when appropriate for multiple 'from' and 'to' transitions", function() {
70112

71113
var called = [];

test/test_basics.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,31 @@ test("callback arguments are correct", function() {
361361

362362
});
363363

364+
//-----------------------------------------------------------------------------
365+
366+
test("no-op transitions (github issue #5)", function() {
367+
368+
var fsm = StateMachine.create({
369+
initial: 'green',
370+
events: [
371+
{ name: 'noop', from: 'green', }, // NOTE: no 'to' option specified
372+
{ name: 'warn', from: 'green', to: 'yellow' },
373+
{ name: 'panic', from: 'yellow', to: 'red' },
374+
{ name: 'calm', from: 'red', to: 'yellow' },
375+
{ name: 'clear', from: 'yellow', to: 'green' }
376+
]});
377+
378+
equals(fsm.current, 'green', "initial state should be green");
379+
380+
ok(fsm.can('noop'), "should be able to noop from green state")
381+
ok(fsm.can('warn'), "should be able to warn from green state")
382+
383+
fsm.noop(); equals(fsm.current, 'green', "noop event should not cause a transition (there is no 'to' specified)");
384+
fsm.warn(); equals(fsm.current, 'yellow', "warn event should transition from green to yellow");
385+
386+
ok(fsm.cannot('noop'), "should NOT be able to noop from yellow state")
387+
ok(fsm.cannot('warn'), "should NOT be able to warn from yellow state")
388+
389+
});
390+
391+

0 commit comments

Comments
 (0)