forked from jakesgordon/javascript-state-machine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathissues.js
More file actions
103 lines (73 loc) · 2.7 KB
/
issues.js
File metadata and controls
103 lines (73 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import test from 'ava'
import StateMachine from '../src/app'
import LifecycleLogger from './helpers/lifecycle_logger'
//-------------------------------------------------------------------------------------------------
test('github issue #12 - transition return values', t => {
var fsm = new StateMachine({
transitions: [
{ name: 'init', from: 'none', to: 'A' },
{ name: 'cancelled', from: 'A', to: 'X' },
{ name: 'async', from: 'A', to: 'X' }
],
methods: {
onBeforeCancelled: function() { return false; },
onBeforeAsync: function() { return new Promise(function(resolve, reject) {}); }
}
});
t.is(fsm.init(), true, 'successful (synchronous) transition returns true')
t.is(fsm.cancelled(), false, 'cancelled (synchronous) transition returns true')
var promise = fsm.async();
t.is(typeof promise.then, 'function', 'asynchronous transition returns a promise');
})
//-------------------------------------------------------------------------------------------------
test('github issue #17 - exceptions in lifecycle events are NOT swallowed', t => {
var fsm = new StateMachine({
transitions: [
{ name: 'step', from: 'none', to: 'complete' }
],
methods: {
onTransition: function() { throw Error('oops') }
}
});
t.is(fsm.state, 'none')
const error = t.throws(() => {
fsm.step();
})
t.is(error.message, 'oops')
})
//-------------------------------------------------------------------------------------------------
test('github issue #19 - lifecycle events have correct this when applying StateMachine to a custom class', t => {
var FSM = function() {
this.stepped = false;
this._fsm();
}
FSM.prototype.onStep = function(lifecycle) { this.stepped = true }
StateMachine.factory(FSM, {
transitions: [
{ name: 'step', from: 'none', to: 'complete' }
]
})
var a = new FSM(),
b = new FSM();
t.is(a.state, 'none')
t.is(b.state, 'none')
t.is(a.stepped, false)
t.is(b.stepped, false)
a.step();
t.is(a.state, 'complete')
t.is(b.state, 'none')
t.is(a.stepped, true)
t.is(b.stepped, false)
});
//-------------------------------------------------------------------------------------------------
test('github issue #64 - double wildcard transition does not change state', t => {
var fsm = new StateMachine({
transitions: [
{ name: 'step', from: '*' /* no-op */ }
]
});
t.is(fsm.state, 'none')
fsm.step(); t.is(fsm.state, 'none')
fsm.step(); t.is(fsm.state, 'none')
})
//-------------------------------------------------------------------------------------------------