3
3
import { createSlotVNodes } from './create-slot-vnodes'
4
4
import addMocks from './add-mocks'
5
5
import { addEventLogger } from './log-events'
6
- import { createComponentStubs } from 'shared/stub-components '
7
- import { throwError , warn , vueVersion } from 'shared/util'
6
+ import { addStubs } from './add-stubs '
7
+ import { throwError , vueVersion } from 'shared/util'
8
8
import { compileTemplate } from 'shared/compile-template'
9
9
import { isRequiredComponent } from 'shared/validators'
10
10
import extractInstanceOptions from './extract-instance-options'
11
11
import createFunctionalComponent from './create-functional-component'
12
12
import { componentNeedsCompiling , isPlainObject } from 'shared/validators'
13
13
import { validateSlots } from './validate-slots'
14
14
import createScopedSlots from './create-scoped-slots'
15
+ import { extendExtendedComponents } from './extend-extended-components'
15
16
16
17
function compileTemplateForSlots ( slots : Object ) : void {
17
18
Object . keys ( slots ) . forEach ( key => {
@@ -33,21 +34,14 @@ export default function createInstance (
33
34
// Remove cached constructor
34
35
delete component . _Ctor
35
36
36
- // mounting options are vue-test-utils specific
37
- //
38
37
// instance options are options that are passed to the
39
38
// root instance when it's instantiated
40
- //
41
- // component options are the root components options
42
- const componentOptions = typeof component === 'function'
43
- ? component . extendOptions
44
- : component
45
-
46
39
const instanceOptions = extractInstanceOptions ( options )
47
40
48
- if ( options . mocks ) {
49
- addMocks ( options . mocks , _Vue )
50
- }
41
+ addEventLogger ( _Vue )
42
+ addMocks ( options . mocks , _Vue )
43
+ addStubs ( component , options . stubs , _Vue )
44
+
51
45
if (
52
46
( component . options && component . options . functional ) ||
53
47
component . functional
@@ -63,8 +57,6 @@ export default function createInstance (
63
57
compileTemplate ( component )
64
58
}
65
59
66
- addEventLogger ( _Vue )
67
-
68
60
// Replace globally registered components with components extended
69
61
// from localVue. This makes sure the beforeMount mixins to add stubs
70
62
// is applied to globally registered components.
@@ -78,77 +70,25 @@ export default function createInstance (
78
70
}
79
71
}
80
72
81
- const stubComponents = createComponentStubs (
82
- component . components ,
83
- // $FlowIgnore
84
- options . stubs
73
+ extendExtendedComponents (
74
+ component ,
75
+ _Vue ,
76
+ options . logModifiedComponents ,
77
+ instanceOptions . components
85
78
)
86
- if ( options . stubs ) {
87
- instanceOptions . components = {
88
- ...instanceOptions . components ,
89
- ...stubComponents
90
- }
91
- }
92
- function addStubComponentsMixin ( ) {
93
- Object . assign (
94
- this . $options . components ,
95
- stubComponents
96
- )
97
- }
98
- _Vue . mixin ( {
99
- beforeMount : addStubComponentsMixin ,
100
- // beforeCreate is for components created in node, which
101
- // never mount
102
- beforeCreate : addStubComponentsMixin
103
- } )
104
- Object . keys ( componentOptions . components || { } ) . forEach ( c => {
105
- if (
106
- componentOptions . components [ c ] . extendOptions &&
107
- ! instanceOptions . components [ c ]
108
- ) {
109
- if ( options . logModifiedComponents ) {
110
- warn (
111
- `an extended child component <${ c } > has been modified ` +
112
- `to ensure it has the correct instance properties. ` +
113
- `This means it is not possible to find the component ` +
114
- `with a component selector. To find the component, ` +
115
- `you must stub it manually using the stubs mounting ` +
116
- `option.`
117
- )
118
- }
119
- instanceOptions . components [ c ] = _Vue . extend (
120
- componentOptions . components [ c ]
121
- )
122
- }
123
- } )
124
79
125
80
if ( component . options ) {
126
81
component . options . _base = _Vue
127
82
}
128
83
129
- function getExtendedComponent ( component , instanceOptions ) {
130
- const extendedComponent = component . extend ( instanceOptions )
131
- // to keep the possible overridden prototype and _Vue mixins,
132
- // we need change the proto chains manually
133
- // @see https://github.com/vuejs/vue-test-utils/pull/856
134
- // code below equals to
135
- // `extendedComponent.prototype.__proto__.__proto__ = _Vue.prototype`
136
- const extendedComponentProto =
137
- Object . getPrototypeOf ( extendedComponent . prototype )
138
- Object . setPrototypeOf ( extendedComponentProto , _Vue . prototype )
139
-
140
- return extendedComponent
141
- }
142
-
143
84
// extend component from _Vue to add properties and mixins
144
- const Constructor = typeof component === 'function'
145
- ? getExtendedComponent ( component , instanceOptions )
85
+ // extend does not work correctly for sub class components in Vue < 2.2
86
+ const Constructor = typeof component === 'function' && vueVersion < 2.3
87
+ ? component . extend ( instanceOptions )
146
88
: _Vue . extend ( component ) . extend ( instanceOptions )
147
89
148
- Object . keys ( instanceOptions . components || { } ) . forEach ( key => {
149
- Constructor . component ( key , instanceOptions . components [ key ] )
150
- _Vue . component ( key , instanceOptions . components [ key ] )
151
- } )
90
+ // Keep reference to component mount was called with
91
+ Constructor . _vueTestUtilsRoot = component
152
92
153
93
if ( options . slots ) {
154
94
compileTemplateForSlots ( options . slots )
0 commit comments