]> git.immae.eu Git - github/fretlink/time-picker.git/blob - tests/TimePicker.spec.jsx
document onOpen and onClose, close #23
[github/fretlink/time-picker.git] / tests / TimePicker.spec.jsx
1 import ReactDOM from 'react-dom';
2 import React from 'react';
3 import TimePicker from '../src/TimePicker';
4
5 import TestUtils from 'react-addons-test-utils';
6 const Simulate = TestUtils.Simulate;
7 import expect from 'expect.js';
8 import async from 'async';
9 import moment from 'moment';
10
11 describe('TimePicker', () => {
12 let container;
13
14 function renderPicker(props) {
15 const showSecond = true;
16 const format = ('HH:mm:ss');
17
18 return ReactDOM.render(
19 <TimePicker
20 format={format}
21 showSecond={showSecond}
22 defaultValue={moment('12:57:58', format)}
23 {...props}
24 />, container);
25 }
26
27 function renderPickerWithoutSeconds(props) {
28 const showSecond = false;
29 const format = ('HH:mm');
30
31 return ReactDOM.render(
32 <TimePicker
33 format={format}
34 showSecond={showSecond}
35 defaultValue={moment('08:24', format)}
36 {...props}
37 />, container);
38 }
39
40 beforeEach(() => {
41 container = document.createElement('div');
42 document.body.appendChild(container);
43 });
44
45 afterEach(() => {
46 ReactDOM.unmountComponentAtNode(container);
47 document.body.removeChild(container);
48 });
49
50 describe('render panel to body', () => {
51 it('popup correctly', (done) => {
52 let change;
53 const picker = renderPicker({
54 onChange(v) {
55 change = v;
56 },
57 });
58 expect(picker.state.open).not.to.be.ok();
59 const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
60 'rc-time-picker-input')[0];
61 expect((input).value).to.be('12:57:58');
62 async.series([(next) => {
63 Simulate.click(input);
64 setTimeout(next, 100);
65 }, (next) => {
66 expect(TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
67 'rc-time-picker-panel-inner')[0]).to.be.ok();
68 expect(picker.state.open).to.be(true);
69 const hour = TestUtils.scryRenderedDOMComponentsWithTag(picker.panelInstance, 'li')[1];
70 Simulate.click(hour);
71 setTimeout(next, 100);
72 }, (next) => {
73 expect(change).to.be.ok();
74 expect(change.hour()).to.be(1);
75 expect(change.minute()).to.be(57);
76 expect(change.second()).to.be(58);
77 expect((input).value).to.be('01:57:58');
78 expect(picker.state.open).to.be.ok();
79 next();
80 }], () => {
81 done();
82 });
83 });
84
85 it('destroy correctly', (done) => {
86 const picker = renderPicker();
87 expect(picker.state.open).not.to.be.ok();
88 const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
89 'rc-time-picker-input')[0];
90 async.series([(next) => {
91 Simulate.click(input);
92 setTimeout(next, 100);
93 }, (next) => {
94 expect(TestUtils.scryRenderedDOMComponentsWithClass(picker,
95 'rc-time-picker-panel-inner')[0]).not.to.be.ok();
96 expect(picker.state.open).to.be(true);
97 if (document.querySelectorAll) {
98 expect(document.querySelectorAll('.rc-time-picker').length).not.to.be(0);
99 }
100 expect(TestUtils.scryRenderedDOMComponentsWithTag(picker.panelInstance,
101 'li')[0]).to.be.ok();
102 ReactDOM.unmountComponentAtNode(container);
103 setTimeout(next, 100);
104 }, (next) => {
105 if (document.querySelectorAll) {
106 expect(document.querySelectorAll('.rc-time-picker').length).to.be(0);
107 }
108 expect(picker.panelInstance).not.to.be.ok();
109 next();
110 }], () => {
111 done();
112 });
113 });
114 });
115
116 describe('render panel to body (without seconds)', () => {
117 it('popup correctly', (done) => {
118 let change;
119 const picker = renderPickerWithoutSeconds({
120 onChange(v) {
121 change = v;
122 },
123 });
124 expect(picker.state.open).not.to.be.ok();
125 const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
126 'rc-time-picker-input')[0];
127 expect((input).value).to.be('08:24');
128 async.series([(next) => {
129 Simulate.click(input);
130 setTimeout(next, 100);
131 }, (next) => {
132 expect(TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
133 'rc-time-picker-panel-inner')[0]).to.be.ok();
134 expect(picker.state.open).to.be(true);
135 const hour = TestUtils.scryRenderedDOMComponentsWithTag(picker.panelInstance, 'li')[1];
136 Simulate.click(hour);
137 setTimeout(next, 100);
138 }, (next) => {
139 expect(change).to.be.ok();
140 expect(change.hour()).to.be(1);
141 expect(change.minute()).to.be(24);
142 expect((input).value).to.be('01:24');
143 expect(picker.state.open).to.be.ok();
144 next();
145 }], () => {
146 done();
147 });
148 });
149 });
150 });