-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
96 lines (76 loc) · 1.93 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict';
// MODULES //
var // Expectation library:
chai = require( 'chai' ),
// Module to be tested:
roundn = require( './../lib' );
// VARIABLES //
var expect = chai.expect,
assert = chai.assert;
// TESTS //
describe( 'compute-roundn', function tests() {
it( 'should export a function', function test() {
expect( roundn ).to.be.a( 'function' );
});
it( 'should throw an error if not provided an array or numeric value', function test(){
var values = [
'5',
true,
undefined,
null,
NaN,
function(){},
{}
];
for ( var i = 0; i < values.length; i++ ) {
expect( badValue( values[i] ) ).to.throw( TypeError );
}
function badValue( value ) {
return function() {
roundn( value, 0 );
};
}
});
it( 'should throw an error if not provided an integer power', function test(){
var values = [
'5',
5.326,
true,
undefined,
null,
NaN,
[],
function(){},
{}
];
for ( var i = 0; i < values.length; i++ ) {
expect( badValue( values[i] ) ).to.throw( TypeError );
}
function badValue( value ) {
return function() {
roundn( Math.PI, value );
};
}
});
it( 'should round a value to a desired number of decimals', function test() {
assert.strictEqual( roundn( Math.PI, -2 ), 3.14 );
});
it( 'should round a value to a desired number of digits', function test() {
assert.strictEqual( roundn( 12368, 3 ), 12000 );
});
it( 'should behave like `Math.round()` if `n` is 0', function test() {
assert.strictEqual( roundn( Math.PI, 0 ), Math.round( Math.PI ) );
});
it( 'should return `null` if provided an empty array', function test() {
assert.isNull( roundn( [], 0 ) );
});
it( 'should round all values in an array', function test() {
var data = new Array( 5 ),
expected = new Array( 5 );
for ( var i = 0; i < data.length; i++ ) {
data[ i ] = Math.PI;
expected[ i ] = 3.14;
}
assert.deepEqual( roundn( data, -2 ), expected );
});
});