-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinteger-assert_test.js
More file actions
74 lines (58 loc) · 1.67 KB
/
integer-assert_test.js
File metadata and controls
74 lines (58 loc) · 1.67 KB
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
/**
* Module dependencies.
*/
import IntegerAssert from '../../src/asserts/integer-assert';
import should from 'should';
import { Assert as BaseAssert, Violation } from 'validator.js';
/**
* Extend `Assert` with `IntegerAssert`.
*/
const Assert = BaseAssert.extend({
Integer: IntegerAssert
});
/**
* Test `IntegerAssert`.
*/
describe('IntegerAssert', () => {
it('should throw an error if the input value is not a number', () => {
const choices = [{}, 'foo', '', [], 1.01, '2'];
choices.forEach(choice => {
try {
new Assert().Integer().validate(choice);
should.fail();
} catch (e) {
e.should.be.instanceOf(Violation);
}
});
});
it('should expose `assert` equal to `Integer`', () => {
try {
new Assert().Integer().validate('foo');
should.fail();
} catch (e) {
e.show().assert.should.equal('Integer');
}
});
it('should throw an error if strings are allowed but input is not valid', () => {
try {
new Assert().Integer({ allowString: true }).validate(' 1');
should.fail();
} catch (e) {
e.show().assert.should.equal('Integer');
}
});
it('should throw an error if only unsigned integers are allowed but input is not valid', () => {
try {
new Assert().Integer({ unsigned: true }).validate(-1);
should.fail();
} catch (e) {
e.show().assert.should.equal('Integer');
}
});
it('should accept an unsigned integer as a string if strings are allowed', () => {
new Assert().Integer({ allowString: true, unsigned: true }).validate('1');
});
it('should accept an integer', () => {
new Assert().Integer().validate(+1);
});
});