Skip to content

Change the way of setting props #1300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/create-instance/create-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import createScopedSlots from './create-scoped-slots'
import { createStubsFromStubsObject } from './create-component-stubs'
import { patchCreateElement } from './patch-create-element'

function createContext(options, scopedSlots) {
function createContext(options, scopedSlots, currentProps) {
const on = {
...(options.context && options.context.on),
...options.listeners
Expand All @@ -21,7 +21,7 @@ function createContext(options, scopedSlots) {
...options.attrs,
// pass as attrs so that inheritAttrs works correctly
// propsData should take precedence over attrs
...options.propsData
...currentProps
},
...(options.context || {}),
on,
Expand Down Expand Up @@ -98,10 +98,16 @@ export default function createInstance(
parentComponentOptions._provided = options.provide
parentComponentOptions.$_doNotStubChildren = true
parentComponentOptions._isFunctionalContainer = componentOptions.functional
const originalDataFn = parentComponentOptions.data
parentComponentOptions.data = function() {
const originalData = originalDataFn ? originalDataFn() : {}
originalData.vueTestUtils_childProps = { ...options.propsData }
return originalData
}
parentComponentOptions.render = function(h) {
return h(
Constructor,
createContext(options, scopedSlots),
createContext(options, scopedSlots, this.vueTestUtils_childProps),
createChildren(this, h, options)
)
}
Expand Down
35 changes: 9 additions & 26 deletions packages/test-utils/src/wrapper.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// @flow

import Vue from 'vue'
import pretty from 'pretty'
import getSelector from './get-selector'
import { REF_SELECTOR, FUNCTIONAL_OPTIONS, VUE_VERSION } from 'shared/consts'
import config from './config'
import WrapperArray from './wrapper-array'
import ErrorWrapper from './error-wrapper'
import { throwError, getCheckedEvent, isPhantomJS } from 'shared/util'
Expand Down Expand Up @@ -478,8 +475,6 @@ export default class Wrapper implements BaseWrapper {
* Sets vm props
*/
setProps(data: Object): void {
const originalConfig = Vue.config.silent
Vue.config.silent = config.silent
if (this.isFunctionalComponent) {
throwError(
`wrapper.setProps() cannot be called on a functional component`
Expand All @@ -503,38 +498,26 @@ export default class Wrapper implements BaseWrapper {
)
}
if (
!this.vm ||
!this.vm.$options._propKeys ||
!this.vm.$options._propKeys.some(prop => prop === key)
VUE_VERSION <= 2.3 &&
// $FlowIgnore : Problem with possibly null this.vm
(!this.vm.$options._propKeys ||
!this.vm.$options._propKeys.some(prop => prop === key))
) {
if (VUE_VERSION > 2.3) {
// $FlowIgnore : Problem with possibly null this.vm
this.vm.$attrs[key] = data[key]
return
}
throwError(
`wrapper.setProps() called with ${key} property which ` +
`is not defined on the component`
)
}

if (this.vm && this.vm._props) {
// Set actual props value
this.vm._props[key] = data[key]
// $FlowIgnore : Problem with possibly null this.vm
this.vm[key] = data[key]
} else {
// $FlowIgnore : Problem with possibly null this.vm.$options
this.vm.$options.propsData[key] = data[key]
// $FlowIgnore : Problem with possibly null this.vm
this.vm[key] = data[key]
// $FlowIgnore : Need to call this twice to fix watcher bug in 2.0.x
this.vm[key] = data[key]
if (this.vm && this.vm.$parent) {
this.vm.$parent.vueTestUtils_childProps[key] = data[key]
}
})
// $FlowIgnore : Problem with possibly null this.vm
this.vm.$forceUpdate()
Vue.config.silent = originalConfig
// We need to explicitly trigger parent watcher to support sync scenarios
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this, sync mode is gone now.

// $FlowIgnore : Problem with possibly null this.vm
this.vm.$parent._watcher.run()
}

/**
Expand Down
22 changes: 22 additions & 0 deletions test/resources/components/component-with-watch-immediate.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script>
export default {
props: ['prop1'],
data: function() {
return {
data1: null
}
},

watch: {
prop1: {
handler() {
this.data1 = this.prop1
},
immediate: true
}
}
}
</script>
<template>
<h1>test</h1>
</template>
4 changes: 1 addition & 3 deletions test/specs/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ describeWithShallowAndMount('config', mountingMethod => {
localVue
})
expect(wrapper.vm.prop1).to.equal('example')
wrapper.setProps({
prop1: 'new value'
})
wrapper.vm.prop1 = 'new value'
expect(console.error).calledWith(sandbox.match('[Vue warn]'))
})
})
12 changes: 12 additions & 0 deletions test/specs/wrapper/setProps.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { compileToFunctions } from 'vue-template-compiler'
import ComponentWithProps from '~resources/components/component-with-props.vue'
import ComponentWithWatch from '~resources/components/component-with-watch.vue'
import ComponentWithWatchImmediate from '~resources/components/component-with-watch-immediate.vue'
import { describeWithShallowAndMount, vueVersion } from '~resources/utils'
import { itDoNotRunIf } from 'conditional-specs'
import Vue from 'vue'
Expand Down Expand Up @@ -244,6 +245,17 @@ describeWithShallowAndMount('setProps', mountingMethod => {
expect(wrapper.vm.propA).to.equal('value')
})

it('correctly sets props in async mode when component has immediate watchers', async () => {
const wrapper = mountingMethod(ComponentWithWatchImmediate, {
sync: false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be missing something here, but I think sync is gone as per #1141?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct.

Copy link
Collaborator

@JessicaSachs JessicaSachs Jan 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pulled it out of the src. That's a typo. Thanks for the catch

})

const prop1 = 'testest'
wrapper.setProps({ prop1 })
await Vue.nextTick()
expect(wrapper.vm.prop1).to.equal(prop1)
})

it('throws an error if node is not a Vue instance', () => {
const message = 'wrapper.setProps() can only be called on a Vue instance'
const compiled = compileToFunctions('<div><p></p></div>')
Expand Down