From c9d9e316ee52c24d73d7aa8f0830fdc8a1da1761 Mon Sep 17 00:00:00 2001 From: nadchif Date: Mon, 15 Nov 2021 00:22:39 +0800 Subject: [PATCH] - added unit tests - fixed issue of picker not increasing value when seconds are hidden --- spec/html-duration-picker.spec.js | 66 ++++++++++++++++++++++++++++++- src/html-duration-picker.js | 2 +- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/spec/html-duration-picker.spec.js b/spec/html-duration-picker.spec.js index 26315c4..707d0c7 100644 --- a/spec/html-duration-picker.spec.js +++ b/spec/html-duration-picker.spec.js @@ -30,7 +30,7 @@ describe('Duration Picker', () => { it('should be less than 60', () => { expect(Number(sectioned[1])).toBeLessThan(60); }); - it('should be more than 0', () => { + it('should be more than -1', () => { expect(Number(sectioned[1])).toBeGreaterThan(-1); }); }); @@ -61,6 +61,70 @@ describe('Duration Picker', () => { }); }); + describe('default mode arrow buttons are clicked 2x', () => { + let testPicker; + beforeEach(() => { + const dom = new JSDOM(` + + + + `); + global.document = dom.window.document; + global.window = dom.window; + + testPicker = document.querySelector('.html-duration-picker'); + HtmlDurationPicker.init(); + }); + const simulateEvent = (event, node) => { + const evt = document.createEvent('HTMLEvents'); + evt.initEvent(event, false, true); + node.dispatchEvent(evt); + }; + it('should increase hh to 2', () => { + testPicker.focus(); + const controls = testPicker.nextSibling; + // 1 + simulateEvent('mousedown', controls.childNodes[0]); + simulateEvent('mouseup', controls.childNodes[0]); + // 2 + simulateEvent('mousedown', controls.childNodes[0]); + simulateEvent('mouseup', controls.childNodes[0]); + expect(testPicker.value).toEqual('02:00:00'); + }); + }); + + describe('data-hide-seconds mode arrow buttons are clicked 2x', () => { + let testPicker; + beforeEach(() => { + const dom = new JSDOM(` + + + + `); + global.document = dom.window.document; + global.window = dom.window; + + testPicker = document.querySelector('.html-duration-picker'); + HtmlDurationPicker.init(); + }); + const simulateEvent = (event, node) => { + const evt = document.createEvent('HTMLEvents'); + evt.initEvent(event, false, true); + node.dispatchEvent(evt); + }; + it('should increase hh to 2', () => { + testPicker.focus(); + const controls = testPicker.nextSibling; + // 1 + simulateEvent('mousedown', controls.childNodes[0]); + simulateEvent('mouseup', controls.childNodes[0]); + // 2 + simulateEvent('mousedown', controls.childNodes[0]); + simulateEvent('mouseup', controls.childNodes[0]); + expect(testPicker.value).toEqual('02:00'); + }); + }); + describe('with min value and duration', () => { let testPicker; beforeEach(() => { diff --git a/src/html-duration-picker.js b/src/html-duration-picker.js index a958b42..df437bf 100644 --- a/src/html-duration-picker.js +++ b/src/html-duration-picker.js @@ -264,7 +264,7 @@ export default (function () { * @return {Number} */ const durationToSeconds = (value) => { - if (!isValidDurationFormat(value)) { + if (!(isValidDurationFormat(value) || isValidDurationFormat(value, true))) { return 0; } const sectioned = value.split(':');