prettier import { expect } from 'chai' ; import { defer , of } from 'rxjs' ; import { mergeMap } from 'rxjs/operators' ; import { TestScheduler } from 'rxjs/testing' ; import { observableMatcher } from '../helpers/observableMatcher' ; test describe ( 'defer' , ( ) => { let rxTestScheduler ; beforeEach ( ( ) => { rxTestScheduler = new TestScheduler ( observableMatcher ) ; } ) ; it ( 'should defer the creation of a simple Observable' , ( ) => { rxTestScheduler . run ( ( { hot , cold , expectObservable , expectSubscriptions } ) => { const expected = '-a--b--c--|' ; const e1 = defer ( ( ) => cold ( '-a--b--c--|' ) ) ; expectObservable ( e1 ) . toBe ( expected ) ; } ) ; } ) ; it ( 'should create an observable from the provided observable factory' , ( ) => { rxTestScheduler . run ( ( { hot , cold , expectObservable , expectSubscriptions } ) => { const source = hot ( '--a--b--c--|' ) ; const sourceSubs = '^----------!' ; const expected = '  --a--b--c--|' ; const e1 = defer ( ( ) => source ) ; expectObservable ( e1 ) . toBe ( expected ) ; expectSubscriptions ( source . subscriptions ) . toBe ( sourceSubs ) ; } ) ; } ) ; it ( 'should create an observable from completed' , ( ) => { rxTestScheduler . run ( ( { hot , cold , expectObservable , expectSubscriptions } ) => { const source = hot ( '|' ) ; const sourceSubs = '(^!)' ; const expected = '  |' ; const e1 = defer ( ( ) => source ) ; expectObservable ( e1 ) . toBe ( expected ) ; expectSubscriptions ( source . subscriptions ) . toBe ( sourceSubs ) ; } ) ; } ) ; it ( 'should accept factory returns promise resolves' , ( done ) => { const expected = 42 ; const e1 = defer ( ( ) => { return new Promise < number > ( ( resolve ) => { resolve ( expected ) ; } ) ; } ) ; e1 . subscribe ( { next : ( x ) => { expect ( x ) . to . equal ( expected ) ; done ( ) ; } , error : ( x ) => { done ( new Error ( 'should not be called' ) ) ; } , } ) ; } ) ; it ( 'should accept factory returns promise rejects' , ( done ) => { const expected = 42 ; const e1 = defer ( ( ) => { return new Promise < number > ( ( resolve , reject ) => { reject ( expected ) ; } ) ; } ) ; e1 . subscribe ( { next : ( x ) => { done ( new Error ( 'should not be called' ) ) ; } , error : ( x ) => { expect ( x ) . to . equal ( expected ) ; done ( ) ; } , complete : ( ) => { done ( new Error ( 'should not be called' ) ) ; } , } ) ; } ) ; it ( 'should create an observable from error' , ( ) => { rxTestScheduler . run ( ( { hot , cold , expectObservable , expectSubscriptions } ) => { const source = hot ( '#' ) ; const sourceSubs = '(^!)' ; const expected = '  #' ; const e1 = defer ( ( ) => source ) ; expectObservable ( e1 ) . toBe ( expected ) ; expectSubscriptions ( source . subscriptions ) . toBe ( sourceSubs ) ; } ) ; } ) ; it ( 'should create an observable when factory does not throw' , ( ) => { rxTestScheduler . run ( ( { hot , cold , expectObservable , expectSubscriptions } ) => { const e1 = defer ( ( ) => { if ( 1 !== Infinity ) { throw 'error' ; } return of ( ) ; } ) ; const expected = '#' ; expectObservable ( e1 ) . toBe ( expected ) ; } ) ; } ) ; it ( 'should error when factory throws' , ( done ) => { const e1 = defer ( ( ) => { if ( 1 + 2 === 3 ) { throw 'error' ; } return of ( ) ; } ) ; e1 . subscribe ( { error : ( ) => done ( ) , } ) ; } ) ; it ( 'should allow unsubscribing early and explicitly' , ( ) => { rxTestScheduler . run ( ( { hot , cold , expectObservable , expectSubscriptions } ) => { const source = hot ( '--a--b--c--|' ) ; const sourceSubs = '^-----!     ' ; const expected = '  --a--b-     ' ; const unsub = '     ------!     ' ; const e1 = defer ( ( ) => source ) ; expectObservable ( e1 , unsub ) . toBe ( expected ) ; expectSubscriptions ( source . subscriptions ) . toBe ( sourceSubs ) ; } ) ; } ) ; it ( 'should not break unsubscription chains when result is unsubscribed explicitly' , ( ) => { rxTestScheduler . run ( ( { hot , cold , expectObservable , expectSubscriptions } ) => { const source = hot ( '--a--b--c--|' ) ; const sourceSubs = '^-----!     ' ; const expected = '  --a--b-     ' ; const unsub = '     ------!     ' ; const e1 = defer ( ( ) => source . pipe ( mergeMap ( ( x ) => of ( x ) ) , mergeMap ( ( x ) => of ( x ) ) ) ) ; expectObservable ( e1 , unsub ) . toBe ( expected ) ; expectSubscriptions ( source . subscriptions ) . toBe ( sourceSubs ) ; } ) ; } ) ; } ) ;