Skip to content

Commit 44c4520

Browse files
authored
fix: handle null in set data (#896)
1 parent 11cfee2 commit 44c4520

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

Diff for: packages/test-utils/src/recursively-set-data.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { isPlainObject } from 'shared/validators'
22

3-
export function recursivelySetData (vm, target, obj) {
4-
Object.keys(obj).forEach(key => {
5-
const val = obj[key]
6-
if (isPlainObject(val)) {
7-
recursivelySetData(vm, target[key], val)
3+
export function recursivelySetData (vm, target, data) {
4+
Object.keys(data).forEach(key => {
5+
const val = data[key]
6+
const targetVal = target[key]
7+
8+
if (isPlainObject(val) && isPlainObject(targetVal)) {
9+
recursivelySetData(vm, targetVal, val)
810
} else {
911
vm.$set(target, key, val)
1012
}

Diff for: test/specs/wrapper/setData.spec.js

+47
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,53 @@ describeWithShallowAndMount('setData', mountingMethod => {
190190
expect(wrapper.vm.anObject.propA.prop2).to.equal('b')
191191
})
192192

193+
it('handles undefined values', () => {
194+
const TestComponent = {
195+
template: `
196+
<div>
197+
{{undefinedProperty && undefinedProperty.foo}}
198+
</div>
199+
`,
200+
data: () => ({
201+
undefinedProperty: undefined
202+
})
203+
}
204+
const wrapper = mountingMethod(TestComponent)
205+
wrapper.setData({
206+
undefinedProperty: {
207+
foo: 'baz'
208+
}
209+
})
210+
expect(wrapper.text()).to.contain('baz')
211+
})
212+
213+
it('handles null values', () => {
214+
const TestComponent = {
215+
template: `
216+
<div>{{nullProperty && nullProperty.foo}}</div>
217+
`,
218+
data: () => ({
219+
nullProperty: null
220+
})
221+
}
222+
const wrapper = mountingMethod(TestComponent)
223+
wrapper.setData({
224+
nullProperty: {
225+
foo: 'bar',
226+
another: null
227+
}
228+
})
229+
expect(wrapper.text()).to.contain('bar')
230+
wrapper.setData({
231+
nullProperty: {
232+
another: {
233+
obj: true
234+
}
235+
}
236+
})
237+
expect(wrapper.vm.nullProperty.another.obj).to.equal(true)
238+
})
239+
193240
it('does not merge arrays', () => {
194241
const TestComponent = {
195242
template: '<div>{{nested.nested.nestedArray[0]}}</div>',

Diff for: test/specs/wrapper/setMethods.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { describeWithShallowAndMount } from '~resources/utils'
66
describeWithShallowAndMount('setMethods', mountingMethod => {
77
it('sets component data and updates nested vm nodes when called on Vue instance', () => {
88
const wrapper = mountingMethod(ComponentWithMethods)
9-
const someMethod = () => console.log('hey')
9+
const someMethod = () => {}
1010
wrapper.setMethods({ someMethod })
1111
expect(wrapper.vm.someMethod).to.equal(someMethod)
1212
})
@@ -25,7 +25,7 @@ describeWithShallowAndMount('setMethods', mountingMethod => {
2525
wrapper.find('.toggle').trigger('click')
2626
expect(wrapper.vm.isActive).to.be.true
2727
// Replace the toggle function so that the data supposedly won't change
28-
const toggleActive = () => console.log('overriden')
28+
const toggleActive = () => {}
2929
wrapper.setMethods({ toggleActive })
3030
wrapper.find('.toggle').trigger('click')
3131
expect(wrapper.vm.isActive).to.be.true

0 commit comments

Comments
 (0)