-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdate-assert_test.js
More file actions
96 lines (76 loc) · 2.18 KB
/
date-assert_test.js
File metadata and controls
96 lines (76 loc) · 2.18 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
/**
* Module dependencies.
*/
import DateAssert from '../../src/asserts/date-assert';
import should from 'should';
import { Assert as BaseAssert, Violation } from 'validator.js';
/**
* Extend `Assert` with `DateAssert`.
*/
const Assert = BaseAssert.extend({
Date: DateAssert
});
/**
* Test `DateAssert`.
*/
describe('DateAssert', () => {
it('should throw an error if the input value is not a string or a date', () => {
const choices = [[], {}, 123];
choices.forEach(choice => {
try {
new Assert().Date().validate(choice);
should.fail();
} catch (e) {
e.should.be.instanceOf(Violation);
e.violation.value.should.equal('must_be_a_date_or_a_string');
}
});
});
it('should throw an error if an invalid format is given', () => {
const formats = [[], {}, 123];
formats.forEach(format => {
try {
new Assert().Date({ 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().Date({ format: 'YYYY-MM-DD' }).validate('20003112');
should.fail();
} catch (e) {
e.should.be.instanceOf(Violation);
e.show().assert.should.equal('Date');
}
});
it('should throw an error if value does not pass strict validation', () => {
try {
new Assert().Date({ format: 'YYYY-MM-DD' }).validate('2000.12.30');
should.fail();
} catch (e) {
e.should.be.instanceOf(Violation);
e.show().assert.should.equal('Date');
}
});
it('should expose `assert` equal to `Date`', () => {
try {
new Assert().Date().validate('foo');
should.fail();
} catch (e) {
e.show().assert.should.equal('Date');
}
});
it('should accept a date', () => {
new Assert().Date().validate(new Date());
});
it('should accept a correctly formatted date', () => {
new Assert().Date({ format: 'MM/YYYY' }).validate('12/2000');
});
it('should accept a string', () => {
new Assert().Date().validate('2014-10-16');
});
});