Skip to content

fix: Support setProps runs computed and watcher when prop is object (#761) #787

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 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions packages/create-instance/add-props.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// @flow

// SetProps on object prop child changes trigger computed or watcher
// https://github.com/vuejs/vue-test-utils/issues/761
export function createProps (propsData: Object): Object {
return Object.keys(propsData).reduce((props, key) => {
Copy link
Member

Choose a reason for hiding this comment

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

Can you just use either the spread opertor (not sure if it will trigger reactivity on nested property objects), or lodash.merge, which we already use in setData.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, use lodash.merge.
(Using spread operators will no longer be instances of classes.)

const data = propsData[key]
if (
typeof data === 'object' &&
data !== null &&
!Array.isArray(data)
) {
props[key] = Object.assign(
Object.create(Object.getPrototypeOf(data)),
data
)
} else {
props[key] = data
}
return props
}, {})
}
6 changes: 5 additions & 1 deletion packages/create-instance/create-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { createSlotVNodes } from './add-slots'
import addMocks from './add-mocks'
import { createProps } from './add-props'
import { addEventLogger } from './log-events'
import { createComponentStubs } from 'shared/stub-components'
import { throwError, warn, vueVersion } from 'shared/util'
Expand Down Expand Up @@ -128,11 +129,14 @@ export default function createInstance (
const slots = options.slots
? createSlotVNodes(h, options.slots)
: undefined
const props = options.propsData
? createProps(options.propsData)
: undefined
return h(
Constructor,
{
ref: 'vm',
props: options.propsData,
props,
on: options.listeners,
attrs: options.attrs
},
Expand Down
4 changes: 2 additions & 2 deletions test/specs/mount.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'mount', () => {
const wrapper = mount(ComponentWithProps, { propsData: { prop1 }})
expect(wrapper.vm).to.be.an('object')
if (wrapper.vm.$props) {
expect(wrapper.vm.$props.prop1).to.equal(prop1)
expect(wrapper.vm.$props.prop1).to.deep.equal(prop1)
Copy link
Member

Choose a reason for hiding this comment

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

I was looking at this fix and I'm not sure we should do it because it changes this behavior. Another solution I could think of was to throw an error and tell the user to call setProps with a new object

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I confirmed the processing of Vue.js, but it was the same value. So, I thought it would be better not to use deep.

Another solution I could think of was to throw an error and tell the user to call setProps with a new object.

I think that is a good way. But I want to use the same object.
I'll look into a way to fix again.

} else {
expect(wrapper.vm.$options.propsData.prop1).to.equal(prop1)
expect(wrapper.vm.$options.propsData.prop1).to.deep.equal(prop1)
}
})

Expand Down
104 changes: 104 additions & 0 deletions test/specs/wrapper/setProps.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,110 @@ describeWithShallowAndMount('setProps', mountingMethod => {
expect(wrapper.vm.data).to.equal('1,2,3,4,5')
})

it('runs computed when prop is object', () => {
class TestClass {
constructor (text) {
this._text = text
}

get text () {
return this._text
}

set text (text) {
this._text = text
}
}

const TestComponent = {
template: `<div id="app">
<div class="shallow">{{ shallowObjText }}</div>
<div class="deep">{{ deepObjText }}</div>
<div class="class">{{ classText }}</div>
</div>`,
props: {
shallowObj: Object,
deepObj: Object,
classInstance: TestClass
},
computed: {
shallowObjText () {
return this.shallowObj.text
},
deepObjText () {
return this.deepObj.obj.text
},
classText () {
return this.classInstance.text
}
}
}

const shallowObj = {
text: 'initial shallow'
}
const deepObj = {
obj: {
text: 'initial deep'
}
}
const classInstance = new TestClass('initial class')
const wrapper = mountingMethod(TestComponent, {
propsData: { shallowObj, deepObj, classInstance }
})
const shallowWraper = wrapper.find('.shallow')
const deepWrapper = wrapper.find('.deep')
const classWrapper = wrapper.find('.class')

expect(shallowWraper.text()).to.equal('initial shallow')
expect(deepWrapper.text()).to.equal('initial deep')
expect(classWrapper.text()).to.equal('initial class')

shallowObj.text = 'updated shallow'
deepObj.obj.text = 'updated deep'
classInstance.text = 'updated class'
wrapper.setProps({ shallowObj, deepObj, classInstance })
expect(shallowWraper.text()).to.equal('updated shallow')
expect(deepWrapper.text()).to.equal('updated deep')
expect(classWrapper.text()).to.equal('updated class')
})

it('runs watcher when prop is object', () => {
const TestComponent = {
template: `<div id="app">
<div class="watch">{{ watchText }}</div>
</div>`,
props: {
obj: Object
},
data: () => ({
watchText: 'initial'
}),
watch: {
'obj.text': 'execute'
},
methods: {
execute () {
this.watchText = 'updated'
}
}
}

const obj = {
text: 'initial shallow'
}
const wrapper = mountingMethod(TestComponent, {
propsData: { obj }
})
const watchWrapper = wrapper.find('.watch')

expect(watchWrapper.text()).to.equal('initial')

obj.text = 'updated shallow'
wrapper.setProps({ obj })
expect(watchWrapper.text()).to.equal('updated')
})

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