From 5c399217429d25cca9c1b3cd9cb5c4ac073be275 Mon Sep 17 00:00:00 2001 From: Li Chuangbo Date: Thu, 18 Jan 2018 16:37:36 +1300 Subject: [PATCH] [fix] Stepper: not fire event on changing the value prop #545 (#546) --- packages/stepper/index.vue | 12 +++++++----- test/unit/specs/stepper.spec.js | 20 +++++++++++++++++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/packages/stepper/index.vue b/packages/stepper/index.vue index 947b0743d..bc09ab782 100644 --- a/packages/stepper/index.vue +++ b/packages/stepper/index.vue @@ -77,11 +77,6 @@ export default create({ }, watch: { - currentValue(val) { - this.$emit('input', val); - this.$emit('change', val); - }, - value(val) { val = this.correctValue(+val); if (val !== this.currentValue) { @@ -105,6 +100,7 @@ export default create({ onInput(event) { const val = +event.target.value; this.currentValue = this.correctValue(val); + this.emitInput(); }, onChange(type) { @@ -116,7 +112,13 @@ export default create({ const step = +this.step; const currentValue = +this.currentValue; this.currentValue = type === 'minus' ? (currentValue - step) : (currentValue + step); + this.emitInput(); this.$emit(type); + }, + + emitInput() { + this.$emit('input', this.currentValue); + this.$emit('change', this.currentValue); } } }); diff --git a/test/unit/specs/stepper.spec.js b/test/unit/specs/stepper.spec.js index 9da978e63..2c67b50f8 100644 --- a/test/unit/specs/stepper.spec.js +++ b/test/unit/specs/stepper.spec.js @@ -79,13 +79,11 @@ describe('Stepper', () => { }); expect(wrapper.hasClass('van-stepper')).to.be.true; - const eventStub = sinon.stub(wrapper.vm, '$emit'); wrapper.vm.value = 2; wrapper.update(); wrapper.vm.$nextTick(() => { - expect(eventStub.calledWith('input')); - expect(eventStub.calledWith('change')); + expect(wrapper.vm.currentValue).to.equal(2); done(); }); }); @@ -135,4 +133,20 @@ describe('Stepper', () => { done(); }); }); + + it('should not fire any event on props changed', (done) => { + wrapper = mount(Stepper, { + propsData: { + value: 1 + } + }); + const eventStub = sinon.stub(wrapper.vm, '$emit'); + + wrapper.vm.value = 2; + wrapper.update(); + wrapper.vm.$nextTick(() => { + expect(eventStub.called).to.be.false; + done(); + }); + }); });