1 import ReactDOM from 'react-dom';
2 import React from 'react';
3 import TimePicker from '../src/TimePicker';
5 import TestUtils from 'react-dom/test-utils';
6 const Simulate = TestUtils.Simulate;
7 import expect from 'expect.js';
8 import async from 'async';
9 import moment from 'moment';
11 describe('TimePicker', () => {
14 function renderPicker(props) {
15 const showSecond = true;
16 const format = ('HH:mm:ss');
18 return ReactDOM.render(
21 showSecond={showSecond}
22 defaultValue={moment('12:57:58', format)}
27 function renderPickerWithoutSeconds(props) {
28 const showSecond = false;
29 const format = ('HH:mm');
31 return ReactDOM.render(
34 showSecond={showSecond}
35 defaultValue={moment('08:24', format)}
41 container = document.createElement('div');
42 document.body.appendChild(container);
46 ReactDOM.unmountComponentAtNode(container);
47 document.body.removeChild(container);
50 describe('render panel to body', () => {
51 it('popup correctly', (done) => {
53 const picker = renderPicker({
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);
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];
71 setTimeout(next, 100);
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();
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);
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);
100 expect(TestUtils.scryRenderedDOMComponentsWithTag(picker.panelInstance,
101 'li')[0]).to.be.ok();
102 ReactDOM.unmountComponentAtNode(container);
103 setTimeout(next, 100);
105 if (document.querySelectorAll) {
106 expect(document.querySelectorAll('.rc-time-picker').length).to.be(0);
108 expect(picker.panelInstance).not.to.be.ok();
115 it('support name', () => {
116 const picker = renderPicker({
117 name: 'time-picker-form-name',
119 const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
120 'rc-time-picker-input')[0];
121 expect(input.name).to.be('time-picker-form-name');
124 it('support focus', () => {
125 const picker = renderPicker({
126 name: 'time-picker-form-name',
128 expect(picker.focus).to.be.a('function');
131 it('should be controlled by open', () => {
132 const picker = renderPicker({
135 expect(picker.state.open).not.to.be.ok();
136 const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
137 'rc-time-picker-input')[0];
138 Simulate.click(input);
139 expect(picker.state.open).not.to.be.ok();
143 describe('render panel to body (without seconds)', () => {
144 it('popup correctly', (done) => {
146 const picker = renderPickerWithoutSeconds({
151 expect(picker.state.open).not.to.be.ok();
152 const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
153 'rc-time-picker-input')[0];
154 expect((input).value).to.be('08:24');
155 async.series([(next) => {
156 Simulate.click(input);
157 setTimeout(next, 100);
159 expect(TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
160 'rc-time-picker-panel-inner')[0]).to.be.ok();
161 expect(picker.state.open).to.be(true);
162 const hour = TestUtils.scryRenderedDOMComponentsWithTag(picker.panelInstance, 'li')[1];
163 Simulate.click(hour);
164 setTimeout(next, 100);
166 expect(change).to.be.ok();
167 expect(change.hour()).to.be(1);
168 expect(change.minute()).to.be(24);
169 expect((input).value).to.be('01:24');
170 expect(picker.state.open).to.be.ok();
178 describe('render panel to body 12pm mode', () => {
179 it('popup correctly', (done) => {
181 const picker = renderPickerWithoutSeconds({
188 expect(picker.state.open).not.to.be.ok();
189 const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
190 'rc-time-picker-input')[0];
191 expect((input).value).to.be('');
192 async.series([(next) => {
193 Simulate.click(input);
194 setTimeout(next, 100);
196 expect(TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
197 'rc-time-picker-panel-inner')[0]).to.be.ok();
198 expect(picker.state.open).to.be(true);
199 const hour = TestUtils.scryRenderedDOMComponentsWithTag(picker.panelInstance, 'li')[1];
200 Simulate.click(hour);
201 setTimeout(next, 100);
203 expect(change).to.be.ok();
204 expect(picker.state.open).to.be.ok();