forked from jakesgordon/javascript-state-machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugins.js
151 lines (109 loc) · 3.87 KB
/
plugins.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import test from 'ava'
import StateMachine from '../src/app'
import LifecycleLogger from './helpers/lifecycle_logger'
//-------------------------------------------------------------------------------------------------
test('an empty plugin object', t => {
var plugin = {
init: function(instance) {
instance.plugged = true
}
};
var fsm = new StateMachine({
plugins: [ plugin ]
});
t.is(fsm.state, 'none')
t.is(fsm.plugged, true)
})
//-------------------------------------------------------------------------------------------------
test('an empty plugin function', t => {
var plugin = function() {
return {
init: function(instance) {
instance.plugged = true
}
}
};
var fsm = new StateMachine({
plugins: [ plugin ]
});
t.is(fsm.state, 'none')
t.is(fsm.plugged, true)
})
//-------------------------------------------------------------------------------------------------
test('an empty plugin function with configuration', t => {
var plugin = function(value) {
return {
init: function(instance) {
instance.plugged = value
}
}
};
var fsm = new StateMachine({
plugins: [ new plugin(42) ]
});
t.is(fsm.state, 'none')
t.is(fsm.plugged, 42)
})
//-------------------------------------------------------------------------------------------------
test('plugin can add methods', t => {
var plugin = {
methods: {
foo: function() { return 'FOO' },
bar: function() { return 'BAR' }
}
};
var fsm = new StateMachine({
plugins: [ plugin ]
});
t.is(fsm.state, 'none')
t.is(fsm.foo(), 'FOO')
t.is(fsm.bar(), 'BAR')
})
//-------------------------------------------------------------------------------------------------
test('plugin can add properties', t => {
var plugin = {
properties: {
color: { get: function() { return 'red' } }
}
};
var fsm = new StateMachine({
plugins: [ plugin ]
});
t.is(fsm.state, 'none')
t.is(fsm.color, 'red')
})
//-------------------------------------------------------------------------------------------------
test('plugin lifecycle hook', t => {
var plugin = {
init: function(instance) {
instance.logger = new LifecycleLogger();
},
lifecycle: function(instance, lifecycle) {
instance.logger(lifecycle)
}
};
var fsm = new StateMachine({
transitions: [
{ name: 'step', from: 'none', to: 'complete' }
],
plugins: [ plugin ]
});
t.is(fsm.state, 'none')
t.deepEqual(fsm.logger.log, [])
fsm.step()
t.is(fsm.state, 'complete')
t.deepEqual(fsm.logger.log, [
{ event: 'onBeforeTransition', transition: 'step', from: 'none', to: 'complete', current: 'none' },
{ event: 'onBeforeStep', transition: 'step', from: 'none', to: 'complete', current: 'none' },
{ event: 'onLeaveState', transition: 'step', from: 'none', to: 'complete', current: 'none' },
{ event: 'onLeaveNone', transition: 'step', from: 'none', to: 'complete', current: 'none' },
{ event: 'onTransition', transition: 'step', from: 'none', to: 'complete', current: 'none' },
{ event: 'onEnterState', transition: 'step', from: 'none', to: 'complete', current: 'complete' },
{ event: 'onEnterComplete', transition: 'step', from: 'none', to: 'complete', current: 'complete' },
{ event: 'onComplete', transition: 'step', from: 'none', to: 'complete', current: 'complete' },
{ event: 'onAfterTransition', transition: 'step', from: 'none', to: 'complete', current: 'complete' },
{ event: 'onAfterStep', transition: 'step', from: 'none', to: 'complete', current: 'complete' },
{ event: 'onStep', transition: 'step', from: 'none', to: 'complete', current: 'complete' }
])
})
//-------------------------------------------------------------------------------------------------