-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrangeDate.test.js
79 lines (63 loc) · 4.95 KB
/
rangeDate.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
'use strict';
const expect = require('chai').expect;
const moment = require('moment');
const validators = require('../src/common-validators');
describe('rangeDate', function() {
it('empty is valid', function() {
expect(validators.rangeDate(undefined, new Date())).to.be.undefined;
});
it('not date is invalid', function() {
expect(validators.rangeDate('abc', {from: new Date(0), to: new Date()})).to.have.property('error');
expect(validators.rangeDate({a: 1}, {from: new Date(0), to: new Date()})).to.have.property('error');
expect(validators.rangeDate([1,2,3], {from: new Date(0), to: new Date()})).to.have.property('error');
});
it('date in range is valid', function() {
expect(validators.rangeDate(new Date(2000, 0, 4, 23, 59, 59, 0), {from: new Date(2000, 0, 3), to: new Date(2000, 0, 5)})).to.be.undefined;
expect(validators.rangeDate(new Date(2000, 0, 4, 23, 59, 59, 0), {from: new Date(2000, 0, 4), to: new Date(2000, 0, 5)})).to.be.undefined;
expect(validators.rangeDate(new Date(2000, 0, 4, 23, 59, 59, 0), {from: new Date(2000, 0, 3), to: new Date(2000, 0, 4)})).to.be.undefined;
});
it('date less then range is invalid', function() {
expect(validators.rangeDate(new Date(2000, 0, 2, 23, 59, 59, 0), {from: new Date(2000, 0, 3), to: new Date(2000, 0, 5)})).to.have.property('error');
});
it('date more then range is invalid', function() {
expect(validators.rangeDate(new Date(2000, 0, 6, 23, 59, 59, 0), {from: new Date(2000, 0, 3), to: new Date(2000, 0, 5)})).to.have.property('error');
});
it('date equal left end of the range is valid', function() {
expect(validators.rangeDate(new Date(2000, 0, 3, 23, 59, 59, 0), {from: new Date(2000, 0, 3), to: new Date(2000, 0, 5)})).to.be.undefined;
});
it('date equal left end of the range is invalid if exclusiveFrom=true', function() {
expect(validators.rangeDate(new Date(2000, 0, 3, 23, 59, 59, 0), {from: new Date(2000, 0, 3), to: new Date(2000, 0, 5), exclusiveFrom: true})).to.have.property('error');
});
it('date equal right end of the range is valid', function() {
expect(validators.rangeDate(new Date(2000, 0, 5, 23, 59, 59, 0), {from: new Date(2000, 0, 3), to: new Date(2000, 0, 5)})).to.be.undefined;
});
it('date equal right end of the range is invalid if exclusiveTo=true', function() {
expect(validators.rangeDate(new Date(2000, 0, 5, 23, 59, 59, 0), {from: new Date(2000, 0, 3), to: new Date(2000, 0, 5), exclusiveTo: true})).to.have.property('error');
});
it('date equal left end of the range is invalid if exclusive=true', function() {
expect(validators.rangeDate(new Date(2000, 0, 3,23, 59, 59, 0), {from: new Date(2000, 0, 3), to: new Date(2000, 0, 5), exclusive: true})).to.have.property('error');
});
it('date equal right end of the range is invalid if exclusive=true', function() {
expect(validators.rangeDate(new Date(2000, 0, 5, 23, 59, 59, 0), {from: new Date(2000, 0, 3), to: new Date(2000, 0, 5), exclusive: true})).to.have.property('error');
});
it('date in GMT format is correct', function() {
expect(validators.rangeDate('Sat Jan 04 2000 23:59:59 GMT+0300 (MSK)', {from: 'Sat Jan 03 2000', to: 'Sat Jan 05 2000'})).to.be.undefined;
expect(validators.rangeDate('Sat Jan 02 2000 23:59:59 GMT+0300 (MSK)', {from: 'Sat Jan 03 2000', to: 'Sat Jan 05 2000'})).to.have.property('error');
expect(validators.rangeDate('Sat Jan 06 2000 23:59:59 GMT+0300 (MSK)', {from: 'Sat Jan 03 2000', to: 'Sat Jan 05 2000'})).to.have.property('error');
});
it('date in ISO format is correct', function() {
expect(validators.rangeDate('2000-01-04T23:59:59.000Z', {from: '2000-01-03Z', to: '2000-01-05Z'})).to.be.undefined;
expect(validators.rangeDate('2000-01-02T23:59:59.000Z', {from: '2000-01-03Z', to: '2000-01-05Z'})).to.have.property('error');
expect(validators.rangeDate('2000-01-06T23:59:59.000Z', {from: '2000-01-03Z', to: '2000-01-05Z'})).to.have.property('error');
});
it('date in time format is correct', function() {
expect(validators.rangeDate(947030399000, {from: 946857600000, to: 947030400000})).to.be.undefined;
expect(validators.rangeDate(946857599000, {from: 946857600000, to: 947030400000})).to.have.property('error');
expect(validators.rangeDate(947203199000, {from: 946857600000, to: 947030400000})).to.have.property('error');
});
it('date in moment.js format is correct', function() {
expect(validators.rangeDate(moment('2000-01-04T23:59:59.000Z'), {from: moment('2000-01-03Z'), to: moment('2000-01-05Z')})).to.be.undefined;
expect(validators.rangeDate(moment('2000-01-02T23:59:59.000Z'), {from: moment('2000-01-03Z'), to: moment('2000-01-05Z')})).to.have.property('error');
expect(validators.rangeDate(moment('2000-01-06T23:59:59.000Z'), {from: moment('2000-01-03Z'), to: moment('2000-01-05Z')})).to.have.property('error');
});
});