import { assertFork , jestAssertNever , jestAssertUntypedNeverCalled } from '../../test/jest-helper' ; import { Task } from '../task' ; import { UnknownError } from '../task/unknown-error' ; describe ( 'Task' , ( ) => { describe ( 'chain' , ( ) => { it ( 'Should handle functions that succeed' , ( cb ) => { const resolved = Task . resolve ( 1 ) ; const result = resolved . chain ( n => Task . resolve ( n * 2 ) ) ; result . fork ( jestAssertNever ( cb ) , assertFork ( cb , x => expect ( x ) . toBe ( 2 ) ) ) ; } ) ; it ( 'Should handle functions that throw' , ( cb ) => { const resolved = Task . resolve ( 0 ) ; const result = resolved . chain ( n => { throw new Error ( 'buu' ) ; } ) ; result . fork ( assertFork ( cb , x => { expect ( x ) . toBeInstanceOf ( UnknownError ) ; } ) , jestAssertUntypedNeverCalled ( cb ) ) ; } ) ; it ( 'Should propagate error' , ( cb ) => { const rejected = Task . reject ( 'buu' ) ; const result = rejected . chain ( jestAssertNever ( cb ) ) ; result . fork ( assertFork ( cb , x => expect ( x ) . toBe ( 'buu' ) ) , jestAssertUntypedNeverCalled ( cb ) ) ; } ) ; it ( 'Should handle rejection in the chained function' , ( cb ) => { const resolved = Task . resolve ( 1 ) ; const result = resolved . chain ( _ => Task . reject ( 'buu' ) ) ; result . fork ( assertFork ( cb , x => expect ( x ) . toBe ( 'buu' ) ) , jestAssertNever ( cb ) ) ; } ) ; } ) ; } ) ;