Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return the correct name when stubbing a script setup component #1783

Merged
merged 4 commits into from Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -52,6 +52,7 @@
"rollup-plugin-typescript2": "0.34.0",
"tslib": "2.4.0",
"typescript": "4.8.3",
"unplugin-vue-components": "0.22.7",
"vite": "3.1.3",
"vitepress": "0.22.4",
"vitest": "0.22.1",
Expand Down
117 changes: 114 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/utils/componentName.ts
Expand Up @@ -38,7 +38,12 @@ export const getComponentName = (
type: VNodeTypes
): string => {
if (isObjectComponent(type)) {
return getComponentNameInSetup(instance, type) || type.name || ''
return (
// If the component we stub is a script setup component and is automatically
// imported by unplugin-vue-components we can only get its name through
// the `__name` property.
getComponentNameInSetup(instance, type) || type.name || type.__name || ''
hershelh marked this conversation as resolved.
Show resolved Hide resolved
)
}

if (isLegacyExtendedComponent(type)) {
Expand Down
3 changes: 3 additions & 0 deletions tests/components/AutoImportScriptSetup.vue
@@ -0,0 +1,3 @@
<template>
<ScriptSetup/>
</template>
12 changes: 12 additions & 0 deletions tests/mountingOptions/global.stubs.spec.ts
Expand Up @@ -6,6 +6,7 @@ import Hello from '../components/Hello.vue'
import ComponentWithoutName from '../components/ComponentWithoutName.vue'
import ComponentWithSlots from '../components/ComponentWithSlots.vue'
import ScriptSetupWithChildren from '../components/ScriptSetupWithChildren.vue'
import AutoImportScriptSetup from '../components/AutoImportScriptSetup.vue'

describe('mounting options: stubs', () => {
let configStubsSave = config.global.stubs
Expand Down Expand Up @@ -393,6 +394,17 @@ describe('mounting options: stubs', () => {
)
})

it('stubs a script setup component imported by unplugin-vue-components', () => {
const wrapper = mount(AutoImportScriptSetup, {
global: {
stubs: {
ScriptSetup: true
}
}
})
expect(wrapper.html()).toBe(`<script-setup-stub></script-setup-stub>`)
})

describe('transition', () => {
it('stubs transition by default', () => {
const CompStubbedByDefault = {
Expand Down
14 changes: 13 additions & 1 deletion vitest.config.ts
Expand Up @@ -2,9 +2,21 @@ import path from 'path'
import { defineConfig } from 'vitest/config'
import vue from '@vitejs/plugin-vue'
import jsx from '@vitejs/plugin-vue-jsx'
import Components from 'unplugin-vue-components/vite'

export default defineConfig({
plugins: [vue(), jsx()],
plugins: [
vue(),
jsx(),
// We need this plugin to test for stubbing a script setup component
// imported by it.
// https://github.com/antfu/unplugin-vue-components/issues/429
Components({
hershelh marked this conversation as resolved.
Show resolved Hide resolved
dts: false,
include: /AutoImportScriptSetup\.vue$/,
dirs: ['tests/components']
})
],
define: {
__USE_BUILD__: process.env.NODE_ENV !== 'test-build',
__BROWSER__: true,
Expand Down