@@ -47,6 +47,7 @@ to maintain.
47
47
* [ ` toHaveAttribute ` ] ( #tohaveattribute )
48
48
* [ ` toHaveClass ` ] ( #tohaveclass )
49
49
* [ ` toHaveStyle ` ] ( #tohavestyle )
50
+ * [ ` toBeVisible ` ] ( #tobevisible )
50
51
* [ Inspiration] ( #inspiration )
51
52
* [ Other Solutions] ( #other-solutions )
52
53
* [ Guiding Principles] ( #guiding-principles )
@@ -194,6 +195,35 @@ This also works with rules that are applied to the element via a class name for
194
195
which some rules are defined in a stylesheet currently active in the document.
195
196
The usual rules of css precedence apply.
196
197
198
+ ### ` toBeVisible `
199
+
200
+ This allows you to check if an element is currently visible to the user.
201
+
202
+ An element is visible if ** all** the following conditions are met:
203
+
204
+ * it does not have its css property ` display ` set to ` none `
205
+ * it does not have its css property ` visibility ` set to either ` hidden ` or
206
+ ` collapse `
207
+ * it does not have its css property ` opacity ` set to ` 0 `
208
+ * its parent element is also visible (and so on up to the top of the DOM tree)
209
+
210
+ ``` javascript
211
+ // add the custom expect matchers once
212
+ import ' jest-dom/extend-expect'
213
+
214
+ // ...
215
+ // <header>
216
+ // <h1 style="display: none">Page title</h1>
217
+ // </header>
218
+ // <section>
219
+ // <p style="visibility: hidden">Hello <strong>World</strong></h1>
220
+ // </section>
221
+ expect (container .querySelector (' header' )).toBeVisible ()
222
+ expect (container .querySelector (' h1' )).not .toBeVisible ()
223
+ expect (container .querySelector (' strong' )).not .toBeVisible ()
224
+ // ...
225
+ ```
226
+
197
227
## Inspiration
198
228
199
229
This whole library was extracted out of Kent C. Dodds' [ dom-testing-library] [ ] ,
0 commit comments