diff --git a/src/domWrapper.ts b/src/domWrapper.ts index 3e13dbb89..af075be73 100644 --- a/src/domWrapper.ts +++ b/src/domWrapper.ts @@ -34,6 +34,10 @@ export class DOMWrapper ): DOMWrapper find(selector: string): DOMWrapper find(selector: string): DOMWrapper { + // allow finding the root element + if (this.element.matches(selector)) { + return this + } const result = this.element.querySelector(selector) if (result) { return new DOMWrapper(result) diff --git a/tests/find.spec.ts b/tests/find.spec.ts index aa54bb1f3..f8114491b 100644 --- a/tests/find.spec.ts +++ b/tests/find.spec.ts @@ -39,7 +39,7 @@ describe('find', () => { - + `, components: { ComponentB } }) @@ -63,6 +63,18 @@ describe('find', () => { expect(wrapper.find('.foo').exists()).toBe(true) }) + it('returns the root element from dom wrapper if it matches', () => { + const Component = defineComponent({ + render() { + return h('div', { class: 'foo' }, 'text') + } + }) + + const wrapper = mount(Component) + const domWrapper = wrapper.find('.foo') + expect(domWrapper.find('.foo').exists()).toBe(true) + }) + it('can be chained', () => { const Component = defineComponent({ render() {