/
stubs.ts
293 lines (257 loc) · 8.12 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import {
transformVNodeArgs,
Transition,
TransitionGroup,
BaseTransition,
Teleport,
h,
defineComponent,
VNodeTypes,
ConcreteComponent,
ComponentPropsOptions,
ComponentObjectPropsOptions
} from 'vue'
import { hyphenate } from './utils/vueShared'
import { matchName } from './utils/matchName'
import { isComponent, isFunctionalComponent } from './utils'
import { ComponentInternalInstance } from '@vue/runtime-core'
import { unwrapLegacyVueExtendComponent } from './utils/vueCompatSupport'
import { Stub, Stubs } from './types'
import {
getComponentName,
getComponentRegisteredName
} from './utils/componentName'
import { config } from './config'
export type CustomCreateStub = (params: {
name: string
component: ConcreteComponent
}) => ConcreteComponent
interface StubOptions {
name: string
type?: ConcreteComponent
renderStubDefaultSlot?: boolean
}
const stubsMap: WeakMap<
ConcreteComponent,
{ source: ConcreteComponent; originalStub?: ConcreteComponent }
> = new WeakMap()
export const registerStub = ({
source,
stub,
originalStub
}: {
source: ConcreteComponent
stub: ConcreteComponent
originalStub?: ConcreteComponent
}) => {
stubsMap.set(stub, { source, originalStub })
}
export const getOriginalVNodeTypeFromStub = (
type: ConcreteComponent
): VNodeTypes | undefined => stubsMap.get(type)?.source
export const getOriginalStubFromSpecializedStub = (
type: ConcreteComponent
): VNodeTypes | undefined => stubsMap.get(type)?.originalStub
const doNotStubComponents: WeakSet<ConcreteComponent> = new WeakSet()
const shouldNotStub = (type: ConcreteComponent) => doNotStubComponents.has(type)
export const addToDoNotStubComponents = (type: ConcreteComponent) =>
doNotStubComponents.add(type)
const stringifySymbols = (props: ComponentPropsOptions) => {
// props are always normalized to object syntax
const $props = props as unknown as ComponentObjectPropsOptions
return Object.keys($props).reduce((acc, key) => {
if (typeof $props[key] === 'symbol') {
return { ...acc, [key]: $props[key]?.toString() }
}
return { ...acc, [key]: $props[key] }
}, {})
}
export const createStub = ({
name,
type,
renderStubDefaultSlot
}: StubOptions) => {
const anonName = 'anonymous-stub'
const tag = name ? `${hyphenate(name)}-stub` : anonName
const propsDeclaration = type
? unwrapLegacyVueExtendComponent(type).props || {}
: {}
return defineComponent({
name: name || anonName,
props: propsDeclaration,
setup(props, { slots }) {
return () => {
// https://github.com/vuejs/test-utils/issues/1076
// Passing a symbol as a static prop is not legal, since Vue will try to do
// something like `el.setAttribute('val', Symbol())` which is not valid and
// causes an error.
// Only a problem when shallow mounting. For this reason we iterate of the
// props that will be passed and stringify any that are symbols.
const propsWithoutSymbols = stringifySymbols(props)
return h(
tag,
propsWithoutSymbols,
renderStubDefaultSlot ? slots : undefined
)
}
}
})
}
const resolveComponentStubByName = (componentName: string, stubs: Stubs) => {
if (Array.isArray(stubs) && stubs.length) {
// ['Foo', 'Bar'] => { Foo: true, Bar: true }
stubs = stubs.reduce((acc, current) => {
acc[current] = true
return acc
}, {} as Record<string, Stub>)
}
for (const [stubKey, value] of Object.entries(stubs)) {
if (matchName(componentName, stubKey)) {
return value
}
}
}
function createStubOnceForType(
type: ConcreteComponent,
factoryFn: () => ConcreteComponent,
cache: WeakMap<{} & VNodeTypes, ConcreteComponent>
): ConcreteComponent {
const cachedStub = cache.get(type)
if (cachedStub) {
return cachedStub
}
const stub = factoryFn()
cache.set(type, stub)
return stub
}
export function stubComponents(
stubs: Stubs = {},
shallow = false,
renderStubDefaultSlot = false
) {
const createdStubsMap: WeakMap<{} & VNodeTypes, ConcreteComponent> =
new WeakMap()
const createStubOnce = (
type: ConcreteComponent,
factoryFn: () => ConcreteComponent
) => createStubOnceForType(type, factoryFn, createdStubsMap)
transformVNodeArgs((args, instance: ComponentInternalInstance | null) => {
const [nodeType, props, children, patchFlag, dynamicProps] = args
const type = nodeType as VNodeTypes | typeof Teleport
// stub transition by default via config.global.stubs
if (
(type === Transition || type === BaseTransition) &&
'transition' in stubs
) {
if (stubs.transition === false) return args
return [
createStub({
name: 'transition',
renderStubDefaultSlot: true
}),
undefined,
children
]
}
// stub transition-group by default via config.global.stubs
if (type === TransitionGroup && 'transition-group' in stubs) {
if (stubs['transition-group'] === false) return args
return [
createStub({
name: 'transition-group',
renderStubDefaultSlot: true
}),
undefined,
children
]
}
// stub teleport by default via config.global.stubs
if (type === Teleport && 'teleport' in stubs) {
if (stubs.teleport === false) return args
return [
createStub({
name: 'teleport',
renderStubDefaultSlot: true
}),
undefined,
() => children
]
}
if (isComponent(type) || isFunctionalComponent(type)) {
if (shouldNotStub(type)) {
return args
}
const registeredName = getComponentRegisteredName(instance, type)
const componentName = getComponentName(instance, type)
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 (isComponent(stub)) {
const unwrappedStub = unwrapLegacyVueExtendComponent(stub)
const stubFn = isFunctionalComponent(unwrappedStub)
? unwrappedStub
: null
const specializedStubComponent: ConcreteComponent = stubFn
? (...args) => stubFn(...args)
: { ...unwrappedStub }
specializedStubComponent.props = unwrappedStub.props
const specializedStub = createStubOnce(
type,
() => specializedStubComponent
)
specializedStub.props = unwrappedStub.props
registerStub({
source: type,
stub: specializedStub,
originalStub: stub
})
// pass the props and children, for advanced stubbing
return [specializedStub, props, children, patchFlag, dynamicProps]
}
if (stub === false) {
// we explicitly opt out of stubbing this component
return args
}
// 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
const stubName = name || registeredName || componentName
if (!isComponent(type)) {
throw new Error('Attempted to stub a non-component')
}
const newStub = createStubOnce(type, () =>
config.plugins.createStubs
? config.plugins.createStubs({
name: stubName,
component: type
})
: createStub({
name: stubName,
type,
renderStubDefaultSlot
})
)
registerStub({ source: type, stub: newStub })
return [newStub, props, children, patchFlag, dynamicProps]
}
}
// do not stub anything what is not a component
return args
})
}