import { assertFork , jestAssertNever , jestAssertUntypedNeverCalled } from '../../test/jest-helper' ; import { Task } from './index' ; describe ( 'Task' , ( ) => { describe ( 'all' , ( ) => { it ( 'should work with a single resolved Task' , cb => { const task = Task . resolve ( 5 ) ; const tAll = Task . all ( [ task ] ) ; tAll . fork ( jestAssertNever ( cb ) , assertFork ( cb , x => expect ( x ) . toEqual ( [ 5 ] ) ) ) ; } ) ; it ( 'should mantain types when given an array of elements of the same type' , cb => { const task = Task . resolve ( 5 ) ; const multipleTasks = [ task , task , task , task , task , task , task , task , task , task , task ] ; const tAll = Task . all ( multipleTasks ) ; tAll . fork ( jestAssertNever ( cb ) , assertFork ( cb , x => expect ( x ) . toEqual ( [ 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 ] ) ) ) ; } ) ; it ( 'should work with a single rejected Task' , cb => { const task = Task . reject ( 'Buu!' ) ; const tAll = Task . all ( [ task ] ) ; tAll . fork ( assertFork ( cb , err => expect ( err ) . toEqual ( 'Buu!' ) ) , jestAssertUntypedNeverCalled ( cb ) ) ; } ) ; it ( 'should resolve if all task are resolved' , cb => { const task1 = Task . resolve ( 10 ) ; const task2 = Task . resolve ( '100' ) ; const task3 = Task . resolve ( true ) ; const tAll = Task . all ( [ task1 , task2 , task3 ] ) ; tAll . fork ( jestAssertNever ( cb ) , assertFork ( cb , x => expect ( x ) . toEqual ( [ 10 , '100' , true ] ) ) ) ; } ) ; it ( 'should wait async tasks' , cb => { const task1 = Task . resolve ( 10 ) ; const task2 = new Task < string , never > ( resolve => setTimeout ( _ => resolve ( 'foo' ) , 10 ) ) ; const task3 = Task . resolve ( true ) ; const tAll = Task . all ( [ task1 , task2 , task3 ] ) ; tAll . fork ( jestAssertNever ( cb ) , assertFork ( cb , x => expect ( x ) . toEqual ( [ 10 , 'foo' , true ] ) ) ) ; } ) ; it ( 'should reject if there is even one rejected one' , cb => { const task1 = Task . resolve ( 10 ) ; const task2 = Task . reject ( 'Buu!' ) ; const task3 = Task . resolve ( 1000 ) ; const tAll = Task . all ( [ task1 , task2 , task3 ] ) ; tAll . fork ( assertFork ( cb , err => expect ( err ) . toEqual ( 'Buu!' ) ) , jestAssertUntypedNeverCalled ( cb ) ) ; } ) ; it ( 'should reject with the first rejection' , cb => { const task1 = Task . reject ( 'Foo' ) ; const task2 = new Task < never , number > ( ( _ , reject ) => setTimeout ( _ => reject ( 9 ) , 0 ) ) ; const task3 = new Task < never , boolean > ( ( _ , reject ) => setTimeout ( _ => reject ( true ) , 0 ) ) ; const tAll = Task . all ( [ task1 , task2 , task3 ] ) ; tAll . fork ( err => { expect ( err ) . toEqual ( 'Foo' ) ; setTimeout ( cb , 10 ) ; } , jestAssertUntypedNeverCalled ( cb ) ) ; } ) ; it ( 'should resolve to an empty array if it\'s called with an empty array' , cb => { const arr = [ ] ; const tAll = Task . all ( arr ) ; tAll . fork ( jestAssertNever ( cb ) , assertFork ( cb , result => { expect ( result ) . toEqual ( [ ] ) ; } ) ) ; } ) ; } ) ; } ) ;