'use strict'; // MODULES // var tape = require( 'tape' ); var noop = require( '@stdlib/utils/noop' ); var funseqAsync = require( './../lib' ); // TESTS // tape( 'main export is a function', function test( t ) { t.ok( true, __filename ); t.equal( typeof funseqAsync, 'function', 'main export is a function' ); t.end(); }); tape( 'the function throws an error if not provided multiple functions to execute sequentially', function test( t ) { t.throws( foo, Error, 'throws an error' ); t.throws( bar, Error, 'throws an error' ); t.end(); function foo() { funseqAsync(); } function bar() { funseqAsync( noop ); } }); tape( 'the function throws an error if not provided a function', function test( t ) { var values; var i; values = [ '5', 5, NaN, null, undefined, true, [], {} ]; for ( i = 0; i < values.length; i++ ) { t.throws( badValue1( values[i] ), TypeError, 'throws an error when provided '+values[i] ); t.throws( badValue2( values[i] ), TypeError, 'throws an error when provided '+values[i] ); t.throws( badValue3( values[i] ), TypeError, 'throws an error when provided '+values[i] ); } t.end(); function badValue1( value ) { return function badValue() { funseqAsync( value, noop ); }; } function badValue2( value ) { return function badValue() { funseqAsync( noop, value ); }; } function badValue3( value ) { return function badValue() { funseqAsync( noop, noop, value ); }; } }); tape( 'the function returns a function', function test( t ) { var fcn = funseqAsync( noop, noop ); t.strictEqual( typeof fcn, 'function', 'returns a function' ); t.end(); }); tape( 'the function returns a pipeline function', function test( t ) { var f; function a( x, next ) { setTimeout( onTimeout, 0 ); function onTimeout() { next( null, x*3 ); } } function b( z, next ) { setTimeout( onTimeout, 0 ); function onTimeout() { next( null, z+5 ); } } function c( r, next ) { setTimeout( onTimeout, 0 ); function onTimeout() { next( null, r/10 ); } } f = funseqAsync( a, b, c ); f( 5, done ); function done( error, v ) { if ( error ) { t.fail( error.message ); } else { t.strictEqual( v, 2, 'returns pipeline result' ); } t.end(); } }); tape( 'the function supports providing a leftmost multi-parameter function', function test( t ) { var f; function a( x, y, next ) { setTimeout( onTimeout, 0 ); function onTimeout() { next( null, x*y ); } } function b( z, next ) { setTimeout( onTimeout, 0 ); function onTimeout() { next( null, z+5 ); } } function c( r, next ) { setTimeout( onTimeout, 0 ); function onTimeout() { next( null, r/10 ); } } f = funseqAsync( a, b, c ); f( 5, 3, done ); function done( error, v ) { if ( error ) { t.fail( error.message ); } else { t.strictEqual( v, 2, 'returns pipeline result' ); } t.end(); } }); tape( 'if an error is encountered while invoking a provided function, the pipeline function suspends execution and immediately returns the error', function test( t ) { var count; var f; function a( x, next ) { setTimeout( onTimeout, 0 ); function onTimeout() { count += 1; next( new Error( 'beep' ) ); } } function b( z, next ) { setTimeout( onTimeout, 0 ); function onTimeout() { count += 1; next( null, z+5 ); } } function c( r, next ) { setTimeout( onTimeout, 0 ); function onTimeout() { count += 1; next( null, r/10 ); } } f = funseqAsync( a, b, c ); count = 0; f( 5, done ); function done( error ) { if ( error ) { t.pass( error.message ); } else { t.fail( 'should return an error' ); } t.strictEqual( count, 1, 'invokes expected number of functions' ); t.end(); } }); tape( 'if an error is encountered while invoking a provided function, the pipeline function suspends execution and immediately returns the error', function test( t ) { var count; var f; function a( x, next ) { setTimeout( onTimeout, 0 ); function onTimeout() { count += 1; next( null, x*3 ); } } function b( z, next ) { setTimeout( onTimeout, 0 ); function onTimeout() { count += 1; next( new Error( 'beep' ) ); } } function c( r, next ) { setTimeout( onTimeout, 0 ); function onTimeout() { count += 1; next( null, r/10 ); } } f = funseqAsync( a, b, c ); count = 0; f( 5, done ); function done( error ) { if ( error ) { t.pass( error.message ); } else { t.fail( 'should return an error' ); } t.strictEqual( count, 2, 'invokes expected number of functions' ); t.end(); } }); tape( 'if an error is encountered while invoking a provided function, the pipeline function suspends execution and immediately returns the error', function test( t ) { var count; var f; function a( x, next ) { setTimeout( onTimeout, 0 ); function onTimeout() { count += 1; next( null, x*3 ); } } function b( z, next ) { setTimeout( onTimeout, 0 ); function onTimeout() { count += 1; next( null, z+5 ); } } function c( r, next ) { setTimeout( onTimeout, 0 ); function onTimeout() { count += 1; next( new Error( 'beep' ) ); } } f = funseqAsync( a, b, c ); count = 0; f( 5, done ); function done( error ) { if ( error ) { t.pass( error.message ); } else { t.fail( 'should return an error' ); } t.strictEqual( count, 3, 'invokes expected number of functions' ); t.end(); } });