diff --git a/packages/test-utils/src/wrapper.js b/packages/test-utils/src/wrapper.js index 6ea73b2cf..f93295386 100644 --- a/packages/test-utils/src/wrapper.js +++ b/packages/test-utils/src/wrapper.js @@ -711,7 +711,17 @@ export default class Wrapper implements BaseWrapper { // $FlowIgnore : Problem with possibly null this.vm this.vm.$forceUpdate() - return nextTick() + return new Promise(resolve => { + nextTick().then(() => { + const isUpdated = Object.keys(data).some(key => { + return ( + // $FlowIgnore : Problem with possibly null this.vm + this.vm[key] === data[key] || this.vm.$attrs[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..ebd54cc17 100644 --- a/test/specs/wrapper/setProps.spec.js +++ b/test/specs/wrapper/setProps.spec.js @@ -187,6 +187,81 @@ describeWithShallowAndMount('setProps', mountingMethod => { await wrapper.setProps({ prop1 }) expect(wrapper.vm.prop2).to.equal(prop1) }) + + it('invokes watchers with immediate set to "true"', 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('invokes watchers with immediate set to "true" with deep objects', async () => { + const callback = sinon.spy() + const TestComponent = { + template: '
', + props: ['propA'], + mounted() { + this.$watch( + 'propA', + function() { + callback() + }, + { immediate: true } + ) + } + } + const wrapper = mountingMethod(TestComponent, { + propsData: { + propA: { + key: { + nestedKey: 'value' + }, + key2: 'value2' + } + } + }) + + expect(callback.calledOnce) + callback.resetHistory() + + await wrapper.setProps({ + propA: { + key: { + nestedKey: 'newValue', + anotherNestedKey: 'value' + }, + key2: 'value2' + } + }) + expect(wrapper.props().propA).to.deep.equal({ + key: { + nestedKey: 'newValue', + anotherNestedKey: 'value' + }, + key2: 'value2' + }) + expect(callback.calledOnce) + }) }) it('props and setProps should return the same reference when called with same object', () => {