forked from vuejs/test-utils
/
componentName.ts
55 lines (47 loc) · 1.41 KB
/
componentName.ts
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
import { ComponentInternalInstance } from '@vue/runtime-core'
import { VNodeTypes } from 'vue'
import { isFunctionalComponent, isObjectComponent } from '../utils'
import {
isLegacyExtendedComponent,
unwrapLegacyVueExtendComponent
} from './vueCompatSupport'
const getComponentNameInSetup = (
instance: any | null,
type: VNodeTypes
): string | undefined =>
Object.keys(instance?.setupState || {}).find(
(key) =>
Object.getOwnPropertyDescriptor(instance.setupState, key)?.value === type
)
export const getComponentRegisteredName = (
instance: ComponentInternalInstance | null,
type: VNodeTypes
): string | null => {
if (!instance || !instance.parent) return null
// try to infer the name based on local resolution
const registry = (instance.type as any).components
for (const key in registry) {
if (registry[key] === type) {
return key
}
}
// try to retrieve name imported in script setup
return getComponentNameInSetup(instance.parent, type) || null
}
export const getComponentName = (
instance: any | null,
type: VNodeTypes
): string => {
if (isObjectComponent(type)) {
return (
getComponentNameInSetup(instance, type) || type.name || type.__name || ''
)
}
if (isLegacyExtendedComponent(type)) {
return unwrapLegacyVueExtendComponent(type).name || ''
}
if (isFunctionalComponent(type)) {
return type.displayName || type.name
}
return ''
}