license import { interval , merge , Observable , of , range } from 'rxjs' ; import { expand , map , mergeAll , mergeMap , mergeMapTo , switchAll , switchMap , switchMapTo , take } from 'rxjs/operators' ; import { asyncTest , ifEnvSupports } from '../test-util' ; import { supportFeature } from './rxjs.util' ; describe ( 'Observable.merge' , ( ) => { let log ; let observable1 ; let defaultTimeout = jasmine . DEFAULT_TIMEOUT_INTERVAL ; beforeEach ( ( ) => { log = [ ] ; jasmine . DEFAULT_TIMEOUT_INTERVAL = 10000 ; } ) ; afterEach ( ( ) => { jasmine . DEFAULT_TIMEOUT_INTERVAL = defaultTimeout ; } ) ; it ( 'expand func callback should run in the correct zone' , ( ) => { const constructorZone1 = Zone . current . fork ( { name : 'Constructor Zone1' } ) ; const expandZone1 = Zone . current . fork ( { name : 'Expand Zone1' } ) ; const subscriptionZone = Zone . current . fork ( { name : 'Subscription Zone' } ) ; const error = new Error ( 'test' ) ; observable1 = constructorZone1 . run ( ( ) => { return of ( 2 ) ; } ) ; observable1 = expandZone1 . run ( ( ) => { return observable1 . pipe ( expand ( ( val ) => { expect ( Zone . current . name ) . toEqual ( expandZone1 . name ) ; return of ( 1 + val ) ; } ) , take ( 2 ) ) ; } ) ; subscriptionZone . run ( ( ) => { observable1 . subscribe ( ( result ) => { log . push ( result ) ; expect ( Zone . current . name ) . toEqual ( subscriptionZone . name ) ; } , ( err ) => { fail ( 'should not call error' ) ; } , ( ) => { log . push ( 'completed' ) ; expect ( Zone . current . name ) . toEqual ( subscriptionZone . name ) ; } ) ; } ) ; expect ( log ) . toEqual ( [ 2 , 3 , 'completed' ] ) ; } ) ; it ( 'merge func callback should run in the correct zone' , asyncTest ( ( done ) => { const constructorZone1 = Zone . current . fork ( { name : 'Constructor Zone1' } ) ; const subscriptionZone = Zone . current . fork ( { name : 'Subscription Zone' } ) ; const error = new Error ( 'test' ) ; observable1 = constructorZone1 . run ( ( ) => { return merge ( interval ( 10 ) . pipe ( take ( 2 ) ) , interval ( 15 ) . pipe ( take ( 1 ) ) ) ; } ) ; subscriptionZone . run ( ( ) => { observable1 . subscribe ( ( result ) => { log . push ( result ) ; expect ( Zone . current . name ) . toEqual ( subscriptionZone . name ) ; } , ( err ) => { fail ( 'should not call error' ) ; } , ( ) => { log . push ( 'completed' ) ; expect ( Zone . current . name ) . toEqual ( subscriptionZone . name ) ; expect ( log ) . toEqual ( [ 0 , 0 , 1 , 'completed' ] ) ; done ( ) ; } ) ; } ) ; } , Zone . root ) ) ; it ( 'mergeAll func callback should run in the correct zone' , asyncTest ( ( done ) => { const constructorZone1 = Zone . current . fork ( { name : 'Constructor Zone1' } ) ; const subscriptionZone = Zone . current . fork ( { name : 'Subscription Zone' } ) ; const error = new Error ( 'test' ) ; observable1 = constructorZone1 . run ( ( ) => { return of ( 1 , 2 ) . pipe ( map ( ( v ) => { return of ( v + 1 ) ; } ) , mergeAll ( ) ) ; } ) ; subscriptionZone . run ( ( ) => { observable1 . subscribe ( ( result ) => { log . push ( result ) ; expect ( Zone . current . name ) . toEqual ( subscriptionZone . name ) ; } , ( err ) => { fail ( 'should not call error' ) ; } , ( ) => { log . push ( 'completed' ) ; expect ( Zone . current . name ) . toEqual ( subscriptionZone . name ) ; expect ( log ) . toEqual ( [ 2 , 3 , 'completed' ] ) ; done ( ) ; } ) ; } ) ; } , Zone . root ) ) ; it ( 'mergeMap func callback should run in the correct zone' , ( ) => { const constructorZone1 = Zone . current . fork ( { name : 'Constructor Zone1' } ) ; const subscriptionZone = Zone . current . fork ( { name : 'Subscription Zone' } ) ; const error = new Error ( 'test' ) ; observable1 = constructorZone1 . run ( ( ) => { return of ( 1 , 2 ) . pipe ( mergeMap ( ( v ) => { return of ( v + 1 ) ; } ) ) ; } ) ; subscriptionZone . run ( ( ) => { observable1 . subscribe ( ( result ) => { log . push ( result ) ; expect ( Zone . current . name ) . toEqual ( subscriptionZone . name ) ; } , ( err ) => { fail ( 'should not call error' ) ; } , ( ) => { log . push ( 'completed' ) ; expect ( Zone . current . name ) . toEqual ( subscriptionZone . name ) ; expect ( log ) . toEqual ( [ 2 , 3 , 'completed' ] ) ; } ) ; } ) ; } ) ; it ( 'mergeMapTo func callback should run in the correct zone' , ( ) => { const constructorZone1 = Zone . current . fork ( { name : 'Constructor Zone1' } ) ; const subscriptionZone = Zone . current . fork ( { name : 'Subscription Zone' } ) ; const error = new Error ( 'test' ) ; observable1 = constructorZone1 . run ( ( ) => { return of ( 1 , 2 ) . pipe ( mergeMapTo ( of ( 10 ) ) ) ; } ) ; subscriptionZone . run ( ( ) => { observable1 . subscribe ( ( result ) => { log . push ( result ) ; expect ( Zone . current . name ) . toEqual ( subscriptionZone . name ) ; } , ( err ) => { fail ( 'should not call error' ) ; } , ( ) => { log . push ( 'completed' ) ; expect ( Zone . current . name ) . toEqual ( subscriptionZone . name ) ; expect ( log ) . toEqual ( [ 10 , 10 , 'completed' ] ) ; } ) ; } ) ; } ) ; it ( 'switch func callback should run in the correct zone' , ( ) => { const constructorZone1 = Zone . current . fork ( { name : 'Constructor Zone1' } ) ; const subscriptionZone = Zone . current . fork ( { name : 'Subscription Zone' } ) ; observable1 = constructorZone1 . run ( ( ) => { return range ( 0 , 3 ) . pipe ( map ( function ( x ) { return range ( x , 3 ) ; } ) , switchAll ( ) ) ; } ) ; subscriptionZone . run ( ( ) => { observable1 . subscribe ( ( result ) => { log . push ( result ) ; expect ( Zone . current . name ) . toEqual ( subscriptionZone . name ) ; } , ( err ) => { fail ( 'should not call error' ) ; } , ( ) => { log . push ( 'completed' ) ; expect ( Zone . current . name ) . toEqual ( subscriptionZone . name ) ; expect ( log ) . toEqual ( [ 0 , 1 , 2 , 1 , 2 , 3 , 2 , 3 , 4 , 'completed' ] ) ; } ) ; } ) ; } ) ; it ( 'switchMap func callback should run in the correct zone' , ( ) => { const constructorZone1 = Zone . current . fork ( { name : 'Constructor Zone1' } ) ; const subscriptionZone = Zone . current . fork ( { name : 'Subscription Zone' } ) ; observable1 = constructorZone1 . run ( ( ) => { return range ( 0 , 3 ) . pipe ( switchMap ( function ( x ) { return range ( x , 3 ) ; } ) ) ; } ) ; subscriptionZone . run ( ( ) => { observable1 . subscribe ( ( result ) => { log . push ( result ) ; expect ( Zone . current . name ) . toEqual ( subscriptionZone . name ) ; } , ( err ) => { fail ( 'should not call error' ) ; } , ( ) => { log . push ( 'completed' ) ; expect ( Zone . current . name ) . toEqual ( subscriptionZone . name ) ; expect ( log ) . toEqual ( [ 0 , 1 , 2 , 1 , 2 , 3 , 2 , 3 , 4 , 'completed' ] ) ; } ) ; } ) ; } ) ; it ( 'switchMapTo func callback should run in the correct zone' , ( ) => { const constructorZone1 = Zone . current . fork ( { name : 'Constructor Zone1' } ) ; const subscriptionZone = Zone . current . fork ( { name : 'Subscription Zone' } ) ; observable1 = constructorZone1 . run ( ( ) => { return range ( 0 , 3 ) . pipe ( switchMapTo ( 'a' ) ) ; } ) ; subscriptionZone . run ( ( ) => { observable1 . subscribe ( ( result ) => { log . push ( result ) ; expect ( Zone . current . name ) . toEqual ( subscriptionZone . name ) ; } , ( err ) => { fail ( 'should not call error' ) ; } , ( ) => { log . push ( 'completed' ) ; expect ( Zone . current . name ) . toEqual ( subscriptionZone . name ) ; expect ( log ) . toEqual ( [ 'a' , 'a' , 'a' , 'completed' ] ) ; } ) ; } ) ; } ) ; } ) ;