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: treat shadow DOM elements as in the document #298

Merged
merged 1 commit into from Oct 21, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/__tests__/to-be-in-the-document.js
Expand Up @@ -2,19 +2,37 @@ import {HtmlElementTypeError} from '../utils'
import document from './helpers/document'

test('.toBeInTheDocument', () => {
const window = document.defaultView

window.customElements.define(
'custom-element',
class extends window.HTMLElement {
constructor() {
super()
this.attachShadow({mode: 'open'}).innerHTML =
'<div data-testid="custom-element-child"></div>'
}
},
)

document.body.innerHTML = `
<span data-testid="html-element"><span>Html Element</span></span>
<svg data-testid="svg-element"></svg>`
<svg data-testid="svg-element"></svg>
<custom-element data-testid="custom-element"></custom-element>`

const htmlElement = document.querySelector('[data-testid="html-element"]')
const svgElement = document.querySelector('[data-testid="svg-element"]')
const customElementChild = document
.querySelector('[data-testid="custom-element"]')
.shadowRoot.querySelector('[data-testid="custom-element-child"]')
const detachedElement = document.createElement('div')
const fakeElement = {thisIsNot: 'an html element'}
const undefinedElement = undefined
const nullElement = null

expect(htmlElement).toBeInTheDocument()
expect(svgElement).toBeInTheDocument()
expect(customElementChild).toBeInTheDocument()
expect(detachedElement).not.toBeInTheDocument()
expect(nullElement).not.toBeInTheDocument()

Expand Down
4 changes: 3 additions & 1 deletion src/to-be-in-the-document.js
Expand Up @@ -6,7 +6,9 @@ export function toBeInTheDocument(element) {
}

const pass =
element === null ? false : element.ownerDocument.contains(element)
element === null
? false
: element.ownerDocument === element.getRootNode({composed: true})

const errorFound = () => {
return `expected document not to contain element, found ${this.utils.stringify(
Expand Down