File tree 4 files changed +72
-3
lines changed
4 files changed +72
-3
lines changed Original file line number Diff line number Diff line change @@ -813,6 +813,37 @@ test('queryAllByRole returns semantic html elements', () => {
813
813
expect ( queryAllByRole ( 'listbox' ) ) . toHaveLength ( 1 )
814
814
} )
815
815
816
+ test ( 'getAllByRole matchers with strictVisibility enabled and disabled' , ( ) => {
817
+ const { getAllByRole} = render ( `
818
+ <div aria-hidden="true">
819
+ <button>Aria Hidden Button</button>
820
+ </div>
821
+ <div style="display: none;">
822
+ <button>Display None Button</button>
823
+ </div>
824
+ <div style="visibility: hidden;">
825
+ <button>Visibility Hidden Button</button>
826
+ </div>
827
+ <div style="visibility: hidden;">
828
+ <div>
829
+ <button>Deep Visibility Hidden Button</button>
830
+ </div>
831
+ </div>
832
+ <div>
833
+ <button>Visible Button</button>
834
+ </div>
835
+ ` )
836
+
837
+ const defaultResults = getAllByRole ( 'button' )
838
+ expect ( defaultResults ) . toHaveLength ( 1 )
839
+
840
+ const strictResults = getAllByRole ( 'button' , { strictVisibilityCheck : true } )
841
+ expect ( strictResults ) . toHaveLength ( 1 )
842
+
843
+ const looseResults = getAllByRole ( 'button' , { strictVisibilityCheck : false } )
844
+ expect ( looseResults ) . toHaveLength ( 1 )
845
+ } )
846
+
816
847
test ( 'getAll* matchers return an array' , ( ) => {
817
848
const {
818
849
getAllByAltText,
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ import {
24
24
prettyRoles ,
25
25
isInaccessible ,
26
26
isSubtreeInaccessible ,
27
+ isInaccessibleLoose ,
27
28
} from '../role-helpers'
28
29
import { wrapAllByQueryWithSuggestion } from '../query-helpers'
29
30
import { checkContainerType } from '../helpers'
@@ -54,6 +55,7 @@ const queryAllByRole: AllByRole = (
54
55
current,
55
56
level,
56
57
expanded,
58
+ strictVisibilityCheck = true ,
57
59
value : {
58
60
now : valueNow ,
59
61
min : valueMin ,
@@ -297,9 +299,11 @@ const queryAllByRole: AllByRole = (
297
299
} )
298
300
. filter ( element => {
299
301
return hidden === false
300
- ? isInaccessible ( element , {
301
- isSubtreeInaccessible : cachedIsSubtreeInaccessible ,
302
- } ) === false
302
+ ? strictVisibilityCheck
303
+ ? isInaccessible ( element , {
304
+ isSubtreeInaccessible : cachedIsSubtreeInaccessible ,
305
+ } ) === false
306
+ : isInaccessibleLoose ( element ) === false
303
307
: true
304
308
} )
305
309
}
Original file line number Diff line number Diff line change @@ -65,6 +65,28 @@ function isInaccessible(element, options = {}) {
65
65
return false
66
66
}
67
67
68
+ /**
69
+ * A fast check to see if an element is inaccessible.
70
+ * @param {Element } element
71
+ * @returns {boolean } true if exluded, otherwise false
72
+ */
73
+ function isInaccessibleLoose ( element ) {
74
+ do {
75
+ const explictlyHidden =
76
+ element . style . visibility === 'hidden' || element . style . display === 'none'
77
+ if ( explictlyHidden ) {
78
+ return true
79
+ }
80
+
81
+ const ariaHidden = element . getAttribute ( 'aria-hidden' ) === 'true'
82
+ if ( ariaHidden ) {
83
+ return true
84
+ }
85
+ } while ( ( element = element . parentElement ) )
86
+
87
+ return false
88
+ }
89
+
68
90
function getImplicitAriaRoles ( currentNode ) {
69
91
// eslint bug here:
70
92
// eslint-disable-next-line no-unused-vars
@@ -382,6 +404,7 @@ export {
382
404
isSubtreeInaccessible ,
383
405
prettyRoles ,
384
406
isInaccessible ,
407
+ isInaccessibleLoose ,
385
408
computeAriaSelected ,
386
409
computeAriaBusy ,
387
410
computeAriaChecked ,
Original file line number Diff line number Diff line change @@ -110,6 +110,17 @@ export interface ByRoleOptions {
110
110
* the `aria-level` attribute.
111
111
*/
112
112
level ?: number
113
+
114
+ /**
115
+ * Whether or not to strictly check the visibliity of the element. Doing so
116
+ * can cause issues with the performance of tests, because it requires the DOM tree
117
+ * is traversed, and the `getComputedStyle` function is called on each element.
118
+ *
119
+ * If you're finding that your tests are slow, you may want to disable this option.
120
+ * @default true
121
+ */
122
+ strictVisibilityCheck ?: boolean
123
+
113
124
value ?: {
114
125
now ?: number
115
126
min ?: number
You can’t perform that action at this time.
0 commit comments