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

toBeVisible implies toBeInTheDocument #339

Merged
merged 5 commits into from Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -420,6 +420,7 @@ This allows you to check if an element is currently visible to the user.

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

- it is present in the document
- it does not have its css property `display` set to `none`
- it does not have its css property `visibility` set to either `hidden` or
`collapse`
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/to-be-visible.js
@@ -1,4 +1,5 @@
import {render} from './helpers/test-utils'
import document from './helpers/document'

describe('.toBeVisible', () => {
it('returns the visibility of an element', () => {
Expand Down Expand Up @@ -35,6 +36,12 @@ describe('.toBeVisible', () => {
).toThrowError()
})

test('detached element is not visible', () => {
const subject = document.createElement('div')
expect(subject).not.toBeVisible()
expect(() => expect(subject).toBeVisible()).toThrowError()
})

describe('with a <details /> element', () => {
let subject

Expand Down
8 changes: 6 additions & 2 deletions src/to-be-visible.js
Expand Up @@ -32,7 +32,9 @@ function isElementVisible(element, previousElement) {

export function toBeVisible(element) {
checkHtmlElement(element, toBeVisible, this)
const isVisible = isElementVisible(element)
const isInDocument =
element.ownerDocument === element.getRootNode({composed: true})
const isVisible = isInDocument && isElementVisible(element)
return {
pass: isVisible,
message: () => {
Expand All @@ -44,7 +46,9 @@ export function toBeVisible(element) {
'',
),
'',
`Received element ${is} visible:`,
`Received element ${is} visible${
isInDocument ? '' : ' (element is not in the document)'
}:`,
` ${this.utils.printReceived(element.cloneNode(false))}`,
].join('\n')
},
Expand Down