Skip to content

Commit 0d3e46d

Browse files
abiliczeddyerburgh
authored andcommitted
feat: undefined attributes parsed as $attrs (#1029)
1 parent e28c53e commit 0d3e46d

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

Diff for: packages/test-utils/src/wrapper.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -713,16 +713,6 @@ export default class Wrapper implements BaseWrapper {
713713
}
714714

715715
Object.keys(data).forEach(key => {
716-
if (
717-
!this.vm ||
718-
!this.vm.$options._propKeys ||
719-
!this.vm.$options._propKeys.some(prop => prop === key)
720-
) {
721-
throwError(
722-
`wrapper.setProps() called with ${key} property which ` +
723-
`is not defined on the component`
724-
)
725-
}
726716
if (
727717
typeof data[key] === 'object' &&
728718
data[key] !== null &&
@@ -736,6 +726,21 @@ export default class Wrapper implements BaseWrapper {
736726
`to trigger reactivity`
737727
)
738728
}
729+
if (
730+
!this.vm ||
731+
!this.vm.$options._propKeys ||
732+
!this.vm.$options._propKeys.some(prop => prop === key)
733+
) {
734+
if (vueVersion > 2.3) {
735+
// $FlowIgnore : Problem with possibly null this.vm
736+
this.vm.$attrs[key] = data[key]
737+
return
738+
}
739+
throwError(
740+
`wrapper.setProps() called with ${key} property which ` +
741+
`is not defined on the component`
742+
)
743+
}
739744

740745
if (this.vm && this.vm._props) {
741746
// Set actual props value

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { compileToFunctions } from 'vue-template-compiler'
22
import ComponentWithProps from '~resources/components/component-with-props.vue'
33
import ComponentWithWatch from '~resources/components/component-with-watch.vue'
44
import { describeWithShallowAndMount, vueVersion } from '~resources/utils'
5+
import { itDoNotRunIf } from 'conditional-specs'
56

67
describeWithShallowAndMount('setProps', mountingMethod => {
78
let info
@@ -38,18 +39,28 @@ describeWithShallowAndMount('setProps', mountingMethod => {
3839
expect(wrapper.is('div')).to.equal(true)
3940
})
4041

41-
it('throws error if component does not include props key', () => {
42+
itDoNotRunIf(vueVersion > 2.3, 'throws error if component does not include props key', () => {
4243
const TestComponent = {
4344
template: '<div></div>'
4445
}
4546
const message = `[vue-test-utils]: wrapper.setProps() called ` +
46-
`with prop1 property which is not defined on the component`
47+
`with prop1 property which is not defined on the component`
4748
const fn = () => mountingMethod(TestComponent).setProps({ prop1: 'prop' })
4849
expect(fn)
4950
.to.throw()
5051
.with.property('message', message)
5152
})
5253

54+
itDoNotRunIf(vueVersion < 2.4, 'attributes not recognized as props are available via the $attrs instance property', () => {
55+
const TestComponent = {
56+
template: '<div></div>'
57+
}
58+
const prop1 = 'prop1'
59+
const wrapper = mountingMethod(TestComponent)
60+
wrapper.setProps({ prop1 })
61+
expect(wrapper.vm.$attrs.prop1).to.equal(prop1)
62+
})
63+
5364
it('throws error when called on functional vnode', () => {
5465
const AFunctionalComponent = {
5566
render: (h, context) => h('div', context.prop1),

0 commit comments

Comments
 (0)