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: Stub instance of the same component #1979

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion src/mount.ts
Expand Up @@ -342,7 +342,8 @@ export function mount(
component = originalComponent
}

addToDoNotStubComponents(component)
// Don't stub component only on root level, but still stub if used inside
addToDoNotStubComponents(component, true)
// We've just replaced our component with its copy
// Let's register it as a stub so user can find it
registerStub({ source: originalComponent, stub: component })
Expand Down
15 changes: 11 additions & 4 deletions src/vnodeTransformers/stubComponentsTransformer.ts
Expand Up @@ -36,10 +36,14 @@ interface StubOptions {
renderStubDefaultSlot?: boolean
}

const doNotStubComponents: WeakSet<ConcreteComponent> = new WeakSet()
const doNotStubComponents = new WeakMap<ConcreteComponent, boolean>()
const shouldNotStub = (type: ConcreteComponent) => doNotStubComponents.has(type)
export const addToDoNotStubComponents = (type: ConcreteComponent) =>
doNotStubComponents.add(type)
const shouldNotStubRoot = (type: ConcreteComponent) =>
!!doNotStubComponents.get(type)
export const addToDoNotStubComponents = (
type: ConcreteComponent,
onlyRoot?: boolean
) => doNotStubComponents.set(type, !!onlyRoot)
Copy link
Member

Choose a reason for hiding this comment

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

maybe we could have two distinct functions, with different names to make it easier to understand?
like addToDoNotStubComponentsButOnlyInRoot? (I admit it's not a great name ^^)


const normalizeStubProps = (props: ComponentPropsOptions) => {
// props are always normalized to object syntax
Expand Down Expand Up @@ -163,7 +167,10 @@ export function createStubComponentsTransformer({
}

if (shouldNotStub(type)) {
return type
// Either don't stub everytime or only on root level
if (!instance?.parent || !shouldNotStubRoot(type)) {
return type
}
}

const registeredName = getComponentRegisteredName(instance, type)
Expand Down
14 changes: 14 additions & 0 deletions tests/components/RecursiveComponent.vue
@@ -0,0 +1,14 @@
<template>
<div>
<Hello />
<RecursiveComponent v-if="first" />
</div>
</template>

<script setup lang="ts">
import Hello from './Hello.vue'

defineProps<{
first?: boolean
}>()
</script>
16 changes: 16 additions & 0 deletions tests/shallowMount.spec.ts
Expand Up @@ -4,6 +4,7 @@ import { mount, shallowMount, VueWrapper } from '../src'
import ComponentWithChildren from './components/ComponentWithChildren.vue'
import ScriptSetupWithChildren from './components/ScriptSetupWithChildren.vue'
import DynamicComponentWithComputedProperty from './components/DynamicComponentWithComputedProperty.vue'
import RecursiveComponent from './components/RecursiveComponent.vue'

describe('shallowMount', () => {
it('renders props for stubbed component in a snapshot', () => {
Expand Down Expand Up @@ -73,6 +74,21 @@ describe('shallowMount', () => {
)
})

it('stub instance of same component', () => {
const wrapper = mount(RecursiveComponent, {
shallow: true,
props: {
first: true
}
})
expect(wrapper.html()).toEqual(
'<div>\n' +
' <hello-stub></hello-stub>\n' +
' <recursive-component-stub first="false"></recursive-component-stub>\n' +
'</div>'
)
})

it('correctly renders slot content', () => {
const ComponentWithSlot = defineComponent({
template: '<div><slot></slot></div>'
Expand Down