forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.js
53 lines (46 loc) · 1.41 KB
/
find.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
// @flow
import findVnodes from './find-vnodes'
import findVueComponents from './find-vue-components'
import findDOMNodes from './find-dom-nodes'
import { COMPONENT_SELECTOR, NAME_SELECTOR, DOM_SELECTOR } from './consts'
import Vue from 'vue'
import getSelectorTypeOrThrow from './get-selector-type'
import { throwError } from 'shared/util'
export default function find (
vm: Component | void,
vnode: VNode | null,
element: Element,
selector: Selector
): Array<VNode | Component> {
const selectorType = getSelectorTypeOrThrow(selector, 'find')
if (!vnode && !vm && selectorType !== DOM_SELECTOR) {
throwError(
`cannot find a Vue instance on a DOM node. The node ` +
`you are calling find on does not exist in the ` +
`VDom. Are you adding the node as innerHTML?`
)
}
if (selectorType === COMPONENT_SELECTOR || selectorType === NAME_SELECTOR) {
const root = vm || vnode
if (!root) {
return []
}
return findVueComponents(root, selectorType, selector)
}
if (
vm &&
vm.$refs &&
selector.ref in vm.$refs &&
vm.$refs[selector.ref] instanceof Vue
) {
return [vm.$refs[selector.ref]]
}
if (vnode) {
const nodes = findVnodes(vnode, vm, selectorType, selector)
if (selectorType !== DOM_SELECTOR) {
return nodes
}
return nodes.length > 0 ? nodes : findDOMNodes(element, selector)
}
return findDOMNodes(element, selector)
}