Skip to content

Commit

Permalink
Merge pull request #1 from pajtai/master
Browse files Browse the repository at this point in the history
Add tests to allowedTransitions
  • Loading branch information
AlexmReynolds committed Jul 11, 2013
2 parents b09db07 + 89e14a7 commit 8f61342
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions spec/machina.fsm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
})
} );

0 comments on commit 8f61342

Please sign in to comment.