33// MODULES //
44
55var tape = require ( 'tape' ) ;
6+ var proxyquire = require ( 'proxyquire' ) ;
7+ var stdlib = require ( './../lib/stdlib.js' ) ;
8+ var noop = require ( stdlib + '@stdlib/utils/noop' ) ;
9+ var isStringArray = require ( stdlib + '@stdlib/utils/is-string' ) . isPrimitiveStringArray ;
610var ls = require ( './../lib/async.js' ) ;
711
812
@@ -14,4 +18,113 @@ tape( 'main export is a function', function test( t ) {
1418 t . end ( ) ;
1519} ) ;
1620
21+ tape ( 'the function throws an error if provided an invalid option' , function test ( t ) {
22+ t . throws ( foo , TypeError , 'throws error' ) ;
23+ t . end ( ) ;
24+ function foo ( ) {
25+ var opts = {
26+ 'dir' : null
27+ } ;
28+ ls ( opts , noop ) ;
29+ }
30+ } ) ;
31+
32+ tape ( 'if provided a callback argument which is not a function, the function throws an error (no options)' , function test ( t ) {
33+ var values ;
34+ var i ;
35+
36+ values = [
37+ '5' ,
38+ 5 ,
39+ NaN ,
40+ true ,
41+ null ,
42+ undefined ,
43+ [ ] ,
44+ { }
45+ ] ;
46+
47+ for ( i = 0 ; i < values . length ; i ++ ) {
48+ t . throws ( badValue ( values [ i ] ) , TypeError , 'throws a type error when provided ' + values [ i ] ) ;
49+ }
50+ t . end ( ) ;
51+
52+ function badValue ( value ) {
53+ return function badValue ( ) {
54+ ls ( value ) ;
55+ } ;
56+ }
57+ } ) ;
58+
59+ tape ( 'if provided a callback argument which is not a function, the function throws an error (options)' , function test ( t ) {
60+ var values ;
61+ var i ;
62+
63+ values = [
64+ '5' ,
65+ 5 ,
66+ NaN ,
67+ true ,
68+ null ,
69+ undefined ,
70+ [ ] ,
71+ { }
72+ ] ;
73+
74+ for ( i = 0 ; i < values . length ; i ++ ) {
75+ t . throws ( badValue ( values [ i ] ) , TypeError , 'throws a type error when provided ' + values [ i ] ) ;
76+ }
77+ t . end ( ) ;
78+
79+ function badValue ( value ) {
80+ return function badValue ( ) {
81+ ls ( { } , value ) ;
82+ } ;
83+ }
84+ } ) ;
85+
86+ tape ( 'the function returns an error to a provided callback if an error is encountered while searching a directory' , function test ( t ) {
87+ var ls = proxyquire ( './../lib/async.js' , {
88+ 'glob' : glob
89+ } ) ;
90+
91+ ls ( clbk ) ;
92+
93+ function glob ( ) {
94+ var cb = arguments [ arguments . length - 1 ] ;
95+ setTimeout ( onTimeout , 0 ) ;
96+ function onTimeout ( ) {
97+ cb ( new Error ( 'beep' ) ) ;
98+ }
99+ }
17100
101+ function clbk ( error ) {
102+ t . ok ( error , 'returns an error' ) ;
103+ t . end ( ) ;
104+ }
105+ } ) ;
106+
107+ tape ( 'the function returns a string array' , function test ( t ) {
108+ ls ( clbk ) ;
109+ function clbk ( error , names ) {
110+ if ( error ) {
111+ t . ok ( false , error . message ) ;
112+ }
113+ t . equal ( isStringArray ( names ) , true , 'returns a string array' ) ;
114+ t . end ( ) ;
115+ }
116+ } ) ;
117+
118+ tape ( 'the function returns a string array (dir option)' , function test ( t ) {
119+ var opts = {
120+ 'dir' : './@stdlib/math/base'
121+ } ;
122+ ls ( opts , clbk ) ;
123+ function clbk ( error , names ) {
124+ if ( error ) {
125+ t . ok ( false , error . message ) ;
126+ }
127+ t . equal ( isStringArray ( names ) , true , 'returns a string array' ) ;
128+ t . end ( ) ;
129+ }
130+ } ) ;
0 commit comments