Skip to content

Commit 7ee54ab

Browse files
authored
fix: treat shadow DOM elements as in the document (#298)
1 parent 3fb1835 commit 7ee54ab

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/__tests__/to-be-in-the-document.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,37 @@ import {HtmlElementTypeError} from '../utils'
22
import document from './helpers/document'
33

44
test('.toBeInTheDocument', () => {
5+
const window = document.defaultView
6+
7+
window.customElements.define(
8+
'custom-element',
9+
class extends window.HTMLElement {
10+
constructor() {
11+
super()
12+
this.attachShadow({mode: 'open'}).innerHTML =
13+
'<div data-testid="custom-element-child"></div>'
14+
}
15+
},
16+
)
17+
518
document.body.innerHTML = `
619
<span data-testid="html-element"><span>Html Element</span></span>
7-
<svg data-testid="svg-element"></svg>`
20+
<svg data-testid="svg-element"></svg>
21+
<custom-element data-testid="custom-element"></custom-element>`
822

923
const htmlElement = document.querySelector('[data-testid="html-element"]')
1024
const svgElement = document.querySelector('[data-testid="svg-element"]')
25+
const customElementChild = document
26+
.querySelector('[data-testid="custom-element"]')
27+
.shadowRoot.querySelector('[data-testid="custom-element-child"]')
1128
const detachedElement = document.createElement('div')
1229
const fakeElement = {thisIsNot: 'an html element'}
1330
const undefinedElement = undefined
1431
const nullElement = null
1532

1633
expect(htmlElement).toBeInTheDocument()
1734
expect(svgElement).toBeInTheDocument()
35+
expect(customElementChild).toBeInTheDocument()
1836
expect(detachedElement).not.toBeInTheDocument()
1937
expect(nullElement).not.toBeInTheDocument()
2038

src/to-be-in-the-document.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ export function toBeInTheDocument(element) {
66
}
77

88
const pass =
9-
element === null ? false : element.ownerDocument.contains(element)
9+
element === null
10+
? false
11+
: element.ownerDocument === element.getRootNode({composed: true})
1012

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

0 commit comments

Comments
 (0)