import { assertFork , jestAssertNever , jestAssertUntypedNeverCalled } from '../../test/jest-helper' ; import { catchError , chain , map } from '../operators' ; import { Task } from './index' ; import { UnknownError } from './unknown-error' ; describe ( 'Task' , ( ) => { describe ( 'pipe' , ( ) => { it ( 'Should return the same task if no function is passed' , ( cb ) => { const task = Task . resolve ( 0 ) ; const result = task . pipe ( ) ; result . fork ( jestAssertNever ( cb ) , assertFork ( cb , x => expect ( x ) . toBe ( 0 ) ) ) ; } ) ; it ( 'Should work with one function' , ( cb ) => { const task = Task . resolve ( 0 ) ; const result = task . pipe ( map ( n => '' + n ) ) ; result . fork ( jestAssertNever ( cb ) , assertFork ( cb , x => expect ( x ) . toBe ( '0' ) ) ) ; } ) ; it ( 'Should work with multiple functions' , ( cb ) => { const task = Task . resolve ( 0 ) ; const result = task . pipe ( map ( n => '' + n ) , map ( s => s + '!' ) , map ( s => s + '!' ) , map ( s => s + '!' ) ) ; result . fork ( jestAssertNever ( cb ) , assertFork ( cb , x => expect ( x ) . toBe ( '0!!!' ) ) ) ; } ) ; it ( 'Should work with multiple functions converting success types correctly' , ( cb ) => { const task = Task . resolve ( 0 ) ; const result = task . pipe ( map ( n => '' + n ) , map ( s => parseInt ( s ) ) , map ( n => '' + n ) , map ( s => parseInt ( s ) ) ) ; result . fork ( jestAssertNever ( cb ) , assertFork ( cb , x => expect ( x ) . toBe ( 0 ) ) ) ; } ) ; it ( 'Should be lazy (dont call if not forked)' , ( cb ) => { const task = Task . resolve ( 0 ) ; const dontCall = ( t ) => { expect ( true ) . toBe ( false ) ; return t ; } ; const result = task . pipe ( dontCall ) ; setTimeout ( cb , 20 ) ; } ) ; it ( 'Should handle pipeable methods that throw' , ( cb ) => { const task = Task . resolve ( 0 ) ; const pipeThatThrows = ( t ) : Task < number , never > => { throw 'oops' ; } ; const result = task . pipe ( pipeThatThrows ) ; result . fork ( assertFork ( cb , x => { expect ( x ) . toBeInstanceOf ( UnknownError ) ; } ) , jestAssertUntypedNeverCalled ( cb ) ) ; } ) ; it ( 'Should be able to recover from pipeable methods that throw' , ( cb ) => { const task = Task . resolve ( 0 ) ; const task2 = Task . resolve ( '0' ) ; const pipeThatThrows = ( t ) : Task < number , never > => { throw 'oops' ; } ; const result = task . pipe ( pipeThatThrows , catchError ( err => task2 ) ) ; result . fork ( jestAssertNever ( cb ) , assertFork ( cb , x => expect ( x ) . toBe ( '0' ) ) ) ; } ) ; it ( 'Should be able to propagate errors correctly' , ( cb ) => { const task = Task . resolve ( 0 ) ; const rej = Task . reject ( 'buu' ) ; const task2 = Task . resolve ( '0' ) ; const result = task . pipe ( chain ( val => rej ) , catchError ( err => task2 ) ) ; result . fork ( jestAssertNever ( cb ) , assertFork ( cb , x => expect ( x ) . toBe ( '0' ) ) ) ; } ) ; } ) ; } ) ;