forked from vuejs/test-utils
/
stubComponentsTransformer.ts
235 lines (204 loc) · 6.8 KB
/
stubComponentsTransformer.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
import type { VTUVNodeTypeTransformer } from './util'
import {
Transition,
TransitionGroup,
BaseTransition,
Teleport,
KeepAlive,
h,
defineComponent,
VNodeTypes,
ConcreteComponent,
ComponentPropsOptions,
ComponentObjectPropsOptions,
DefineComponent,
Component
} from 'vue'
import { hyphenate } from '../utils/vueShared'
import { matchName } from '../utils/matchName'
import { isComponent, isFunctionalComponent } from '../utils'
import { unwrapLegacyVueExtendComponent } from '../utils/vueCompatSupport'
import {
getComponentName,
getComponentRegisteredName
} from '../utils/componentName'
import { config } from '../config'
import { registerStub } from '../stubs'
export type CustomCreateStub = (params: {
name: string
component: ConcreteComponent
}) => ConcreteComponent
interface StubOptions {
name: string
type?: VNodeTypes | typeof Teleport | typeof KeepAlive
renderStubDefaultSlot?: boolean
}
const doNotStubComponents: WeakSet<ConcreteComponent> = new WeakSet()
const shouldNotStub = (type: ConcreteComponent) => doNotStubComponents.has(type)
export const addToDoNotStubComponents = (type: ConcreteComponent) =>
doNotStubComponents.add(type)
const normalizeStubProps = (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() }
}
if (typeof $props[key] === 'function') {
return { ...acc, [key]: '[Function]' }
}
return { ...acc, [key]: $props[key] }
}, {})
}
export const createStub = ({
name,
type,
renderStubDefaultSlot
}: StubOptions): DefineComponent => {
const anonName = 'anonymous-stub'
const tag = name ? `${hyphenate(name)}-stub` : anonName
const componentOptions = type
? unwrapLegacyVueExtendComponent(type) || {}
: {}
return defineComponent({
name: name || anonName,
props: (componentOptions as ConcreteComponent).props || {},
// fix #1550 - respect old-style v-model for shallow mounted components with @vue/compat
// @ts-expect-error
model: componentOptions.model,
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.
// Also having function text as attribute is useless and annoying so
// we replace it with "[Function]""
const stubProps = normalizeStubProps(props)
return h(tag, stubProps, renderStubDefaultSlot ? slots : undefined)
}
}
})
}
const resolveComponentStubByName = (
componentName: string,
stubs: Record<string, Component | boolean>
) => {
for (const [stubKey, value] of Object.entries(stubs)) {
if (matchName(componentName, stubKey)) {
return value
}
}
}
interface CreateStubComponentsTransformerConfig {
stubs?: Record<string, Component | boolean>
shallow?: boolean
renderStubDefaultSlot: boolean
}
export function createStubComponentsTransformer({
stubs = {},
shallow = false,
renderStubDefaultSlot = false
}: CreateStubComponentsTransformerConfig): VTUVNodeTypeTransformer {
return function componentsTransformer(type, instance) {
// stub teleport by default via config.global.stubs
if ((type as any) === Teleport && 'teleport' in stubs) {
if (stubs.teleport === false) return type
return createStub({
name: 'teleport',
type,
renderStubDefaultSlot: true
})
}
// stub keep-alive by default via config.global.stubs
if ((type as any) === KeepAlive && 'keep-alive' in stubs) {
if (stubs['keep-alive'] === false) return type
return createStub({
name: 'keep-alive',
type,
renderStubDefaultSlot: true
})
}
// stub transition by default via config.global.stubs
if (
(type === Transition || (type as any) === BaseTransition) &&
'transition' in stubs
) {
if (stubs.transition === false) return type
return createStub({
name: 'transition',
type,
renderStubDefaultSlot: true
})
}
// stub transition-group by default via config.global.stubs
if ((type as any) === TransitionGroup && 'transition-group' in stubs) {
if (stubs['transition-group'] === false) return type
return createStub({
name: 'transition-group',
type,
renderStubDefaultSlot: true
})
}
if (shouldNotStub(type)) {
return type
}
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
// Edge case: stub is component, we will not render stub but instead will create
// a new "copy" of stub component definition, but we want user still to be able
// to find our component by stub definition, so we register it manually
registerStub({ source: type, stub })
const specializedStubComponent: ConcreteComponent = stubFn
? (...args) => stubFn(...args)
: { ...unwrappedStub }
specializedStubComponent.props = unwrappedStub.props
return specializedStubComponent
}
if (stub === false) {
// we explicitly opt out of stubbing this component
return type
}
// 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
const newStub =
config.plugins.createStubs?.({
name: stubName,
component: type
}) ??
createStub({
name: stubName,
type,
renderStubDefaultSlot
})
return newStub
}
return type
}
}