11import { expect } from 'chai' ;
2- import { find , mergeMap } from 'rxjs/operators' ;
2+ import { find , mergeMap , delay } from 'rxjs/operators' ;
3+ import { TestScheduler } from 'rxjs/testing' ;
34import { hot , cold , expectObservable , expectSubscriptions } from '../helpers/marble-testing' ;
45import { of , Observable , from } from 'rxjs' ;
56
67declare function asDiagram ( arg : string ) : Function ;
78
9+ declare const rxTestScheduler : TestScheduler ;
10+
811/** @test {find} */
912describe ( 'find operator' , ( ) => {
1013 function truePredicate ( x : any ) {
@@ -19,13 +22,13 @@ describe('find operator', () => {
1922
2023 const predicate = function ( x : number ) { return x % 5 === 0 ; } ;
2124
22- expectObservable ( ( < any > source ) . pipe ( find ( predicate ) ) ) . toBe ( expected , values ) ;
25+ expectObservable ( source . pipe ( find ( predicate ) ) ) . toBe ( expected , values ) ;
2326 expectSubscriptions ( source . subscriptions ) . toBe ( subs ) ;
2427 } ) ;
2528
2629 it ( 'should throw if not provided a function' , ( ) => {
2730 expect ( ( ) => {
28- ( < any > of ( 'yut' , 'yee' , 'sam' ) ) . pipe ( find ( 'yee' as any ) ) ;
31+ of ( 'yut' , 'yee' , 'sam' ) . pipe ( find ( 'yee' as any ) ) ;
2932 } ) . to . throw ( TypeError , 'predicate is not a function' ) ;
3033 } ) ;
3134
@@ -34,7 +37,7 @@ describe('find operator', () => {
3437 const subs = '^' ;
3538 const expected = '-' ;
3639
37- expectObservable ( ( < any > source ) . pipe ( find ( truePredicate ) ) ) . toBe ( expected ) ;
40+ expectObservable ( source . pipe ( find ( truePredicate ) ) ) . toBe ( expected ) ;
3841 expectSubscriptions ( source . subscriptions ) . toBe ( subs ) ;
3942 } ) ;
4043
@@ -43,7 +46,7 @@ describe('find operator', () => {
4346 const subs = '(^!)' ;
4447 const expected = '(x|)' ;
4548
46- const result = ( < any > source ) . pipe ( find ( truePredicate ) ) ;
49+ const result = source . pipe ( find ( truePredicate ) ) ;
4750
4851 expectObservable ( result ) . toBe ( expected , { x : undefined } ) ;
4952 expectSubscriptions ( source . subscriptions ) . toBe ( subs ) ;
@@ -58,7 +61,7 @@ describe('find operator', () => {
5861 return value === 'a' ;
5962 } ;
6063
61- expectObservable ( ( < any > source ) . pipe ( find ( predicate ) ) ) . toBe ( expected ) ;
64+ expectObservable ( source . pipe ( find ( predicate ) ) ) . toBe ( expected ) ;
6265 expectSubscriptions ( source . subscriptions ) . toBe ( subs ) ;
6366 } ) ;
6467
@@ -71,7 +74,7 @@ describe('find operator', () => {
7174 return value === 'b' ;
7275 } ;
7376
74- expectObservable ( ( < any > source ) . pipe ( find ( predicate ) ) ) . toBe ( expected ) ;
77+ expectObservable ( source . pipe ( find ( predicate ) ) ) . toBe ( expected ) ;
7578 expectSubscriptions ( source . subscriptions ) . toBe ( subs ) ;
7679 } ) ;
7780
@@ -87,7 +90,7 @@ describe('find operator', () => {
8790 return value === this . target ;
8891 } ;
8992
90- expectObservable ( ( < any > source ) . pipe ( find ( predicate , finder ) ) ) . toBe ( expected ) ;
93+ expectObservable ( source . pipe ( find ( predicate , finder ) ) ) . toBe ( expected ) ;
9194 expectSubscriptions ( source . subscriptions ) . toBe ( subs ) ;
9295 } ) ;
9396
@@ -100,7 +103,7 @@ describe('find operator', () => {
100103 return value === 'z' ;
101104 } ;
102105
103- expectObservable ( ( < any > source ) . pipe ( find ( predicate ) ) ) . toBe ( expected , { x : undefined } ) ;
106+ expectObservable ( source . pipe ( find ( predicate ) ) ) . toBe ( expected , { x : undefined } ) ;
104107 expectSubscriptions ( source . subscriptions ) . toBe ( subs ) ;
105108 } ) ;
106109
@@ -110,7 +113,7 @@ describe('find operator', () => {
110113 const expected = '------- ' ;
111114 const unsub = ' ! ' ;
112115
113- const result = ( < any > source ) . pipe ( find ( ( value : string ) => value === 'z' ) ) ;
116+ const result = source . pipe ( find ( ( value : string ) => value === 'z' ) ) ;
114117
115118 expectObservable ( result , unsub ) . toBe ( expected ) ;
116119 expectSubscriptions ( source . subscriptions ) . toBe ( subs ) ;
@@ -122,7 +125,7 @@ describe('find operator', () => {
122125 const expected = '------- ' ;
123126 const unsub = ' ! ' ;
124127
125- const result = ( < any > source ) . pipe (
128+ const result = source . pipe (
126129 mergeMap ( ( x : string ) => of ( x ) ) ,
127130 find ( ( value : string ) => value === 'z' ) ,
128131 mergeMap ( ( x : string ) => of ( x ) )
@@ -132,6 +135,20 @@ describe('find operator', () => {
132135 expectSubscriptions ( source . subscriptions ) . toBe ( subs ) ;
133136 } ) ;
134137
138+ it ( 'should unsubscribe when the predicate is matched' , ( ) => {
139+ const source = hot ( '--a--b---c-|' ) ;
140+ const subs = '^ !' ;
141+ const expected = '-------(b|)' ;
142+
143+ const duration = rxTestScheduler . createTime ( '--|' ) ;
144+
145+ expectObservable ( source . pipe (
146+ find ( ( value : string ) => value === 'b' ) ,
147+ delay ( duration , rxTestScheduler )
148+ ) ) . toBe ( expected ) ;
149+ expectSubscriptions ( source . subscriptions ) . toBe ( subs ) ;
150+ } ) ;
151+
135152 it ( 'should raise if source raise error while element does not match with predicate' , ( ) => {
136153 const source = hot ( '--a--b--#' ) ;
137154 const subs = '^ !' ;
@@ -141,7 +158,7 @@ describe('find operator', () => {
141158 return value === 'z' ;
142159 } ;
143160
144- expectObservable ( ( < any > source ) . pipe ( find ( predicate ) ) ) . toBe ( expected ) ;
161+ expectObservable ( source . pipe ( find ( predicate ) ) ) . toBe ( expected ) ;
145162 expectSubscriptions ( source . subscriptions ) . toBe ( subs ) ;
146163 } ) ;
147164
@@ -154,7 +171,7 @@ describe('find operator', () => {
154171 throw 'error' ;
155172 } ;
156173
157- expectObservable ( ( < any > source ) . pipe ( find ( predicate ) ) ) . toBe ( expected ) ;
174+ expectObservable ( source . pipe ( find ( predicate ) ) ) . toBe ( expected ) ;
158175 expectSubscriptions ( source . subscriptions ) . toBe ( subs ) ;
159176 } ) ;
160177
0 commit comments