forked from Hacker0x01/react-datepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatepicker_test.js
1014 lines (944 loc) · 37.8 KB
/
datepicker_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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import React from "react";
import ReactDOM from "react-dom";
import TestUtils from "react-dom/test-utils";
import { mount } from "enzyme";
import defer from "lodash/defer";
import DatePicker from "../src/index.jsx";
import Day from "../src/day";
import TestWrapper from "./test_wrapper.jsx";
import PopperComponent from "../src/popper_component.jsx";
import TimezoneDatePicker from "./timezone_date_picker.jsx";
import CustomInput from "./helper_components/custom_input.jsx";
import * as utils from "../src/date_utils";
function getKey(key) {
switch (key) {
case "Backspace":
return { key, code: 8, which: 8 };
case "Tab":
return { key, code: 9, which: 9 };
case "Enter":
return { key, code: 13, which: 13 };
case "Escape":
return { key, code: 27, which: 27 };
case "PageUp":
return { key, keyCode: 33, which: 33 };
case "PageDown":
return { key, keyCode: 34, which: 34 };
case "End":
return { key, keyCode: 35, which: 35 };
case "Home":
return { key, keyCode: 36, which: 36 };
case "ArrowLeft":
return { key, code: 37, which: 37 };
case "ArrowUp":
return { key, code: 38, which: 38 };
case "ArrowRight":
return { key, code: 39, which: 39 };
case "ArrowDown":
return { key, code: 40, which: 40 };
}
throw new Error("Unknown key :" + key);
}
describe("DatePicker", () => {
let sandbox;
beforeEach(() => {
sandbox = sinon.sandbox.create();
});
afterEach(() => {
sandbox.restore();
});
it("should show the calendar when focusing on the date input", () => {
var datePicker = TestUtils.renderIntoDocument(<DatePicker />);
var dateInput = datePicker.input;
TestUtils.Simulate.focus(ReactDOM.findDOMNode(dateInput));
expect(datePicker.calendar).to.exist;
});
it("should allow the user to supply a wrapper component for the popper", () => {
var datePicker = mount(<DatePicker popperContainer={TestWrapper} />);
const dateInput = datePicker.instance().input;
TestUtils.Simulate.focus(ReactDOM.findDOMNode(dateInput));
expect(datePicker.find(".test-wrapper").length).to.equal(1);
expect(datePicker.instance().calendar).to.exist;
});
it("should allow the user to pass a wrapper component for the calendar", () => {
var datePicker = mount(<DatePicker calendarContainer={TestWrapper} />);
let dateInput = datePicker.instance().input;
TestUtils.Simulate.focus(ReactDOM.findDOMNode(dateInput));
datePicker.update();
expect(datePicker.find(".test-wrapper").length).to.equal(1);
expect(datePicker.instance().calendar).to.exist;
});
it("should pass a custom class to the popper container", () => {
var datePicker = TestUtils.renderIntoDocument(
<DatePicker popperClassName="some-class-name" />
);
var dateInput = datePicker.input;
TestUtils.Simulate.focus(ReactDOM.findDOMNode(dateInput));
const element = ReactDOM.findDOMNode(datePicker);
const popper = element.querySelector(".react-datepicker-popper");
expect(popper).to.exist;
expect(popper.className).to.contain("some-class-name");
});
it("should show the calendar when clicking on the date input", () => {
var datePicker = TestUtils.renderIntoDocument(<DatePicker />);
var dateInput = datePicker.input;
TestUtils.Simulate.click(ReactDOM.findDOMNode(dateInput));
expect(datePicker.calendar).to.exist;
});
it("should not set open state when it is disabled and gets clicked", function() {
var datePicker = TestUtils.renderIntoDocument(<DatePicker disabled />);
var dateInput = datePicker.input;
TestUtils.Simulate.click(ReactDOM.findDOMNode(dateInput));
expect(datePicker.state.open).to.be.false;
});
it("should keep the calendar shown when blurring the date input", done => {
var datePicker = TestUtils.renderIntoDocument(<DatePicker />);
var dateInput = datePicker.input;
var focusSpy = sandbox.spy(dateInput, "focus");
TestUtils.Simulate.focus(ReactDOM.findDOMNode(dateInput));
TestUtils.Simulate.blur(ReactDOM.findDOMNode(dateInput));
defer(() => {
expect(datePicker.calendar).to.exist;
assert(focusSpy.calledOnce, "should refocus the date input");
done();
});
});
it("should not re-focus the date input when focusing the year dropdown", done => {
const onBlurSpy = sandbox.spy();
const datePicker = mount(
<DatePicker
showMonthDropdown
showYearDropdown
dropdownMode="select"
onBlur={onBlurSpy}
/>
);
const dateInput = datePicker.instance().input;
const dateInputWrapper = datePicker.find("input");
const focusSpy = sandbox.spy(dateInput, "focus");
dateInputWrapper.simulate("focus");
const calendarWrapper = datePicker.find("Calendar");
const yearSelect = calendarWrapper.find(".react-datepicker__year-select");
dateInputWrapper.simulate("blur");
yearSelect.simulate("focus");
defer(() => {
assert(focusSpy.called === false, "should not refocus the date input");
assert(onBlurSpy.called === false, "should not call DatePicker onBlur");
done();
});
});
it("should fire onYearChange when the year is selected", done => {
const onYearChangeSpy = sinon.spy();
const datePicker = mount(
<DatePicker
showYearDropdown
dropdownMode="select"
onYearChange={onYearChangeSpy}
/>
);
const dateInputWrapper = datePicker.find("input");
dateInputWrapper.simulate("click");
const calendarWrapper = datePicker.find("Calendar");
const yearSelect = calendarWrapper.find(".react-datepicker__year-select");
yearSelect.simulate("change");
defer(() => {
assert(onYearChangeSpy.called === true, "onYearChange should be called");
done();
});
});
it("should keep the calendar shown when clicking the calendar", () => {
var datePicker = TestUtils.renderIntoDocument(<DatePicker />);
var dateInput = datePicker.input;
TestUtils.Simulate.focus(ReactDOM.findDOMNode(dateInput));
TestUtils.Simulate.click(ReactDOM.findDOMNode(datePicker.calendar));
expect(datePicker.calendar).to.exist;
});
it("should not set open state when it is disabled and gets clicked", function() {
var datePicker = TestUtils.renderIntoDocument(<DatePicker disabled />);
var dateInput = datePicker.input;
TestUtils.Simulate.click(ReactDOM.findDOMNode(dateInput));
expect(datePicker.state.open).to.be.false;
});
it("should hide the calendar when clicking a day on the calendar", () => {
var datePicker = TestUtils.renderIntoDocument(<DatePicker />);
var dateInput = datePicker.input;
TestUtils.Simulate.focus(ReactDOM.findDOMNode(dateInput));
var day = TestUtils.scryRenderedComponentsWithType(
datePicker.calendar,
Day
)[0];
TestUtils.Simulate.click(ReactDOM.findDOMNode(day));
expect(datePicker.calendar).to.not.exist;
});
it("should not hide the calendar when clicking a day on the calendar and shouldCloseOnSelect prop is false", () => {
var datePicker = TestUtils.renderIntoDocument(
<DatePicker shouldCloseOnSelect={false} />
);
var dateInput = datePicker.input;
TestUtils.Simulate.focus(ReactDOM.findDOMNode(dateInput));
var day = TestUtils.scryRenderedComponentsWithType(
datePicker.calendar,
Day
)[0];
TestUtils.Simulate.click(ReactDOM.findDOMNode(day));
expect(datePicker.state.open).to.be.true;
});
it("should not hide the calendar when selecting a day in the calendar with Enter press, and shouldCloseOnSelect prop is false", () => {
var data = getOnInputKeyDownStuff({ shouldCloseOnSelect: false });
var dateInput = data.datePicker.input;
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowUp"));
TestUtils.Simulate.keyDown(
ReactDOM.findDOMNode(dateInput),
getKey("Enter")
);
expect(data.datePicker.state.open).to.be.true;
});
it("should update the preSelection state when a day is selected with Enter press", () => {
var data = getOnInputKeyDownStuff({ shouldCloseOnSelect: false });
var dateInput = data.datePicker.input;
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowDown"));
TestUtils.Simulate.keyDown(
ReactDOM.findDOMNode(dateInput),
getKey("Enter")
);
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowDown"));
utils.addWeeks(data.copyM, 2);
expect(
utils.formatDate(data.datePicker.state.preSelection, data.testFormat)
).to.equal(utils.formatDate(data.copyM, data.testFormat));
});
it("should update the preSelection state when a day is selected with mouse click", () => {
// Note: We need monthsShown=2 so that today can still be clicked when
// ArrowLeft selects the previous month. (On the 1st 2 days of the month.)
// On the last week of the month, when the next month includes the current
// week, we need monthsShown=1 to prevent today from appearing twice.
const dayOfMonth = utils.getDate(utils.now());
var data = getOnInputKeyDownStuff({
shouldCloseOnSelect: false,
monthsShown: dayOfMonth < 15 ? 2 : 1
});
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowLeft"));
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowLeft"));
var day = TestUtils.findRenderedDOMComponentWithClass(
data.datePicker.calendar,
"react-datepicker__day--today"
);
TestUtils.Simulate.click(ReactDOM.findDOMNode(day));
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowDown"));
utils.addWeeks(data.copyM, 1);
expect(
utils.formatDate(data.datePicker.state.preSelection, data.testFormat)
).to.equal(utils.formatDate(data.copyM, data.testFormat));
});
it("should hide the calendar when pressing enter in the date input", () => {
var datePicker = TestUtils.renderIntoDocument(<DatePicker />);
var dateInput = datePicker.input;
TestUtils.Simulate.focus(ReactDOM.findDOMNode(dateInput));
TestUtils.Simulate.keyDown(
ReactDOM.findDOMNode(dateInput),
getKey("Enter")
);
expect(datePicker.calendar).to.not.exist;
});
it("should hide the calendar when the pressing escape in the date input", () => {
var datePicker = TestUtils.renderIntoDocument(<DatePicker />);
var dateInput = datePicker.input;
TestUtils.Simulate.focus(ReactDOM.findDOMNode(dateInput));
TestUtils.Simulate.keyDown(
ReactDOM.findDOMNode(dateInput),
getKey("Escape")
);
expect(datePicker.calendar).to.not.exist;
});
it("should hide the calendar when tabbing from the date input", () => {
var onBlurSpy = sandbox.spy();
var datePicker = TestUtils.renderIntoDocument(
<DatePicker onBlur={onBlurSpy} />
);
var dateInput = datePicker.input;
TestUtils.Simulate.focus(ReactDOM.findDOMNode(dateInput));
TestUtils.Simulate.keyDown(ReactDOM.findDOMNode(dateInput), getKey("Tab"));
TestUtils.Simulate.blur(ReactDOM.findDOMNode(dateInput));
expect(datePicker.calendar).to.not.exist;
assert(onBlurSpy.calledOnce, "should call onBlur");
});
it("should not apply the react-datepicker-ignore-onclickoutside class to the date input when closed", () => {
var datePicker = TestUtils.renderIntoDocument(<DatePicker />);
var dateInput = datePicker.input;
expect(ReactDOM.findDOMNode(dateInput).className).to.not.contain(
"react-datepicker-ignore-onclickoutside"
);
});
it("should apply the react-datepicker-ignore-onclickoutside class to date input when open", () => {
var datePicker = TestUtils.renderIntoDocument(<DatePicker />);
var dateInput = datePicker.input;
TestUtils.Simulate.focus(ReactDOM.findDOMNode(dateInput));
expect(ReactDOM.findDOMNode(dateInput).className).to.contain(
"react-datepicker-ignore-onclickoutside"
);
});
it("should set the type attribute on the clear button to button", () => {
var datePicker = TestUtils.renderIntoDocument(
<DatePicker selected={utils.newDate("2015-12-15")} isClearable />
);
var clearButton = TestUtils.findRenderedDOMComponentWithClass(
datePicker,
"react-datepicker__close-icon"
);
expect(clearButton.type).to.equal("button");
});
it("should allow clearing the date when isClearable is true", () => {
var cleared = false;
function handleChange(d) {
if (d === null) {
cleared = true;
}
}
var datePicker = TestUtils.renderIntoDocument(
<DatePicker
selected={utils.newDate("2015-12-15")}
isClearable
onChange={handleChange}
/>
);
var clearButton = TestUtils.findRenderedDOMComponentWithClass(
datePicker,
"react-datepicker__close-icon"
);
TestUtils.Simulate.click(clearButton);
expect(cleared).to.be.true;
});
it("should clear input value in the local state", () => {
var datePicker = TestUtils.renderIntoDocument(
<DatePicker selected={utils.newDate("2015-12-15")} isClearable />
);
var clearButton = TestUtils.findRenderedDOMComponentWithClass(
datePicker,
"react-datepicker__close-icon"
);
TestUtils.Simulate.click(clearButton);
expect(datePicker.state.inputValue).to.be.null;
});
it("should set the title attribute on the clear button if clearButtonTitle is supplied", () => {
const datePicker = TestUtils.renderIntoDocument(
<DatePicker
selected={utils.newDate("2018-03-19")}
isClearable
clearButtonTitle="clear button"
/>
);
const clearButtonText = TestUtils.findRenderedDOMComponentWithClass(
datePicker,
"react-datepicker__close-icon"
).getAttribute("title");
expect(clearButtonText).to.equal("clear button");
});
it("should save time from the selected date", () => {
const selected = utils.newDate("2015-12-20 10:11:12");
let date;
var datePicker = TestUtils.renderIntoDocument(
<DatePicker
inline
selected={selected}
onChange={d => {
date = d;
}}
/>
);
var dayButton = TestUtils.scryRenderedDOMComponentsWithClass(
datePicker,
"react-datepicker__day"
)[0];
TestUtils.Simulate.click(dayButton);
expect(utils.getHour(date)).to.equal(10);
expect(utils.getMinute(date)).to.equal(11);
expect(utils.getSecond(date)).to.equal(12);
});
it("should mount and unmount properly", done => {
class TestComponent extends React.Component {
constructor(props) {
super(props);
this.state = { mounted: true };
}
render() {
return this.state.mounted ? <DatePicker /> : null;
}
}
var element = TestUtils.renderIntoDocument(<TestComponent />);
element.setState({ mounted: false }, done);
});
it("should render calendar inside PopperComponent when inline prop is not set", () => {
var datePicker = TestUtils.renderIntoDocument(<DatePicker />);
expect(function() {
TestUtils.findRenderedComponentWithType(datePicker, PopperComponent);
}).to.not.throw();
});
it("should render calendar directly without PopperComponent when inline prop is set", () => {
var datePicker = TestUtils.renderIntoDocument(<DatePicker inline />);
expect(function() {
TestUtils.findRenderedComponentWithType(datePicker, PopperComponent);
}).to.throw();
expect(datePicker.calendar).to.exist;
});
it("should ignore disable prop when inline prop is set", () => {
var datePicker = TestUtils.renderIntoDocument(
<DatePicker inline disabled />
);
expect(datePicker.calendar).to.exist;
});
it("should render Calendar in portal when withPortal is set and input has focus", () => {
var datePicker = TestUtils.renderIntoDocument(<DatePicker withPortal />);
var dateInput = datePicker.input;
TestUtils.Simulate.focus(ReactDOM.findDOMNode(dateInput));
expect(function() {
TestUtils.findRenderedDOMComponentWithClass(
datePicker,
"react-datepicker__portal"
);
}).to.not.throw();
expect(datePicker.calendar).to.exist;
});
it("should not render Calendar when withPortal is set and no focus is given to input", () => {
var datePicker = TestUtils.renderIntoDocument(<DatePicker withPortal />);
expect(function() {
TestUtils.findRenderedDOMComponentWithClass(
datePicker,
"react-datepicker__portal"
);
}).to.throw();
expect(datePicker.calendar).not.to.exist;
});
function getOnInputKeyDownStuff(opts) {
opts = opts || {};
var m = utils.newDate();
var copyM = utils.cloneDate(m);
var testFormat = "YYYY-MM-DD";
var exactishFormat = "YYYY-MM-DD HH: ZZ";
var callback = sandbox.spy();
var datePicker = TestUtils.renderIntoDocument(
<DatePicker selected={m} onChange={callback} {...opts} />
);
var dateInput = datePicker.input;
var nodeInput = ReactDOM.findDOMNode(dateInput);
TestUtils.Simulate.focus(nodeInput);
return {
m,
copyM,
testFormat,
exactishFormat,
callback,
datePicker,
dateInput,
nodeInput
};
}
it("should handle onInputKeyDown ArrowLeft", () => {
var data = getOnInputKeyDownStuff();
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowLeft"));
utils.subtractDays(data.copyM, 1);
expect(
utils.formatDate(data.datePicker.state.preSelection, data.testFormat)
).to.equal(utils.formatDate(data.copyM, data.testFormat));
});
it("should handle onInputKeyDown ArrowRight", () => {
var data = getOnInputKeyDownStuff();
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowRight"));
utils.addDays(data.copyM, 1);
expect(
utils.formatDate(data.datePicker.state.preSelection, data.testFormat)
).to.equal(utils.formatDate(data.copyM, data.testFormat));
});
it("should handle onInputKeyDown ArrowUp", () => {
var data = getOnInputKeyDownStuff();
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowUp"));
utils.subtractWeeks(data.copyM, 1);
expect(
utils.formatDate(data.datePicker.state.preSelection, data.testFormat)
).to.equal(utils.formatDate(data.copyM, data.testFormat));
});
it("should handle onInputKeyDown ArrowDown", () => {
var data = getOnInputKeyDownStuff();
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowDown"));
utils.addWeeks(data.copyM, 1);
expect(
utils.formatDate(data.datePicker.state.preSelection, data.testFormat)
).to.equal(utils.formatDate(data.copyM, data.testFormat));
});
it("should handle onInputKeyDown PageUp", () => {
var data = getOnInputKeyDownStuff();
TestUtils.Simulate.keyDown(data.nodeInput, getKey("PageUp"));
utils.subtractMonths(data.copyM, 1);
expect(
utils.formatDate(data.datePicker.state.preSelection, data.testFormat)
).to.equal(utils.formatDate(data.copyM, data.testFormat));
});
it("should handle onInputKeyDown PageDown", () => {
var data = getOnInputKeyDownStuff();
TestUtils.Simulate.keyDown(data.nodeInput, getKey("PageDown"));
utils.addMonths(data.copyM, 1);
expect(
utils.formatDate(data.datePicker.state.preSelection, data.testFormat)
).to.equal(utils.formatDate(data.copyM, data.testFormat));
});
it("should handle onInputKeyDown End", () => {
var data = getOnInputKeyDownStuff();
TestUtils.Simulate.keyDown(data.nodeInput, getKey("End"));
utils.addYears(data.copyM, 1);
expect(
utils.formatDate(data.datePicker.state.preSelection, data.testFormat)
).to.equal(utils.formatDate(data.copyM, data.testFormat));
});
it("should handle onInputKeyDown Home", () => {
var data = getOnInputKeyDownStuff();
TestUtils.Simulate.keyDown(data.nodeInput, getKey("Home"));
utils.subtractYears(data.copyM, 1);
expect(
utils.formatDate(data.datePicker.state.preSelection, data.testFormat)
).to.equal(utils.formatDate(data.copyM, data.testFormat));
});
it("should not preSelect date if not between minDate and maxDate", () => {
var data = getOnInputKeyDownStuff({
minDate: utils.subtractDays(utils.newDate(), 1),
maxDate: utils.addDays(utils.newDate(), 1)
});
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowDown"));
expect(
utils.formatDate(data.datePicker.state.preSelection, data.testFormat)
).to.equal(utils.formatDate(data.copyM, data.testFormat));
});
describe("onInputKeyDown Enter", () => {
it("should update the selected date", () => {
var data = getOnInputKeyDownStuff();
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowLeft"));
TestUtils.Simulate.keyDown(data.nodeInput, getKey("Enter"));
utils.subtractDays(data.copyM, 1);
expect(data.callback.calledOnce).to.be.true;
var result = data.callback.args[0][0];
expect(utils.formatDate(result, data.testFormat)).to.equal(
utils.formatDate(data.copyM, data.testFormat)
);
});
it("should update the selected date on manual input", () => {
var data = getOnInputKeyDownStuff();
TestUtils.Simulate.change(data.nodeInput, {
target: { value: "02/02/2017" }
});
TestUtils.Simulate.keyDown(data.nodeInput, getKey("Enter"));
data.copyM = utils.newDate("2017-02-02");
expect(
utils.formatDate(data.callback.args[0][0], data.testFormat)
).to.equal(utils.formatDate(data.copyM, data.testFormat));
});
it("should not update the selected date if the date input manually it has something wrong", () => {
var data = getOnInputKeyDownStuff();
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowDown"));
TestUtils.Simulate.keyDown(data.nodeInput, getKey("Backspace"));
TestUtils.Simulate.keyDown(data.nodeInput, getKey("Enter"));
expect(data.callback.calledOnce).to.be.false;
});
it("should not select excludeDates", () => {
var data = getOnInputKeyDownStuff({
excludeDates: [utils.subtractDays(utils.newDate(), 1)]
});
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowLeft"));
TestUtils.Simulate.keyDown(data.nodeInput, getKey("Enter"));
expect(data.callback.calledOnce).to.be.false;
});
it("should not select dates excluded from filterDate", () => {
var data = getOnInputKeyDownStuff({
filterDate: date =>
utils.getDay(date) !==
utils.getDay(utils.subtractDays(utils.newDate(), 1))
});
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowLeft"));
TestUtils.Simulate.keyDown(data.nodeInput, getKey("Enter"));
expect(data.callback.calledOnce).to.be.false;
});
});
it("should reset the keyboard selection when closed", () => {
var data = getOnInputKeyDownStuff();
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowLeft"));
data.datePicker.setOpen(false);
expect(
utils.formatDate(data.datePicker.state.preSelection, data.testFormat)
).to.equal(utils.formatDate(data.copyM, data.testFormat));
});
it("should retain the keyboard selection when already open", () => {
var data = getOnInputKeyDownStuff();
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowLeft"));
data.datePicker.setOpen(true);
utils.subtractDays(data.copyM, 1);
expect(
utils.formatDate(data.datePicker.state.preSelection, data.testFormat)
).to.equal(utils.formatDate(data.copyM, data.testFormat));
});
it("should open the calendar when an arrow key is pressed", () => {
var data = getOnInputKeyDownStuff();
data.datePicker.setOpen(false);
expect(data.datePicker.state.open).to.be.false;
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowLeft"));
expect(data.datePicker.state.open).to.be.true;
});
it("should default to the current day on Enter", () => {
const data = getOnInputKeyDownStuff({ selected: null });
TestUtils.Simulate.keyDown(data.nodeInput, getKey("Enter"));
expect(data.callback.calledOnce).to.be.true;
const selected = data.callback.getCall(0).args[0];
expect(utils.formatDate(selected, data.exactishFormat)).to.equal(
utils.formatDate(data.copyM, data.exactishFormat)
);
expect(selected.isLocal()).to.equal(true);
});
it("should autofocus the input given the autoFocus prop", () => {
var div = document.createElement("div");
document.body.appendChild(div);
ReactDOM.render(<DatePicker autoFocus />, div);
expect(div.querySelector("input")).to.equal(document.activeElement);
});
it("should autofocus the input when calling the setFocus method", () => {
var div = document.createElement("div");
document.body.appendChild(div);
var datePicker = ReactDOM.render(<DatePicker />, div);
datePicker.setFocus();
expect(div.querySelector("input")).to.equal(document.activeElement);
});
it("should clear preventFocus timeout id when component is unmounted", () => {
var div = document.createElement("div");
document.body.appendChild(div);
var datePicker = ReactDOM.render(<DatePicker inline />, div);
datePicker.clearPreventFocusTimeout = sinon.spy();
ReactDOM.unmountComponentAtNode(div);
assert(
datePicker.clearPreventFocusTimeout.calledOnce,
"should call clearPreventFocusTimeout"
);
});
function getOnInputKeyDownDisabledKeyboardNavigationStuff() {
var m = utils.newDate();
var copyM = utils.cloneDate(m);
var testFormat = "YYYY-MM-DD";
var callback = sandbox.spy();
var datePicker = TestUtils.renderIntoDocument(
<DatePicker selected={m} onChange={callback} disabledKeyboardNavigation />
);
var dateInput = datePicker.input;
var nodeInput = ReactDOM.findDOMNode(dateInput);
TestUtils.Simulate.focus(nodeInput);
return {
m,
copyM,
testFormat,
callback,
datePicker,
dateInput,
nodeInput
};
}
it("should not handle onInputKeyDown ArrowLeft", () => {
var data = getOnInputKeyDownDisabledKeyboardNavigationStuff();
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowLeft"));
expect(data.callback.called).to.be.false;
});
it("should not handle onInputKeyDown ArrowRight", () => {
var data = getOnInputKeyDownDisabledKeyboardNavigationStuff();
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowRight"));
expect(data.callback.called).to.be.false;
});
it("should not handle onInputKeyDown ArrowUp", () => {
var data = getOnInputKeyDownDisabledKeyboardNavigationStuff();
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowUp"));
expect(data.callback.called).to.be.false;
});
it("should not handle onInputKeyDown ArrowDown", () => {
var data = getOnInputKeyDownDisabledKeyboardNavigationStuff();
TestUtils.Simulate.keyDown(data.nodeInput, getKey("ArrowDown"));
expect(data.callback.called).to.be.false;
});
it("should not handle onInputKeyDown PageUp", () => {
var data = getOnInputKeyDownDisabledKeyboardNavigationStuff();
TestUtils.Simulate.keyDown(data.nodeInput, getKey("PageUp"));
expect(data.callback.called).to.be.false;
});
it("should not handle onInputKeyDown PageDown", () => {
var data = getOnInputKeyDownDisabledKeyboardNavigationStuff();
TestUtils.Simulate.keyDown(data.nodeInput, getKey("PageDown"));
expect(data.callback.called).to.be.false;
});
it("should not handle onInputKeyDown Home", () => {
var data = getOnInputKeyDownDisabledKeyboardNavigationStuff();
TestUtils.Simulate.keyDown(data.nodeInput, getKey("Home"));
expect(data.callback.called).to.be.false;
});
it("should not handle onInputKeyDown End", () => {
var data = getOnInputKeyDownDisabledKeyboardNavigationStuff();
TestUtils.Simulate.keyDown(data.nodeInput, getKey("End"));
expect(data.callback.called).to.be.false;
});
it("should correctly clear date with empty input string", () => {
var cleared = false;
function handleChange(d) {
// Internally DateInput calls it's onChange prop with null
// when the input value is an empty string
if (d === null) {
cleared = true;
}
}
var datePicker = TestUtils.renderIntoDocument(
<DatePicker
selected={utils.newDate("2016-11-22")}
onChange={handleChange}
/>
);
var input = ReactDOM.findDOMNode(datePicker.input);
input.value = "";
TestUtils.Simulate.change(input);
expect(cleared).to.be.true;
});
it("should correctly update the date input when utcOffset is all that changes on the selected date", () => {
var date = utils.setUTCOffset(utils.newDate("2016-11-22T00:00:00Z"), -6);
var tmzDatePicker = mount(<TimezoneDatePicker />);
tmzDatePicker.setState({ startDate: date, utcOffset: -6 });
expect(tmzDatePicker.find("input").prop("value")).to.equal(
"2016-11-21 18:00"
);
tmzDatePicker.setState({
utcOffset: 6,
startDate: utils.setUTCOffset(utils.cloneDate(date), 6)
});
expect(tmzDatePicker.find("input").prop("value")).to.equal(
"2016-11-22 06:00"
);
});
it("should correctly update the input when the value prop changes", () => {
const datePicker = mount(<DatePicker />);
expect(datePicker.find("input").prop("value")).to.equal("");
datePicker.setProps({ value: "foo" });
expect(datePicker.find("input").prop("value")).to.equal("foo");
});
it("should preserve user input as they are typing", () => {
const onChange = date => datePicker.setProps({ selected: date });
const datePicker = mount(
<DatePicker
dateFormat={["YYYY-MM-DD", "MM/DD/YYYY", "MM/DD/YY"]}
onChange={onChange}
/>
);
expect(datePicker.find("input").prop("value")).to.equal("");
const str = "12/30/1982";
datePicker.find("input").simulate("focus");
str.split("").forEach((c, i) => {
datePicker.find("input").simulate("change", {
target: { value: datePicker.find("input").prop("value") + c }
});
datePicker.update();
expect(datePicker.find("input").prop("value")).to.equal(
str.substring(0, i + 1)
);
});
expect(
utils.formatDate(datePicker.prop("selected"), "YYYY-MM-DD")
).to.equal("1982-12-30");
});
it("should invoke provided onChangeRaw function on manual input change", () => {
const inputValue = "test";
const onChangeRawSpy = sandbox.spy();
const datePicker = TestUtils.renderIntoDocument(
<DatePicker
selected={utils.newDate()}
onChange={sandbox.spy()}
onChangeRaw={onChangeRawSpy}
/>
);
expect(onChangeRawSpy.called).to.be.false;
const input = ReactDOM.findDOMNode(datePicker.input);
input.value = inputValue;
TestUtils.Simulate.change(input);
expect(onChangeRawSpy.calledOnce).to.be.true;
expect(onChangeRawSpy.args[0][0].target.value).to.equal(inputValue);
});
it("should allow onChangeRaw to prevent a change", () => {
const onChangeRaw = e => e.target.value > "2" && e.preventDefault();
const datePicker = mount(<DatePicker onChangeRaw={onChangeRaw} />);
expect(datePicker.find("input").prop("value")).to.equal("");
datePicker.find("input").simulate("change", { target: { value: "3" } });
datePicker.update();
expect(datePicker.find("input").prop("value")).to.equal("");
datePicker.find("input").simulate("change", { target: { value: "1" } });
datePicker.update();
expect(datePicker.find("input").prop("value")).to.equal("1");
});
it("should call onChangeRaw with all arguments", () => {
const inputValue = "test";
const onChangeRawSpy = sandbox.spy();
const datePicker = TestUtils.renderIntoDocument(
<DatePicker
selected={utils.newDate()}
onChange={sandbox.spy()}
customInput={<CustomInput />}
onChangeRaw={onChangeRawSpy}
/>
);
expect(onChangeRawSpy.called).to.be.false;
const input = ReactDOM.findDOMNode(datePicker.input);
input.value = inputValue;
TestUtils.Simulate.change(input);
expect(onChangeRawSpy.calledOnce).to.be.true;
expect(onChangeRawSpy.args[0][0].target.value).to.equal(inputValue);
expect(onChangeRawSpy.args[0][1]).to.equal("test");
});
it("should handle the lack of an 'event' object as the first argument to handleChange analogously to 'preventDefault' being called", () => {
const inputValue = "test";
const onChangeRawSpy = sandbox.spy();
let customInput = <CustomInput onChangeArgs={e => [e.target.value]} />;
const datePicker = TestUtils.renderIntoDocument(
<DatePicker
selected={utils.newDate()}
onChange={sandbox.spy()}
customInput={customInput}
onChangeRaw={onChangeRawSpy}
/>
);
expect(onChangeRawSpy.called).to.be.false;
const input = ReactDOM.findDOMNode(datePicker.input);
input.value = inputValue;
TestUtils.Simulate.change(input);
expect(onChangeRawSpy.calledOnce).to.be.true;
expect(onChangeRawSpy.args[0][0]).to.equal("test");
});
it("should handle a click outside of the calendar", () => {
const datePicker = mount(
<DatePicker selected={utils.newDate()} withPortal />
).instance();
const openSpy = sandbox.spy(datePicker, "setOpen");
datePicker.handleCalendarClickOutside(
sandbox.stub({ preventDefault: () => {} })
);
expect(openSpy.calledOnce).to.be.true;
expect(openSpy.calledWithExactly(false)).to.be.true;
});
it("should default to the currently selected date", () => {
const datePicker = mount(
<DatePicker selected={utils.newDate("1988-12-30")} />
);
expect(
utils.formatDate(datePicker.state("preSelection"), "YYYY-MM-DD")
).to.equal("1988-12-30");
});
it("should default to the start date when selecting an end date", () => {
const datePicker = mount(
<DatePicker startDate={utils.newDate("1988-11-30")} selectsEnd />
);
expect(
utils.formatDate(datePicker.state("preSelection"), "YYYY-MM-DD")
).to.equal("1988-11-30");
});
it("should default to the end date when selecting a start date", () => {
const datePicker = mount(
<DatePicker endDate={utils.newDate("1988-12-31")} selectsStart />
);
expect(
utils.formatDate(datePicker.state("preSelection"), "YYYY-MM-DD")
).to.equal("1988-12-31");
});
it("should default to a date <= maxDate", () => {
const datePicker = mount(
<DatePicker maxDate={utils.newDate("1982-01-01")} />
);
expect(
utils.formatDate(datePicker.state("preSelection"), "YYYY-MM-DD")
).to.equal("1982-01-01");
});
it("should default to a date >= minDate", () => {
const datePicker = mount(
<DatePicker minDate={utils.newDate("2063-04-05")} />
);
expect(
utils.formatDate(datePicker.state("preSelection"), "YYYY-MM-DD")
).to.equal("2063-04-05");
});
it("should default to the openToDate if there is one", () => {
const datePicker = mount(
<DatePicker openToDate={utils.newDate("2020-01-23")} />
);
expect(
utils.formatDate(datePicker.state("preSelection"), "YYYY-MM-DD")
).to.equal("2020-01-23");
});
it("should otherwise default to the current date", () => {
const datePicker = mount(<DatePicker />);
expect(
utils.formatDate(datePicker.state("preSelection"), "YYYY-MM-DD")
).to.equal(utils.formatDate(utils.newDate(), "YYYY-MM-DD"));
});
it("should support an initial null `selected` value in inline mode", () => {
const datePicker = mount(<DatePicker inline selected={null} />);
expect(() =>
datePicker.setProps({ selected: utils.newDate() })
).to.not.throw();
});
it("should switch month in inline mode immediately", () => {
const selected = utils.newDate();
const future = utils.addDays(utils.newDate(), 100);
const datePicker = mount(<DatePicker inline selected={selected} />);
expect(
utils.formatDate(datePicker.state("preSelection"), "YYYY-MM-DD")
).to.equal(utils.formatDate(selected, "YYYY-MM-DD"));
datePicker.setProps({ selected: future });
expect(
utils.formatDate(datePicker.state("preSelection"), "YYYY-MM-DD")
).to.equal(utils.formatDate(future, "YYYY-MM-DD"));
});
it("should switch month in inline mode immediately, when year is updated", () => {
const selected = utils.newDate();
const future = utils.addYears(utils.newDate(), 1);
const datePicker = mount(<DatePicker inline selected={selected} />);
expect(
utils.formatDate(datePicker.state("preSelection"), "YYYY-MM-DD")
).to.equal(utils.formatDate(selected, "YYYY-MM-DD"));
datePicker.setProps({ selected: future });
expect(
utils.formatDate(datePicker.state("preSelection"), "YYYY-MM-DD")
).to.equal(utils.formatDate(future, "YYYY-MM-DD"));
});
it("should not switch months in inline mode when a day is clicked", () => {
const selected = utils.newDate();
const datePicker = TestUtils.renderIntoDocument(
<DatePicker inline selected={selected} monthsShown={2} />
);
expect(
utils.formatDate(datePicker.state.preSelection, "YYYY-MM-DD")
).to.equal(utils.formatDate(selected, "YYYY-MM-DD"));
let days = TestUtils.scryRenderedComponentsWithType(datePicker, Day);
let nextMonthDay = days.find(d => d.props.month !== selected.month());
TestUtils.Simulate.click(ReactDOM.findDOMNode(nextMonthDay));
expect(
utils.formatDate(datePicker.state.preSelection, "YYYY-MM-DD")
).to.equal(utils.formatDate(selected, "YYYY-MM-DD"));
});
it("should not set open state when focusing on the date input and the preventOpenOnFocus prop is set", () => {
const datePicker = TestUtils.renderIntoDocument(
<DatePicker preventOpenOnFocus />
);
const dateInput = datePicker.input;
TestUtils.Simulate.focus(ReactDOM.findDOMNode(dateInput));
expect(datePicker.state.open).to.be.false;
});
it("should not set open state onInputKeyDown when preventOpenOnFocus prop is set", () => {
const datePicker = TestUtils.renderIntoDocument(
<DatePicker preventOpenOnFocus />
);
const dateInput = datePicker.input;