-
Notifications
You must be signed in to change notification settings - Fork 672
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -420,8 +420,16 @@ 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] === 'object' && data[key] !== null) { | ||
Object.keys(data[key]).forEach((key2) => { | ||
const newObj = { ...data[key], ...data[key][key2] } | ||
// $FlowIgnore : Problem with possibly null this.vm | ||
this.vm.$set(this.vm, [key], newObj) | ||
}) | ||
} else { | ||
// $FlowIgnore : Problem with possibly null this.vm | ||
this.vm.$set(this.vm, [key], data[key]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
}) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
const Cmp = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The convention we follow in this repo is to name these TestComponent |
||
data: () => ({ | ||
anObject: { | ||
propA: { | ||
prop1: 'a' | ||
}, | ||
propB: 'b' | ||
} | ||
}) | ||
} | ||
const wrapper = mountingMethod(Cmp) | ||
wrapper.setData({ | ||
anObject: { | ||
propA: { | ||
prop1: 'c' | ||
} | ||
} | ||
}) | ||
expect(wrapper.vm.anObject.propA.prop1).to.equal('c') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
}) | ||
}) |
There was a problem hiding this comment.
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