Skip to content

Commit 9a3e6f9

Browse files
fix(setprops): allowed for setProps to be synced with nextTick intervals (#1618)
* 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 * Update packages/test-utils/src/wrapper.js Co-authored-by: Bill Glesias <[email protected]> Co-authored-by: Lachlan <[email protected]>
1 parent cc4619e commit 9a3e6f9

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

Diff for: packages/test-utils/src/wrapper.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,17 @@ export default class Wrapper implements BaseWrapper {
711711

712712
// $FlowIgnore : Problem with possibly null this.vm
713713
this.vm.$forceUpdate()
714-
return nextTick()
714+
return new Promise(resolve => {
715+
nextTick().then(() => {
716+
const isUpdated = Object.keys(data).some(key => {
717+
return (
718+
// $FlowIgnore : Problem with possibly null this.vm
719+
this.vm[key] === data[key] || this.vm.$attrs[key] === data[key]
720+
)
721+
})
722+
return !isUpdated ? this.setProps(data).then(resolve()) : resolve()
723+
})
724+
})
715725
} catch (err) {
716726
throw err
717727
} finally {

Diff for: test/specs/wrapper/setProps.spec.js

+75
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,81 @@ describeWithShallowAndMount('setProps', mountingMethod => {
187187
await wrapper.setProps({ prop1 })
188188
expect(wrapper.vm.prop2).to.equal(prop1)
189189
})
190+
191+
it('invokes watchers with immediate set to "true"', async () => {
192+
const callback = sinon.spy()
193+
const TestComponent = {
194+
template: '<div />',
195+
props: ['propA'],
196+
mounted() {
197+
this.$watch(
198+
'propA',
199+
function() {
200+
callback()
201+
},
202+
{ immediate: true }
203+
)
204+
}
205+
}
206+
const wrapper = mountingMethod(TestComponent, {
207+
propsData: { propA: 'none' }
208+
})
209+
210+
expect(callback.calledOnce)
211+
callback.resetHistory()
212+
213+
await wrapper.setProps({ propA: 'value' })
214+
expect(wrapper.props().propA).to.equal('value')
215+
expect(callback.calledOnce)
216+
})
217+
218+
it('invokes watchers with immediate set to "true" with deep objects', async () => {
219+
const callback = sinon.spy()
220+
const TestComponent = {
221+
template: '<div />',
222+
props: ['propA'],
223+
mounted() {
224+
this.$watch(
225+
'propA',
226+
function() {
227+
callback()
228+
},
229+
{ immediate: true }
230+
)
231+
}
232+
}
233+
const wrapper = mountingMethod(TestComponent, {
234+
propsData: {
235+
propA: {
236+
key: {
237+
nestedKey: 'value'
238+
},
239+
key2: 'value2'
240+
}
241+
}
242+
})
243+
244+
expect(callback.calledOnce)
245+
callback.resetHistory()
246+
247+
await wrapper.setProps({
248+
propA: {
249+
key: {
250+
nestedKey: 'newValue',
251+
anotherNestedKey: 'value'
252+
},
253+
key2: 'value2'
254+
}
255+
})
256+
expect(wrapper.props().propA).to.deep.equal({
257+
key: {
258+
nestedKey: 'newValue',
259+
anotherNestedKey: 'value'
260+
},
261+
key2: 'value2'
262+
})
263+
expect(callback.calledOnce)
264+
})
190265
})
191266

192267
it('props and setProps should return the same reference when called with same object', () => {

0 commit comments

Comments
 (0)