forked from vuejs/test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstubs.ts
210 lines (179 loc) · 5.58 KB
/
stubs.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import {
transformVNodeArgs,
Transition,
TransitionGroup,
h,
ComponentPublicInstance,
Slots,
ComponentOptions,
defineComponent,
VNodeProps,
VNodeTypes
} from 'vue'
import { hyphenate } from './utils/vueShared'
import { MOUNT_COMPONENT_REF, MOUNT_PARENT_NAME } from './constants'
import { config } from './config'
import { matchName } from './utils/matchName'
import { ComponentInternalInstance } from '@vue/runtime-core'
interface StubOptions {
name: string
props?: any
propsDeclaration?: any
}
function getSlots(ctx: ComponentPublicInstance): Slots | undefined {
return !config.renderStubDefaultSlot ? undefined : ctx.$slots
}
export const createStub = ({
name,
props,
propsDeclaration
}: StubOptions): ComponentOptions => {
const anonName = 'anonymous-stub'
const tag = name ? `${hyphenate(name)}-stub` : anonName
const render = (ctx: ComponentPublicInstance) => {
return h(tag, props, getSlots(ctx))
}
return defineComponent({
name: name || anonName,
render,
props: propsDeclaration
})
}
const createTransitionStub = ({
name,
props
}: StubOptions): ComponentOptions => {
const render = (ctx: ComponentPublicInstance) => {
return h(name, {}, ctx.$slots)
}
return defineComponent({ name, render, props })
}
const resolveComponentStubByName = (
componentName: string,
stubs: Record<any, any>
) => {
if (Array.isArray(stubs) && stubs.length) {
// ['Foo', 'Bar'] => { Foo: true, Bar: true }
stubs = stubs.reduce((acc, current) => {
acc[current] = true
return acc
}, {})
}
for (const [stubKey, value] of Object.entries(stubs)) {
if (matchName(componentName, stubKey)) {
return value
}
}
}
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
}
}
return null
}
const isHTMLElement = (type: VNodeTypes) => typeof type === 'string'
const isCommentOrFragment = (type: VNodeTypes) => typeof type === 'symbol'
const isParent = (type: VNodeTypes) =>
isComponent(type) && type['name'] === MOUNT_PARENT_NAME
const isMountedComponent = (
type: VNodeTypes,
props: ({ [key: string]: unknown } & VNodeProps) | null | undefined
) => isComponent(type) && props && props['ref'] === MOUNT_COMPONENT_REF
const isComponent = (type: VNodeTypes): type is ComponentOptions =>
typeof type === 'object'
const isFunctionalComponent = (type: VNodeTypes): type is ComponentOptions =>
typeof type === 'function' && ('name' in type || 'displayName' in type)
export function stubComponents(
stubs: Record<any, any> = {},
shallow: boolean = false
) {
transformVNodeArgs((args, instance: ComponentInternalInstance | null) => {
const [nodeType, props, children, patchFlag, dynamicProps] = args
const type = nodeType as VNodeTypes
// stub transition by default via config.global.stubs
if (type === Transition && stubs['transition']) {
return [
createTransitionStub({
name: 'transition-stub',
propsDeclaration: undefined
}),
undefined,
children
]
}
// stub transition-group by default via config.global.stubs
if (type === TransitionGroup && stubs['transition-group']) {
return [
createTransitionStub({
name: 'transition-group-stub',
propsDeclaration: undefined
}),
undefined,
children
]
}
// args[0] can either be:
// 1. a HTML tag (div, span...)
// 2. An object of component options, such as { name: 'foo', render: [Function], props: {...} }
// Depending what it is, we do different things.
if (
isHTMLElement(type) ||
isCommentOrFragment(type) ||
isParent(type) ||
isMountedComponent(type, props)
) {
return args
}
if (isComponent(type) || isFunctionalComponent(type)) {
const registeredName = getComponentRegisteredName(instance, type)
const componentName = type['name'] || type['displayName']
// No name found?
if (!registeredName && !componentName) {
return shallow ? ['stub'] : args
}
let stub = null
let name = null
// Prio 1 using the key in locally registered components in the parent
if (registeredName) {
stub = resolveComponentStubByName(registeredName, stubs)
if (stub) {
name = registeredName
}
}
// Prio 2 using the name attribute in the component
if (!stub && componentName) {
stub = resolveComponentStubByName(componentName, stubs)
if (stub) {
name = componentName
}
}
// case 2: custom implementation
if (stub && typeof stub === 'object') {
// pass the props and children, for advanced stubbing
return [stubs[name], props, children, patchFlag, dynamicProps]
}
// we return a stub by matching Vue's `h` function
// where the signature is h(Component, props, slots)
// case 1: default stub
if (stub === true || shallow) {
// Set name when using shallow without stub
if (!name) {
name = registeredName || componentName
}
const propsDeclaration = type?.props || {}
const newStub = createStub({ name, propsDeclaration, props })
stubs[name] = newStub
return [newStub, props, children, patchFlag, dynamicProps]
}
}
return args
})
}