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

feat: Added dynamic return for element getter #2406

Merged
merged 2 commits into from
Apr 9, 2024
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
2 changes: 1 addition & 1 deletion src/vueWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export class VueWrapper<
}
}

get element(): Element {
get element(): T['$el'] {
// if the component has multiple root elements, we use the parent's element
return this.hasMultipleRoots ? this.parentElement : this.vm.$el
}
Expand Down
10 changes: 10 additions & 0 deletions test-dts/wrapper.d-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ const domWrapper = wrapper.find('#other')
let inputMaybe = wrapper.find('input')
expectType<HTMLInputElement | undefined>(inputMaybe.element)

let buttonComp = mount(
defineComponent({ template: '<button>Click me</button>' })
)
expectType<HTMLButtonElement>(buttonComp.element)

Copy link
Member

Choose a reason for hiding this comment

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

Do we have a test for the case with multiple root elements?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a type test and here's an actual test for it: https://github.com/vuejs/test-utils/blob/main/tests/element.spec.ts#L46

let multiRootComp = mount(defineComponent({ template: '<span /><span />' }))
// this will be a wrapper div element like so: <div data-v-app="">
expectType<HTMLDivElement>(multiRootComp.element)
expectType<HTMLSpanElement>(multiRootComp.element.firstElementChild)

// SVG element selector
let lineMaybe = wrapper.find('line')
expectType<SVGLineElement | undefined>(lineMaybe.element)
Expand Down