Skip to content

Commit 1c65bf7

Browse files
committed
tests: stub component with camelcase naming
1 parent 2355f55 commit 1c65bf7

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

src/stubs.ts

+18-8
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,32 @@ export const createStub = (options: IStubOptions) => {
1717
return { name: tag, render }
1818
}
1919

20-
const resolveComponentStubByName = (name: string, stubs: Record<any, any>) => {
21-
const pascal = pascalCase(name)
22-
const kebab = kebabCase(name)
20+
const resolveComponentStubByName = (
21+
componentName: string,
22+
stubs: Record<any, any>
23+
) => {
24+
const componentPascalName = pascalCase(componentName)
25+
const componentKebabName = kebabCase(componentName)
2326

24-
for (const [key, value] of Object.entries(stubs)) {
25-
if (name === pascal || name === kebab) {
27+
for (const [stubKey, value] of Object.entries(stubs)) {
28+
if (
29+
stubKey === componentPascalName ||
30+
stubKey === componentKebabName ||
31+
stubKey === componentName
32+
) {
2633
return value
2734
}
2835
}
2936
}
3037

3138
const isHTMLElement = (args: VNodeArgs) =>
3239
Array.isArray(args) && typeof args[0] === 'string'
40+
3341
const isCommentOrFragment = (args: VNodeArgs) => typeof args[0] === 'symbol'
42+
3443
const isParent = (args: VNodeArgs) =>
3544
typeof args[0] === 'object' && args[0]['name'] === 'VTU_COMPONENT'
45+
3646
const isComponent = (args: VNodeArgs) => typeof args[0] === 'object'
3747

3848
export function stubComponents(stubs: Record<any, any>) {
@@ -48,15 +58,15 @@ export function stubComponents(stubs: Record<any, any>) {
4858
}
4959

5060
const stub = resolveComponentStubByName(name, stubs)
61+
5162
// we return a stub by matching Vue's `h` function
5263
// where the signature is h(Component, props)
53-
54-
// default stub
64+
// case 1: default stub
5565
if (stub === true) {
5666
return [createStub({ name }), {}]
5767
}
5868

59-
// custom implementation
69+
// case 2: custom implementation
6070
if (typeof stub === 'object') {
6171
return [stubs[name], {}]
6272
}

tests/mountingOptions/stubs.global.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,23 @@ describe('mounting options: stubs', () => {
198198

199199
expect(wrapper.html()).toBe('<foobar-stub></foobar-stub>')
200200
})
201+
202+
it('stubs a component with registered with strange casing', () => {
203+
const FooBar = {
204+
name: 'fooBar',
205+
render: () => h('span', 'real foobar')
206+
}
207+
const Comp = {
208+
render: () => h(FooBar)
209+
}
210+
const wrapper = mount(Comp, {
211+
global: {
212+
stubs: {
213+
fooBar: true
214+
}
215+
}
216+
})
217+
218+
expect(wrapper.html()).toBe('<foobar-stub></foobar-stub>')
219+
})
201220
})

0 commit comments

Comments
 (0)