From 60e47a29cdf00cd2eadcfc5cec12df3ea6d4e1f7 Mon Sep 17 00:00:00 2001 From: Illya Klymov Date: Wed, 17 Nov 2021 13:28:55 +0200 Subject: [PATCH] fix(find): Allow finding self from DOMWrapper (#1077) --- src/domWrapper.ts | 4 ++++ tests/find.spec.ts | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) 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() {