diff --git a/src/vueWrapper.ts b/src/vueWrapper.ts index 4e7a17679..195851bc2 100644 --- a/src/vueWrapper.ts +++ b/src/vueWrapper.ts @@ -164,11 +164,6 @@ export class VueWrapper } } - const result = find(this.vm.$.subTree, selector) - if (result.length) { - return createWrapper(null, result[0]) - } - // https://github.com/vuejs/vue-test-utils-next/issues/211 // VTU v1 supported finding the component mounted itself. // eg: mount(Comp).findComponent(Comp) @@ -177,6 +172,11 @@ export class VueWrapper return createWrapper(null, this.vm.$.vnode.component?.proxy!) } + const result = find(this.vm.$.subTree, selector) + if (result.length) { + return createWrapper(null, result[0]) + } + return createWrapperError('VueWrapper') } diff --git a/tests/findComponent.spec.ts b/tests/findComponent.spec.ts index 103fa6f84..6684706a4 100644 --- a/tests/findComponent.spec.ts +++ b/tests/findComponent.spec.ts @@ -67,15 +67,6 @@ describe('findComponent', () => { ) }) - it('finds a component when root of mounted component', async () => { - const wrapper = mount(compD) - // make sure it finds the component, not its root - expect(wrapper.findComponent('.c-as-root-on-d').vm).toHaveProperty( - '$options.name', - 'ComponentC' - ) - }) - it('finds component by name', () => { const wrapper = mount(compA) expect(wrapper.findComponent({ name: 'Hello' }).text()).toBe('Hello world') @@ -100,6 +91,19 @@ describe('findComponent', () => { expect(wrapper.html()).toContain('bar') }) + it('finds root component when recursion is used', async () => { + const Comp = defineComponent({ + props: ['depth'], + name: 'Comp', + template: ` + + Depth {{ depth }} + ` + }) + const wrapper = mount(Comp, { props: { depth: 0 } }) + expect(wrapper.findComponent(Comp).props('depth')).toBe(0) + }) + it('finds component without a name by using its object definition', () => { const Component = { template: '
',