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

refactor: Update setData method (address #563) #565

Merged
merged 3 commits into from Apr 27, 2018
Merged

Conversation

shortdiv
Copy link
Contributor

This is a refactor of the setData method to allow for setting data values and objects. The current implementation of setData only allows for setting values and not objects.

Addresses #563

@eddyerburgh
Copy link
Member

eddyerburgh commented Apr 26, 2018

Ah I'm so sorry, I wasn't clear in my comment!

I think we should keep the signature the same as it is currently. Instead, we could update the function to do a deep merge:

const Cmp = {
  data: () => ({
     anObject: {
       propA: 'a',
       propB: 'b'
     }
  }) 
}
const wrapper = mount(Cmp)
wrapper.setData({
  anObject: {
    propB: 'c'
  }
})
wrapper.vm.anObject.propA // 'a'
wrapper.vm.anObject.propB // 'c'

@shortdiv
Copy link
Contributor Author

Ah gotcha, will update accordingly!

@@ -421,7 +421,14 @@ export default class Wrapper implements BaseWrapper {

Object.keys(data).forEach((key) => {
// $FlowIgnore : Problem with possibly null this.vm
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that this line should be removed.

if (typeof data[key] === typeof {} && data[key] !== null) {
Object.keys(data[key]).forEach((key2) => {
var newObj = Object.assign({}, data[key], data[key][key2])
this.vm.$set(this.vm, [key], newObj)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is necessary to add the following line before this line.
// $FlowIgnore : Problem with possibly null this.vm

this.vm.$set(this.vm, [key], newObj)
})
} else {
this.vm.$set(this.vm, [key], data[key])
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is necessary to add the following line before this line.
// $FlowIgnore : Problem with possibly null this.vm

this.vm.$set(this.vm, [key], data[key])
if (typeof data[key] === typeof {} && data[key] !== null) {
Object.keys(data[key]).forEach((key2) => {
var newObj = Object.assign({}, data[key], data[key][key2])
Copy link
Contributor

Choose a reason for hiding this comment

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

I think spread syntax is frequently used in this repository.
I think it is better to change var to const.

const newObj = { ...data[key], ...data[key][key2]}

@@ -421,7 +421,14 @@ export default class Wrapper implements BaseWrapper {

Object.keys(data).forEach((key) => {
// $FlowIgnore : Problem with possibly null this.vm
this.vm.$set(this.vm, [key], data[key])
if (typeof data[key] === typeof {} && data[key] !== null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you tell me why are you using typeof data[key] === typeof {}, not typeof data[key] === 'object' ?
I think typeof data[key] === 'object' is better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The null check was necessary because null is of type object. If you're updating an object with {message: null}, it will pass this conditional, which it technically shouldn't since it's not a nested object.

@38elements
Copy link
Contributor

It is necessary to execute yarn flow.

Object.keys(data[key]).forEach((key2) => {
var newObj = Object.assign({}, data[key], data[key][key2])
const newObj = { ...data[key], ...data[key][key2]}
Copy link
Contributor

Choose a reason for hiding this comment

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

It is necessary to add space.

const newObj = { ...data[key], ...data[key][key2] }

@38elements
Copy link
Contributor

It is necessary to execute yarn lint.

@@ -132,4 +132,25 @@ describeWithShallowAndMount('setData', (mountingMethod) => {
wrapper.setData({ message: null })
expect(wrapper.text()).to.equal('There is no message yet')
})
it('should update a data object', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is better to add a blank line before this line.

}
}
})
expect(wrapper.vm.anObject.propA.prop1).to.equal('c')
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 give propA another property, and add an assertion that the value is unchanged after calling setData.

@@ -132,4 +132,25 @@ describeWithShallowAndMount('setData', (mountingMethod) => {
wrapper.setData({ message: null })
expect(wrapper.text()).to.equal('There is no message yet')
})
it('should update a data object', () => {
const Cmp = {
Copy link
Member

Choose a reason for hiding this comment

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

The convention we follow in this repo is to name these TestComponent

@shortdiv
Copy link
Contributor Author

Updated! ✨

Copy link
Member

@eddyerburgh eddyerburgh left a comment

Choose a reason for hiding this comment

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

Great work, thanks :)

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.

None yet

3 participants