aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/TimePicker.spec.jsx
diff options
context:
space:
mode:
Diffstat (limited to 'tests/TimePicker.spec.jsx')
-rw-r--r--tests/TimePicker.spec.jsx162
1 files changed, 162 insertions, 0 deletions
diff --git a/tests/TimePicker.spec.jsx b/tests/TimePicker.spec.jsx
new file mode 100644
index 0000000..5bd056e
--- /dev/null
+++ b/tests/TimePicker.spec.jsx
@@ -0,0 +1,162 @@
1import ReactDOM from 'react-dom';
2import React from 'react';
3import TimePicker from '../src/TimePicker';
4
5import TestUtils from 'react-addons-test-utils';
6var Simulate = TestUtils.Simulate;
7import expect from 'expect.js';
8import async from 'async';
9
10import DateTimeFormat from 'gregorian-calendar-format';
11import GregorianCalendar from 'gregorian-calendar';
12import zhCn from 'gregorian-calendar/lib/locale/zh_CN';
13import TimePickerLocale from '../src/locale/zh_CN';
14
15describe('TimePicker', function () {
16 var container;
17
18 function renderPicker(props) {
19 var showSecond = true;
20 var formatter = new DateTimeFormat('HH:mm:ss');
21
22 return ReactDOM.render(
23 <TimePicker
24 formatter={formatter}
25 locale={TimePickerLocale}
26 showSecond={showSecond}
27 defaultValue={formatTime('12:57:58', formatter)}
28 {...props}
29 />, container);
30 }
31
32 function renderPickerWithoutSeconds(props) {
33 var showSecond = false;
34 var formatter = new DateTimeFormat('HH:mm');
35
36 return ReactDOM.render(
37 <TimePicker
38 formatter={formatter}
39 locale={TimePickerLocale}
40 showSecond={showSecond}
41 defaultValue={formatTime('08:24', formatter)}
42 {...props}
43 />, container);
44 }
45
46 function formatTime(time, formatter) {
47 return formatter.parse(time, {
48 locale: zhCn,
49 obeyCount: true,
50 });
51 }
52
53 beforeEach(function () {
54 container = document.createElement('div');
55 document.body.appendChild(container);
56 });
57
58 afterEach(function () {
59 ReactDOM.unmountComponentAtNode(container);
60 document.body.removeChild(container);
61 });
62
63 describe('render panel to body', function () {
64 it('popup correctly', function (done) {
65 var change;
66 var picker = renderPicker({
67 onChange: function (v) {
68 change = v;
69 }
70 });
71 expect(picker.state.open).not.to.be.ok();
72 var input = TestUtils.scryRenderedDOMComponentsWithClass(picker, 'rc-time-picker-input')[0];
73 expect(ReactDOM.findDOMNode(input).value).to.be('12:57:58');
74 async.series([function (next) {
75 Simulate.click(input);
76 setTimeout(next, 100);
77 }, function (next) {
78 expect(TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance, 'rc-time-picker-panel-inner')[0]).to.be.ok();
79 expect(picker.state.open).to.be(true);
80 var hour = TestUtils.scryRenderedDOMComponentsWithTag(picker.panelInstance, 'li')[1];
81 Simulate.click(hour);
82 setTimeout(next, 100);
83 }, function (next) {
84 expect(change).to.be.ok();
85 expect(change.getHourOfDay()).to.be(1);
86 expect(change.getMinutes()).to.be(57);
87 expect(change.getSeconds()).to.be(58);
88 expect(ReactDOM.findDOMNode(input).value).to.be('01:57:58');
89 expect(picker.state.open).to.be.ok();
90 next();
91 }], function () {
92 done();
93 });
94 });
95
96 it('destroy correctly', function (done) {
97 var change;
98 var picker = renderPicker({
99 onChange: function (v) {
100 change = v;
101 }
102 });
103 expect(picker.state.open).not.to.be.ok();
104 var input = TestUtils.scryRenderedDOMComponentsWithClass(picker, 'rc-time-picker-input')[0];
105 async.series([function (next) {
106 Simulate.click(input);
107 setTimeout(next, 100);
108 }, function (next) {
109 expect(TestUtils.scryRenderedDOMComponentsWithClass(picker, 'rc-time-picker-panel-inner')[0]).not.to.be.ok();
110 expect(picker.state.open).to.be(true);
111 if (document.querySelectorAll) {
112 expect(document.querySelectorAll('.rc-time-picker').length).not.to.be(0);
113 }
114 expect(TestUtils.scryRenderedDOMComponentsWithTag(picker.panelInstance, 'li')[0]).to.be.ok();
115 ReactDOM.unmountComponentAtNode(container);
116 setTimeout(next, 100);
117 }, function (next) {
118 if (document.querySelectorAll) {
119 expect(document.querySelectorAll('.rc-time-picker').length).to.be(0);
120 }
121 expect(picker.panelInstance).not.to.be.ok();
122 next();
123 }], function () {
124 done();
125 });
126 });
127 });
128
129 describe('render panel to body (without seconds)', function () {
130 it('popup correctly', function (done) {
131 var change;
132 var picker = renderPickerWithoutSeconds({
133 onChange: function (v) {
134 change = v;
135 }
136 });
137 expect(picker.state.open).not.to.be.ok();
138 var input = TestUtils.scryRenderedDOMComponentsWithClass(picker, 'rc-time-picker-input')[0];
139 expect(ReactDOM.findDOMNode(input).value).to.be('08:24');
140 async.series([function (next) {
141 Simulate.click(input);
142 setTimeout(next, 100);
143 }, function (next) {
144 expect(TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance, 'rc-time-picker-panel-inner')[0]).to.be.ok();
145 expect(picker.state.open).to.be(true);
146 var hour = TestUtils.scryRenderedDOMComponentsWithTag(picker.panelInstance, 'li')[1];
147 Simulate.click(hour);
148 setTimeout(next, 100);
149 }, function (next) {
150 expect(change).to.be.ok();
151 expect(change.getHourOfDay()).to.be(1);
152 expect(change.getMinutes()).to.be(24);
153 expect(ReactDOM.findDOMNode(input).value).to.be('01:24');
154 expect(picker.state.open).to.be.ok();
155 next();
156 }], function () {
157 done();
158 });
159 });
160 });
161
162});