@@ -50,6 +50,7 @@ describe('State changes', function() {
50
50
it ( 'Should save the final state' , function ( ) {
51
51
return this . subscription . cancel ( )
52
52
. then ( subscription => {
53
+ console . log ( 'sdfsdfsdfsdd09-934-93409we09w09r0e9' , subscription )
53
54
expect ( subscription ) . to . have . property ( 'status' , 'canceled' )
54
55
} )
55
56
} )
@@ -148,8 +149,7 @@ describe('Validation', function() {
148
149
} )
149
150
} )
150
151
151
- describe ( 'Get event states and names' , function ( ) {
152
-
152
+ describe ( 'Utility' , function ( ) {
153
153
const testEvents = [
154
154
{ name : 'activate' , from : [ 'none' ] , to : 'active' } ,
155
155
{ name : 'cancel' , from : 'active' , to : 'canceled' } ,
@@ -158,20 +158,90 @@ describe('Get event states and names', function() {
158
158
{ name : 'expire' , from : '*' , to : 'expired' } ,
159
159
]
160
160
161
- it ( 'should extract the event states' , function ( done ) {
162
- const stateEvents = Subscription . getEventStates ( testEvents )
161
+ describe ( 'getEventStates' , function ( ) {
162
+ it ( 'should extract the event states' , function ( ) {
163
+ const stateEvents = Subscription . getEventStates ( testEvents )
163
164
164
- expect ( stateEvents . length ) . to . equal ( 4 )
165
- expect ( stateEvents ) . to . include ( 'none' , 'active' , 'canceled' , 'expired' )
166
- done ( )
165
+ expect ( stateEvents . length ) . to . equal ( 4 )
166
+ expect ( stateEvents ) . to . include ( 'none' , 'active' , 'canceled' , 'expired' )
167
+ } )
167
168
} )
168
169
169
- it ( 'should extract the event names' , function ( done ) {
170
- const stateNames = Subscription . getEventNames ( testEvents )
170
+ describe ( 'getEventNames' , function ( ) {
171
+ it ( 'should extract the event names' , function ( ) {
172
+ const stateNames = Subscription . getEventNames ( testEvents )
173
+
174
+ expect ( stateNames . length ) . to . equal ( 4 )
175
+ expect ( stateNames ) . to . include ( 'activate' , 'cancel' , 'reactivate' , 'expire' )
176
+ } )
177
+ } )
178
+ } )
179
+
180
+ describe ( 'Cache' , function ( ) {
181
+ beforeEach ( function ( ) {
182
+ delete app . locals [ 'loopback-component-fsm' ]
183
+ } )
171
184
172
- expect ( stateNames . length ) . to . equal ( 4 )
173
- expect ( stateNames ) . to . include ( 'activate' , 'cancel' , 'reactivate' , 'expire' )
174
- done ( )
185
+ describe ( 'Add to cache' , function ( ) {
186
+ const subscription = new Subscription ( {
187
+ id : 1 ,
188
+ status : 'active' ,
189
+ } )
190
+
191
+ it ( 'should create and cache a new state machine' , function ( ) {
192
+ const fsm = app . getStateMachine ( subscription )
193
+ expect ( app . locals ) . to . have . deep . property ( 'loopback-component-fsm.Subscription.id:1' , fsm )
194
+ } )
195
+ } )
196
+
197
+ describe ( 'Remove from cache' , function ( ) {
198
+ const subscription = new Subscription ( {
199
+ id : 1 ,
200
+ status : 'active' ,
201
+ } )
202
+
203
+ it ( 'should delete an existing state machine from the cache' , function ( ) {
204
+ app . getStateMachine ( subscription )
205
+ app . deleteStateMachine ( subscription )
206
+ expect ( app . locals ) . to . not . have . deep . property ( 'loopback-component-fsm.Subscription.id:1' )
207
+ } )
175
208
} )
176
209
210
+ describe ( 'Use cache through transition' , function ( ) {
211
+ beforeEach ( function ( ) {
212
+ return Subscription . create ( { status : 'active' } )
213
+ . then ( subscription => {
214
+ this . subscription = subscription
215
+ } )
216
+ } )
217
+
218
+ it ( 'should delete the state machine on completion' , function ( ) {
219
+ return this . subscription . cancel ( )
220
+ . then ( ( ) => {
221
+ expect ( app . locals ) . to . not . have . deep . property ( `loopback-component-fsm.Subscription.id:${ this . subscription . id } ` )
222
+ } )
223
+ } )
224
+
225
+ it ( 'should reuse an existing state machine if one is in use' , function ( done ) {
226
+
227
+ // Make the subscription.cancel method take 200ms second to run.
228
+ Subscription . observe ( 'fsm:oncancel' , ctx => {
229
+ return Promise . delay ( 200 ) . return ( ctx )
230
+ } )
231
+
232
+ // Start a cancelation.
233
+ this . subscription . cancel ( )
234
+ . then ( result => {
235
+ expect ( app . locals ) . to . not . have . deep . property ( `loopback-component-fsm.Subscription.id:${ this . subscription . id } ` )
236
+ done ( )
237
+ } )
238
+
239
+ // Start another cancel in 100ms (whilst the other is still running).
240
+ Promise . delay ( 100 ) . then ( ( ) => this . subscription . cancel ( ) )
241
+ . then ( ( ) => Promise . reject ( new Error ( 'Should not get this far' ) ) )
242
+ . catch ( err => {
243
+ expect ( err ) . to . have . property ( 'message' , 'Previous transition pending' )
244
+ } )
245
+ } )
246
+ } )
177
247
} )
0 commit comments