From f0823c8dff3d2fcb97284a96ce08620e5f174ffc Mon Sep 17 00:00:00 2001 From: Caleb Eby Date: Fri, 5 Feb 2021 10:47:18 -0800 Subject: [PATCH 1/4] toBeVisible implies toBeInTheDocument --- src/__tests__/to-be-visible.js | 8 ++++++++ src/to-be-visible.js | 8 ++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/__tests__/to-be-visible.js b/src/__tests__/to-be-visible.js index a715e2b9..c04c04c6 100644 --- a/src/__tests__/to-be-visible.js +++ b/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', () => { @@ -242,4 +243,11 @@ describe('.toBeVisible', () => { }) }) }) + describe('element not in document', () => { + test('detached element is not visible', () => { + const subject = document.createElement('div') + expect(subject).not.toBeVisible() + expect(() => expect(subject).toBeVisible()).toThrowError() + }) + }) }) diff --git a/src/to-be-visible.js b/src/to-be-visible.js index 8837105e..518b7d56 100644 --- a/src/to-be-visible.js +++ b/src/to-be-visible.js @@ -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: () => { @@ -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') }, From 6550b35a286dd402bfceee159c0f7358f004ec11 Mon Sep 17 00:00:00 2001 From: Caleb Eby Date: Fri, 5 Feb 2021 10:50:14 -0800 Subject: [PATCH 2/4] Update README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 49865515..0fbac949 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ clear to read and to maintain. + - [Installation](#installation) - [Usage](#usage) - [With TypeScript](#with-typescript) @@ -429,6 +430,7 @@ An element is visible if **all** the following conditions are met: [`hidden`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/hidden) attribute - if `
` it has the `open` attribute +- it is present in the document #### Examples From 99b5d782183126dd767085d69a921cf6f71ca601 Mon Sep 17 00:00:00 2001 From: Caleb Eby Date: Tue, 9 Feb 2021 12:55:27 -0800 Subject: [PATCH 3/4] Reorder in readme --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 0fbac949..77104c1e 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,6 @@ clear to read and to maintain. - - [Installation](#installation) - [Usage](#usage) - [With TypeScript](#with-typescript) @@ -421,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` @@ -430,7 +430,6 @@ An element is visible if **all** the following conditions are met: [`hidden`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/hidden) attribute - if `
` it has the `open` attribute -- it is present in the document #### Examples From 88f80a52e38af5407be6d1b97fec546d9b5cb5cd Mon Sep 17 00:00:00 2001 From: Caleb Eby Date: Tue, 9 Feb 2021 12:58:15 -0800 Subject: [PATCH 4/4] Reorder tests --- src/__tests__/to-be-visible.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/__tests__/to-be-visible.js b/src/__tests__/to-be-visible.js index c04c04c6..f2b4277a 100644 --- a/src/__tests__/to-be-visible.js +++ b/src/__tests__/to-be-visible.js @@ -36,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
element', () => { let subject @@ -243,11 +249,4 @@ describe('.toBeVisible', () => { }) }) }) - describe('element not in document', () => { - test('detached element is not visible', () => { - const subject = document.createElement('div') - expect(subject).not.toBeVisible() - expect(() => expect(subject).toBeVisible()).toThrowError() - }) - }) })