File tree Expand file tree Collapse file tree 2 files changed +30
-8
lines changed
test/unit/specs/mount/options Expand file tree Collapse file tree 2 files changed +30
-8
lines changed Original file line number Diff line number Diff line change @@ -11,23 +11,29 @@ import { throwError } from './util'
11
11
import cloneDeep from 'lodash/cloneDeep'
12
12
import { compileTemplate } from './compile-template'
13
13
14
- export default function createConstructor ( component : Component , options : Options ) : Component {
14
+ export default function createConstructor (
15
+ component : Component ,
16
+ options : Options
17
+ ) : Component {
15
18
const vue = options . localVue || Vue
16
19
17
- if ( options . context ) {
18
- if ( ! component . functional ) {
19
- throwError ( 'mount.context can only be used when mounting a functional component' )
20
- }
21
-
22
- if ( typeof options . context !== 'object' ) {
20
+ if ( component . functional ) {
21
+ if ( options . context && typeof options . context !== 'object' ) {
23
22
throwError ( 'mount.context must be an object' )
24
23
}
25
24
const clonedComponent = cloneDeep ( component )
26
25
component = {
27
26
render ( h ) {
28
- return h ( clonedComponent , options . context )
27
+ return h (
28
+ clonedComponent ,
29
+ options . context || component . FunctionalRenderContext
30
+ )
29
31
}
30
32
}
33
+ } else if ( options . context ) {
34
+ throwError (
35
+ 'mount.context can only be used when mounting a functional component'
36
+ )
31
37
}
32
38
33
39
if ( options . provide ) {
Original file line number Diff line number Diff line change @@ -49,4 +49,20 @@ describe('context', () => {
49
49
const fn = ( ) => mount ( Component , { context } )
50
50
expect ( fn ) . to . throw ( ) . with . property ( 'message' , message )
51
51
} )
52
+
53
+ it ( 'mounts functional component with a defined context when no context object passed in options' , ( ) => {
54
+ const defaultValue = '[vue-test-utils]: testProp default value'
55
+ const Component = {
56
+ functional : true ,
57
+ props : {
58
+ testProp : {
59
+ type : String ,
60
+ default : defaultValue
61
+ }
62
+ } ,
63
+ render : ( h , { props } ) => h ( 'div' , props . testProp )
64
+ }
65
+ const wrapper = mount ( Component )
66
+ expect ( wrapper . element . textContent ) . to . equal ( defaultValue )
67
+ } )
52
68
} )
You can’t perform that action at this time.
0 commit comments