|
1 | 1 | /** |
2 | 2 | * @license Apache-2.0 |
3 | 3 | * |
4 | | -* Copyright (c) 2018 The Stdlib Authors. |
| 4 | +* Copyright (c) 2023 The Stdlib Authors. |
5 | 5 | * |
6 | 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | 7 | * you may not use this file except in compliance with the License. |
|
21 | 21 | // MODULES // |
22 | 22 |
|
23 | 23 | var tape = require( 'tape' ); |
24 | | -var noop = require( '@stdlib/utils-noop' ); |
25 | | -var ifelseAsync = require( './../../dist' ); |
| 24 | +var main = require( './../../dist' ); |
26 | 25 |
|
27 | 26 |
|
28 | 27 | // TESTS // |
29 | 28 |
|
30 | | -tape( 'main export is a function', function test( t ) { |
| 29 | +tape( 'main export is defined', function test( t ) { |
31 | 30 | t.ok( true, __filename ); |
32 | | - t.strictEqual( typeof ifelseAsync, 'function', 'main export is a function' ); |
| 31 | + t.strictEqual( main !== void 0, true, 'main export is defined' ); |
33 | 32 | t.end(); |
34 | 33 | }); |
35 | | - |
36 | | -tape( 'the function throws an error if provided a first argument which is not a function', function test( t ) { |
37 | | - var values; |
38 | | - var i; |
39 | | - |
40 | | - values = [ |
41 | | - '5', |
42 | | - 5, |
43 | | - NaN, |
44 | | - true, |
45 | | - false, |
46 | | - null, |
47 | | - void 0, |
48 | | - [], |
49 | | - {} |
50 | | - ]; |
51 | | - |
52 | | - for ( i = 0; i < values.length; i++ ) { |
53 | | - t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); |
54 | | - } |
55 | | - t.end(); |
56 | | - |
57 | | - function badValue( value ) { |
58 | | - return function badValue() { |
59 | | - ifelseAsync( value, 1.0, -1.0, noop ); |
60 | | - }; |
61 | | - } |
62 | | -}); |
63 | | - |
64 | | -tape( 'the function throws an error if provided a last argument which is not a function', function test( t ) { |
65 | | - var values; |
66 | | - var i; |
67 | | - |
68 | | - values = [ |
69 | | - '5', |
70 | | - 5, |
71 | | - NaN, |
72 | | - true, |
73 | | - false, |
74 | | - null, |
75 | | - void 0, |
76 | | - [], |
77 | | - {} |
78 | | - ]; |
79 | | - |
80 | | - for ( i = 0; i < values.length; i++ ) { |
81 | | - t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); |
82 | | - } |
83 | | - t.end(); |
84 | | - |
85 | | - function badValue( value ) { |
86 | | - return function badValue() { |
87 | | - ifelseAsync( noop, 1.0, -1.0, value ); |
88 | | - }; |
89 | | - } |
90 | | -}); |
91 | | - |
92 | | -tape( 'if a predicate function returns a truthy value, the function returns the second argument', function test( t ) { |
93 | | - ifelseAsync( predicate, 'beep', 'boop', done ); |
94 | | - |
95 | | - function predicate( clbk ) { |
96 | | - setTimeout( onTimeout, 0 ); |
97 | | - function onTimeout() { |
98 | | - clbk( null, true ); |
99 | | - } |
100 | | - } |
101 | | - |
102 | | - function done( error, result ) { |
103 | | - if ( error ) { |
104 | | - t.fail( error.message ); |
105 | | - } else { |
106 | | - t.strictEqual( result, 'beep', 'returns expected value' ); |
107 | | - } |
108 | | - t.end(); |
109 | | - } |
110 | | -}); |
111 | | - |
112 | | -tape( 'if a predicate function returns a falsy value, the function returns the third argument', function test( t ) { |
113 | | - ifelseAsync( predicate, 'beep', 'boop', done ); |
114 | | - |
115 | | - function predicate( clbk ) { |
116 | | - setTimeout( onTimeout, 0 ); |
117 | | - function onTimeout() { |
118 | | - clbk( null, false ); |
119 | | - } |
120 | | - } |
121 | | - |
122 | | - function done( error, result ) { |
123 | | - if ( error ) { |
124 | | - t.fail( error.message ); |
125 | | - } else { |
126 | | - t.strictEqual( result, 'boop', 'returns expected value' ); |
127 | | - } |
128 | | - t.end(); |
129 | | - } |
130 | | -}); |
131 | | - |
132 | | -tape( 'if a predicate function returns an error, the function returns the error', function test( t ) { |
133 | | - ifelseAsync( predicate, 'beep', 'boop', done ); |
134 | | - |
135 | | - function predicate( clbk ) { |
136 | | - setTimeout( onTimeout, 0 ); |
137 | | - function onTimeout() { |
138 | | - clbk( new Error( 'beep' ) ); |
139 | | - } |
140 | | - } |
141 | | - |
142 | | - function done( error ) { |
143 | | - if ( error ) { |
144 | | - t.pass( error.message ); |
145 | | - } else { |
146 | | - t.fail( 'should return an error' ); |
147 | | - } |
148 | | - t.end(); |
149 | | - } |
150 | | -}); |
151 | | - |
152 | | -tape( 'the function does not guarantee asynchronous execution', function test( t ) { |
153 | | - var i = 0; |
154 | | - ifelseAsync( predicate, 'beep', 'boop', done ); |
155 | | - i = 1; |
156 | | - |
157 | | - function predicate( clbk ) { |
158 | | - clbk( null, true ); |
159 | | - } |
160 | | - |
161 | | - function done( error, result ) { |
162 | | - if ( error ) { |
163 | | - t.fail( error.message ); |
164 | | - } else { |
165 | | - t.strictEqual( result, 'beep', 'returns expected value' ); |
166 | | - } |
167 | | - t.strictEqual( i, 0, 'releases the zalgo' ); |
168 | | - t.end(); |
169 | | - } |
170 | | -}); |
0 commit comments