]> git.immae.eu Git - github/fretlink/time-picker.git/commitdiff
Add tests; fix the logic
authorVahagn Aharonian <vahagn.aharonian@gmail.com>
Thu, 1 Feb 2018 08:50:34 +0000 (12:50 +0400)
committerVahagn Aharonian <vahagn.aharonian@gmail.com>
Thu, 1 Feb 2018 08:50:34 +0000 (12:50 +0400)
src/Combobox.jsx
tests/Select.spec.jsx

index 18fd54c317c1fe64384dfe5406e9063c35b6d297..19fbd196e3d5f51654322ea316447c314e598d82 100644 (file)
@@ -90,8 +90,12 @@ class Combobox extends Component {
       hourOptionsAdj = [12].concat(hourOptions.filter(h => h < 12 && h > 0));
       hourAdj = (hour % 12) || 12;
 
-      if (!this.isAM() && Array.isArray(disabledOptions)) {
-        disabledOptions = disabledOptions.map(h => h - 12);
+      if (Array.isArray(disabledOptions)) {
+        if (this.isAM()) {
+          disabledOptions = disabledOptions.filter(h => h < 12).map(h => (h === 0 ? 12 : h));
+        } else {
+          disabledOptions = disabledOptions.map(h => (h === 12 ? 12 : h - 12));
+        }
       }
     } else {
       hourOptionsAdj = hourOptions;
index 2a15e7c0174892e44d1f6d67ee593e66b286b52e..f414c1804ef8f4211db0f4b8b40bf37899d68a85 100644 (file)
@@ -552,5 +552,125 @@ describe('Select', () => {
         done();
       });
     });
+
+    it('disabled correctly', done => {
+      let change;
+      const picker = renderPicker({
+        use12Hours: true,
+        format: undefined,
+        onChange(v) {
+          change = v;
+        },
+        disabledHours() {
+          return [0, 2, 6, 18, 12];
+        },
+        defaultValue: moment()
+          .hour(0)
+          .minute(0)
+          .second(0),
+        showSecond: false,
+      });
+      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('12:00 am');
+            expect(input.value).to.be('12:00 am');
+
+            const selector = TestUtils.scryRenderedDOMComponentsWithClass(
+              picker.panelInstance,
+              'rc-time-picker-panel-select',
+            )[0];
+            const option = selector.getElementsByTagName('li')[2];
+            Simulate.click(option);
+            setTimeout(next, 100);
+          },
+          next => {
+            expect(change).not.to.be.ok();
+            expect(header.value).to.be('12:00 am');
+            expect(input.value).to.be('12:00 am');
+            expect(picker.state.open).to.be.ok();
+
+            const selector = TestUtils.scryRenderedDOMComponentsWithClass(
+              picker.panelInstance,
+              'rc-time-picker-panel-select',
+            )[0];
+            const option = selector.getElementsByTagName('li')[5];
+            Simulate.click(option);
+            setTimeout(next, 100);
+          },
+          next => {
+            expect(change).to.be.ok();
+            expect(change.hour()).to.be(5);
+            expect(header.value).to.be('5:00 am');
+            expect(input.value).to.be('5:00 am');
+            expect(picker.state.open).to.be.ok();
+
+            const selector = TestUtils.scryRenderedDOMComponentsWithClass(
+              picker.panelInstance,
+              'rc-time-picker-panel-select',
+            )[2];
+            Simulate.click(selector.getElementsByTagName('li')[1]);
+
+            setTimeout(next, 200);
+            change = null;
+          },
+          next => {
+            expect(change).not.to.be.ok();
+            expect(header.value).to.be('5:00 pm');
+            expect(input.value).to.be('5:00 pm');
+            expect(picker.state.open).to.be.ok();
+
+            const selector = TestUtils.scryRenderedDOMComponentsWithClass(
+              picker.panelInstance,
+              'rc-time-picker-panel-select',
+            )[0];
+            const option = selector.getElementsByTagName('li')[0];
+            Simulate.click(option);
+            setTimeout(next, 100);
+          },
+          next => {
+            expect(change).not.to.be.ok();
+            expect(header.value).to.be('5:00 pm');
+            expect(input.value).to.be('5:00 pm');
+            expect(picker.state.open).to.be.ok();
+
+            const selector = TestUtils.scryRenderedDOMComponentsWithClass(
+              picker.panelInstance,
+              'rc-time-picker-panel-select',
+            )[0];
+            const option = selector.getElementsByTagName('li')[5];
+            Simulate.click(option);
+            setTimeout(next, 100);
+          },
+          next => {
+            expect(change).to.be.ok();
+            expect(change.hour()).to.be(17);
+            expect(header.value).to.be('5:00 pm');
+            expect(input.value).to.be('5:00 pm');
+            expect(picker.state.open).to.be.ok();
+
+            next();
+          },
+        ],
+        () => {
+          done();
+        },
+      );
+    });
   });
 });