forked from Hacker0x01/react-datepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate_utils_test.js
198 lines (167 loc) · 6.43 KB
/
date_utils_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import {
isSameDay,
isDayDisabled,
allDaysDisabledBefore,
allDaysDisabledAfter,
getEffectiveMinDate,
getEffectiveMaxDate
} from '../src/date_utils'
import moment from 'moment'
describe('date_utils', function () {
describe('isSameDay', function () {
it('should return true for null dates', function () {
expect(isSameDay(null, null)).to.be.true
})
it('should return false for a null and non-null date', function () {
expect(isSameDay(moment(), null)).to.be.false
expect(isSameDay(null, moment())).to.be.false
})
it('should return true for non-equal dates', function () {
expect(isSameDay(moment('2016-02-10'), moment('2016-02-11'))).to.be.false
})
it('should return true for equal dates', function () {
expect(isSameDay(moment('2016-02-10'), moment('2016-02-10'))).to.be.true
})
})
describe('isDayDisabled', function () {
it('should be enabled by default', () => {
const day = moment()
expect(isDayDisabled(day)).to.be.false
})
it('should be enabled if on the min date', () => {
const day = moment()
expect(isDayDisabled(day, { minDate: day })).to.be.false
})
it('should be disabled if before the min date', () => {
const day = moment()
const minDate = day.clone().add(1, 'day')
expect(isDayDisabled(day, { minDate })).to.be.true
})
it('should be enabled if on the max date', () => {
const day = moment()
expect(isDayDisabled(day, { maxDate: day })).to.be.false
})
it('should be disabled if after the max date', () => {
const day = moment()
const maxDate = day.clone().subtract(1, 'day')
expect(isDayDisabled(day, { maxDate })).to.be.true
})
it('should be disabled if in excluded dates', () => {
const day = moment()
expect(isDayDisabled(day, { excludeDates: [day] })).to.be.true
})
it('should be enabled if in included dates', () => {
const day = moment()
expect(isDayDisabled(day, { includeDates: [day] })).to.be.false
})
it('should be disabled if not in included dates', () => {
const day = moment()
const includeDates = [day.clone().add(1, 'day')]
expect(isDayDisabled(day, { includeDates })).to.be.true
})
it('should be enabled if date filter returns true', () => {
const day = moment()
const filterDate = d => d.isSame(day)
expect(isDayDisabled(day, { filterDate })).to.be.false
})
it('should be disabled if date filter returns false', () => {
const day = moment()
const filterDate = d => !d.isSame(day)
expect(isDayDisabled(day, { filterDate })).to.be.true
})
it('should not allow date filter to modify input date', () => {
const day = moment()
const dayClone = day.clone()
const filterDate = d => {
d.add(1, 'day')
return true
}
isDayDisabled(day, { filterDate })
expect(day.isSame(dayClone)).to.be.true
})
})
describe('allDaysDisabledBefore', () => {
it('should return false by default', () => {
expect(allDaysDisabledBefore(moment(), 'month')).to.be.false
})
it('should return true if min date is in the same unit', () => {
const day = moment('2016-03-19')
const minDate = moment('2016-03-01')
expect(allDaysDisabledBefore(day, 'month', { minDate })).to.be.true
})
it('should return false if min date is in the previous unit', () => {
const day = moment('2016-03-19')
const minDate = moment('2016-02-29')
expect(allDaysDisabledBefore(day, 'month', { minDate })).to.be.false
})
it('should return true if previous unit is before include dates', () => {
const day = moment('2016-03-19')
const includeDates = [moment('2016-03-01')]
expect(allDaysDisabledBefore(day, 'month', { includeDates })).to.be.true
})
})
describe('allDaysDisabledAfter', () => {
it('should return false by default', () => {
expect(allDaysDisabledAfter(moment(), 'month')).to.be.false
})
it('should return true if max date is in the same unit', () => {
const day = moment('2016-03-19')
const maxDate = moment('2016-03-31')
expect(allDaysDisabledAfter(day, 'month', { maxDate })).to.be.true
})
it('should return false if max date is in the next unit', () => {
const day = moment('2016-03-19')
const maxDate = moment('2016-04-01')
expect(allDaysDisabledAfter(day, 'month', { maxDate })).to.be.false
})
it('should return true if next unit is after include dates', () => {
const day = moment('2016-03-19')
const includeDates = [moment('2016-03-01')]
expect(allDaysDisabledAfter(day, 'month', { includeDates })).to.be.true
})
})
describe('getEffectiveMinDate', () => {
it('should return null by default', () => {
expect(getEffectiveMinDate({})).to.not.exist
})
it('should return the min date', () => {
const minDate = moment('2016-03-30')
assert(getEffectiveMinDate({ minDate }).isSame(minDate))
})
it('should return the minimum include date', () => {
const date1 = moment('2016-03-30')
const date2 = moment('2016-04-01')
const includeDates = [date1, date2]
assert(getEffectiveMinDate({ includeDates }).isSame(date1))
})
it('should return the minimum include date satisfying the min date', () => {
const minDate = moment('2016-03-31')
const date1 = moment('2016-03-30')
const date2 = moment('2016-04-01')
const includeDates = [date1, date2]
assert(getEffectiveMinDate({ minDate, includeDates }).isSame(date2))
})
})
describe('getEffectiveMaxDate', () => {
it('should return null by default', () => {
expect(getEffectiveMaxDate({})).to.not.exist
})
it('should return the max date', () => {
const maxDate = moment('2016-03-30')
assert(getEffectiveMaxDate({ maxDate }).isSame(maxDate))
})
it('should return the maximum include date', () => {
const date1 = moment('2016-03-30')
const date2 = moment('2016-04-01')
const includeDates = [date1, date2]
assert(getEffectiveMaxDate({ includeDates }).isSame(date2))
})
it('should return the maximum include date satisfying the max date', () => {
const maxDate = moment('2016-03-31')
const date1 = moment('2016-03-30')
const date2 = moment('2016-04-01')
const includeDates = [date1, date2]
assert(getEffectiveMaxDate({ maxDate, includeDates }).isSame(date1))
})
})
})