forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmount.js
105 lines (87 loc) · 2.96 KB
/
mount.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
import Vue from 'vue'
import createInstance from 'create-instance'
import createElement from './create-element'
import { throwIfInstancesThrew, addGlobalErrorHandler } from './error'
import { mergeOptions } from 'shared/merge-options'
import config from './config'
import warnIfNoWindow from './warn-if-no-window'
import createWrapper from './create-wrapper'
import createLocalVue from './create-local-vue'
import { warn } from 'shared/util'
import semver from 'semver'
import { COMPAT_SYNC_MODE } from 'shared/consts'
import { validateOptions } from 'shared/validate-options'
import TransitionGroupStub from './components/TransitionGroupStub'
import TransitionStub from './components/TransitionStub'
Vue.config.productionTip = false
Vue.config.devtools = false
function getSyncOption(syncOption) {
if (syncOption === false) {
Vue.config.async = true
return false
}
if (semver.lt(Vue.version, '2.5.18')) {
warn(
`Vue Test Utils runs in sync mode by default. Due to bugs, sync mode ` +
`requires Vue > 2.5.18. In Vue Test Utils 1.0 sync mode will only be ` +
`supported with Vue 2.5.18+ running in development mode. If you are ` +
`unable to upgrade, you should rewrite your tests to run asynchronously` +
`you can do this by setting the sync mounting option to false.`
)
return COMPAT_SYNC_MODE
}
if (typeof Vue.config.async === 'undefined') {
warn(
`Sync mode only works when Vue runs in dev mode. ` +
`Please set Vue to run in dev mode, or set sync to false`
)
}
Vue.config.async = false
return true
}
function addTransitionStubs(options) {
if (config.stubs === false) {
return
}
if (
options.stubs &&
options.stubs.transition !== false &&
!options.stubs.transition
) {
options.stubs.transition = TransitionStub
}
if (
options.stubs &&
options.stubs['transition-group'] !== false &&
!options.stubs['transition-group']
) {
options.stubs['transition-group'] = TransitionGroupStub
}
}
export default function mount(component, options = {}) {
warnIfNoWindow()
addGlobalErrorHandler(Vue)
const _Vue = createLocalVue(options.localVue)
const mergedOptions = mergeOptions(options, config)
const sync = getSyncOption(mergedOptions.sync)
validateOptions(mergedOptions, component)
// Stub transition and transition-group if in compat sync mode to keep old
// behavior
// TODO: Remove when compat sync mode is removed
if (sync === COMPAT_SYNC_MODE) {
addTransitionStubs(mergedOptions)
}
const parentVm = createInstance(component, mergedOptions, _Vue)
const el = options.attachToDocument ? createElement() : undefined
const vm = parentVm.$mount(el)
component._Ctor = {}
throwIfInstancesThrew(vm)
const wrapperOptions = {
attachedToDocument: !!mergedOptions.attachToDocument,
sync
}
const root = parentVm.$options._isFunctionalContainer
? vm._vnode
: vm.$children[0]
return createWrapper(root, wrapperOptions)
}