-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnull-or-date-assert_test.js
More file actions
100 lines (79 loc) · 2.37 KB
/
null-or-date-assert_test.js
File metadata and controls
100 lines (79 loc) · 2.37 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Module dependencies.
*/
import NullOrDateAssert from '../../src/asserts/null-or-date-assert';
import should from 'should';
import { Assert as BaseAssert, Violation } from 'validator.js';
/**
* Extend `Assert` with `NullOrDateAssert`.
*/
const Assert = BaseAssert.extend({
NullOrDate: NullOrDateAssert
});
/**
* Test `NullOrDateAssert`.
*/
describe('NullOrDateAssert', () => {
it('should throw an error if the input value is not a `null` or a date', () => {
const choices = [[], {}, 123];
choices.forEach(choice => {
try {
new Assert().NullOrDate().validate(choice);
should.fail();
} catch (e) {
e.should.be.instanceOf(Violation);
e.violation.value.should.equal('must_be_null_or_a_date');
}
});
});
it('should throw an error if an invalid format is given', () => {
const formats = [[], {}, 123];
formats.forEach(format => {
try {
new Assert().NullOrDate({ format }).validate();
should.fail();
} catch (e) {
e.should.be.instanceOf(Error);
e.message.should.equal(`Unsupported format ${format} given`);
}
});
});
it('should throw an error if value is not correctly formatted', () => {
try {
new Assert().NullOrDate({ format: 'YYYY-MM-DD' }).validate('20003112');
should.fail();
} catch (e) {
e.should.be.instanceOf(Violation);
e.show().assert.should.equal('NullOrDate');
}
});
it('should throw an error if value does not pass strict validation', () => {
try {
new Assert().NullOrDate({ format: 'YYYY-MM-DD' }).validate('2000.12.30');
should.fail();
} catch (e) {
e.should.be.instanceOf(Violation);
e.show().assert.should.equal('NullOrDate');
}
});
it('should expose `assert` equal to `NullOrDate`', () => {
try {
new Assert().NullOrDate().validate({});
should.fail();
} catch (e) {
e.show().assert.should.equal('NullOrDate');
}
});
it('should accept `null`', () => {
new Assert().NullOrDate().validate(null);
});
it('should accept a date', () => {
new Assert().NullOrDate().validate(new Date());
});
it('should accept a correctly formatted date', () => {
new Assert().NullOrDate({ format: 'MM/YYYY' }).validate('12/2000');
});
it('should accept a string', () => {
new Assert().NullOrDate().validate('2014-10-16');
});
});