Skip to content

feat: undefined attributes parsed as $attrs #1029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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