forked from Hacker0x01/react-datepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonth_dropdown_test.js
218 lines (197 loc) · 6.53 KB
/
month_dropdown_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
import React from "react";
import range from "lodash/range";
import MonthDropdown from "../src/month_dropdown.jsx";
import MonthDropdownOptions from "../src/month_dropdown_options.jsx";
import { mount } from "enzyme";
import { newDate, getMonthInLocale, registerLocale } from "../src/date_utils";
import zh_cn from "date-fns/locale/zh-CN";
import el from "date-fns/locale/el";
import ru from "date-fns/locale/ru";
describe("MonthDropdown", () => {
let monthDropdown;
let handleChangeResult;
const mockHandleChange = function(changeInput) {
handleChangeResult = changeInput;
};
let sandbox;
function getMonthDropdown(overrideProps) {
return mount(
<MonthDropdown
dropdownMode="scroll"
month={11}
onChange={mockHandleChange}
{...overrideProps}
/>
);
}
beforeEach(() => {
handleChangeResult = null;
sandbox = sinon.createSandbox();
});
afterEach(() => {
sandbox.restore();
});
describe("scroll mode", () => {
beforeEach(function() {
monthDropdown = getMonthDropdown();
});
it("shows the selected month in the initial view", () => {
expect(monthDropdown.text()).to.contain("December");
});
it("opens a list when read view is clicked", () => {
monthDropdown
.find(".react-datepicker__month-read-view")
.simulate("click");
var optionsView = monthDropdown.find(MonthDropdownOptions);
expect(optionsView).to.exist;
});
it("applies the 'selected' modifier class to the selected month", () => {
monthDropdown
.find(".react-datepicker__month-read-view")
.simulate("click");
var selectedMonth = monthDropdown.find(
".react-datepicker__month-option--selected_month"
);
expect(selectedMonth.text()).to.contain("December");
});
it("closes the dropdown when a month is clicked", () => {
monthDropdown
.find(".react-datepicker__month-read-view")
.simulate("click");
monthDropdown
.find(".react-datepicker__month-option")
.at(1)
.simulate("click");
expect(monthDropdown.find(MonthDropdownOptions)).to.have.length(0);
});
it("closes the dropdown if outside is clicked", () => {
const monthNames = range(0, 12).map(M => getMonthInLocale(M));
const onCancelSpy = sandbox.spy();
const monthDropdownOptionsInstance = mount(
<MonthDropdownOptions
onCancel={onCancelSpy}
onChange={sandbox.spy()}
month={11}
monthNames={monthNames}
/>
).instance();
monthDropdownOptionsInstance.handleClickOutside();
expect(onCancelSpy.calledOnce).to.be.true;
});
it("does not call the supplied onChange function when the same month is clicked", () => {
monthDropdown
.find(".react-datepicker__month-read-view")
.simulate("click");
monthDropdown
.find(".react-datepicker__month-option")
.at(11)
.simulate("click");
expect(handleChangeResult).to.be.null;
});
it("calls the supplied onChange function when a different month is clicked", () => {
monthDropdown
.find(".react-datepicker__month-read-view")
.simulate("click");
monthDropdown
.find(".react-datepicker__month-option")
.at(2)
.simulate("click");
expect(handleChangeResult).to.eq(2);
});
it("should use locale stand-alone formatting to display month names", () => {
registerLocale("el", el);
registerLocale("ru", ru);
let dropdownDateFormat = getMonthDropdown();
expect(dropdownDateFormat.text()).to.contain("December");
dropdownDateFormat = getMonthDropdown({ locale: "el" });
expect(dropdownDateFormat.text()).to.contain("Δεκέμβριος");
dropdownDateFormat = getMonthDropdown({ locale: "ru" });
expect(dropdownDateFormat.text()).to.contain("декабрь");
});
});
describe("select mode", () => {
it("renders a select", () => {
monthDropdown = getMonthDropdown({ dropdownMode: "select" });
var select = monthDropdown.find(".react-datepicker__month-select");
expect(select).to.have.length(1);
expect(select.prop("value")).to.eq(11);
var options = select.find("option");
expect(options.map(o => o.prop("value"))).to.eql(range(0, 12));
});
it("renders month options with default locale", () => {
monthDropdown = getMonthDropdown({ dropdownMode: "select" });
var options = monthDropdown.find("option");
expect(options.map(o => o.text())).to.eql([
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
]);
});
// Short Month Names
it("renders month options with short name and default locale", () => {
monthDropdown = getMonthDropdown({
dropdownMode: "select",
useShortMonthInDropdown: true
});
var options = monthDropdown.find("option");
expect(options.map(o => o.text())).to.eql([
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
]);
});
// Failing on Travis CI.
it("renders month options with specified locale", () => {
registerLocale("zh-cn", zh_cn);
monthDropdown = getMonthDropdown({
dropdownMode: "select",
locale: "zh-cn"
});
var options = monthDropdown.find("option");
expect(options.map(o => o.text())).to.eql([
"一月",
"二月",
"三月",
"四月",
"五月",
"六月",
"七月",
"八月",
"九月",
"十月",
"十一月",
"十二月"
]);
});
it("does not call the supplied onChange function when the same month is clicked", () => {
monthDropdown = getMonthDropdown({ dropdownMode: "select", month: 11 });
var select = monthDropdown.find(".react-datepicker__month-select");
select.simulate("change", { target: { value: 11 } });
expect(handleChangeResult).to.not.exist;
});
it("calls the supplied onChange function when a different month is clicked", () => {
monthDropdown = getMonthDropdown({ dropdownMode: "select", month: 11 });
var select = monthDropdown.find(".react-datepicker__month-select");
select.simulate("change", { target: { value: 9 } });
expect(handleChangeResult).to.equal(9);
});
});
});