@@ -18,25 +18,12 @@ import {
18
18
} from 'rxjs/operators' ;
19
19
import { TestScheduler } from 'rxjs/testing' ;
20
20
import { observableMatcher } from '../helpers/observableMatcher' ;
21
- import { SinonSpy , spy } from 'sinon' ;
21
+ import { spy } from 'sinon' ;
22
22
23
23
const syncNotify = of ( 1 ) ;
24
24
const asapNotify = scheduled ( syncNotify , asapScheduler ) ;
25
25
const syncError = throwError ( ( ) => new Error ( ) ) ;
26
26
27
- function spyOnUnhandledError ( fn : ( spy : SinonSpy ) => void ) : void {
28
- const prevOnUnhandledError = config . onUnhandledError ;
29
-
30
- try {
31
- const onUnhandledError = spy ( ) ;
32
- config . onUnhandledError = onUnhandledError ;
33
-
34
- fn ( onUnhandledError ) ;
35
- } finally {
36
- config . onUnhandledError = prevOnUnhandledError ;
37
- }
38
- }
39
-
40
27
/** @test {share} */
41
28
describe ( 'share' , ( ) => {
42
29
let rxTest : TestScheduler ;
@@ -810,9 +797,22 @@ describe('share', () => {
810
797
} ) ;
811
798
} ) ;
812
799
813
- it ( 'should not reset on refCount 0 if reset notifier errors before emitting any value' , ( ) => {
814
- spyOnUnhandledError ( ( onUnhandledError ) => {
800
+ describe ( 'when config.onUnhandledError is set' , ( ) => {
801
+ afterEach ( ( ) => {
802
+ config . onUnhandledError = null ;
803
+ } ) ;
804
+
805
+ it ( 'should not reset on refCount 0 if reset notifier errors before emitting any value' , ( done ) => {
815
806
const error = new Error ( ) ;
807
+ let calls = 0 ;
808
+
809
+ config . onUnhandledError = spy ( ( err ) => {
810
+ calls ++ ;
811
+ expect ( err ) . to . equal ( error ) ;
812
+ if ( calls === 2 ) {
813
+ done ( ) ;
814
+ }
815
+ } ) ;
816
816
817
817
rxTest . run ( ( { hot, cold, expectObservable, expectSubscriptions } ) => {
818
818
const source = hot ( ' ---1---2---3---4---(5 )---|' ) ;
@@ -830,17 +830,16 @@ describe('share', () => {
830
830
expectObservable ( result , subscription ) . toBe ( expected ) ;
831
831
expectSubscriptions ( source . subscriptions ) . toBe ( sourceSubs ) ;
832
832
} ) ;
833
-
834
- expect ( onUnhandledError ) . to . have . been . calledTwice ;
835
- expect ( onUnhandledError . getCall ( 0 ) ) . to . have . been . calledWithExactly ( error ) ;
836
- expect ( onUnhandledError . getCall ( 1 ) ) . to . have . been . calledWithExactly ( error ) ;
837
833
} ) ;
838
- } ) ;
839
834
840
- it ( 'should not reset on error if reset notifier errors before emitting any value' , ( ) => {
841
- spyOnUnhandledError ( ( onUnhandledError ) => {
835
+ it ( 'should not reset on error if reset notifier errors before emitting any value' , ( done ) => {
842
836
const error = new Error ( ) ;
843
837
838
+ config . onUnhandledError = spy ( ( err ) => {
839
+ expect ( err ) . to . equal ( error ) ;
840
+ done ( ) ;
841
+ } ) ;
842
+
844
843
rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
845
844
const source = cold ( ' ---1---2---# ' ) ;
846
845
const sourceSubs = ' ^----------! ' ;
@@ -856,89 +855,86 @@ describe('share', () => {
856
855
expectObservable ( result , subscription ) . toBe ( expected ) ;
857
856
expectSubscriptions ( source . subscriptions ) . toBe ( sourceSubs ) ;
858
857
} ) ;
859
-
860
- expect ( onUnhandledError ) . to . have . been . calledOnce ;
861
- expect ( onUnhandledError . getCall ( 0 ) ) . to . have . been . calledWithExactly ( error ) ;
862
858
} ) ;
863
859
} ) ;
864
860
865
- it ( 'should not reset on complete if reset notifier errors before emitting any value' , ( ) => {
866
- spyOnUnhandledError ( ( onUnhandledError ) => {
867
- const error = new Error ( ) ;
868
-
869
- rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
870
- const source = cold ( ' ---1---2---| ' ) ;
871
- const sourceSubs = ' ^----------! ' ;
872
- const expected = ' ---1---2------|' ;
873
- const subscription = ' ^--------------' ;
874
- const firstPause = cold ( ' -------|' ) ;
875
- const reset = cold ( ' --# ' , undefined , error ) ;
876
-
877
- const sharedSource = source . pipe ( share ( { resetOnComplete : ( ) => reset , resetOnRefCountZero : false } ) , take ( 2 ) ) ;
861
+ it ( 'should not reset on complete if reset notifier errors before emitting any value' , ( done ) => {
862
+ const error = new Error ( ) ;
878
863
879
- const result = concat ( sharedSource , firstPause , sharedSource ) ;
880
-
881
- expectObservable ( result , subscription ) . toBe ( expected ) ;
882
- expectSubscriptions ( source . subscriptions ) . toBe ( sourceSubs ) ;
883
- } ) ;
884
-
885
- expect ( onUnhandledError ) . to . have . been . calledOnce ;
886
- expect ( onUnhandledError . getCall ( 0 ) ) . to . have . been . calledWithExactly ( error ) ;
864
+ config . onUnhandledError = spy ( ( err ) => {
865
+ expect ( err ) . to . equal ( error ) ;
866
+ done ( ) ;
887
867
} ) ;
888
- } ) ;
889
868
890
- it ( 'should not call "resetOnRefCountZero" on error' , ( ) => {
891
869
rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
892
- const resetOnRefCountZero = spy ( ( ) => EMPTY ) ;
893
-
894
- const source = cold ( ' ---1---(2#) ' ) ;
895
- // source: ' ---1---(2#) '
896
- const sourceSubs = [
897
- ' ^------(! ) ' ,
898
- // break the line, please
899
- ' -------(- )---^------(! ) ' ,
900
- ] ;
901
- const expected = ' ---1---(2 )------1---(2#) ' ;
902
- const subscription = ' ^------(- )----------(- ) ' ;
903
- const firstPause = cold ( ' (- )---| ' ) ;
904
- const reset = cold ( ' (- )-r ' ) ;
905
- // reset: ' (- )-r'
870
+ const source = cold ( ' ---1---2---| ' ) ;
871
+ const sourceSubs = ' ^----------! ' ;
872
+ const expected = ' ---1---2------|' ;
873
+ const subscription = ' ^--------------' ;
874
+ const firstPause = cold ( ' -------|' ) ;
875
+ const reset = cold ( ' --# ' , undefined , error ) ;
906
876
907
- const sharedSource = source . pipe ( share ( { resetOnError : ( ) => reset , resetOnRefCountZero } ) ) ;
877
+ const sharedSource = source . pipe ( share ( { resetOnComplete : ( ) => reset , resetOnRefCountZero : false } ) , take ( 2 ) ) ;
908
878
909
- const result = concat ( sharedSource . pipe ( onErrorResumeNextWith ( firstPause ) ) , sharedSource ) ;
879
+ const result = concat ( sharedSource , firstPause , sharedSource ) ;
910
880
911
881
expectObservable ( result , subscription ) . toBe ( expected ) ;
912
882
expectSubscriptions ( source . subscriptions ) . toBe ( sourceSubs ) ;
913
- expect ( resetOnRefCountZero ) . to . not . have . been . called ;
914
883
} ) ;
915
884
} ) ;
885
+ } ) ;
916
886
917
- it ( 'should not call "resetOnRefCountZero" on complete' , ( ) => {
918
- rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
919
- const resetOnRefCountZero = spy ( ( ) => EMPTY ) ;
920
-
921
- const source = cold ( ' ---1---(2|) ' ) ;
922
- // source: ' ---1---(2|) '
923
- const sourceSubs = [
924
- ' ^------(! ) ' ,
925
- // break the line, please
926
- ' -------(- )---^------(! ) ' ,
927
- ] ;
928
- const expected = ' ---1---(2 )------1---(2|) ' ;
929
- const subscription = ' ^------(- )----------(- ) ' ;
930
- const firstPause = cold ( ' (- )---| ' ) ;
931
- const reset = cold ( ' (- )-r ' ) ;
932
- // reset: ' (- )-r'
933
-
934
- const sharedSource = source . pipe ( share ( { resetOnComplete : ( ) => reset , resetOnRefCountZero } ) ) ;
935
-
936
- const result = concat ( sharedSource , firstPause , sharedSource ) ;
887
+ it ( 'should not call "resetOnRefCountZero" on error' , ( ) => {
888
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
889
+ const resetOnRefCountZero = spy ( ( ) => EMPTY ) ;
890
+
891
+ const source = cold ( ' ---1---(2#) ' ) ;
892
+ // source: ' ---1---(2#) '
893
+ const sourceSubs = [
894
+ ' ^------(! ) ' ,
895
+ // break the line, please
896
+ ' -------(- )---^------(! ) ' ,
897
+ ] ;
898
+ const expected = ' ---1---(2 )------1---(2#) ' ;
899
+ const subscription = ' ^------(- )----------(- ) ' ;
900
+ const firstPause = cold ( ' (- )---| ' ) ;
901
+ const reset = cold ( ' (- )-r ' ) ;
902
+ // reset: ' (- )-r'
903
+
904
+ const sharedSource = source . pipe ( share ( { resetOnError : ( ) => reset , resetOnRefCountZero } ) ) ;
905
+
906
+ const result = concat ( sharedSource . pipe ( onErrorResumeNextWith ( firstPause ) ) , sharedSource ) ;
907
+
908
+ expectObservable ( result , subscription ) . toBe ( expected ) ;
909
+ expectSubscriptions ( source . subscriptions ) . toBe ( sourceSubs ) ;
910
+ expect ( resetOnRefCountZero ) . to . not . have . been . called ;
911
+ } ) ;
912
+ } ) ;
937
913
938
- expectObservable ( result , subscription ) . toBe ( expected ) ;
939
- expectSubscriptions ( source . subscriptions ) . toBe ( sourceSubs ) ;
940
- expect ( resetOnRefCountZero ) . to . not . have . been . called ;
941
- } ) ;
914
+ it ( 'should not call "resetOnRefCountZero" on complete' , ( ) => {
915
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
916
+ const resetOnRefCountZero = spy ( ( ) => EMPTY ) ;
917
+
918
+ const source = cold ( ' ---1---(2|) ' ) ;
919
+ // source: ' ---1---(2|) '
920
+ const sourceSubs = [
921
+ ' ^------(! ) ' ,
922
+ // break the line, please
923
+ ' -------(- )---^------(! ) ' ,
924
+ ] ;
925
+ const expected = ' ---1---(2 )------1---(2|) ' ;
926
+ const subscription = ' ^------(- )----------(- ) ' ;
927
+ const firstPause = cold ( ' (- )---| ' ) ;
928
+ const reset = cold ( ' (- )-r ' ) ;
929
+ // reset: ' (- )-r'
930
+
931
+ const sharedSource = source . pipe ( share ( { resetOnComplete : ( ) => reset , resetOnRefCountZero } ) ) ;
932
+
933
+ const result = concat ( sharedSource , firstPause , sharedSource ) ;
934
+
935
+ expectObservable ( result , subscription ) . toBe ( expected ) ;
936
+ expectSubscriptions ( source . subscriptions ) . toBe ( sourceSubs ) ;
937
+ expect ( resetOnRefCountZero ) . to . not . have . been . called ;
942
938
} ) ;
943
939
} ) ;
944
940
} ) ;
0 commit comments