Skip to content

Commit

Permalink
feat: undefined attributes parsed as $attrs (#1029)
Browse files Browse the repository at this point in the history
  • Loading branch information
abilicz authored and eddyerburgh committed Nov 23, 2018
1 parent e28c53e commit 0d3e46d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
25 changes: 15 additions & 10 deletions packages/test-utils/src/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -736,6 +726,21 @@ export default class Wrapper implements BaseWrapper {
`to trigger reactivity`
)
}
if (
!this.vm ||
!this.vm.$options._propKeys ||
!this.vm.$options._propKeys.some(prop => prop === key)
) {
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) {
// Set actual props value
Expand Down
15 changes: 13 additions & 2 deletions test/specs/wrapper/setProps.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,18 +39,28 @@ describeWithShallowAndMount('setProps', mountingMethod => {
expect(wrapper.is('div')).to.equal(true)
})

it('throws error if component does not include props key', () => {
itDoNotRunIf(vueVersion > 2.3, 'throws error if component does not include props key', () => {
const TestComponent = {
template: '<div></div>'
}
const message = `[vue-test-utils]: wrapper.setProps() called ` +
`with prop1 property which is not defined on the component`
`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: '<div></div>'
}
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', () => {
const AFunctionalComponent = {
render: (h, context) => h('div', context.prop1),
Expand Down

0 comments on commit 0d3e46d

Please sign in to comment.