Skip to content

Commit 71233ea

Browse files
committed
Fix: touch events
* Added touch events handlers for controls button * Added unit tests for touch events
1 parent 0e98bb1 commit 71233ea

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/components/VueNumberInput.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
@mouseup="buttonUpHandler"
2121
@keydown.enter="buttonDownHandler('dec')"
2222
@keyup.enter="buttonUpHandler"
23+
@touchstart.prevent="buttonDownHandler('dec')"
24+
@touchend="buttonUpHandler"
2325
>
2426
<slot name="button-decrease">
2527
<VueNumberInputButton :type="'dec'" />
@@ -63,6 +65,8 @@
6365
@mouseup="buttonUpHandler"
6466
@keydown.enter="buttonDownHandler('inc')"
6567
@keyup.enter="buttonUpHandler"
68+
@touchstart.prevent="buttonDownHandler('inc')"
69+
@touchend="buttonUpHandler"
6670
>
6771
<slot name="button-increase">
6872
<VueNumberInputButton :type="'inc'" />
@@ -254,6 +258,7 @@ export default {
254258
: this.value - this.step
255259
);
256260
// After 500 ms will start value increasing process
261+
/* istanbul ignore next */
257262
this.timeoutId = setTimeout(
258263
(this.intervalId = setInterval(
259264
() =>

tests/unit/vuenumberinput.spec.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,33 @@ describe('Tests for VueNumberInput.vue component', () => {
193193
setTimeout.mockClear();
194194
});
195195

196+
it('buttonDownHandler method should invoke setTimeout function', () => {
197+
wrapper.setMethods({
198+
makeStep: jest.fn()
199+
});
200+
wrapper.vm.buttonDownHandler('inc');
201+
expect(setTimeout).toHaveBeenCalledWith(
202+
expect.any(Number),
203+
expect.any(Number)
204+
);
205+
setInterval.mockClear();
206+
setTimeout.mockClear();
207+
});
208+
209+
it('buttonDownHandler method should invoke setInterval function', () => {
210+
wrapper.setMethods({
211+
makeStep: jest.fn()
212+
});
213+
wrapper.vm.buttonDownHandler('inc');
214+
expect(setInterval).toHaveBeenCalledWith(
215+
expect.any(Function),
216+
expect.any(Number)
217+
);
218+
expect(wrapper.vm.makeStep).toHaveBeenCalledWith(3);
219+
setInterval.mockClear();
220+
setTimeout.mockClear();
221+
});
222+
196223
it('Component should have buttonUpHandler method and it should invoke clearTimeout and clearInterval', () => {
197224
wrapper.setData({ intervalId: 1, timeoutId: 2 });
198225
expect(typeof wrapper.vm.buttonUpHandler).toBe('function');
@@ -605,6 +632,22 @@ describe('Tests for VueNumberInput.vue component', () => {
605632
expect(wrapper.vm.buttonDownHandler).lastCalledWith('inc');
606633
});
607634

635+
it('Touchstart event on .vue-number-input__btn-inc should invoke methods.buttonDownHandler', () => {
636+
wrapper.setMethods({ buttonDownHandler: jest.fn() });
637+
wrapper
638+
.find('.vue-number-input__btn-inc')
639+
.trigger('touchstart');
640+
expect(wrapper.vm.buttonDownHandler).toHaveBeenCalledWith(
641+
'inc'
642+
);
643+
});
644+
645+
it('Touchend event on .vue-number-input__btn-inc should invoke methods.buttonUpHandler', () => {
646+
wrapper.setMethods({ buttonUpHandler: jest.fn() });
647+
wrapper.find('.vue-number-input__btn-inc').trigger('touchend');
648+
expect(wrapper.vm.buttonUpHandler).toHaveBeenCalled();
649+
});
650+
608651
it('Click on .vue-number-input__btn-dec should invoke methods.buttonDownHandler "dec" argument', () => {
609652
wrapper.setMethods({ buttonDownHandler: jest.fn() });
610653
wrapper
@@ -630,6 +673,22 @@ describe('Tests for VueNumberInput.vue component', () => {
630673
'dec'
631674
);
632675
});
676+
677+
it('Touchstart event on .vue-number-input__btn-dec should invoke methods.buttonDownHandler', () => {
678+
wrapper.setMethods({ buttonDownHandler: jest.fn() });
679+
wrapper
680+
.find('.vue-number-input__btn-dec')
681+
.trigger('touchstart');
682+
expect(wrapper.vm.buttonDownHandler).toHaveBeenCalledWith(
683+
'dec'
684+
);
685+
});
686+
687+
it('Touchend event on .vue-number-input__btn-dec should invoke methods.buttonUpHandler', () => {
688+
wrapper.setMethods({ buttonUpHandler: jest.fn() });
689+
wrapper.find('.vue-number-input__btn-dec').trigger('touchend');
690+
expect(wrapper.vm.buttonUpHandler).toHaveBeenCalled();
691+
});
633692
});
634693
});
635694
});

0 commit comments

Comments
 (0)