@@ -255,4 +255,43 @@ describe("StateCallbacks", () => {
255
255
assert . strictEqual ( 4 , onPropertyListen ) ;
256
256
} ) ;
257
257
258
+ describe ( "ArraySchema" , ( ) => {
259
+ xit ( "consecutive shift + unshift should trigger onAdd at 0 index" , ( ) => {
260
+ class Card extends Schema {
261
+ @type ( "string" ) suit : string ;
262
+ @type ( "number" ) num : number ;
263
+ }
264
+ class State extends Schema {
265
+ @type ( [ Card ] ) deck = new ArraySchema < Card > ( ) ;
266
+ @type ( [ Card ] ) discardPile = new ArraySchema < Card > ( ) ;
267
+ }
268
+
269
+ const state = new State ( ) ;
270
+ const decodedState = createInstanceFromReflection ( state ) ;
271
+
272
+ // create a deck of cards
273
+ for ( let i = 0 ; i < 13 ; i ++ ) {
274
+ state . deck . push ( new Card ( ) . assign ( { suit : "hearts" , num : i } ) ) ;
275
+ }
276
+
277
+ decodedState . decode ( state . encode ( ) ) ;
278
+
279
+ let onChange : number [ ] = [ ] ;
280
+ let onAdd : number [ ] = [ ] ;
281
+
282
+ const $ = getCallbacks ( decodedState ) ;
283
+ $ ( decodedState ) . discardPile . onChange ( ( item , index ) => onChange . push ( index ) ) ;
284
+ $ ( decodedState ) . discardPile . onAdd ( ( item , index ) => onAdd . push ( index ) ) ;
285
+
286
+ for ( let i = 0 ; i < 3 ; i ++ ) {
287
+ state . discardPile . unshift ( state . deck . shift ( ) ) ;
288
+ decodedState . decode ( state . encode ( ) ) ;
289
+ }
290
+
291
+ assert . deepStrictEqual ( onChange , [ 0 , 0 , 0 ] ) ;
292
+ assert . deepStrictEqual ( onAdd , [ 0 , 0 , 0 ] ) ;
293
+ } ) ;
294
+
295
+ } )
296
+
258
297
} ) ;
0 commit comments