@@ -536,6 +536,70 @@ describe('firestoreActions', () => {
536
536
'Listeners must be an Array of listener configs (Strings/Objects).' ,
537
537
) ;
538
538
} ) ;
539
+
540
+ describe ( 'oneListenerPerPath' , ( ) => {
541
+ it ( 'works with one listener' , async ( ) => {
542
+ const fakeFirebaseWithOneListener = {
543
+ _ : {
544
+ listeners : { } ,
545
+ config : { ...defaultConfig , oneListenerPerPath : true } ,
546
+ } ,
547
+ firestore : ( ) => ( {
548
+ collection : collectionClass ,
549
+ } ) ,
550
+ } ;
551
+ const instance = createFirestoreInstance (
552
+ fakeFirebaseWithOneListener ,
553
+ { helpersNamespace : 'test' } ,
554
+ dispatchSpy ,
555
+ ) ;
556
+ const listeners = [
557
+ {
558
+ collection : 'test' ,
559
+ doc : '1' ,
560
+ subcollections : [ { collection : 'test2' } ] ,
561
+ } ,
562
+ ] ;
563
+ const forEachMock = sinon . spy ( listeners , 'forEach' ) ;
564
+ await instance . test . setListeners ( listeners ) ;
565
+ expect ( forEachMock ) . to . be . calledOnce ;
566
+ // SET_LISTENER, LISTENER_RESPONSE, LISTENER_ERROR
567
+ expect ( dispatchSpy ) . to . be . calledThrice ;
568
+ } ) ;
569
+
570
+ it ( 'works with two listeners of the same path (only attaches once)' , async ( ) => {
571
+ const fakeFirebaseWithOneListener = {
572
+ _ : {
573
+ listeners : { } ,
574
+ config : { ...defaultConfig , oneListenerPerPath : true } ,
575
+ } ,
576
+ firestore : ( ) => ( {
577
+ collection : collectionClass ,
578
+ } ) ,
579
+ } ;
580
+ const instance = createFirestoreInstance (
581
+ fakeFirebaseWithOneListener ,
582
+ { helpersNamespace : 'test' } ,
583
+ dispatchSpy ,
584
+ ) ;
585
+ const listeners = [
586
+ {
587
+ collection : 'test' ,
588
+ doc : '1' ,
589
+ subcollections : [ { collection : 'test3' } ] ,
590
+ } ,
591
+ {
592
+ collection : 'test' ,
593
+ doc : '1' ,
594
+ subcollections : [ { collection : 'test3' } ] ,
595
+ } ,
596
+ ] ;
597
+ const forEachMock = sinon . spy ( listeners , 'forEach' ) ;
598
+ await instance . test . setListeners ( listeners ) ;
599
+ expect ( forEachMock ) . to . be . calledOnce ;
600
+ expect ( dispatchSpy ) . to . be . calledThrice ;
601
+ } ) ;
602
+ } ) ;
539
603
} ) ;
540
604
541
605
describe ( 'unsetListener' , ( ) => {
@@ -586,6 +650,62 @@ describe('firestoreActions', () => {
586
650
type : actionTypes . UNSET_LISTENER ,
587
651
} ) ;
588
652
} ) ;
653
+
654
+ describe ( 'oneListenerPerPath option enabled' , ( ) => {
655
+ it ( 'dispatches UNSET_LISTENER action' , async ( ) => {
656
+ const fakeFirebaseWithOneListener = {
657
+ _ : {
658
+ listeners : { } ,
659
+ config : { ...defaultConfig , oneListenerPerPath : true } ,
660
+ } ,
661
+ firestore : ( ) => ( {
662
+ collection : collectionClass ,
663
+ } ) ,
664
+ } ;
665
+ const instance = createFirestoreInstance (
666
+ fakeFirebaseWithOneListener ,
667
+ { helpersNamespace : 'test' } ,
668
+ dispatchSpy ,
669
+ ) ;
670
+ await instance . test . unsetListeners ( [ { collection : 'test' } ] ) ;
671
+ expect ( dispatchSpy ) . to . have . callCount ( 0 ) ;
672
+ } ) ;
673
+
674
+ it ( 'dispatches UNSET_LISTENER action if there is more than one listener' , async ( ) => {
675
+ const fakeFirebaseWithOneListener = {
676
+ _ : {
677
+ listeners : { } ,
678
+ config : { ...defaultConfig , oneListenerPerPath : true } ,
679
+ } ,
680
+ firestore : ( ) => ( {
681
+ collection : collectionClass ,
682
+ } ) ,
683
+ } ;
684
+ const instance = createFirestoreInstance (
685
+ fakeFirebaseWithOneListener ,
686
+ { helpersNamespace : 'test' } ,
687
+ dispatchSpy ,
688
+ ) ;
689
+ await instance . test . setListeners ( [
690
+ { collection : 'test' } ,
691
+ { collection : 'test' } ,
692
+ ] ) ;
693
+ await instance . test . unsetListeners ( [ { collection : 'test' } ] ) ;
694
+ expect ( dispatchSpy ) . to . be . calledThrice ;
695
+ } ) ;
696
+ } ) ;
697
+ } ) ;
698
+
699
+ describe ( 'runTransaction' , ( ) => {
700
+ it ( 'throws if invalid path config is provided' , ( ) => {
701
+ const instance = createFirestoreInstance (
702
+ { } ,
703
+ { helpersNamespace : 'test' } ,
704
+ ) ;
705
+ expect ( ( ) => instance . test . runTransaction ( ) ) . to . throw (
706
+ 'dispatch is not a function' ,
707
+ ) ;
708
+ } ) ;
589
709
} ) ;
590
710
} ) ;
591
711
} ) ;
0 commit comments