@@ -6,6 +6,7 @@ const shouldSinon = require('should-sinon') // eslint-disable-line
66describe ( 'appState' , ( ) => {
77 it ( 'handling undefined logger callback function' , ( ) => {
88 const appState = require ( './appState' ) ( )
9+ appState . reset ( )
910 appState . get ( ) . should . equal ( 'INIT' )
1011 appState . running ( )
1112 appState . get ( ) . should . equal ( 'RUNNING' )
@@ -14,16 +15,19 @@ describe('appState', () => {
1415 it ( 'handling logger callback function, logging changes' , ( ) => {
1516 const loggerCB = sinon . spy ( )
1617 const appState = require ( './appState' ) ( loggerCB )
18+ appState . reset ( )
1719 appState . init ( )
20+ appState . running ( )
1821 loggerCB . should . be . calledOnce ( )
1922 // appState hasn't changed: called one
20- appState . init ( )
23+ appState . running ( )
2124 loggerCB . should . be . calledOnce ( )
2225 } )
2326
2427 it ( 'changed: logged' , ( ) => {
2528 const loggerCB = sinon . spy ( )
2629 const appState = require ( './appState' ) ( loggerCB )
30+ appState . reset ( )
2731 appState . running ( )
2832 loggerCB . should . be . calledOnce ( )
2933 appState . stopped ( )
@@ -36,68 +40,149 @@ describe('appState', () => {
3640 info ( appState , newAppState ) { console . log ( `App state has changed from ${ appState } to ${ newAppState } ` ) }
3741 }
3842 const appState = require ( './appState' ) ( logger . info )
39- appState . init ( )
43+ appState . reset ( )
44+ appState . running ( )
4045 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 ' )
4247 console . log . restore ( )
4348 } )
4449
4550 it ( 'is INIT' , ( ) => {
4651 const appState = require ( './appState' ) ( )
52+ appState . reset ( )
4753 appState . init ( )
4854 appState . get ( ) . should . be . equal ( 'INIT' )
4955 } )
5056
5157 it ( 'is RUNNING' , ( ) => {
5258 const appState = require ( './appState' ) ( )
59+ appState . reset ( )
5360 appState . running ( )
5461 appState . get ( ) . should . be . equal ( 'RUNNING' )
5562 } )
5663
5764 it ( 'is STOPPED' , ( ) => {
5865 const appState = require ( './appState' ) ( )
66+ appState . reset ( )
5967 appState . stopped ( )
6068 appState . get ( ) . should . be . equal ( 'STOPPED' )
6169 } )
6270
6371 it ( 'is ERROR' , ( ) => {
6472 const appState = require ( './appState' ) ( )
73+ appState . reset ( )
6574 appState . error ( )
6675 appState . get ( ) . should . be . equal ( 'ERROR' )
6776 } )
6877
6978 it ( 'isInit ok' , ( ) => {
7079 const appState = require ( './appState' ) ( )
80+ appState . reset ( )
7181 appState . init ( )
7282 appState . isInit ( ) . should . be . true ( )
7383 } )
7484
7585 it ( 'isRunning ok' , ( ) => {
7686 const appState = require ( './appState' ) ( )
87+ appState . reset ( )
7788 appState . running ( )
7889 appState . isRunning ( ) . should . be . true ( )
7990 } )
8091
8192 it ( 'isStopped ok' , ( ) => {
8293 const appState = require ( './appState' ) ( )
94+ appState . reset ( )
8395 appState . stopped ( )
8496 appState . isStopped ( ) . should . be . true ( )
8597 } )
8698
8799 it ( 'isError ok' , ( ) => {
88100 const appState = require ( './appState' ) ( )
101+ appState . reset ( )
89102 appState . error ( )
90103 appState . isError ( ) . should . be . true ( )
91104 } )
92105
93106 it ( 'list state values' , ( ) => {
94107 const appState = require ( './appState' ) ( )
108+ appState . reset ( )
95109 appState . error ( )
96110 appState . list ( ) . should . be . eql ( {
97111 INIT : 'INIT' ,
98112 RUNNING : 'RUNNING' ,
113+ STOPPED : 'STOPPED' ,
99114 ERROR : 'ERROR' ,
100- STOPPED : 'STOPPED '
115+ FATAL : 'FATAL '
101116 } )
102117 } )
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+ } )
103188} )
0 commit comments