From c9589f836a537f965c01405b7b1e8afcff539124 Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Sat, 4 Aug 2018 07:02:36 +0100 Subject: [PATCH 1/2] fix: handle null objects --- .../test-utils/src/recursively-set-data.js | 12 ++++--- test/specs/wrapper/setData.spec.js | 32 +++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/packages/test-utils/src/recursively-set-data.js b/packages/test-utils/src/recursively-set-data.js index e76a54612..9ad68533d 100644 --- a/packages/test-utils/src/recursively-set-data.js +++ b/packages/test-utils/src/recursively-set-data.js @@ -1,10 +1,12 @@ import { isPlainObject } from 'shared/validators' -export function recursivelySetData (vm, target, obj) { - Object.keys(obj).forEach(key => { - const val = obj[key] - if (isPlainObject(val)) { - recursivelySetData(vm, target[key], val) +export function recursivelySetData (vm, target, data) { + Object.keys(data).forEach(key => { + const val = data[key] + const targetVal = target[key] + + if (isPlainObject(val) && isPlainObject(targetVal)) { + recursivelySetData(vm, targetVal, val) } else { vm.$set(target, key, val) } diff --git a/test/specs/wrapper/setData.spec.js b/test/specs/wrapper/setData.spec.js index 41e3f97ac..f2b1a90be 100644 --- a/test/specs/wrapper/setData.spec.js +++ b/test/specs/wrapper/setData.spec.js @@ -190,6 +190,38 @@ describeWithShallowAndMount('setData', mountingMethod => { expect(wrapper.vm.anObject.propA.prop2).to.equal('b') }) + it('handles null, undefined, and boolean values', () => { + const TestComponent = { + template: ` +
+ {{nullProperty && nullProperty.foo}} + {{undefinedProperty && undefinedProperty.foo}} + {{booleanProperty && booleanProperty.foo}} +
+ `, + data: () => ({ + nullProperty: null, + undefinedProperty: undefined, + booleanProperty: false + }) + } + const wrapper = mountingMethod(TestComponent) + wrapper.setData({ + nullProperty: { + foo: 'bar' + }, + undefinedProperty: { + foo: 'baz' + }, + booleanProperty: { + foo: 'foobar' + } + }) + expect(wrapper.text()).to.contain('bar') + expect(wrapper.text()).to.contain('baz') + expect(wrapper.text()).to.contain('foobar') + }) + it('does not merge arrays', () => { const TestComponent = { template: '
{{nested.nested.nestedArray[0]}}
', From 7b519420e7127ad9a62da03e64e7294dfd0b9a9f Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Sat, 4 Aug 2018 07:12:13 +0100 Subject: [PATCH 2/2] test: split tests --- test/specs/wrapper/setData.spec.js | 43 ++++++++++++++++++--------- test/specs/wrapper/setMethods.spec.js | 4 +-- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/test/specs/wrapper/setData.spec.js b/test/specs/wrapper/setData.spec.js index f2b1a90be..1af53de54 100644 --- a/test/specs/wrapper/setData.spec.js +++ b/test/specs/wrapper/setData.spec.js @@ -190,36 +190,51 @@ describeWithShallowAndMount('setData', mountingMethod => { expect(wrapper.vm.anObject.propA.prop2).to.equal('b') }) - it('handles null, undefined, and boolean values', () => { + it('handles undefined values', () => { const TestComponent = { template: `
- {{nullProperty && nullProperty.foo}} {{undefinedProperty && undefinedProperty.foo}} - {{booleanProperty && booleanProperty.foo}}
`, data: () => ({ - nullProperty: null, - undefinedProperty: undefined, - booleanProperty: false + undefinedProperty: undefined }) } const wrapper = mountingMethod(TestComponent) wrapper.setData({ - nullProperty: { - foo: 'bar' - }, undefinedProperty: { foo: 'baz' - }, - booleanProperty: { - foo: 'foobar' } }) - expect(wrapper.text()).to.contain('bar') expect(wrapper.text()).to.contain('baz') - expect(wrapper.text()).to.contain('foobar') + }) + + it('handles null values', () => { + const TestComponent = { + template: ` +
{{nullProperty && nullProperty.foo}}
+ `, + data: () => ({ + nullProperty: null + }) + } + const wrapper = mountingMethod(TestComponent) + wrapper.setData({ + nullProperty: { + foo: 'bar', + another: null + } + }) + expect(wrapper.text()).to.contain('bar') + wrapper.setData({ + nullProperty: { + another: { + obj: true + } + } + }) + expect(wrapper.vm.nullProperty.another.obj).to.equal(true) }) it('does not merge arrays', () => { diff --git a/test/specs/wrapper/setMethods.spec.js b/test/specs/wrapper/setMethods.spec.js index 8eef77da6..b6457b181 100644 --- a/test/specs/wrapper/setMethods.spec.js +++ b/test/specs/wrapper/setMethods.spec.js @@ -6,7 +6,7 @@ import { describeWithShallowAndMount } from '~resources/utils' describeWithShallowAndMount('setMethods', mountingMethod => { it('sets component data and updates nested vm nodes when called on Vue instance', () => { const wrapper = mountingMethod(ComponentWithMethods) - const someMethod = () => console.log('hey') + const someMethod = () => {} wrapper.setMethods({ someMethod }) expect(wrapper.vm.someMethod).to.equal(someMethod) }) @@ -25,7 +25,7 @@ describeWithShallowAndMount('setMethods', mountingMethod => { wrapper.find('.toggle').trigger('click') expect(wrapper.vm.isActive).to.be.true // Replace the toggle function so that the data supposedly won't change - const toggleActive = () => console.log('overriden') + const toggleActive = () => {} wrapper.setMethods({ toggleActive }) wrapper.find('.toggle').trigger('click') expect(wrapper.vm.isActive).to.be.true