Skip to content

Commit cbf1bd4

Browse files
xanfJessicaSachs
authored andcommitted
fix: add support for proper props update in async mode
Since we're already wrapping a component into a parent one we can get rid of explicitly setting props on component, instead modifying them being passed from parent component instead
1 parent 689d76b commit cbf1bd4

File tree

2 files changed

+18
-29
lines changed

2 files changed

+18
-29
lines changed

Diff for: packages/create-instance/create-instance.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import createScopedSlots from './create-scoped-slots'
1111
import { createStubsFromStubsObject } from './create-component-stubs'
1212
import { patchCreateElement } from './patch-create-element'
1313

14-
function createContext(options, scopedSlots) {
14+
function createContext(options, scopedSlots, currentProps) {
1515
const on = {
1616
...(options.context && options.context.on),
1717
...options.listeners
@@ -21,7 +21,7 @@ function createContext(options, scopedSlots) {
2121
...options.attrs,
2222
// pass as attrs so that inheritAttrs works correctly
2323
// propsData should take precedence over attrs
24-
...options.propsData
24+
...currentProps
2525
},
2626
...(options.context || {}),
2727
on,
@@ -98,10 +98,16 @@ export default function createInstance(
9898
parentComponentOptions._provided = options.provide
9999
parentComponentOptions.$_doNotStubChildren = true
100100
parentComponentOptions._isFunctionalContainer = componentOptions.functional
101+
const originalDataFn = parentComponentOptions.data
102+
parentComponentOptions.data = function() {
103+
const originalData = originalDataFn ? originalDataFn() : {}
104+
originalData.vueTestUtils_childProps = { ...options.propsData }
105+
return originalData
106+
}
101107
parentComponentOptions.render = function(h) {
102108
return h(
103109
Constructor,
104-
createContext(options, scopedSlots),
110+
createContext(options, scopedSlots, this.vueTestUtils_childProps),
105111
createChildren(this, h, options)
106112
)
107113
}

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

+9-26
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
// @flow
2-
3-
import Vue from 'vue'
42
import pretty from 'pretty'
53
import getSelector from './get-selector'
64
import { REF_SELECTOR, FUNCTIONAL_OPTIONS, VUE_VERSION } from 'shared/consts'
7-
import config from './config'
85
import WrapperArray from './wrapper-array'
96
import ErrorWrapper from './error-wrapper'
107
import { throwError, getCheckedEvent, isPhantomJS } from 'shared/util'
@@ -478,8 +475,6 @@ export default class Wrapper implements BaseWrapper {
478475
* Sets vm props
479476
*/
480477
setProps(data: Object): void {
481-
const originalConfig = Vue.config.silent
482-
Vue.config.silent = config.silent
483478
if (this.isFunctionalComponent) {
484479
throwError(
485480
`wrapper.setProps() cannot be called on a functional component`
@@ -503,38 +498,26 @@ export default class Wrapper implements BaseWrapper {
503498
)
504499
}
505500
if (
506-
!this.vm ||
507-
!this.vm.$options._propKeys ||
508-
!this.vm.$options._propKeys.some(prop => prop === key)
501+
VUE_VERSION <= 2.3 &&
502+
// $FlowIgnore : Problem with possibly null this.vm
503+
(!this.vm.$options._propKeys ||
504+
!this.vm.$options._propKeys.some(prop => prop === key))
509505
) {
510-
if (VUE_VERSION > 2.3) {
511-
// $FlowIgnore : Problem with possibly null this.vm
512-
this.vm.$attrs[key] = data[key]
513-
return
514-
}
515506
throwError(
516507
`wrapper.setProps() called with ${key} property which ` +
517508
`is not defined on the component`
518509
)
519510
}
520511

521-
if (this.vm && this.vm._props) {
522-
// Set actual props value
523-
this.vm._props[key] = data[key]
524-
// $FlowIgnore : Problem with possibly null this.vm
525-
this.vm[key] = data[key]
526-
} else {
527-
// $FlowIgnore : Problem with possibly null this.vm.$options
528-
this.vm.$options.propsData[key] = data[key]
529-
// $FlowIgnore : Problem with possibly null this.vm
530-
this.vm[key] = data[key]
531-
// $FlowIgnore : Need to call this twice to fix watcher bug in 2.0.x
532-
this.vm[key] = data[key]
512+
if (this.vm && this.vm.$parent) {
513+
this.vm.$parent.vueTestUtils_childProps[key] = data[key]
533514
}
534515
})
535516
// $FlowIgnore : Problem with possibly null this.vm
536517
this.vm.$forceUpdate()
537-
Vue.config.silent = originalConfig
518+
// We need to explicitly trigger parent watcher to support sync scenarios
519+
// $FlowIgnore : Problem with possibly null this.vm
520+
this.vm.$parent._watcher.run()
538521
}
539522

540523
/**

0 commit comments

Comments
 (0)