forked from Hacker0x01/react-datepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimepicker_test.js
87 lines (75 loc) · 2.76 KB
/
timepicker_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
import React from "react";
import DatePicker from "../src/index.jsx";
import TestUtils from "react-dom/test-utils";
import ReactDOM from "react-dom";
import Time from "../src/time";
import { newDate, formatDate } from "../src/date_utils";
describe("TimePicker", () => {
let datePicker;
let div;
let onChangeMoment;
beforeEach(() => {
div = document.createElement("div");
});
it("should update on input time change", () => {
renderDatePicker("February 28, 2018 4:43 PM");
expect(getInputString()).to.equal("February 28, 2018 4:43 PM");
setManually("February 28, 2018 4:45 PM");
expect(formatDate(onChangeMoment, "MMMM d, yyyy p")).to.equal(
"February 28, 2018 4:45 PM"
);
});
it("should allow time changes after input change", () => {
renderDatePicker("February 28, 2018 4:43 PM");
setManually("February 28, 2018 4:45 PM");
TestUtils.Simulate.focus(ReactDOM.findDOMNode(datePicker.input));
const time = TestUtils.findRenderedComponentWithType(datePicker, Time);
const lis = TestUtils.scryRenderedDOMComponentsWithTag(time, "li");
TestUtils.Simulate.click(lis[1]);
expect(getInputString()).to.equal("February 28, 2018 12:30 AM");
});
it("should allow for injected date if input does not have focus", () => {
renderDatePicker("February 28, 2018 4:43 PM");
setManually("February 28, 2018 4:45 PM");
TestUtils.Simulate.blur(datePicker.input);
renderDatePicker("February 28, 2018 4:43 PM");
expect(getInputString()).to.equal("February 28, 2018 4:43 PM");
});
it("should not close datepicker after time clicked when shouldCloseOnSelect is false", () => {
var datePicker = TestUtils.renderIntoDocument(
<DatePicker shouldCloseOnSelect={false} showTimeSelect />
);
var dateInput = datePicker.input;
TestUtils.Simulate.focus(ReactDOM.findDOMNode(dateInput));
const time = TestUtils.findRenderedComponentWithType(datePicker, Time);
const lis = TestUtils.scryRenderedDOMComponentsWithTag(time, "li");
TestUtils.Simulate.click(lis[0]);
expect(datePicker.state.open).to.be.true;
});
function setManually(string) {
TestUtils.Simulate.focus(datePicker.input);
TestUtils.Simulate.change(datePicker.input, { target: { value: string } });
}
function getInputString() {
return ReactDOM.findDOMNode(datePicker.input).value;
}
function renderDatePicker(string) {
return renderDatePickerFor(new Date(string));
}
function renderDatePickerFor(selected) {
datePicker = ReactDOM.render(
<DatePicker
selected={selected}
dateFormat={"MMMM d, yyyy p"}
allowSameDay
onChange={onChange}
showTimeSelect
/>,
div
);
}
function onChange(m) {
onChangeMoment = newDate(m);
renderDatePicker(m);
}
});