Skip to content

Commit

Permalink
fix: treat shadow DOM elements as in the document (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrich committed Oct 21, 2020
1 parent 3fb1835 commit 7ee54ab
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
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

0 comments on commit 7ee54ab

Please sign in to comment.