]> git.immae.eu Git - github/fretlink/time-picker.git/blobdiff - tests/Select.spec.jsx
Merge branch 'shaleynikov-master'
[github/fretlink/time-picker.git] / tests / Select.spec.jsx
index 036a0f91dc69f6760ab6a656de5c7f1728ebc443..fd2ec326800dfb80d65058e99b8b571ee03d6529 100644 (file)
 import ReactDOM from 'react-dom';
 import React from 'react';
 import TimePicker from '../src/TimePicker';
-
 import TestUtils from 'react-addons-test-utils';
-var Simulate = TestUtils.Simulate;
+const Simulate = TestUtils.Simulate;
 import expect from 'expect.js';
 import async from 'async';
-import {KeyCode} from 'rc-util';
-
-import DateTimeFormat from 'gregorian-calendar-format';
-import GregorianCalendar from 'gregorian-calendar';
-import zhCn from 'gregorian-calendar/lib/locale/zh_CN';
-import TimePickerLocale from '../src/locale/zh_CN';
+import moment from 'moment';
 
-describe('Select', function () {
-  var container;
+describe('Select', () => {
+  let container;
 
   function renderPicker(props) {
-    var showSecond = true;
-    var formatter = new DateTimeFormat('HH:mm:ss');
+    const showSecond = true;
+    const format = 'HH:mm:ss';
 
     return ReactDOM.render(
       <TimePicker
-        formatter={formatter}
-        locale={TimePickerLocale}
+        format={format}
         showSecond={showSecond}
-        defaultValue={formatTime('01:02:03', formatter)}
+        defaultValue={moment('01:02:04', format)}
         {...props}
       />, container);
   }
 
-  function formatTime(time, formatter) {
-    return formatter.parse(time, {
-      locale: zhCn,
-      obeyCount: true,
-    });
-  }
-
-  beforeEach(function () {
+  beforeEach(() => {
     container = document.createElement('div');
     document.body.appendChild(container);
   });
 
-  afterEach(function () {
+  afterEach(() => {
     ReactDOM.unmountComponentAtNode(container);
     document.body.removeChild(container);
   });
 
-  describe('select number', function () {
-
-    it('select number correctly', function (done) {
-      var picker = renderPicker();
+  describe('select number', () => {
+    it('select number correctly', (done) => {
+      const picker = renderPicker();
       expect(picker.state.open).not.to.be.ok();
-      var input = TestUtils.scryRenderedDOMComponentsWithClass(picker, 'rc-time-picker-input')[0];
-      var selector;
-      async.series([function (next) {
+      const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
+        'rc-time-picker-input')[0];
+      let selector;
+      async.series([(next) => {
         expect(picker.state.open).to.be(false);
 
         Simulate.click(input);
         setTimeout(next, 100);
-      }, function (next) {
+      }, (next) => {
         expect(picker.state.open).to.be(true);
-        selector = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance, 'rc-time-picker-panel-select');
+        selector = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+          'rc-time-picker-panel-select');
 
         setTimeout(next, 100);
-      }, function (next) {
+      }, (next) => {
         expect(selector.length).to.be(3);
 
         next();
-      }], function () {
+      }], () => {
         done();
       });
     });
-
   });
 
-  describe('select to change value', function () {
+  describe('select to change value', () => {
+    it('hour correctly', (done) => {
+      let change;
+      const picker = renderPicker({
+        onChange(v) {
+          change = v;
+        },
+      });
+      expect(picker.state.open).not.to.be.ok();
+      const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
+        'rc-time-picker-input')[0];
+      let header;
+      async.series([(next) => {
+        expect(picker.state.open).to.be(false);
+
+        Simulate.click(input);
+        setTimeout(next, 100);
+      }, (next) => {
+        expect(picker.state.open).to.be(true);
+        header = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+          'rc-time-picker-panel-input')[0];
+        expect(header).to.be.ok();
+        expect((header).value).to.be('01:02:04');
+        expect((input).value).to.be('01:02:04');
+
+        const selector = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+          'rc-time-picker-panel-select')[0];
+        const option = selector.getElementsByTagName('li')[19];
+        Simulate.click(option);
+        setTimeout(next, 100);
+      }, (next) => {
+        expect(change).to.be.ok();
+        expect(change.hour()).to.be(19);
+        expect((header).value).to.be('19:02:04');
+        expect((input).value).to.be('19:02:04');
+        expect(picker.state.open).to.be.ok();
+
+        next();
+      }], () => {
+        done();
+      });
+    });
+
+    it('minute correctly', (done) => {
+      let change;
+      const picker = renderPicker({
+        onChange(v) {
+          change = v;
+        },
+      });
+      expect(picker.state.open).not.to.be.ok();
+      const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
+        'rc-time-picker-input')[0];
+      let header;
+      async.series([(next) => {
+        expect(picker.state.open).to.be(false);
+
+        Simulate.click(input);
+        setTimeout(next, 100);
+      }, (next) => {
+        expect(picker.state.open).to.be(true);
+        header = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+          'rc-time-picker-panel-input')[0];
+        expect(header).to.be.ok();
+        expect((header).value).to.be('01:02:04');
+        expect((input).value).to.be('01:02:04');
+
+        const selector = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+          'rc-time-picker-panel-select')[1];
+        const option = selector.getElementsByTagName('li')[19];
+        Simulate.click(option);
+        setTimeout(next, 100);
+      }, (next) => {
+        expect(change).to.be.ok();
+        expect(change.minute()).to.be(19);
+        expect((header).value).to.be('01:19:04');
+        expect((input).value).to.be('01:19:04');
+        expect(picker.state.open).to.be.ok();
 
-    it('hour correctly', function (done) {
-      var change;
-      var picker = renderPicker({
-        onChange: function (v) {
+        next();
+      }], () => {
+        done();
+      });
+    });
+
+    it('second correctly', (done) => {
+      let change;
+      const picker = renderPicker({
+        onChange(v) {
           change = v;
-        }
+        },
       });
       expect(picker.state.open).not.to.be.ok();
-      var input = TestUtils.scryRenderedDOMComponentsWithClass(picker, 'rc-time-picker-input')[0];
-      var header;
-      async.series([function (next) {
+      const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
+        'rc-time-picker-input')[0];
+      let header;
+      async.series([(next) => {
         expect(picker.state.open).to.be(false);
 
         Simulate.click(input);
         setTimeout(next, 100);
-      }, function (next) {
+      }, (next) => {
         expect(picker.state.open).to.be(true);
-        header = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance, 'rc-time-picker-panel-input')[0];
-        var selector = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance, 'rc-time-picker-panel-select')[0];
-        var option = selector.getElementsByTagName('li')[19];
+        header = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+          'rc-time-picker-panel-input')[0];
         expect(header).to.be.ok();
-        expect(ReactDOM.findDOMNode(header).value).to.be('01:02:03');
-        expect(ReactDOM.findDOMNode(input).value).to.be('01:02:03');
+        expect((header).value).to.be('01:02:04');
+        expect((input).value).to.be('01:02:04');
 
+        const selector = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+          'rc-time-picker-panel-select')[2];
+        const option = selector.getElementsByTagName('li')[19];
         Simulate.click(option);
         setTimeout(next, 100);
-      }, function (next) {
+      }, (next) => {
         expect(change).to.be.ok();
-        expect(change.getHourOfDay()).to.be(19);
-        expect(ReactDOM.findDOMNode(header).value).to.be('19:02:03');
-        expect(ReactDOM.findDOMNode(input).value).to.be('19:02:03');
+        expect(change.second()).to.be(19);
+        expect((header).value).to.be('01:02:19');
+        expect((input).value).to.be('01:02:19');
         expect(picker.state.open).to.be.ok();
 
         next();
-      }], function () {
+      }], () => {
         done();
       });
     });
 
-    it('minute correctly', function (done) {
-      var change;
-      var picker = renderPicker({
-        onChange: function (v) {
+    it('disabled correctly', (done) => {
+      let change;
+      const picker = renderPicker({
+        onChange(v) {
           change = v;
-        }
+        },
+        disabledMinutes(h) {
+          return [h];
+        },
+        disabledSeconds(h, m) {
+          return [h + m % 60];
+        },
       });
       expect(picker.state.open).not.to.be.ok();
-      var input = TestUtils.scryRenderedDOMComponentsWithClass(picker, 'rc-time-picker-input')[0];
-      var header;
-      async.series([function (next) {
+      const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
+        'rc-time-picker-input')[0];
+      let header;
+      async.series([(next) => {
         expect(picker.state.open).to.be(false);
 
         Simulate.click(input);
         setTimeout(next, 100);
-      }, function (next) {
+      }, (next) => {
         expect(picker.state.open).to.be(true);
-        header = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance, 'rc-time-picker-panel-input')[0];
-        var selector = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance, 'rc-time-picker-panel-select')[1];
-        var option = selector.getElementsByTagName('li')[19];
+        header = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+          'rc-time-picker-panel-input')[0];
         expect(header).to.be.ok();
-        expect(ReactDOM.findDOMNode(header).value).to.be('01:02:03');
-        expect(ReactDOM.findDOMNode(input).value).to.be('01:02:03');
+        expect((header).value).to.be('01:02:04');
+        expect((input).value).to.be('01:02:04');
+
+        const selector = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+          'rc-time-picker-panel-select')[1];
+        const option = selector.getElementsByTagName('li')[1];
+        Simulate.click(option);
+        setTimeout(next, 100);
+      }, (next) => {
+        expect(change).not.to.be.ok();
+        expect((header).value).to.be('01:02:04');
+        expect((input).value).to.be('01:02:04');
+        expect(picker.state.open).to.be.ok();
+
+        const selector = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+          'rc-time-picker-panel-select')[2];
+        const option = selector.getElementsByTagName('li')[3];
+        Simulate.click(option);
+        setTimeout(next, 100);
+      }, (next) => {
+        expect(change).not.to.be.ok();
+        expect((header).value).to.be('01:02:04');
+        expect((input).value).to.be('01:02:04');
+        expect(picker.state.open).to.be.ok();
 
+        const selector = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+          'rc-time-picker-panel-select')[1];
+        const option = selector.getElementsByTagName('li')[7];
         Simulate.click(option);
         setTimeout(next, 100);
-      }, function (next) {
+      }, (next) => {
         expect(change).to.be.ok();
-        expect(change.getMinutes()).to.be(19);
-        expect(ReactDOM.findDOMNode(header).value).to.be('01:19:03');
-        expect(ReactDOM.findDOMNode(input).value).to.be('01:19:03');
+        expect(change.minute()).to.be(7);
+        expect((header).value).to.be('01:07:04');
+        expect((input).value).to.be('01:07:04');
         expect(picker.state.open).to.be.ok();
 
         next();
-      }], function () {
+      }], () => {
         done();
       });
     });
 
-    it('second correctly', function (done) {
-      var change;
-      var picker = renderPicker({
-        onChange: function (v) {
+    it('hidden correctly', (done) => {
+      let change;
+      const picker = renderPicker({
+        onChange(v) {
           change = v;
-        }
+        },
+        disabledHours() {
+          return [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23];
+        },
+        hideDisabledOptions: true,
       });
       expect(picker.state.open).not.to.be.ok();
-      var input = TestUtils.scryRenderedDOMComponentsWithClass(picker, 'rc-time-picker-input')[0];
-      var header;
-      async.series([function (next) {
+      const input = TestUtils.scryRenderedDOMComponentsWithClass(picker, 'rc-time-picker-input')[0];
+      let header;
+      async.series([(next) => {
         expect(picker.state.open).to.be(false);
 
         Simulate.click(input);
         setTimeout(next, 100);
-      }, function (next) {
+      }, (next) => {
         expect(picker.state.open).to.be(true);
-        header = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance, 'rc-time-picker-panel-input')[0];
-        var selector = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance, 'rc-time-picker-panel-select')[2];
-        var option = selector.getElementsByTagName('li')[19];
+        header = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+          'rc-time-picker-panel-input')[0];
         expect(header).to.be.ok();
-        expect(ReactDOM.findDOMNode(header).value).to.be('01:02:03');
-        expect(ReactDOM.findDOMNode(input).value).to.be('01:02:03');
+        expect((header).value).to.be('01:02:04');
+        expect((input).value).to.be('01:02:04');
 
+        const selector = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+          'rc-time-picker-panel-select')[0];
+        const option = selector.getElementsByTagName('li')[3];
         Simulate.click(option);
         setTimeout(next, 100);
-      }, function (next) {
+      }, (next) => {
         expect(change).to.be.ok();
-        expect(change.getSeconds()).to.be(19);
-        expect(ReactDOM.findDOMNode(header).value).to.be('01:02:19');
-        expect(ReactDOM.findDOMNode(input).value).to.be('01:02:19');
+        expect(change.hour()).to.be(6);
+        expect((header).value).to.be('06:02:04');
+        expect((input).value).to.be('06:02:04');
         expect(picker.state.open).to.be.ok();
 
+        const selector = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+          'rc-time-picker-panel-select')[0];
+        const option = selector.getElementsByTagName('li')[4];
+        Simulate.click(option);
+        setTimeout(next, 100);
+      }, (next) => {
+        expect(change).to.be.ok();
+        expect(change.hour()).to.be(8);
+        expect((header).value).to.be('08:02:04');
+        expect((input).value).to.be('08:02:04');
+        expect(picker.state.open).to.be.ok();
+
+        next();
+      }], () => {
+        done();
+      });
+    });
+  });
+
+
+  describe('select in 12 hours mode', () => {
+    it('renders correctly', (done) => {
+      const picker = renderPicker({
+        use12Hours: true,
+        defaultValue: moment().hour(14).minute(0).second(0),
+        showSecond: false,
+        format: undefined,
+      });
+      expect(picker.state.open).not.to.be.ok();
+      const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
+        'rc-time-picker-input')[0];
+      let selector;
+      async.series([(next) => {
+        expect(picker.state.open).to.be(false);
+
+        Simulate.click(input);
+        setTimeout(next, 100);
+      }, (next) => {
+        expect(picker.state.open).to.be(true);
+        selector = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+          'rc-time-picker-panel-select');
+        expect((input).value).to.be('2:00 pm');
+
+        setTimeout(next, 100);
+      }, (next) => {
+        expect(selector.length).to.be(3);
+
+        next();
+      }], () => {
+        done();
+      });
+    });
+
+
+    it('renders 12am correctly', (done) => {
+      const picker = renderPicker({
+        use12Hours: true,
+        defaultValue: moment().hour(0).minute(0).second(0),
+        showSecond: false,
+        format: undefined,
+      });
+      expect(picker.state.open).not.to.be.ok();
+      const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
+        'rc-time-picker-input')[0];
+      let selector;
+      async.series([(next) => {
+        expect(picker.state.open).to.be(false);
+
+        Simulate.click(input);
+        setTimeout(next, 100);
+      }, (next) => {
+        expect(picker.state.open).to.be(true);
+        selector = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+          'rc-time-picker-panel-select');
+        setTimeout(next, 100);
+      }, (next) => {
+        expect(selector.length).to.be(3);
+
+        next();
+      }], () => {
+        done();
+      });
+    });
+
+
+    it('renders 5am correctly', (done) => {
+      const picker = renderPicker({
+        use12Hours: true,
+        defaultValue: moment().hour(0).minute(0).second(0),
+        showSecond: false,
+        format: undefined,
+      });
+      expect(picker.state.open).not.to.be.ok();
+      const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
+        'rc-time-picker-input')[0];
+      let selector;
+      async.series([(next) => {
+        expect(picker.state.open).to.be(false);
+
+        Simulate.click(input);
+        setTimeout(next, 100);
+      }, (next) => {
+        expect(picker.state.open).to.be(true);
+        selector = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+          'rc-time-picker-panel-select')[0];
+        expect((input).value).to.be('12:00 am');
+        const option = selector.getElementsByTagName('li')[3];
+        Simulate.click(option);
+        setTimeout(next, 100);
+      }, (next) => {
+        expect((input).value).to.be('3:00 am');
+        next();
+      }], () => {
+        done();
+      });
+    });
+
+
+    it('renders 12am/pm correctly', (done) => {
+      const picker = renderPicker({
+        use12Hours: true,
+        defaultValue: moment().hour(0).minute(0).second(0),
+        showSecond: false,
+        format: undefined,
+      });
+      expect(picker.state.open).not.to.be.ok();
+      const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
+        'rc-time-picker-input')[0];
+      let selector;
+      async.series([(next) => {
+        expect(picker.state.open).to.be(false);
+
+        Simulate.click(input);
+        setTimeout(next, 100);
+      }, (next) => {
+        expect(picker.state.open).to.be(true);
+        selector = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+          'rc-time-picker-panel-select')[2];
+        expect((input).value).to.be('12:00 am');
+        const option = selector.getElementsByTagName('li')[1];
+        Simulate.click(option);
+        setTimeout(next, 200);
+      }, (next) => {
+        expect((input).value).to.be('12:00 pm');
         next();
-      }], function () {
+      }, (next) => {
+        Simulate.click(selector.getElementsByTagName('li')[0]);
+        setTimeout(next, 200);
+      }, (next) => {
+        expect((input).value).to.be('12:00 am');
+        next();
+      }], () => {
         done();
       });
     });
 
+    it('renders uppercase AM correctly', (done) => {
+      const picker = renderPicker({
+        use12Hours: true,
+        defaultValue: moment().hour(0).minute(0).second(0),
+        showSecond: false,
+        format: 'h:mm A',
+      });
+      expect(picker.state.open).not.to.be.ok();
+      const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
+        'rc-time-picker-input')[0];
+      let selector;
+      async.series([(next) => {
+        expect(picker.state.open).to.be(false);
+
+        Simulate.click(input);
+        setTimeout(next, 100);
+      }, (next) => {
+        expect(picker.state.open).to.be(true);
+        selector = TestUtils.scryRenderedDOMComponentsWithClass(picker.panelInstance,
+          'rc-time-picker-panel-select')[2];
+        expect((input).value).to.be('12:00 AM');
+        const option = selector.getElementsByTagName('li')[1];
+        Simulate.click(option);
+        setTimeout(next, 200);
+      }, (next) => {
+        expect((input).value).to.be('12:00 PM');
+        next();
+      }, (next) => {
+        Simulate.click(selector.getElementsByTagName('li')[0]);
+        setTimeout(next, 200);
+      }, (next) => {
+        expect((input).value).to.be('12:00 AM');
+        next();
+      }], () => {
+        done();
+      });
+    });
   });
 });