]> git.immae.eu Git - github/fretlink/time-picker.git/blame - tests/Header.spec.jsx
fix react createClass and PropTypes warning
[github/fretlink/time-picker.git] / tests / Header.spec.jsx
CommitLineData
02d1b455
M
1import ReactDOM from 'react-dom';
2import React from 'react';
3import TimePicker from '../src/TimePicker';
4
3ab3a128 5import TestUtils from 'react-dom/test-utils';
e9060dda 6const Simulate = TestUtils.Simulate;
02d1b455
M
7import expect from 'expect.js';
8import async from 'async';
19b96283 9import KeyCode from 'rc-util/lib/KeyCode';
4984ed85 10import moment from 'moment';
e9060dda 11
12describe('Header', () => {
13 let container;
02d1b455
M
14
15 function renderPicker(props) {
e9060dda 16 const showSecond = true;
4984ed85 17 const format = 'HH:mm:ss';
02d1b455
M
18
19 return ReactDOM.render(
20 <TimePicker
4984ed85 21 format={format}
02d1b455 22 showSecond={showSecond}
4984ed85 23 defaultValue={moment('01:02:03', format)}
02d1b455
M
24 {...props}
25 />, container);
26 }
27
e9060dda 28 beforeEach(() => {
02d1b455
M
29 container = document.createElement('div');
30 document.body.appendChild(container);
31 });
32
e9060dda 33 afterEach(() => {
02d1b455
M
34 ReactDOM.unmountComponentAtNode(container);
35 document.body.removeChild(container);
36 });
37
e9060dda 38 describe('input to change value', () => {
39 it('input correctly', (done) => {
40 const picker = renderPicker();
02d1b455 41 expect(picker.state.open).not.to.be.ok();
4984ed85 42 const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
43 'rc-time-picker-input')[0];
e9060dda 44 let header;
45 async.series([(next) => {
02d1b455
M
46 Simulate.click(input);
47 setTimeout(next, 100);
e9060dda 48 }, (next) => {
02d1b455 49 expect(picker.state.open).to.be(true);
4984ed85 50 header = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
51 'rc-time-picker-panel-input')[0];
02d1b455 52 expect(header).to.be.ok();
4984ed85 53 expect((header).value).to.be('01:02:03');
54 expect((input).value).to.be('01:02:03');
02d1b455 55
4984ed85 56 (header).value = '12:34:56';
02d1b455
M
57 Simulate.change(header);
58 setTimeout(next, 100);
e9060dda 59 }, (next) => {
02d1b455 60 expect(picker.state.open).to.be(true);
4984ed85 61 expect((header).value).to.be('12:34:56');
62 expect((input).value).to.be('12:34:56');
02d1b455
M
63
64 next();
e9060dda 65 }], () => {
02d1b455
M
66 done();
67 });
68 });
69
e9060dda 70 it('carry correctly', (done) => {
71 const picker = renderPicker();
02d1b455 72 expect(picker.state.open).not.to.be.ok();
4984ed85 73 const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
74 'rc-time-picker-input')[0];
e9060dda 75 let header;
76 async.series([(next) => {
02d1b455
M
77 Simulate.click(input);
78 setTimeout(next, 100);
e9060dda 79 }, (next) => {
02d1b455 80 expect(picker.state.open).to.be(true);
4984ed85 81 header = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
82 'rc-time-picker-panel-input')[0];
02d1b455 83 expect(header).to.be.ok();
4984ed85 84 expect((header).value).to.be('01:02:03');
85 expect((input).value).to.be('01:02:03');
02d1b455 86
4984ed85 87 (header).value = '33:44:55';
02d1b455
M
88 Simulate.change(header);
89 setTimeout(next, 100);
e9060dda 90 }, (next) => {
02d1b455 91 expect(picker.state.open).to.be(true);
4984ed85 92 expect((header).value).to.be('33:44:55');
93 expect((input).value).to.be('01:02:03');
02d1b455 94
4984ed85 95 (header).value = '10:90:30';
02d1b455
M
96 Simulate.change(header);
97 setTimeout(next, 100);
e9060dda 98 }, (next) => {
02d1b455 99 expect(picker.state.open).to.be(true);
4984ed85 100 expect((header).value).to.be('10:90:30');
101 expect((input).value).to.be('01:02:03');
02d1b455 102
4984ed85 103 (header).value = '34:56:78';
02d1b455
M
104 Simulate.change(header);
105 setTimeout(next, 100);
e9060dda 106 }, (next) => {
02d1b455 107 expect(picker.state.open).to.be(true);
4984ed85 108 expect((header).value).to.be('34:56:78');
109 expect((input).value).to.be('01:02:03');
02d1b455
M
110
111 next();
e9060dda 112 }], () => {
02d1b455
M
113 done();
114 });
115 });
116
0e62bf0b
M
117 it('carry disabled correctly', (done) => {
118 const picker = renderPicker({
71bd9bc1
M
119 disabledMinutes(h) {
120 return [h];
121 },
122 disabledSeconds(h, m) {
123 return [h + m % 60];
124 },
0e62bf0b
M
125 });
126 expect(picker.state.open).not.to.be.ok();
4984ed85 127 const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
128 'rc-time-picker-input')[0];
0e62bf0b
M
129 let header;
130 async.series([(next) => {
131 Simulate.click(input);
132 setTimeout(next, 100);
133 }, (next) => {
134 expect(picker.state.open).to.be(true);
4984ed85 135 header = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
136 'rc-time-picker-panel-input')[0];
0e62bf0b 137 expect(header).to.be.ok();
4984ed85 138 expect((header).value).to.be('01:02:03');
139 expect((input).value).to.be('01:02:03');
0e62bf0b 140
4984ed85 141 (header).value = '10:09:78';
0e62bf0b
M
142 Simulate.change(header);
143 setTimeout(next, 100);
144 }, (next) => {
145 expect(picker.state.open).to.be(true);
4984ed85 146 expect((header).className).to.contain('rc-time-picker-panel-input-invalid');
147 expect((header).value).to.be('10:09:78');
148 expect((input).value).to.be('01:02:03');
0e62bf0b 149
4984ed85 150 (header).value = '10:10:78';
0e62bf0b
M
151 Simulate.change(header);
152 setTimeout(next, 100);
153 }, (next) => {
154 expect(picker.state.open).to.be(true);
4984ed85 155 expect((header).value).to.be('10:10:78');
156 expect((input).value).to.be('01:02:03');
71bd9bc1 157
4984ed85 158 (header).value = '10:09:19';
71bd9bc1
M
159 Simulate.change(header);
160 setTimeout(next, 100);
161 }, (next) => {
162 expect(picker.state.open).to.be(true);
4984ed85 163 expect((header).className).to.contain('rc-time-picker-panel-input-invalid');
164 expect((header).value).to.be('10:09:19');
165 expect((input).value).to.be('01:02:03');
71bd9bc1 166
4984ed85 167 (header).value = '10:09:20';
71bd9bc1
M
168 Simulate.change(header);
169 setTimeout(next, 100);
170 }, (next) => {
171 expect(picker.state.open).to.be(true);
4984ed85 172 expect((header).value).to.be('10:09:20');
173 expect((input).value).to.be('10:09:20');
0e62bf0b
M
174
175 next();
176 }], () => {
177 done();
178 });
179 });
180
181 it('carry hidden correctly', (done) => {
182 const picker = renderPicker({
71bd9bc1
M
183 disabledMinutes(h) {
184 return [h];
185 },
186 disabledSeconds(h, m) {
187 return [h + m % 60];
188 },
189 hideDisabledOptions: true,
0e62bf0b
M
190 });
191 expect(picker.state.open).not.to.be.ok();
4984ed85 192 const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
193 'rc-time-picker-input')[0];
0e62bf0b
M
194 let header;
195 async.series([(next) => {
196 Simulate.click(input);
197 setTimeout(next, 100);
198 }, (next) => {
199 expect(picker.state.open).to.be(true);
4984ed85 200 header = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
201 'rc-time-picker-panel-input')[0];
0e62bf0b 202 expect(header).to.be.ok();
4984ed85 203 expect((header).value).to.be('01:02:03');
204 expect((input).value).to.be('01:02:03');
0e62bf0b 205
4984ed85 206 (header).value = '10:09:78';
0e62bf0b
M
207 Simulate.change(header);
208 setTimeout(next, 100);
209 }, (next) => {
210 expect(picker.state.open).to.be(true);
4984ed85 211 expect((header).className).to.contain('rc-time-picker-panel-input-invalid');
212 expect((header).value).to.be('10:09:78');
213 expect((input).value).to.be('01:02:03');
0e62bf0b 214
4984ed85 215 (header).value = '10:10:78';
71bd9bc1
M
216 Simulate.change(header);
217 setTimeout(next, 100);
218 }, (next) => {
219 expect(picker.state.open).to.be(true);
4984ed85 220 expect((header).value).to.be('10:10:78');
221 expect((input).value).to.be('01:02:03');
71bd9bc1 222
4984ed85 223 (header).value = '10:09:19';
71bd9bc1
M
224 Simulate.change(header);
225 setTimeout(next, 100);
226 }, (next) => {
227 expect(picker.state.open).to.be(true);
4984ed85 228 expect((header).className).to.contain('rc-time-picker-panel-input-invalid');
229 expect((header).value).to.be('10:09:19');
230 expect((input).value).to.be('01:02:03');
71bd9bc1 231
4984ed85 232 (header).value = '10:09:20';
0e62bf0b
M
233 Simulate.change(header);
234 setTimeout(next, 100);
235 }, (next) => {
236 expect(picker.state.open).to.be(true);
4984ed85 237 expect((header).value).to.be('10:09:20');
238 expect((input).value).to.be('10:09:20');
0e62bf0b
M
239
240 next();
241 }], () => {
242 done();
243 });
244 });
245
e9060dda 246 it('check correctly', (done) => {
247 const picker = renderPicker();
02d1b455 248 expect(picker.state.open).not.to.be.ok();
4984ed85 249 const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
250 'rc-time-picker-input')[0];
e9060dda 251 let header;
252 async.series([(next) => {
02d1b455
M
253 Simulate.click(input);
254 setTimeout(next, 100);
e9060dda 255 }, (next) => {
02d1b455 256 expect(picker.state.open).to.be(true);
4984ed85 257 header = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
258 'rc-time-picker-panel-input')[0];
02d1b455 259 expect(header).to.be.ok();
4984ed85 260 expect((header).value).to.be('01:02:03');
261 expect((input).value).to.be('01:02:03');
02d1b455 262
4984ed85 263 (header).value = '3:34:56';
02d1b455
M
264 Simulate.change(header);
265 setTimeout(next, 100);
e9060dda 266 }, (next) => {
02d1b455 267 expect(picker.state.open).to.be(true);
4984ed85 268 expect((header).value).to.be('3:34:56');
269 expect((input).value).to.be('01:02:03');
270 expect((header).className).to.contain('rc-time-picker-panel-input-invalid');
02d1b455 271
4984ed85 272 (header).value = '13:3:56';
02d1b455
M
273 Simulate.change(header);
274 setTimeout(next, 100);
e9060dda 275 }, (next) => {
02d1b455 276 expect(picker.state.open).to.be(true);
4984ed85 277 expect((header).value).to.be('13:3:56');
278 expect((input).value).to.be('01:02:03');
279 expect((header).className).to.contain('rc-time-picker-panel-input-invalid');
02d1b455 280
4984ed85 281 (header).value = '13:34:5';
02d1b455
M
282 Simulate.change(header);
283 setTimeout(next, 100);
e9060dda 284 }, (next) => {
02d1b455 285 expect(picker.state.open).to.be(true);
4984ed85 286 expect((header).value).to.be('13:34:5');
287 expect((input).value).to.be('01:02:03');
288 expect((header).className).to.contain('rc-time-picker-panel-input-invalid');
02d1b455 289 next();
e9060dda 290 }], () => {
02d1b455
M
291 done();
292 });
293 });
02d1b455
M
294 });
295
e9060dda 296 describe('other operations', () => {
297 it('clear correctly', (done) => {
298 let change;
299 const picker = renderPicker({
300 onChange(v) {
02d1b455 301 change = v;
e9060dda 302 },
02d1b455
M
303 });
304 expect(picker.state.open).not.to.be.ok();
4984ed85 305 const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
306 'rc-time-picker-input')[0];
e9060dda 307 let header;
308 async.series([(next) => {
02d1b455
M
309 expect(picker.state.open).to.be(false);
310
311 Simulate.click(input);
312 setTimeout(next, 100);
e9060dda 313 }, (next) => {
02d1b455 314 expect(picker.state.open).to.be(true);
4984ed85 315 header = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
316 'rc-time-picker-panel-input')[0];
317 const clearButton = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
318 'rc-time-picker-panel-clear-btn')[0];
02d1b455
M
319 expect(header).to.be.ok();
320 expect(clearButton).to.be.ok();
4984ed85 321 expect((header).value).to.be('01:02:03');
322 expect((input).value).to.be('01:02:03');
02d1b455
M
323
324 Simulate.mouseDown(clearButton);
325 setTimeout(next, 100);
e9060dda 326 }, (next) => {
02d1b455
M
327 expect(picker.state.open).to.be(false);
328 expect(change).to.be(null);
4984ed85 329 expect((header).value).to.be('');
330 expect((input).value).to.be('');
02d1b455
M
331
332 next();
e9060dda 333 }], () => {
02d1b455
M
334 done();
335 });
336 });
337
e9060dda 338 it('exit correctly', (done) => {
339 const picker = renderPicker();
02d1b455 340 expect(picker.state.open).not.to.be.ok();
4984ed85 341 const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
342 'rc-time-picker-input')[0];
e9060dda 343 let header;
344 async.series([(next) => {
02d1b455
M
345 expect(picker.state.open).to.be(false);
346
347 Simulate.click(input);
348 setTimeout(next, 100);
e9060dda 349 }, (next) => {
02d1b455 350 expect(picker.state.open).to.be(true);
4984ed85 351 header = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
352 'rc-time-picker-panel-input')[0];
02d1b455 353 expect(header).to.be.ok();
4984ed85 354 expect((header).value).to.be('01:02:03');
355 expect((input).value).to.be('01:02:03');
02d1b455 356
4984ed85 357 Simulate.keyDown((header), {
e9060dda 358 keyCode: KeyCode.ESC,
02d1b455
M
359 });
360 setTimeout(next, 100);
e9060dda 361 }, (next) => {
02d1b455 362 expect(picker.state.open).to.be(false);
4984ed85 363 expect((header).value).to.be('01:02:03');
364 expect((input).value).to.be('01:02:03');
02d1b455
M
365
366 next();
e9060dda 367 }], () => {
02d1b455
M
368 done();
369 });
370 });
02d1b455
M
371 });
372});