Skip to content
Permalink
Browse files

fix: treat shadow DOM elements as in the document (#298)

  • Loading branch information
geoffrich committed Oct 21, 2020
1 parent 3fb1835 commit 7ee54abf20aa4c52f0bc0fa862999a98f7ffbc3f
Showing with 22 additions and 2 deletions.
  1. +19 −1 src/__tests__/to-be-in-the-document.js
  2. +3 −1 src/to-be-in-the-document.js
@@ -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()

@@ -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(

0 comments on commit 7ee54ab

Please sign in to comment.
You can’t perform that action at this time.