Skip to content

Commit

Permalink
fix(find): Allow finding self from DOMWrapper (#1077)
Browse files Browse the repository at this point in the history
  • Loading branch information
xanf authored Nov 17, 2021
1 parent 1809ef0 commit 60e47a2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/domWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export class DOMWrapper<ElementType extends Element>
): DOMWrapper<SVGElementTagNameMap[K]>
find<T extends Element>(selector: string): DOMWrapper<T>
find(selector: string): DOMWrapper<Element> {
// allow finding the root element
if (this.element.matches(selector)) {
return this
}
const result = this.element.querySelector(selector)
if (result) {
return new DOMWrapper(result)
Expand Down
14 changes: 13 additions & 1 deletion tests/find.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('find', () => {
<component-b v-for="item in [1, 2]" :key="item">
<input type="text" :value="item">
</component-b>
</div>
</div>
`,
components: { ComponentB }
})
Expand All @@ -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() {
Expand Down

0 comments on commit 60e47a2

Please sign in to comment.