forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-instance.js
204 lines (183 loc) · 6.11 KB
/
create-instance.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// @flow
import { createSlotVNodes } from './create-slot-vnodes'
import addMocks from './add-mocks'
import { addEventLogger } from './log-events'
import { createComponentStubs } from 'shared/stub-components'
import { throwError, warn, vueVersion } from 'shared/util'
import { compileTemplate } from 'shared/compile-template'
import { isRequiredComponent } from 'shared/validators'
import extractInstanceOptions from './extract-instance-options'
import createFunctionalComponent from './create-functional-component'
import { componentNeedsCompiling, isPlainObject } from 'shared/validators'
import { validateSlots } from './validate-slots'
import createScopedSlots from './create-scoped-slots'
function compileTemplateForSlots (slots: Object): void {
Object.keys(slots).forEach(key => {
const slot = Array.isArray(slots[key]) ? slots[key] : [slots[key]]
slot.forEach(slotValue => {
if (componentNeedsCompiling(slotValue)) {
compileTemplate(slotValue)
}
})
})
}
export default function createInstance (
component: Component,
options: Options,
_Vue: Component,
elm?: Element
): Component {
// Remove cached constructor
delete component._Ctor
// mounting options are vue-test-utils specific
//
// instance options are options that are passed to the
// root instance when it's instantiated
//
// component options are the root components options
const componentOptions = typeof component === 'function'
? component.extendOptions
: component
const instanceOptions = extractInstanceOptions(options)
if (options.mocks) {
addMocks(options.mocks, _Vue)
}
if (
(component.options && component.options.functional) ||
component.functional
) {
component = createFunctionalComponent(component, options)
} else if (options.context) {
throwError(
`mount.context can only be used when mounting a ` + `functional component`
)
}
if (componentNeedsCompiling(component)) {
compileTemplate(component)
}
addEventLogger(_Vue)
// Replace globally registered components with components extended
// from localVue. This makes sure the beforeMount mixins to add stubs
// is applied to globally registered components.
// Vue version must be 2.3 or greater, because of a bug resolving
// extended constructor options (https://github.com/vuejs/vue/issues/4976)
if (vueVersion > 2.2) {
for (const c in _Vue.options.components) {
if (!isRequiredComponent(c)) {
_Vue.component(c, _Vue.extend(_Vue.options.components[c]))
}
}
}
const stubComponents = createComponentStubs(
component.components,
// $FlowIgnore
options.stubs
)
if (options.stubs) {
instanceOptions.components = {
...instanceOptions.components,
...stubComponents
}
}
function addStubComponentsMixin () {
Object.assign(
this.$options.components,
stubComponents
)
}
_Vue.mixin({
beforeMount: addStubComponentsMixin,
// beforeCreate is for components created in node, which
// never mount
beforeCreate: addStubComponentsMixin
})
Object.keys(componentOptions.components || {}).forEach(c => {
if (
componentOptions.components[c].extendOptions &&
!instanceOptions.components[c]
) {
if (options.logModifiedComponents) {
warn(
`an extended child component <${c}> has been modified ` +
`to ensure it has the correct instance properties. ` +
`This means it is not possible to find the component ` +
`with a component selector. To find the component, ` +
`you must stub it manually using the stubs mounting ` +
`option.`
)
}
instanceOptions.components[c] = _Vue.extend(
componentOptions.components[c]
)
}
})
if (component.options) {
component.options._base = _Vue
}
function getExtendedComponent (component, instanceOptions) {
const extendedComponent = component.extend(instanceOptions)
// to keep the possible overridden prototype and _Vue mixins,
// we need change the proto chains manually
// @see https://github.com/vuejs/vue-test-utils/pull/856
// code below equals to
// `extendedComponent.prototype.__proto__.__proto__ = _Vue.prototype`
const extendedComponentProto =
Object.getPrototypeOf(extendedComponent.prototype)
Object.setPrototypeOf(extendedComponentProto, _Vue.prototype)
return extendedComponent
}
// extend component from _Vue to add properties and mixins
const Constructor = typeof component === 'function'
? getExtendedComponent(component, instanceOptions)
: _Vue.extend(component).extend(instanceOptions)
Object.keys(instanceOptions.components || {}).forEach(key => {
Constructor.component(key, instanceOptions.components[key])
_Vue.component(key, instanceOptions.components[key])
})
if (options.slots) {
compileTemplateForSlots(options.slots)
// $FlowIgnore
validateSlots(options.slots)
}
// Objects are not resolved in extended components in Vue < 2.5
// https://github.com/vuejs/vue/issues/6436
if (
options.provide &&
typeof options.provide === 'object' &&
vueVersion < 2.5
) {
const obj = { ...options.provide }
options.provide = () => obj
}
const scopedSlots = createScopedSlots(options.scopedSlots)
if (options.parentComponent && !isPlainObject(options.parentComponent)) {
throwError(
`options.parentComponent should be a valid Vue component ` +
`options object`
)
}
const parentComponentOptions = options.parentComponent || {}
parentComponentOptions.provide = options.provide
parentComponentOptions.render = function (h) {
const slots = options.slots
? createSlotVNodes(this, options.slots)
: undefined
return h(
Constructor,
{
ref: 'vm',
on: options.listeners,
attrs: {
...options.attrs,
// pass as attrs so that inheritAttrs works correctly
// propsData should take precedence over attrs
...options.propsData
},
scopedSlots
},
slots
)
}
const Parent = _Vue.extend(parentComponentOptions)
return new Parent()
}