From ca5645640cd9bbd16217d3c95c6faa0bcd542079 Mon Sep 17 00:00:00 2001 From: Bill Date: Tue, 21 Jul 2020 23:10:51 -0400 Subject: [PATCH] fix(setprops): allowed for setProps to be synced with nextTick intervals setProps in certain cases was being blown away by nextTick intervals. If the property is not up to date, setProps will be called again to sync the changes. fix #1419 --- packages/test-utils/src/wrapper.js | 10 +++++++++- test/specs/wrapper/setProps.spec.js | 27 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/packages/test-utils/src/wrapper.js b/packages/test-utils/src/wrapper.js index 6ea73b2cf..73a9b0247 100644 --- a/packages/test-utils/src/wrapper.js +++ b/packages/test-utils/src/wrapper.js @@ -711,7 +711,15 @@ export default class Wrapper implements BaseWrapper { // $FlowIgnore : Problem with possibly null this.vm this.vm.$forceUpdate() - return nextTick() + return new Promise((resolve, reject) => { + nextTick().then(() => { + const isUpdated = Object.keys(data).some(key => { + // $FlowIgnore : Problem with possibly null this.vm + return this.vm[key] === data[key] + }) + return !isUpdated ? this.setProps(data).then(resolve()) : resolve() + }) + }) } catch (err) { throw err } finally { diff --git a/test/specs/wrapper/setProps.spec.js b/test/specs/wrapper/setProps.spec.js index 635ffae66..db5963d04 100644 --- a/test/specs/wrapper/setProps.spec.js +++ b/test/specs/wrapper/setProps.spec.js @@ -187,6 +187,33 @@ describeWithShallowAndMount('setProps', mountingMethod => { await wrapper.setProps({ prop1 }) expect(wrapper.vm.prop2).to.equal(prop1) }) + + it('invokes watchers with immediate set to "true" when ', async () => { + const callback = sinon.spy() + const TestComponent = { + template: '
', + props: ['propA'], + mounted() { + this.$watch( + 'propA', + function() { + callback() + }, + { immediate: true } + ) + } + } + const wrapper = mountingMethod(TestComponent, { + propsData: { propA: 'none' } + }) + + expect(callback.calledOnce) + callback.resetHistory() + + await wrapper.setProps({ propA: 'value' }) + expect(wrapper.props().propA).to.equal('value') + expect(callback.calledOnce) + }) }) it('props and setProps should return the same reference when called with same object', () => {