diff --git a/src/mount.ts b/src/mount.ts index e0d8e379d..f3c383d96 100644 --- a/src/mount.ts +++ b/src/mount.ts @@ -35,7 +35,12 @@ import { import { processSlot } from './utils/compileSlots' import { createWrapper, VueWrapper } from './vueWrapper' import { attachEmitListener } from './emit' -import { createStub, stubComponents, addToDoNotStubComponents } from './stubs' +import { + createStub, + stubComponents, + addToDoNotStubComponents, + registerStub +} from './stubs' import { isLegacyFunctionalComponent, unwrapLegacyVueExtendComponent @@ -244,6 +249,7 @@ export function mount( } addToDoNotStubComponents(component) + registerStub(originalComponent, component) const el = document.createElement('div') if (options?.attachTo) { diff --git a/src/stubs.ts b/src/stubs.ts index e91680651..886e5540f 100644 --- a/src/stubs.ts +++ b/src/stubs.ts @@ -26,7 +26,13 @@ interface StubOptions { renderStubDefaultSlot?: boolean } -const stubsMap: WeakMap = new WeakMap() +const stubsMap: WeakMap = new WeakMap() +export const registerStub = ( + source: ConcreteComponent, + stub: ConcreteComponent +) => { + stubsMap.set(stub, source) +} export const getOriginalVNodeTypeFromStub = ( type: ConcreteComponent @@ -119,7 +125,7 @@ const getComponentName = (type: VNodeTypes): string => { } function createStubOnceForType( - type: {} & VNodeTypes, + type: ConcreteComponent, factoryFn: () => ConcreteComponent, cache: WeakMap<{} & VNodeTypes, ConcreteComponent> ): ConcreteComponent { @@ -129,7 +135,7 @@ function createStubOnceForType( } const stub = factoryFn() - stubsMap.set(stub, type) + registerStub(type, stub) cache.set(type, stub) return stub } @@ -143,7 +149,7 @@ export function stubComponents( new WeakMap() const createStubOnce = ( - type: {} & VNodeTypes, + type: ConcreteComponent, factoryFn: () => ConcreteComponent ) => createStubOnceForType(type, factoryFn, createdStubsMap) @@ -216,6 +222,8 @@ export function stubComponents( type, () => specializedStubComponent ) + specializedStub.props = stub.props + registerStub(type, specializedStub) // pass the props and children, for advanced stubbing return [specializedStub, props, children, patchFlag, dynamicProps] } diff --git a/tests/findComponent.spec.ts b/tests/findComponent.spec.ts index d212ecfaf..a7b4a146d 100644 --- a/tests/findComponent.spec.ts +++ b/tests/findComponent.spec.ts @@ -113,6 +113,22 @@ describe('findComponent', () => { expect(wrapper.findComponent(Comp).props('depth')).toBe(0) }) + it('finds root component without name', async () => { + const Comp = defineComponent({ + template: ` + + {{ msg }} + `, + data() { + return { msg: 'foo' } + } + }) + const wrapper = mount(Comp) + expect(wrapper.findComponent(Comp).exists()).toBe(true) + await wrapper.find('input').setValue('bar') + expect(wrapper.html()).toContain('bar') + }) + it('finds component without a name by using its object definition', () => { const Component = { template: '
', diff --git a/tests/getComponent.spec.ts b/tests/getComponent.spec.ts index 9f084f249..a642b7516 100644 --- a/tests/getComponent.spec.ts +++ b/tests/getComponent.spec.ts @@ -48,7 +48,7 @@ describe('getComponent', () => { }) it('should throw if not found with a component selector that has no name', () => { - const wrapper = mount(compA) + const wrapper = mount(compB) expect(() => wrapper.getComponent(compA)).toThrowError( 'Unable to get specified component within:
' )