Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(find): Allow finding self from DOMWrapper #1077

Merged
merged 1 commit into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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