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

Conversation

mya-ake
Copy link
Contributor

@mya-ake mya-ake commented Jul 1, 2018

Related: #761

Changes

  • Changed to pass clones to props of createInstance in case of Object
  • As a result of this change, test of mount.spec.js has been updated

// 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.)

@@ -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.

@mya-ake
Copy link
Contributor Author

mya-ake commented Jul 1, 2018

@eddyerburgh

Changes

  • To clone data when it is an object.
  • It does not affect other tests

@eddyerburgh
Copy link
Member

This is a good solution, but it still suffers from this problem:

it('setProps removes reference', () => {
  const TestComponent = {
    template: `<div val="value"></div>`,
    props: ['value']
  }
  const wrapper = mountingMethod(TestComponent)
  const value = {}
  wrapper.setProps({value})
  expect(wrapper.props().value).to.equal(value)
})

I think a better solution would be to throw an error if the object in setProps has the same reference, and make the user create a new object. That way we situations that might be confusing, like the above.

@mya-ake
Copy link
Contributor Author

mya-ake commented Jul 3, 2018

Hmmm, I can't think of a way to pass the test.
I will withdraw this Pull request and the next Pull request will throw an error when the same reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants