forked from Hacker0x01/react-datepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_test.js
233 lines (197 loc) · 7.65 KB
/
day_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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import React from "react";
import ReactDOM from "react-dom";
import TestUtils from "react-addons-test-utils";
import moment from "moment";
import Day from "../src/day";
function renderDay(day, props = {}) {
const dayNode = TestUtils.renderIntoDocument(
<Day day={day} {...props} />
);
return ReactDOM.findDOMNode(dayNode);
}
describe("Day", () => {
describe("rendering", () => {
it("should render the specified day", () => {
const dayDOM = renderDay(moment());
expect(dayDOM.className).to.contain("datepicker__day");
expect(dayDOM.textContent).to.equal(moment().date() + "");
});
});
describe("selected", () => {
const className = "datepicker__day--selected";
it("should apply the selected class if selected", () => {
const day = moment();
const dayDOM = renderDay(day, { selected: day });
expect(dayDOM.className).to.contain(className);
});
it("should not apply the selected class if not selected", () => {
const day = moment();
const selected = day.clone().add(1, "day");
const dayDOM = renderDay(day, { selected });
expect(dayDOM.className).to.not.contain(className);
});
});
describe("in range", () => {
const className = "datepicker__day--in-range";
it("should apply the in-range class if in range", () => {
const day = moment();
const startDate = day.clone().subtract(1, "day");
const endDate = day.clone().add(1, "day");
const dayDOM = renderDay(day, { startDate, endDate });
expect(dayDOM.className).to.contain(className);
});
it("should not apply the in-range class if not in range", () => {
const day = moment();
const startDate = day.clone().add(1, "day");
const endDate = day.clone().add(2, "days");
const dayDOM = renderDay(day, { startDate, endDate });
expect(dayDOM.className).to.not.contain(className);
});
it("should apply the in-range class if equal to start date", () => {
const day = moment();
const startDate = day.clone();
const endDate = day.clone().add(1, "day");
const dayDOM = renderDay(day, { startDate, endDate });
expect(dayDOM.className).to.contain(className);
});
it("should apply the in-range class if equal to end date", () => {
const day = moment();
const startDate = day.clone().subtract(1, "day");
const endDate = day.clone();
const dayDOM = renderDay(day, { startDate, endDate });
expect(dayDOM.className).to.contain(className);
});
it("should not apply the in-range class if start date missing", () => {
const day = moment();
const startDate = day.clone().subtract(1, "day");
const dayDOM = renderDay(day, { startDate });
expect(dayDOM.className).to.not.contain(className);
});
it("should not apply the in-range class if end date missing", () => {
const day = moment();
const endDate = day.clone().add(1, "day");
const dayDOM = renderDay(day, { endDate });
expect(dayDOM.className).to.not.contain(className);
});
});
describe("today", () => {
const className = "datepicker__day--today";
it("should apply the today class if today", () => {
const dayDOM = renderDay(moment());
expect(dayDOM.className).to.contain(className);
});
it("should not apply the today class if not today", () => {
const dayDOM = renderDay(moment().add(1, "day"));
expect(dayDOM.className).to.not.contain(className);
});
});
describe("weekend", () => {
const className = "datepicker__day--weekend";
it("should apply the weekend class if Saturday", () => {
const dayDOM = renderDay(moment("2015-12-19"));
expect(dayDOM.className).to.contain(className);
});
it("should apply the weekend class if Sunday", () => {
const dayDOM = renderDay(moment("2015-12-20"));
expect(dayDOM.className).to.contain(className);
});
it("should not apply the today class if not the weekend", () => {
const dayDOM = renderDay(moment("2015-12-21"));
expect(dayDOM.className).to.not.contain(className);
});
});
describe("disabled", () => {
const className = "datepicker__day--disabled";
it("should be enabled by default", () => {
const dayDOM = renderDay(moment());
expect(dayDOM.className).to.not.contain(className);
});
it("should be enabled if on the min date", () => {
const day = moment();
const dayDOM = renderDay(day, { minDate: day });
expect(dayDOM.className).to.not.contain(className);
});
it("should be disabled if before the min date", () => {
const day = moment();
const minDate = day.clone().add(1, "day");
const dayDOM = renderDay(day, { minDate });
expect(dayDOM.className).to.contain(className);
});
it("should be enabled if on the max date", () => {
const day = moment();
const dayDOM = renderDay(day, { maxDate: day });
expect(dayDOM.className).to.not.contain(className);
});
it("should be disabled if after the max date", () => {
const day = moment();
const maxDate = day.clone().subtract(1, "day");
const dayDOM = renderDay(day, { maxDate });
expect(dayDOM.className).to.contain(className);
});
it("should be disabled if in excluded dates", () => {
const day = moment();
const dayDOM = renderDay(day, { excludeDates: [day] });
expect(dayDOM.className).to.contain(className);
});
it("should be enabled if in included dates", () => {
const day = moment();
const dayDOM = renderDay(day, { includeDates: [day] });
expect(dayDOM.className).to.not.contain(className);
});
it("should be disabled if not in included dates", () => {
const day = moment();
const includeDates = [day.clone().add(1, "day")];
const dayDOM = renderDay(day, { includeDates });
expect(dayDOM.className).to.contain(className);
});
it("should be enabled if date filter returns true", () => {
const day = moment();
const filterDate = d => d.isSame(day);
const dayDOM = renderDay(day, { filterDate });
expect(dayDOM.className).to.not.contain(className);
});
it("should be disabled if date filter returns false", () => {
const day = moment();
const filterDate = d => !d.isSame(day);
const dayDOM = renderDay(day, { filterDate });
expect(dayDOM.className).to.contain(className);
});
it("should not allow date filter to modify input date", () => {
const day = moment();
const dayClone = day.clone();
const filterDate = d => {
d.add(1, "day"); // jscs:disable momentImmutability
return true;
};
const dayDOM = renderDay(day, { filterDate });
expect(day.isSame(dayClone)).to.be.true;
});
});
describe("click", () => {
var onClickCalled;
function onClick() {
onClickCalled = true;
}
beforeEach(() => {
onClickCalled = false;
});
it("should call onClick if day is enabled", () => {
const day = moment();
const dayNode = TestUtils.renderIntoDocument(
<Day day={day} onClick={onClick} />
);
const dayDOM = TestUtils.findRenderedDOMComponentWithClass(dayNode, "datepicker__day");
TestUtils.Simulate.click(dayDOM);
expect(onClickCalled).to.be.true;
});
it("should not call onClick if day is disabled", () => {
const day = moment();
const dayNode = TestUtils.renderIntoDocument(
<Day day={day} excludeDates={[day]} onClick={onClick} />
);
const dayDOM = TestUtils.findRenderedDOMComponentWithClass(dayNode, "datepicker__day");
TestUtils.Simulate.click(dayDOM);
expect(onClickCalled).to.be.false;
});
});
});