Skip to content

Commit f687eb7

Browse files
calebebygnapse
andauthored
toBeVisible implies toBeInTheDocument (#339)
Co-authored-by: Ernesto García <gnapse@gmail.com>
1 parent a750063 commit f687eb7

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ This allows you to check if an element is currently visible to the user.
420420

421421
An element is visible if **all** the following conditions are met:
422422

423+
- it is present in the document
423424
- it does not have its css property `display` set to `none`
424425
- it does not have its css property `visibility` set to either `hidden` or
425426
`collapse`

src/__tests__/to-be-visible.js

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {render} from './helpers/test-utils'
2+
import document from './helpers/document'
23

34
describe('.toBeVisible', () => {
45
it('returns the visibility of an element', () => {
@@ -35,6 +36,12 @@ describe('.toBeVisible', () => {
3536
).toThrowError()
3637
})
3738

39+
test('detached element is not visible', () => {
40+
const subject = document.createElement('div')
41+
expect(subject).not.toBeVisible()
42+
expect(() => expect(subject).toBeVisible()).toThrowError()
43+
})
44+
3845
describe('with a <details /> element', () => {
3946
let subject
4047

src/to-be-visible.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ function isElementVisible(element, previousElement) {
3232

3333
export function toBeVisible(element) {
3434
checkHtmlElement(element, toBeVisible, this)
35-
const isVisible = isElementVisible(element)
35+
const isInDocument =
36+
element.ownerDocument === element.getRootNode({composed: true})
37+
const isVisible = isInDocument && isElementVisible(element)
3638
return {
3739
pass: isVisible,
3840
message: () => {
@@ -44,7 +46,9 @@ export function toBeVisible(element) {
4446
'',
4547
),
4648
'',
47-
`Received element ${is} visible:`,
49+
`Received element ${is} visible${
50+
isInDocument ? '' : ' (element is not in the document)'
51+
}:`,
4852
` ${this.utils.printReceived(element.cloneNode(false))}`,
4953
].join('\n')
5054
},

0 commit comments

Comments
 (0)