Skip to content

Commit 4f67a93

Browse files
committed
Added tests.
1 parent e9487e1 commit 4f67a93

File tree

4 files changed

+115
-5
lines changed

4 files changed

+115
-5
lines changed

components/date_picker/DatePicker.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,8 @@ const DatePickerDialog = datePickerDialogFactory(InjectDialog, Calendar);
115115
const DatePicker = factory(InjectInput, DatePickerDialog);
116116

117117
export default themr(DATE_PICKER)(DatePicker);
118-
export { factory as datePickerFactory };
118+
export {
119+
DatePickerDialog as DatePickerDialog,
120+
factory as datePickerFactory
121+
};
119122
export { DatePicker };

components/date_picker/DatePickerDialog.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ const factory = (Dialog, Calendar) => {
4747
this.updateStateDate(nextProps.value);
4848
}
4949

50-
handleCalendarChange = (value, dayClick) => {
50+
handleNewDate = (value, dayClick) => {
5151
const state = {display: 'months', date: value};
5252
if (time.dateOutOfRange(value, this.props.minDate, this.props.maxDate)) {
53-
state.date = this.props.maxDate || this.props.minDate;
53+
if (this.props.maxDate && this.props.minDate) {
54+
state.date = time.closestDate(value, this.props.maxDate, this.props.minDate);
55+
} else {
56+
state.date = this.props.maxDate || this.props.minDate;
57+
}
5458
}
5559
this.setState(state);
5660
if (dayClick && this.props.autoOk && this.props.onSelect) {
@@ -68,7 +72,7 @@ const factory = (Dialog, Calendar) => {
6872

6973
updateStateDate = (date) => {
7074
if (Object.prototype.toString.call(date) === '[object Date]') {
71-
this.handleCalendarChange(date, false);
75+
this.handleNewDate(date, false);
7276
}
7377
};
7478

@@ -106,7 +110,7 @@ const factory = (Dialog, Calendar) => {
106110
display={this.state.display}
107111
maxDate={this.props.maxDate}
108112
minDate={this.props.minDate}
109-
onChange={this.handleCalendarChange}
113+
onChange={this.handleNewDate}
110114
selectedDate={this.state.date}
111115
theme={this.props.theme} />
112116
</div>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import expect from 'expect';
2+
import React from 'react';
3+
import theme from '../theme.scss';
4+
import { DatePickerDialog } from '../DatePicker';
5+
import utils from '../../utils/testing';
6+
7+
describe('DatePickerDialog', function () {
8+
describe('#on mount', function () {
9+
it('passes value through to calendar if no maxDate/minDate specified', function () {
10+
const value = new Date(2016, 1, 1);
11+
12+
const wrapper = utils.shallowRenderComponent(DatePickerDialog, {theme, value});
13+
14+
expect(getDatePassedToCalendar(wrapper)).toBe(value);
15+
});
16+
17+
describe('when minDate but not maxDate specified', function () {
18+
const minDate = new Date(2016, 1, 2);
19+
20+
it('passes through a value after minDate', function () {
21+
const value = new Date(2016, 1, 3);
22+
23+
const wrapper = utils.shallowRenderComponent(DatePickerDialog, {theme, value, minDate});
24+
25+
expect(getDatePassedToCalendar(wrapper)).toBe(value);
26+
});
27+
28+
it('sanitises a value before minDate to minDate', function () {
29+
const wrapper = utils.shallowRenderComponent(DatePickerDialog, {
30+
theme, value: new Date(2016, 1, 1), minDate
31+
});
32+
33+
expect(getDatePassedToCalendar(wrapper)).toBe(minDate);
34+
});
35+
});
36+
37+
describe('when maxDate but not minDate specified', function () {
38+
const maxDate = new Date(2016, 1, 2);
39+
40+
it('passes through a value before maxDate', function () {
41+
const value = new Date(2016, 1, 1);
42+
43+
const wrapper = utils.shallowRenderComponent(DatePickerDialog, {theme, value, maxDate});
44+
45+
expect(getDatePassedToCalendar(wrapper)).toBe(value);
46+
});
47+
48+
it('sanitises a value after maxDate to maxDate', function () {
49+
const wrapper = utils.shallowRenderComponent(DatePickerDialog, {
50+
theme, value: new Date(2016, 1, 3), maxDate
51+
});
52+
53+
expect(getDatePassedToCalendar(wrapper)).toBe(maxDate);
54+
});
55+
});
56+
57+
describe('if both minDate and maxDate are set', function () {
58+
const minDate = new Date(2016, 1, 2);
59+
const maxDate = new Date(2016, 1, 4);
60+
61+
it('sanitises value to minDate if value is before minDate', function () {
62+
const wrapper = utils.shallowRenderComponent(DatePickerDialog, {
63+
theme,
64+
value: new Date(2016, 1, 1),
65+
minDate,
66+
maxDate
67+
});
68+
69+
expect(getDatePassedToCalendar(wrapper)).toBe(minDate);
70+
});
71+
72+
it('sanitises value to maxDate if value is after maxDate', function () {
73+
const wrapper = utils.shallowRenderComponent(DatePickerDialog, {
74+
theme,
75+
value: new Date(2016, 1, 5),
76+
minDate,
77+
maxDate
78+
});
79+
80+
expect(getDatePassedToCalendar(wrapper)).toBe(maxDate);
81+
});
82+
83+
it('doesn\'t sanitise when value is between maxDate/minDate', function () {
84+
const value = new Date(2016, 1, 3);
85+
const wrapper = utils.shallowRenderComponent(DatePickerDialog, {theme, value, minDate, maxDate});
86+
expect(getDatePassedToCalendar(wrapper)).toBe(value);
87+
});
88+
});
89+
90+
function getDatePassedToCalendar(wrapper) {
91+
return wrapper.props.children[1].props.children.props.selectedDate;
92+
}
93+
});
94+
});

components/utils/time.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,15 @@ const time = {
177177
return ((minDate && !(date >= minDate)) || (maxDate && !(date <= maxDate)));
178178
},
179179

180+
closestDate (to, date1, date2) {
181+
const toTime = to.getTime();
182+
183+
const diff1 = Math.abs(toTime - date1.getTime());
184+
const diff2 = Math.abs(toTime - date2.getTime());
185+
186+
return diff1 < diff2 ? date1 : date2;
187+
},
188+
180189
formatDate (date) {
181190
return `${date.getDate()} ${time.getFullMonth(date)} ${date.getFullYear()}`;
182191
}

0 commit comments

Comments
 (0)