@@ -372,7 +372,7 @@ describe("Type: MapSchema", () => {
372
372
assertDeepStrictEqualEncodeAll ( state ) ;
373
373
} ) ;
374
374
375
- it ( "should not encode item if added and removed at the same patch" , ( ) => {
375
+ it ( "should not encode item if added and removed at the same patch (Schema child) " , ( ) => {
376
376
const state = new State ( ) ;
377
377
state . mapOfPlayers = new MapSchema < Player > ( ) ;
378
378
state . mapOfPlayers . set ( 'one' , new Player ( "Jake" , 10 , 10 ) ) ;
@@ -383,9 +383,10 @@ describe("Type: MapSchema", () => {
383
383
384
384
let onRemoveCalls = 0 ;
385
385
let onAddCalls = 0 ;
386
-
387
- $ ( decodedState ) . mapOfPlayers . onRemove ( ( ) => onRemoveCalls ++ ) ;
388
- $ ( decodedState ) . mapOfPlayers . onAdd ( ( ) => onAddCalls ++ ) ;
386
+ let onChangeCalls = 0 ;
387
+ $ ( decodedState ) . mapOfPlayers . onRemove ( ( value , key ) => onRemoveCalls ++ ) ;
388
+ $ ( decodedState ) . mapOfPlayers . onAdd ( ( _ , key ) => onAddCalls ++ ) ;
389
+ $ ( decodedState ) . mapOfPlayers . onChange ( ( _ , key ) => onChangeCalls ++ ) ;
389
390
390
391
decodedState . decode ( state . encode ( ) ) ;
391
392
@@ -396,9 +397,9 @@ describe("Type: MapSchema", () => {
396
397
const patchBytes = state . encode ( ) ;
397
398
398
399
//
399
- // TODO: improve me! `DELETE` operation should not be encoded here.
400
- // this test conflicts with encodeAll() + encode() for other structures, where DELETE operation is necessary.
401
- // // assert.deepStrictEqual([ 4, 1, 0, 1, 11, 193 ], patchBytes);
400
+ // TODO / FIXME: There's an additional 2 bytes for the "remove" operation here, even though no "add" was made .
401
+ // (the "ADD" + "DELETE" operations on same patch are being encoded as "DELETE")
402
+ // // assert.deepStrictEqual([ 255, 2, 129, 11, 255, 1 ], Array.from( patchBytes) );
402
403
//
403
404
404
405
decodedState . decode ( patchBytes ) ;
@@ -408,8 +409,56 @@ describe("Type: MapSchema", () => {
408
409
state . mapOfPlayers . delete ( 'one' ) ;
409
410
410
411
decodedState . decode ( state . encode ( ) ) ;
412
+ assert . deepStrictEqual ( state . toJSON ( ) , decodedState . toJSON ( ) ) ;
413
+
414
+ assert . strictEqual ( 1 , onRemoveCalls ) ;
415
+ assert . strictEqual ( 1 , onAddCalls ) ;
416
+ assert . strictEqual ( 2 , onChangeCalls ) ;
417
+
418
+ assertDeepStrictEqualEncodeAll ( state ) ;
419
+ } ) ;
420
+
421
+ it ( "should not encode item if added and removed at the same patch (primitive child)" , ( ) => {
422
+ class MyState extends Schema {
423
+ @type ( { map : "string" } ) mapOfStrings = new MapSchema < string > ( ) ;
424
+ }
425
+ const state = new MyState ( ) ;
426
+ state . mapOfStrings . set ( 'one' , "one" ) ;
427
+
428
+ const decodedState = new MyState ( ) ;
429
+ const $ = getCallbacks ( decodedState ) ;
430
+
431
+ let onRemoveCalls = 0 ;
432
+ let onAddCalls = 0 ;
433
+ let onChangeCalls = 0 ;
434
+ $ ( decodedState ) . mapOfStrings . onRemove ( ( ) => onRemoveCalls ++ ) ;
435
+ $ ( decodedState ) . mapOfStrings . onAdd ( ( ) => onAddCalls ++ ) ;
436
+ $ ( decodedState ) . mapOfStrings . onChange ( ( ) => onChangeCalls ++ ) ;
437
+
438
+ decodedState . decode ( state . encode ( ) ) ;
439
+
440
+ state . mapOfStrings . set ( 'two' , "two" ) ;
441
+ state . mapOfStrings . delete ( 'two' ) ;
442
+
443
+ const patchBytes = state . encode ( ) ;
444
+
445
+ //
446
+ // TODO / FIXME: There's an additional 2 bytes for the "remove" operation here, even though no "add" was made.
447
+ // (the "ADD" + "DELETE" operations on same patch are being encoded as "DELETE")
448
+ // // assert.deepStrictEqual([ 255, 2, 129, 11, 255, 1 ], Array.from(patchBytes));
449
+ //
450
+
451
+ decodedState . decode ( patchBytes ) ;
452
+ assert . strictEqual ( 0 , onRemoveCalls ) ;
453
+
454
+ state . mapOfStrings . delete ( 'one' ) ;
455
+
456
+ decodedState . decode ( state . encode ( ) ) ;
457
+ assert . deepStrictEqual ( state . toJSON ( ) , decodedState . toJSON ( ) ) ;
411
458
412
459
assert . strictEqual ( 1 , onRemoveCalls ) ;
460
+ assert . strictEqual ( 1 , onAddCalls ) ;
461
+ assert . strictEqual ( 2 , onChangeCalls ) ;
413
462
414
463
assertDeepStrictEqualEncodeAll ( state ) ;
415
464
} ) ;
0 commit comments