-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from vstirbu/feature/noop-stale-transition-cle…
…anup handle errors only thrown by instance
- Loading branch information
Showing
6 changed files
with
142 additions
and
11 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
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
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,96 @@ | ||
module.exports = function (promise) { | ||
StateMachine.Promise = promise; | ||
|
||
describe('FSM error isolation', function () { | ||
it('instance has unique id', function() { | ||
const config = { | ||
initial: 'here', | ||
events: [ | ||
{ name: 'stay', from: 'here' }, | ||
{ name: 'move', from: 'here', to: 'there' } | ||
] | ||
}; | ||
|
||
const fsm1 = StateMachine(config); | ||
const fsm2 = StateMachine(config); | ||
|
||
expect(fsm1.instanceId()).to.not.equal(fsm2.instanceId); | ||
}); | ||
|
||
it('`Invalid event in current state` error has originating instance id property', function() { | ||
var fsm = StateMachine({ | ||
initial: 'here', | ||
events: [ | ||
{ name: 'stay', from: 'here' }, | ||
{ name: 'move', from: 'here', to: 'there' }, | ||
{ name: 'rest', from: 'there' } | ||
] | ||
}); | ||
|
||
return fsm.rest().catch(err => { | ||
expect(err.message).to.equal('Invalid event in current state'); | ||
expect(err.instanceId).to.equal(fsm.instanceId()); | ||
return fsm.move(); | ||
}); | ||
}); | ||
|
||
it('handle noop transition error from another fsm instance', function (done) { | ||
var other = StateMachine({ | ||
initial: 'one', | ||
events: [ | ||
{ name: 'init', from: 'one', to: 'two' }, | ||
{ name: 'crash', from: 'two' } | ||
] | ||
}); | ||
|
||
var fsm = StateMachine({ | ||
initial: 'here', | ||
events: [ | ||
{ name: 'stay', from: 'here' }, | ||
{ name: 'move', from: 'here', to: 'there' } | ||
], | ||
callbacks: { | ||
onstay: function(opts) { | ||
// throws a FsmError from a different fsm instance | ||
return other.crash(); | ||
} | ||
} | ||
}); | ||
|
||
fsm.stay().catch((e) => { | ||
fsm.move() | ||
.then(() => { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('handle inter transition error from another fsm instance', function () { | ||
var other = StateMachine({ | ||
initial: 'one', | ||
events: [ | ||
{ name: 'init', from: 'one', to: 'two' }, | ||
{ name: 'crash', from: 'two' } | ||
] | ||
}); | ||
|
||
var fsm = StateMachine({ | ||
initial: 'here', | ||
events: [ | ||
{ name: 'stay', from: 'here' }, | ||
{ name: 'move', from: 'here', to: 'there' }, | ||
{ name: 'run', from: 'here', to: 'there' }, | ||
|
||
], | ||
callbacks: { | ||
onmove: function(opts) { | ||
// throws a FsmError from a different fsm instance | ||
return other.crash(); | ||
} | ||
} | ||
}); | ||
|
||
return fsm.move().catch(() => fsm.run()); | ||
}); | ||
}); | ||
} |
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