From 89e14a7dda269b17606f3f9eb03207185ab00b9e Mon Sep 17 00:00:00 2001 From: Peter Ajtai Date: Thu, 11 Jul 2013 08:05:33 -0700 Subject: [PATCH] Adding tests for allowedTransitions --- spec/machina.fsm.spec.js | 75 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/spec/machina.fsm.spec.js b/spec/machina.fsm.spec.js index c1fee9c..7967a10 100644 --- a/spec/machina.fsm.spec.js +++ b/spec/machina.fsm.spec.js @@ -943,4 +943,79 @@ describe( "machina.Fsm", function () { } ); } ); } ); + + describe( "When providing allowedTransitions", function(){ + var invalidstateTriggered = false, + SomeFsm = machina.Fsm.extend( { + initialState : "notStarted", + states : { + "notStarted" : { + start : function () { + this.transition( "started" ); + }, + allowedTransitions: [ + "started" + ] + }, + "started" : { + finish : function () { + this.transition( "finished" ); + }, + allowedTransitions: [ + "finished" + ] + }, + "finished" : { + _onEnter : function () { + + }, + allowedTransitions: [ + // Final state + ] + } + }, + eventListeners: { + invalidstate: [ + function() { + invalidstateTriggered = true; + } + ] + } + }), + someFsm; + + beforeEach(function() { + someFsm = new SomeFsm(); + invalidstateTriggered = false; + }); + + it( " should not transition to disallowed states", function() { + expect(someFsm.state).to.be("notStarted"); + someFsm.transition("finished"); + expect(someFsm.state).to.be("notStarted"); + }); + + it( " should transition to allowed states", function() { + expect(someFsm.state).to.be("notStarted"); + someFsm.transition("started"); + expect(someFsm.state).to.be("started"); + }); + + it( " should not be able to transition out of a final state", function() { + someFsm.transition("started"); + someFsm.transition("finished"); + expect(someFsm.state).to.be("finished"); + someFsm.transition("started"); + expect(someFsm.state).to.be("finished"); + someFsm.transition("notStarted"); + expect(someFsm.state).to.be("finished"); + }); + + it( " should trigger invalidstate when trying to transition to disallowed state", function() { + expect(someFsm.state).to.be("notStarted"); + expect(invalidstateTriggered).to.be(false); + someFsm.transition("finished"); + expect(invalidstateTriggered).to.be(true); + }); + }) } );