From 9c9e8bf9bbc8c55051ccd790cb9ed842be2df9b2 Mon Sep 17 00:00:00 2001 From: Illya Klymov Date: Mon, 28 Jun 2021 16:33:45 +0300 Subject: [PATCH] fix: find component by its definition when using stub without name --- src/stubs.ts | 6 ++++-- tests/findComponent.spec.ts | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/stubs.ts b/src/stubs.ts index 7fd443d3bf..96ea5fcd01 100644 --- a/src/stubs.ts +++ b/src/stubs.ts @@ -8,7 +8,8 @@ import { ComponentOptions, defineComponent, VNodeProps, - VNodeTypes + VNodeTypes, + ConcreteComponent } from 'vue' import { hyphenate } from './utils/vueShared' import { MOUNT_COMPONENT_REF, MOUNT_PARENT_NAME } from './constants' @@ -23,7 +24,7 @@ interface StubOptions { renderStubDefaultSlot?: boolean } -const stubsMap: WeakMap = new WeakMap() +const stubsMap: WeakMap = new WeakMap() export const getOriginalVNodeTypeFromStub = ( type: ComponentOptions @@ -184,6 +185,7 @@ export function stubComponents( // case 2: custom implementation if (stub && stub !== true) { + stubsMap.set(stubs[name], type) // pass the props and children, for advanced stubbing return [stubs[name], props, children, patchFlag, dynamicProps] } diff --git a/tests/findComponent.spec.ts b/tests/findComponent.spec.ts index 693bf5c651..33f259d504 100644 --- a/tests/findComponent.spec.ts +++ b/tests/findComponent.spec.ts @@ -122,6 +122,24 @@ describe('findComponent', () => { expect(wrapper.findComponent(Hello).exists()).toBe(true) }) + it('finds a component by its definition when using stub without name', () => { + const StubWithoutName = { + template: '
stub-without-name
' + } + + const Component = { + template: '
', + components: { + OtherName: Hello + } + } + + const wrapper = mount(Component, { + global: { stubs: { Hello: StubWithoutName } } + }) + expect(wrapper.findComponent(Hello).exists()).toBe(true) + }) + it('finds a component without a name by its locally assigned name', () => { const Component = { template: '
',