Skip to content

fix: return wrapper error from DOMWrapper ctor if element is nullish #1996

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

Merged
merged 1 commit into from
Mar 9, 2023
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: 1 addition & 5 deletions docs/guide/extending-vtu/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,7 @@ const DataTestIdPlugin = (wrapper) => {
function findByTestId(selector) {
const dataSelector = `[data-testid='${selector}']`
const element = wrapper.element.querySelector(dataSelector)
if (element) {
return new DOMWrapper(element)
}

return createWrapperError('DOMWrapper')
return new DOMWrapper(element)
}

return {
Expand Down
5 changes: 4 additions & 1 deletion src/domWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import { isRefSelector } from './utils'
import { createWrapperError } from './errorWrapper'

export class DOMWrapper<NodeType extends Node> extends BaseWrapper<NodeType> {
constructor(element: NodeType) {
constructor(element: NodeType | null | undefined) {
if (!element) {
return createWrapperError('DOMWrapper')
}
super(element)
// plugins hook
config.plugins.DOMWrapper.extend(this)
Expand Down
4 changes: 3 additions & 1 deletion src/wrapperFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export enum WrapperType {
VueWrapper
}

type DOMWrapperFactory = <T extends Node>(element: T) => DOMWrapperType<T>
type DOMWrapperFactory = <T extends Node>(
element: T | null | undefined
) => DOMWrapperType<T>
type VueWrapperFactory = <T extends ComponentPublicInstance>(
app: App | null,
vm: T,
Expand Down
7 changes: 6 additions & 1 deletion tests/exists.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest'
import { h, defineComponent } from 'vue'

import { mount } from '../src'
import { DOMWrapper, mount } from '../src'

describe('exists', () => {
it('returns false when element does not exist', () => {
Expand Down Expand Up @@ -50,4 +50,9 @@ describe('exists', () => {
await wrapper.setProps({ hide: true })
expect(child.exists()).toBe(false)
})

it('returns false when wrapper is manually constructed against nullish element', () => {
const wrapper = new DOMWrapper(null)
expect(wrapper.exists()).toBe(false)
})
})
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy