Skip to content

Commit 2bfc73f

Browse files
authored
refactor: throw error for functional components in props (#460)
1 parent bb28074 commit 2bfc73f

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

packages/test-utils/src/wrapper.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ export default class Wrapper implements BaseWrapper {
385385
* Returns an Object containing the prop name/value pairs on the element
386386
*/
387387
props (): { [name: string]: any } {
388+
if (this.isFunctionalComponent) {
389+
throwError('wrapper.props() cannot be called on a mounted functional component.')
390+
}
388391
if (!this.vm) {
389392
throwError('wrapper.props() must be called on a Vue instance')
390393
}

test/specs/wrapper/props.spec.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { compileToFunctions } from 'vue-template-compiler'
22
import ComponentWithProps from '~resources/components/component-with-props.vue'
3-
import { describeWithShallowAndMount } from '~resources/utils'
3+
import {
4+
describeWithShallowAndMount,
5+
itSkipIf,
6+
functionalSFCsSupported
7+
} from '~resources/utils'
48

59
describeWithShallowAndMount('props', (mountingMethod) => {
610
it('returns true if wrapper has prop', () => {
@@ -32,6 +36,30 @@ describeWithShallowAndMount('props', (mountingMethod) => {
3236
expect(wrapper.props()).to.eql({ prop1: {}, prop2: 'val2' }) // fail
3337
})
3438

39+
itSkipIf(!functionalSFCsSupported(),
40+
'works correctly a functional component', () => {
41+
const FunctionalComponent = {
42+
render: h => h('div'),
43+
functional: true,
44+
props: ['prop1']
45+
}
46+
const TestComponent = {
47+
template: '<div><functional-component /></div>',
48+
components: { FunctionalComponent }
49+
}
50+
const prop1 = 'some prop'
51+
const wrapper = mountingMethod(TestComponent, {
52+
propsData: {
53+
prop1
54+
}
55+
})
56+
if (mountingMethod.name === 'mount') {
57+
const message = '[vue-test-utils]: wrapper.props() cannot be called on a mounted functional component.'
58+
const fn = () => wrapper.find(FunctionalComponent).props()
59+
expect(fn).to.throw().with.property('message', message)
60+
}
61+
})
62+
3563
it('throws an error if called on a non vm wrapper', () => {
3664
const compiled = compileToFunctions('<div><p /></div>')
3765
const p = mountingMethod(compiled).findAll('p').at(0)

0 commit comments

Comments
 (0)