forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-instance.js
122 lines (103 loc) · 3.63 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
// @flow
import Vue from 'vue'
import { addSlots } from './add-slots'
import { addScopedSlots } from './add-scoped-slots'
import addMocks from './add-mocks'
import addAttrs from './add-attrs'
import addListeners from './add-listeners'
import addProvide from './add-provide'
import { addEventLogger } from './log-events'
import { createComponentStubs } from 'shared/stub-components'
import { throwError } from 'shared/util'
import { compileTemplate } from './compile-template'
import deleteoptions from './delete-mounting-options'
import createFunctionalComponent from './create-functional-component'
import { componentNeedsCompiling } from 'shared/validators'
function isDestructuringSlotScope (slotScope: string): boolean {
return slotScope[0] === '{' && slotScope[slotScope.length - 1] === '}'
}
function getVueTemplateCompilerHelpers (proxy: Object): Object {
const helpers = {}
const names = ['_c', '_o', '_n', '_s', '_l', '_t', '_q', '_i', '_m', '_f', '_k', '_b', '_v', '_e', '_u', '_g']
names.forEach((name) => {
helpers[name] = proxy[name]
})
return helpers
}
export default function createInstance (
component: Component,
options: Options,
vue: Component
): Component {
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 (options.provide) {
addProvide(component, options.provide, options)
}
if (componentNeedsCompiling(component)) {
compileTemplate(component)
}
addEventLogger(vue)
const Constructor = vue.extend(component)
const instanceOptions = { ...options }
deleteoptions(instanceOptions)
// $FlowIgnore
const stubComponents = createComponentStubs(component.components, options.stubs)
if (options.stubs) {
instanceOptions.components = {
...instanceOptions.components,
// $FlowIgnore
...stubComponents
}
}
Object.keys(stubComponents).forEach(c => {
vue.component(c, stubComponents[c])
})
const vm = new Constructor(instanceOptions)
addAttrs(vm, options.attrs)
addListeners(vm, options.listeners)
if (options.scopedSlots) {
if (window.navigator.userAgent.match(/PhantomJS/i)) {
throwError('the scopedSlots option does not support PhantomJS. Please use Puppeteer, or pass a component.')
}
const vueVersion = Number(`${Vue.version.split('.')[0]}.${Vue.version.split('.')[1]}`)
if (vueVersion >= 2.5) {
vm.$_vueTestUtils_scopedSlots = {}
vm.$_vueTestUtils_slotScopes = {}
const renderSlot = vm._renderProxy._t
vm._renderProxy._t = function (name, feedback, props, bindObject) {
const scopedSlotFn = vm.$_vueTestUtils_scopedSlots[name]
const slotScope = vm.$_vueTestUtils_slotScopes[name]
if (scopedSlotFn) {
props = { ...bindObject, ...props }
const helpers = getVueTemplateCompilerHelpers(vm._renderProxy)
let proxy = { ...helpers }
if (isDestructuringSlotScope(slotScope)) {
proxy = { ...helpers, ...props }
} else {
proxy[slotScope] = props
}
return scopedSlotFn.call(proxy)
} else {
return renderSlot.call(vm._renderProxy, name, feedback, props, bindObject)
}
}
// $FlowIgnore
addScopedSlots(vm, options.scopedSlots)
} else {
throwError('the scopedSlots option is only supported in [email protected]+.')
}
}
if (options.slots) {
addSlots(vm, options.slots)
}
return vm
}