From 13ad0961ab8c7af3887b8438a59ba65b1ed9e714 Mon Sep 17 00:00:00 2001 From: abilicz Date: Sun, 18 Nov 2018 14:06:09 +0100 Subject: [PATCH 1/2] feat: undefined attributes parsed as $attrs --- packages/test-utils/src/wrapper.js | 19 +++++++++---------- test/specs/wrapper/setProps.spec.js | 12 +++++------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/packages/test-utils/src/wrapper.js b/packages/test-utils/src/wrapper.js index 1ef78370c..56f458990 100644 --- a/packages/test-utils/src/wrapper.js +++ b/packages/test-utils/src/wrapper.js @@ -713,16 +713,6 @@ export default class Wrapper implements BaseWrapper { } Object.keys(data).forEach(key => { - if ( - !this.vm || - !this.vm.$options._propKeys || - !this.vm.$options._propKeys.some(prop => prop === key) - ) { - throwError( - `wrapper.setProps() called with ${key} property which ` + - `is not defined on the component` - ) - } if ( typeof data[key] === 'object' && data[key] !== null && @@ -736,6 +726,15 @@ export default class Wrapper implements BaseWrapper { `to trigger reactivity` ) } + if ( + !this.vm || + !this.vm.$options._propKeys || + !this.vm.$options._propKeys.some(prop => prop === key) + ) { + // $FlowIgnore : Problem with possibly null this.vm + this.vm.$attrs[key] = data[key] + return + } if (this.vm && this.vm._props) { // Set actual props value diff --git a/test/specs/wrapper/setProps.spec.js b/test/specs/wrapper/setProps.spec.js index 228cc7027..6b0551664 100644 --- a/test/specs/wrapper/setProps.spec.js +++ b/test/specs/wrapper/setProps.spec.js @@ -38,16 +38,14 @@ describeWithShallowAndMount('setProps', mountingMethod => { expect(wrapper.is('div')).to.equal(true) }) - it('throws error if component does not include props key', () => { + it('attributes not recognized as props are available via the $attrs instance property', () => { const TestComponent = { template: '
' } - const message = `[vue-test-utils]: wrapper.setProps() called ` + - `with prop1 property which is not defined on the component` - const fn = () => mountingMethod(TestComponent).setProps({ prop1: 'prop' }) - expect(fn) - .to.throw() - .with.property('message', message) + const prop1 = 'prop1' + const wrapper = mountingMethod(TestComponent) + wrapper.setProps({ prop1 }) + expect(wrapper.vm.$attrs.prop1).to.equal(prop1) }) it('throws error when called on functional vnode', () => { From 654776c798348c09713202db4c057e6844e8dfe8 Mon Sep 17 00:00:00 2001 From: abilicz Date: Sun, 18 Nov 2018 15:48:11 +0100 Subject: [PATCH 2/2] add check for attrs support --- packages/test-utils/src/wrapper.js | 12 +++++++++--- test/specs/wrapper/setProps.spec.js | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/packages/test-utils/src/wrapper.js b/packages/test-utils/src/wrapper.js index 56f458990..86047d251 100644 --- a/packages/test-utils/src/wrapper.js +++ b/packages/test-utils/src/wrapper.js @@ -731,9 +731,15 @@ export default class Wrapper implements BaseWrapper { !this.vm.$options._propKeys || !this.vm.$options._propKeys.some(prop => prop === key) ) { - // $FlowIgnore : Problem with possibly null this.vm - this.vm.$attrs[key] = data[key] - return + if (vueVersion > 2.3) { + // $FlowIgnore : Problem with possibly null this.vm + this.vm.$attrs[key] = data[key] + return + } + throwError( + `wrapper.setProps() called with ${key} property which ` + + `is not defined on the component` + ) } if (this.vm && this.vm._props) { diff --git a/test/specs/wrapper/setProps.spec.js b/test/specs/wrapper/setProps.spec.js index 6b0551664..e7833a6be 100644 --- a/test/specs/wrapper/setProps.spec.js +++ b/test/specs/wrapper/setProps.spec.js @@ -2,6 +2,7 @@ import { compileToFunctions } from 'vue-template-compiler' import ComponentWithProps from '~resources/components/component-with-props.vue' import ComponentWithWatch from '~resources/components/component-with-watch.vue' import { describeWithShallowAndMount, vueVersion } from '~resources/utils' +import { itDoNotRunIf } from 'conditional-specs' describeWithShallowAndMount('setProps', mountingMethod => { let info @@ -38,7 +39,19 @@ describeWithShallowAndMount('setProps', mountingMethod => { expect(wrapper.is('div')).to.equal(true) }) - it('attributes not recognized as props are available via the $attrs instance property', () => { + itDoNotRunIf(vueVersion > 2.3, 'throws error if component does not include props key', () => { + const TestComponent = { + template: '
' + } + const message = `[vue-test-utils]: wrapper.setProps() called ` + + `with prop1 property which is not defined on the component` + const fn = () => mountingMethod(TestComponent).setProps({ prop1: 'prop' }) + expect(fn) + .to.throw() + .with.property('message', message) + }) + + itDoNotRunIf(vueVersion < 2.4, 'attributes not recognized as props are available via the $attrs instance property', () => { const TestComponent = { template: '
' }