Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/utils/componentName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export const getComponentRegisteredName = (
}
}

// try to infer the name based on global resolution
const globalRegistry = instance.appContext.components
for (const key in globalRegistry) {
if (globalRegistry[key] === type) {
return key
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add this as the last case (after getComponentNameInSetup): this will avoid breaking existing tests and make sure local registration wins over global registration

// try to retrieve name imported in script setup
return getComponentNameInSetup(instance.parent, type) || null
}
Expand Down
30 changes: 30 additions & 0 deletions tests/mountingOptions/global.stubs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,36 @@
expect(wrapper.html()).toBe('<foo-bar-stub></foo-bar-stub>')
})

it('stubs a globally registered component by its registration name', () => {
const LibraryComponent = defineComponent({
name: 'LibraryInternalName',
template: '<span>real library component</span>'
})

const Plugin = {
install(app: any) {
app.component('GlobalAlias', LibraryComponent)
}
}

const Component = defineComponent({
template: '<div><GlobalAlias /></div>'
})

const wrapper = mount(Component, {
global: {
plugins: [Plugin],
stubs: {
GlobalAlias: true
}
}
})

expect(wrapper.html()).toBe(
'<div>\n' + ' <global-alias-stub></global-alias-stub>\n' + '</div>'
)
})

it('stubs components within script setup', () => {
const wrapper = mount(ScriptSetupWithChildren as any, {
global: {
Expand Down Expand Up @@ -590,7 +620,7 @@
)
})

it('opts in to stubbing teleport ', () => {

Check warning on line 623 in tests/mountingOptions/global.stubs.spec.ts

View workflow job for this annotation

GitHub Actions / build (22)

vitest(valid-title)

Should not have leading or trailing spaces
const spy = vi.spyOn(console, 'warn')
const Comp = {
template: `<teleport to="body"><div id="content" /></teleport>`
Expand Down
Loading