forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-instance.js
109 lines (93 loc) · 3.19 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
// @flow
import { createSlotVNodes } from './add-slots'
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 deleteMountingOptions from './delete-mounting-options'
import createFunctionalComponent from './create-functional-component'
import { componentNeedsCompiling } from 'shared/validators'
import { validateSlots } from './validate-slots'
export default function createInstance (
component: Component,
options: Options,
_Vue: Component,
elm?: Element
): Component {
// Remove cached constructor
delete component._Ctor
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)
const instanceOptions = {
...options
}
deleteMountingOptions(instanceOptions)
// $FlowIgnore
const stubComponents = createComponentStubs(component.components, options.stubs)
if (options.stubs) {
instanceOptions.components = {
...instanceOptions.components,
// $FlowIgnore
...stubComponents
}
}
Object.keys(component.components || {}).forEach((c) => {
if (component.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(component.components[c])
}
})
Object.keys(stubComponents).forEach(c => {
_Vue.component(c, stubComponents[c])
})
const Constructor = vueVersion < 2.3 && typeof component === 'function'
? component.extend(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) {
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 Parent = _Vue.extend({
provide: options.provide,
render (h) {
const slots = options.slots
? createSlotVNodes(h, options.slots)
: undefined
return h(Constructor, {
ref: 'vm',
props: options.propsData,
on: options.listeners,
attrs: options.attrs
}, slots)
}
})
return new Parent()
}