Skip to content
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

Adding method to set data object #563

Closed
shortdiv opened this issue Apr 25, 2018 · 5 comments
Closed

Adding method to set data object #563

shortdiv opened this issue Apr 25, 2018 · 5 comments

Comments

@shortdiv
Copy link
Contributor

What problem does this feature solve?

Currently, setData only works with setting single data values, but it doesn't allow for setting objects. For example, given { text : { message: 'null' } }, I can't set the data with setData without mutating the entire object; i.e. setData({ text: { message: 'hello' } }).

This is problematic because I might want to append a k/v pair to an object while still preserving existing k/v pairs. While this functionality is available via "vanilla vue" through wrapper.vm.$set(obj, key, value), it feels like an inconsistency given that you can set data using setData otherwise.

What does the proposed API look like?

The proposed API adds a setDataObj method to vue-test-utils thereby allowing you to set data if updating an object rather than a value. It would be very similar to Vue.set or vm.$set but be more consistent with how data is currently set in tests.

setDataObj(obj, key, value)
@shortdiv
Copy link
Contributor Author

I implemented a proof of concept, to make sure this could work -> shortdiv@ca44136

@eddyerburgh
Copy link
Member

This is a good idea.

Instead of adding a new method, I think we should change the behavior of setData to not overwrite objects.

@shortdiv
Copy link
Contributor Author

Sweet! I'll update my code and submit a PR for this

@johnyluyte
Copy link

Hello,

Using 1.0.0-beta.16, I'm having similar problem with setData(), not with Object, but with Arrays.

My Component:

data() {
  return {
    items: [
      { id: 1, name: "Apple" },
      { id: 2, name: "Banana" }
    ]
  }
}

Testing:

wrapper.setData({
  items: []
})
// What's wrong: Does not clear the items array
// wrapper.vm.items
// { id: 1, name: "Apple" },
// { id: 2, name: "Banana" }

wrapper.setData({
  items: [
    { id: 1, name: "SuperWatermelon" }
  ]
})
// What's wrong: Modified the 1st item, but didn't remove the 2nd item.
// wrapper.vm.items
// { id: 1, name: "SuperWatermelon" },
// { id: 2, name: "Banana" }

wrapper.setData({
  items: [
    { id: 1, name: "Dog" }
    { id: 2, name: "Cat" }
  ]
})
// What's wrong: Nothing, this one is successful
// wrapper.vm.itees
// { id: 1, name: "Dog" },
// { id: 2, name: "Cat" }

wrapper.setData({
  items: [
    { id: 1, name: "Dog" }
    { id: 2, name: "Cat" },
    { id: 3, name: "Mouse" },
    { id: 4, name: "Horse" },
  ]
})
// What's wrong: The 1st and 2nd items are modified, but didn't add 3rd and 4th items
// wrapper.vm.items
// { id: 1, name: "Dog" },
// { id: 2, name: "Cat" }

If I move the items variable from data() to props, and use setProps() instead of setData(). Then everything will work as expected.

So I suppose this has something to do with Vue Component Reactivity.

Has this been fixed in #565 ? or should I open another issue? or is this an intended behavior? Thanks you.

@lchanmann
Copy link
Contributor

@johnyluyte, I observed the same. A probable fix is on the way #604.

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

No branches or pull requests

4 participants