Skip to content

Commit

Permalink
fix(findComponent): allow finding top-level component (#1496)
Browse files Browse the repository at this point in the history
* allow finding top-level component when findComponent is chained
from dom node rendered by child
  • Loading branch information
xanf committed May 20, 2022
1 parent 3c55ab1 commit 1e56cc7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/domWrapper.ts
Expand Up @@ -22,7 +22,11 @@ export class DOMWrapper<NodeType extends Node> extends BaseWrapper<NodeType> {
}

getCurrentComponent() {
return this.element.__vueParentComponent
let component = this.element.__vueParentComponent
while (component?.parent?.vnode.el === this.element) {
component = component.parent
}
return component
}

find<K extends keyof HTMLElementTagNameMap>(
Expand Down
29 changes: 29 additions & 0 deletions tests/findComponent.spec.ts
Expand Up @@ -510,5 +510,34 @@ describe('findComponent', () => {
.classes('inside')
).toBe(true)
})

it('finds top component when searching from nested node', () => {
const DeepNestedComponent = defineComponent({
template: '<div class="deep-nested"></div>'
})

const NestedComponent = defineComponent({
components: { DeepNestedComponent },
template: '<deep-nested-component />'
})

const RootComponent = defineComponent({
components: { NestedComponent },
template: '<nested-component />'
})

const wrapper = mount(RootComponent)
expect(
wrapper.find('.deep-nested').findComponent(DeepNestedComponent).exists()
).toBe(true)

expect(
wrapper.find('.deep-nested').findComponent(NestedComponent).exists()
).toBe(true)

expect(
wrapper.find('.deep-nested').findComponent(RootComponent).exists()
).toBe(true)
})
})
})

0 comments on commit 1e56cc7

Please sign in to comment.