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: Gracefully handle empty root node for nested component #660

Merged
merged 3 commits into from
Jun 12, 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
6 changes: 4 additions & 2 deletions src/vueWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class VueWrapper<T extends ComponentPublicInstance>
const result = this.parentElement['__vue_app__']
? // force using the parentElement to allow finding the root element
this.parentElement.querySelector(selector)
: this.element.querySelector(selector)
: this.element.querySelector && this.element.querySelector(selector)

if (result) {
return new DOMWrapper(result)
Expand Down Expand Up @@ -223,7 +223,9 @@ export class VueWrapper<T extends ComponentPublicInstance>
findAll(selector: string): DOMWrapper<Element>[] {
const results = this.parentElement['__vue_app__']
? this.parentElement.querySelectorAll(selector)
: this.element.querySelectorAll(selector)
: this.element.querySelectorAll
? this.element.querySelectorAll(selector)
: ([] as unknown as NodeListOf<Element>)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: is this cast really necessary here? I would have thought that returning a simple array would be enough.

Copy link
Member

@cexbrayat cexbrayat Jun 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave it a quick try, and it looks like it's unnecessary. I'll make a PR to remove it. (update: see #662)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad: it was necessary!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, tsd doesn't like it :-P


return Array.from(results).map((element) => new DOMWrapper(element))
}
Expand Down
32 changes: 32 additions & 0 deletions tests/find.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ describe('find', () => {
)
expect(foundElement.exists()).toBeFalsy()
})

test('handle empty root node', () => {
const EmptyTestComponent = {
name: 'EmptyTestComponent',
render: () => null
}
const Component = defineComponent({
render() {
return h('div', [h(EmptyTestComponent)])
}
})

const wrapper = mount(Component)
const etc = wrapper.findComponent({ name: 'EmptyTestComponent' })
expect(etc.find('p').exists()).toBe(false)
})
})

describe('findAll', () => {
Expand Down Expand Up @@ -178,4 +194,20 @@ describe('findAll', () => {

expect(wrapper.find('#foo').find('#bar').exists()).toBe(true)
})

test('handle empty/comment root node', () => {
const EmptyTestComponent = {
name: 'EmptyTestComponent',
render: () => null
}
const Component = defineComponent({
render() {
return h('div', [h(EmptyTestComponent)])
}
})

const wrapper = mount(Component)
const etc = wrapper.findComponent({ name: 'EmptyTestComponent' })
expect(etc.findAll('p')).toHaveLength(0)
})
})