forked from Hacker0x01/react-datepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_test.js
185 lines (155 loc) · 6 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
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("outside month", () => {
const className = "datepicker__day--outside-month";
it("should not apply the outside-month class if in same month", () => {
const day = moment();
const dayDOM = renderDay(day, { month: day.month() });
expect(dayDOM.className).to.not.contain(className);
});
it("should apply the outside-month class if not in same month", () => {
const day = moment();
const dayDOM = renderDay(day, { month: day.month() + 1 });
expect(dayDOM.className).to.contain(className);
});
});
describe("disabled", () => {
const className = "datepicker__day--disabled";
it("should be enabled if date is enabled", () => {
const dayDOM = renderDay(moment());
expect(dayDOM.className).to.not.contain(className);
});
it("should be disabled if date is disabled", () => {
const day = moment();
const dayDOM = renderDay(day, { excludeDates: [day] });
expect(dayDOM.className).to.contain(className);
});
});
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;
});
});
});