@@ -49,14 +49,7 @@ function queryTest(observable: Observable<any>, subject: Subject<any>, done: any
49
49
}
50
50
51
51
describe ( 'FirebaseListFactory' , ( ) => {
52
- var subscription : Subscription ;
53
- var questions : FirebaseListObservable < any > ;
54
- var questionsSnapshotted : FirebaseListObservable < any > ;
55
- var ref : any ;
56
- var refSnapshotted : any ;
57
- var val1 : any ;
58
- var val2 : any ;
59
- var val3 : any ;
52
+
60
53
var app : firebase . app . App ;
61
54
62
55
beforeEach ( ( ) => {
@@ -351,13 +344,22 @@ describe('FirebaseListFactory', () => {
351
344
expect ( observable . update instanceof Function ) . toBe ( true ) ;
352
345
expect ( observable . remove instanceof Function ) . toBe ( true ) ;
353
346
} ) ;
354
-
355
-
356
347
} ) ;
357
348
358
349
describe ( 'methods' , ( ) => {
359
350
351
+ var toKey ;
352
+ var val1 : any ;
353
+ var val2 : any ;
354
+ var val3 : any ;
355
+ var questions : FirebaseListObservable < any > ;
356
+ var questionsSnapshotted : FirebaseListObservable < any > ;
357
+ var ref : any ;
358
+ var refSnapshotted : any ;
359
+ var subscription : Subscription ;
360
+
360
361
beforeEach ( ( done : any ) => {
362
+ toKey = ( val ) => val . key ;
361
363
val1 = { key : 'key1' } ;
362
364
val2 = { key : 'key2' } ;
363
365
val3 = { key : 'key3' } ;
@@ -368,6 +370,7 @@ describe('FirebaseListFactory', () => {
368
370
refSnapshotted = questionsSnapshotted . $ref ;
369
371
} ) ;
370
372
373
+
371
374
afterEach ( ( done : any ) => {
372
375
if ( subscription && ! subscription . closed ) {
373
376
subscription . unsubscribe ( ) ;
@@ -434,6 +437,46 @@ describe('FirebaseListFactory', () => {
434
437
} ) ;
435
438
436
439
440
+ it ( 'should re-emit identical instances of unchanged children' , ( done : any ) => {
441
+
442
+ let prev ;
443
+
444
+ take . call ( questions , 2 ) . subscribe (
445
+ ( list ) => {
446
+ if ( prev ) {
447
+ expect ( list [ 0 ] ) . toBe ( prev [ 0 ] ) ;
448
+ done ( ) ;
449
+ } else {
450
+ prev = list ;
451
+ questions . push ( { name : 'bob' } ) ;
452
+ }
453
+ } ,
454
+ done . fail
455
+ ) ;
456
+ questions . push ( { name : 'alice' } ) ;
457
+ } ) ;
458
+
459
+
460
+ it ( 'should re-emit identical instances of unchanged children as snapshots' , ( done : any ) => {
461
+
462
+ let prev ;
463
+
464
+ take . call ( questionsSnapshotted , 2 ) . subscribe (
465
+ ( list ) => {
466
+ if ( prev ) {
467
+ expect ( list [ 0 ] ) . toBe ( prev [ 0 ] ) ;
468
+ done ( ) ;
469
+ } else {
470
+ prev = list ;
471
+ questionsSnapshotted . push ( { name : 'bob' } ) ;
472
+ }
473
+ } ,
474
+ done . fail
475
+ ) ;
476
+ questionsSnapshotted . push ( { name : 'alice' } ) ;
477
+ } ) ;
478
+
479
+
437
480
it ( 'should call off on all events when disposed' , ( done : any ) => {
438
481
const questionRef = firebase . database ( ) . ref ( ) . child ( 'questions' ) ;
439
482
var firebaseSpy = spyOn ( questionRef , 'off' ) . and . callThrough ( ) ;
@@ -447,65 +490,57 @@ describe('FirebaseListFactory', () => {
447
490
448
491
449
492
describe ( 'onChildAdded' , ( ) => {
493
+
450
494
it ( 'should add the child after the prevKey' , ( ) => {
451
- expect ( onChildAdded ( [ val1 , val2 ] , val3 , 'key1' ) ) . toEqual ( [ val1 , val3 , val2 ] ) ;
495
+ expect ( onChildAdded ( [ val1 , val2 ] , val3 , toKey , 'key1' ) ) . toEqual ( [ val1 , val3 , val2 ] ) ;
452
496
} ) ;
453
497
454
498
455
499
it ( 'should not mutate the input array' , ( ) => {
456
500
var inputArr = [ val1 ] ;
457
- expect ( onChildAdded ( inputArr , val2 , 'key1' ) ) . not . toEqual ( inputArr ) ;
501
+ expect ( onChildAdded ( inputArr , val2 , toKey , 'key1' ) ) . not . toEqual ( inputArr ) ;
458
502
} ) ;
459
503
} ) ;
460
504
461
505
462
506
describe ( 'onChildChanged' , ( ) => {
507
+
463
508
it ( 'should move the child after the specified prevKey' , ( ) => {
464
- expect ( onChildChanged ( [ val1 , val2 ] , val1 , 'key2' ) ) . toEqual ( [ val2 , val1 ] ) ;
509
+ expect ( onChildChanged ( [ val1 , val2 ] , val1 , toKey , 'key2' ) ) . toEqual ( [ val2 , val1 ] ) ;
465
510
} ) ;
466
511
467
512
468
513
it ( 'should move the child to the beginning if prevKey is null' , ( ) => {
469
514
expect (
470
- onChildChanged ( [ val1 , val2 , val3 ] , val2 , null )
515
+ onChildChanged ( [ val1 , val2 , val3 ] , val2 , toKey , null )
471
516
) . toEqual ( [ val2 , val1 , val3 ] ) ;
472
517
} ) ;
473
518
474
519
it ( 'should not duplicate the first item if it is the one that changed' , ( ) => {
475
520
expect (
476
- onChildChanged ( [ val1 , val2 , val3 ] , val1 , null )
521
+ onChildChanged ( [ val1 , val2 , val3 ] , val1 , toKey , null )
477
522
) . not . toEqual ( [ val1 , val1 , val2 , val3 ] ) ;
478
523
} ) ;
479
524
480
525
it ( 'should not mutate the input array' , ( ) => {
481
526
var inputArr = [ val1 , val2 ] ;
482
- expect ( onChildChanged ( inputArr , val1 , 'key2' ) ) . not . toEqual ( inputArr ) ;
527
+ expect ( onChildChanged ( inputArr , val1 , toKey , 'key2' ) ) . not . toEqual ( inputArr ) ;
483
528
} ) ;
484
529
485
530
486
531
it ( 'should update the child' , ( ) => {
487
532
expect (
488
- onChildUpdated ( [ val1 , val2 , val3 ] , { key : 'newkey' } , 'key1' ) . map ( v => v . key )
533
+ onChildUpdated ( [ val1 , val2 , val3 ] , { key : 'newkey' } , toKey , 'key1' ) . map ( v => v . key )
489
534
) . toEqual ( [ 'key1' , 'newkey' , 'key3' ] ) ;
490
535
} ) ;
491
536
} ) ;
492
537
493
538
494
539
describe ( 'onChildRemoved' , ( ) => {
495
- var val1 : any ;
496
- var val2 : any ;
497
- var val3 : any ;
498
-
499
- beforeEach ( ( ) => {
500
- val1 = { key : ( ) => 'key1' } ;
501
- val2 = { key : ( ) => 'key2' } ;
502
- val3 = { key : ( ) => 'key3' } ;
503
- } ) ;
504
-
505
540
506
541
it ( 'should remove the child' , ( ) => {
507
542
expect (
508
- onChildRemoved ( [ val1 , val2 , val3 ] , val2 )
543
+ onChildRemoved ( [ val1 , val2 , val3 ] , val2 , toKey )
509
544
) . toEqual ( [ val1 , val3 ] ) ;
510
545
} ) ;
511
546
} ) ;
0 commit comments