Skip to content

Commit

Permalink
fix: do not match component name on getter that returns component (#1312
Browse files Browse the repository at this point in the history
)

* test: Add failing tests for shallowMount issue with dynamic components

* fix: Do not match component name on getter that returns component obj

* test: Removing extraneous/illustrative test code
  • Loading branch information
phobetron committed Feb 16, 2022
1 parent 8e71d4e commit c3046b6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/utils/componentName.ts
Expand Up @@ -11,7 +11,8 @@ const getComponentNameInSetup = (
type: VNodeTypes
): string | undefined =>
Object.keys(instance?.setupState || {}).find(
(key) => instance.setupState[key] === type
(key) =>
Object.getOwnPropertyDescriptor(instance.setupState, key)?.value === type
)

export const getComponentRegisteredName = (
Expand Down
10 changes: 10 additions & 0 deletions tests/components/DynamicComponentWithComputedProperty.vue
@@ -0,0 +1,10 @@
<template>
<component :is="computedProperty" />
</template>

<script setup lang="ts">
import { computed } from 'vue'
import Hello from './Hello.vue'
const computedProperty = computed(() => Hello)
</script>
13 changes: 13 additions & 0 deletions tests/shallowMount.spec.ts
Expand Up @@ -2,6 +2,7 @@ import { defineAsyncComponent, defineComponent } from 'vue'
import { mount, shallowMount, VueWrapper } from '../src'
import ComponentWithChildren from './components/ComponentWithChildren.vue'
import ScriptSetupWithChildren from './components/ScriptSetupWithChildren.vue'
import DynamicComponentWithComputedProperty from './components/DynamicComponentWithComputedProperty.vue'

describe('shallowMount', () => {
it('renders props for stubbed component in a snapshot', () => {
Expand Down Expand Up @@ -196,4 +197,16 @@ describe('shallowMount', () => {
'</div>'
)
})

it('stubs a given component that is also returned by a computed property', () => {
const wrapper = shallowMount(DynamicComponentWithComputedProperty)

expect(wrapper.find('hello-stub').exists()).toBe(true)
})

it('does not attempt to stub a dynamic component based on the name of a computed property', () => {
const wrapper = shallowMount(DynamicComponentWithComputedProperty)

expect(wrapper.find('computed-property-stub').exists()).toBe(false)
})
})

0 comments on commit c3046b6

Please sign in to comment.