@@ -6,6 +6,7 @@ const shouldSinon = require('should-sinon') // eslint-disable-line
6
6
describe ( 'appState' , ( ) => {
7
7
it ( 'handling undefined logger callback function' , ( ) => {
8
8
const appState = require ( './appState' ) ( )
9
+ appState . reset ( )
9
10
appState . get ( ) . should . equal ( 'INIT' )
10
11
appState . running ( )
11
12
appState . get ( ) . should . equal ( 'RUNNING' )
@@ -14,16 +15,19 @@ describe('appState', () => {
14
15
it ( 'handling logger callback function, logging changes' , ( ) => {
15
16
B41A
td> const loggerCB = sinon . spy ( )
16
17
const appState = require ( './appState' ) ( loggerCB )
18
+ appState . reset ( )
17
19
appState . init ( )
20
+ appState . running ( )
18
21
loggerCB . should . be . calledOnce ( )
19
22
// appState hasn't changed: called one
20
- appState . init ( )
23
+ appState . running ( )
21
24
loggerCB . should . be . calledOnce ( )
22
25
} )
23
26
24
27
it ( 'changed: logged' , ( ) => {
25
28
const loggerCB = sinon . spy ( )
26
29
const appState = require ( './appState' ) ( loggerCB )
30
+ appState . reset ( )
27
31
appState . running ( )
28
32
loggerCB . should . be . calledOnce ( )
29
33
appState . stopped ( )
@@ -36,68 +40,149 @@ describe('appState', () => {
36
40
info ( appState , newAppState ) { console . log ( `App state has changed from ${ appState } to ${ newAppState } ` ) }
37
41
}
38
42
const appState = require ( './appState' ) ( logger . info )
39
- appState . init ( )
43
+ appState . reset ( )
44
+ appState . running ( )
40
45
console . log . should . be . calledOnce ( )
41
- console . log . should . be . calledWith ( 'App state has changed from STOPPED to INIT ' )
46
+ console . log . should . be . calledWith ( 'App state has changed from INIT to RUNNING ' )
42
47
console . log . restore ( )
43
48
} )
44
49
45
50
it ( 'is INIT' , ( ) => {
46
51
const appState = require ( './appState' ) ( )
52
+ appState . reset ( )
47
53
appState . init ( )
48
54
appState . get ( ) . should . be . equal ( 'INIT' )
49
55
} )
50
56
51
57
it ( 'is RUNNING' , ( ) => {
52
58
const appState = require ( './appState' ) ( )
59
+ appState . reset ( )
53
60
appState . running ( )
54
61
appState . get ( ) . should . be . equal ( 'RUNNING' )
55
62
} )
56
63
57
64
it ( 'is STOPPED' , ( ) => {
58
65
const appState = require ( './appState' ) ( )
66
+ appState . reset ( )
59
67
appState . stopped ( )
60
68
appState . get ( ) . should . be . equal ( 'STOPPED' )
61
69
} )
62
70
63
71
it ( 'is ERROR' , ( ) => {
64
72
const appState = require ( './appState' ) ( )
73
+ appState . reset ( )
65
74
appState . error ( )
66
75
appState . get ( ) . should . be . equal ( 'ERROR' )
67
76
} )
68
77
69
78
it ( 'isInit ok' , ( ) => {
70
79
const appState = require ( './appState' ) ( )
80
+ appState . reset ( )
71
81
appState . init ( )
72
82
appState . isInit ( ) . should . be . true ( )
73
83
} )
74
84
75
85
it ( 'isRunning ok' , ( ) => {
76
86
const appState = require ( './appState' ) ( )
87
+ appState . reset ( )
77
88
appState . running ( )
78
89
appState . isRunning ( ) . should . be . true ( )
79
90
} )
80
91
81
92
it ( 'isStopped ok'
10000
, ( ) => {
82
93
const appState = require ( './appState' ) ( )
94
+ appState . reset ( )
83
95
appState . stopped ( )
84
96
appState . isStopped ( ) . should . be . true ( )
85
97
} )
86
98
87
99
it ( 'isError ok' , ( ) => {
88
100
const appState = require ( './appState' ) ( )
101
+ appState . reset ( )
89
102
appState . error ( )
90
103
appState . isError ( ) . should . be . true ( )
91
104
} )
92
105
93
106
it ( 'list state values' , ( ) => {
94
107
const appState = require ( './appState' ) ( )
108
+ appState . reset ( )
95
109
appState . error ( )
96
110
appState . list ( ) . should . be . eql ( {
97
111
INIT : 'INIT' ,
98
112
RUNNING : 'RUNNING' ,
113
+ STOPPED : 'STOPPED' ,
99
114
ERROR : 'ERROR' ,
100
- STOPPED : 'STOPPED '
115
+ FATAL : 'FATAL '
101
116
} )
102
117
} )
118
+
119
+ it ( 'get state machine values' , ( ) => {
120
+ const appState = require ( './appState' ) ( )
121
+ const state = appState . list ( )
122
+ appState . reset ( )
123
+ appState . error ( )
124
+ appState . getStateMachine ( ) . should . be . eql ( {
125
+ INIT : [ state . INIT , state . RUNNING , state . STOPPED , state . ERROR , state . FATAL ] ,
126
+ RUNNING : [ state . INIT , state . RUNNING , state . STOPPED , state . ERROR , state . FATAL ] ,
127
+ STOPPED : [ state . INIT , state . RUNNING , state . STOPPED , state . ERROR , state . FATAL ] ,
128
+ ERROR : [ state . INIT , state . RUNNING , state . STOPPED , state . ERROR , state . FATAL ] ,
129
+ FATAL : [ state . FATAL ]
130
+ } )
131
+ } )
132
+
133
+ it ( 'invalid state change FATAL to INIT' , ( ) => {
134
+ const loggerCB = sinon . spy ( )
135
+ const appState = require ( './appState' ) ( loggerCB )
136
+ appState . reset ( )
137
+ appState . get ( ) . should . equal ( 'INIT' )
138
+ appState . running ( )
139
+ loggerCB . should . be . calledOnce ( )
140
+ appState . get ( ) . should . equal ( 'RUNNING' )
141
+ appState . fatal ( )
142
+ loggerCB . should . be . calledTwice ( )
143
+ appState . init ( )
144
+ loggerCB . should . be . calledTwice ( ) // not called
145
+ } )
146
+
147
+ it ( 'invalid state change FATAL to RUNNING' , ( ) => {
148
+ const loggerCB = sinon . spy ( )
149
+ const appState = require ( './appState' ) ( loggerCB )
150
+ appState . reset ( )
151
+ appState . get ( ) . should . equal ( 'INIT' )
152
+ appState . running ( )
153
+ loggerCB . should . be . calledOnce ( )
154
+ appState . get ( ) . should . equal ( 'RUNNING' )
155
+ appState . fatal ( )
156
+ loggerCB . should . be . calledTwice ( )
157
+ appState . running ( )
158
+ loggerCB . should . be . calledTwice ( ) // not called
159
+ } )
160
+
161
+ it ( 'invalid state change FATAL to STOPPED' , ( ) => {
162
+ const loggerCB = sinon . spy ( )
163
+ const appState = require ( './appState' ) ( loggerCB )
164
+ appState . reset ( )
165
+ appState . get ( ) . should . equal ( 'INIT' )
166
+ appState . running ( )
167
+ loggerCB . should . be . calledOnce ( )
168
+ appState . get ( ) . should . equal ( 'RUNNING' )
169
+ appState . fatal ( )
170
+ loggerCB . should . be . calledTwice ( )
171
+ appState . stopped ( )
172
+ loggerCB . should . be . calledTwice ( ) // not called
173
+ } )
174
+
175
+ it ( 'invalid state change FATAL to ERROR' , ( ) => {
176
+ const loggerCB = sinon . spy ( )
177
+ const appState = require ( './appState' ) ( loggerCB )
178
+ appState . reset ( )
179
+ appState . get ( ) . should . equal ( 'INIT' )
180
+ appState . running ( )
181
+ loggerCB . should . be . calledOnce ( )
182
+ appState . get ( ) . should . equal ( 'RUNNING' )
183
+ appState . fatal ( )
184
+ loggerCB . should . be . calledTwice ( )
185
+ appState . error ( )
186
+ loggerCB . should . be . calledTwice ( ) // not called
187
+ } )
103
188
} )
0 commit comments