import { assertFork , jestAssertNever , jestAssertUntypedNeverCalled } from '../../test/jest-helper' ; import { Task } from '../task' ; import { UnknownError } from '../task/unknown-error' ; describe ( 'Task' , ( ) => { describe ( 'catch' , ( ) => { it ( 'Should not call the function when the task is not failed' , ( cb ) => { const task = Task . resolve ( '0' ) ; const result = task . catch ( x => { jestAssertNever ( cb ) ( x ) ; return Task . resolve ( 0 ) ; } ) ; result . fork ( jestAssertNever ( cb ) , assertFork ( cb , x => expect ( x ) . toBe ( '0' ) ) ) ; } ) ; it ( 'Should resolve to the catched value from a rejected task' , ( cb ) => { const task = Task . reject ( 'buu' ) ; const result = task . catch ( x => Task . resolve ( 0 ) ) ; result . fork ( jestAssertNever ( cb ) , assertFork ( cb , x => expect ( x ) . toBe ( 0 ) ) ) ; } ) ; it ( 'Should resolve to the catched value from a task that throws' , ( cb ) => { const task = Task . resolve ( 'wii' ) . map ( val => { const t = true ; if ( t === true ) { throw new Error ( 'buu' ) ; } return val ; } ) ; const result = task . catch ( x => Task . resolve ( 0 ) ) ; result . fork ( jestAssertNever ( cb ) , assertFork ( cb , x => expect ( x ) . toBe ( 0 ) ) ) ; } ) ; it ( 'Should transform the error' , ( cb ) => { const task = Task . reject ( 'buu' ) ; const result = task . catch ( x => Task . reject ( true ) ) ; result . fork ( assertFork ( cb , err => expect ( err ) . toBe ( true ) ) , jestAssertNever ( cb ) ) ; } ) ; it ( 'Should work with functions that throws' , ( cb ) => { const task = Task . reject ( 'buu' ) ; const result = task . catch ( x => { throw new Error ( 'buu' ) ; } ) ; result . fork ( assertFork ( cb , x => { expect ( x ) . toBeInstanceOf ( UnknownError ) ; } ) , jestAssertUntypedNeverCalled ( cb ) ) ; } ) ; } ) ; } ) ;