@@ -93,6 +93,33 @@ export class ElementAssertion extends Assertion<ReactTestInstance> {
93
93
} ) ;
94
94
}
95
95
96
+ /**
97
+ * Check if the element is visible.
98
+ *
99
+ * @example
100
+ * ```
101
+ * expect(element).toBeVisible();
102
+ * ```
103
+ *
104
+ * @returns the assertion instance
105
+ */
106
+ public toBeVisible ( ) : this {
107
+ const error = new AssertionError ( {
108
+ actual : this . actual ,
109
+ message : `Expected element ${ this . toString ( ) } to be visible.` ,
110
+ } ) ;
111
+ const invertedError = new AssertionError ( {
112
+ actual : this . actual ,
113
+ message : `Expected element ${ this . toString ( ) } NOT to be visible.` ,
114
+ } ) ;
115
+
116
+ return this . execute ( {
117
+ assertWhen : this . isElementVisible ( this . actual ) && ! this . isAncestorNotVisible ( this . actual ) ,
118
+ error,
119
+ invertedError,
120
+ } ) ;
121
+ }
122
+
96
123
private isElementDisabled ( element : ReactTestInstance ) : boolean {
97
124
const { type } = element ;
98
125
const elementType = type . toString ( ) ;
@@ -112,4 +139,24 @@ export class ElementAssertion extends Assertion<ReactTestInstance> {
112
139
const { parent } = element ;
113
140
return parent !== null && ( this . isElementDisabled ( element ) || this . isAncestorDisabled ( parent ) ) ;
114
141
}
142
+
143
+ private isElementVisible ( element : ReactTestInstance ) : boolean {
144
+ const { type } = element ;
145
+ const elementType = type . toString ( ) ;
146
+ if ( elementType === "Modal" && ! element ?. props ?. visible === true ) {
147
+ return false ;
148
+ }
149
+
150
+ return (
151
+ get ( element , "props.style.display" ) !== "none"
152
+ && get ( element , "props.style.opacity" ) !== 0
153
+ && get ( element , "props.accessibilityElementsHidden" ) !== true
154
+ && get ( element , "props.importantForAccessibility" ) !== "no-hide-descendants"
155
+ ) ;
156
+ }
157
+
158
+ private isAncestorNotVisible ( element : ReactTestInstance ) : boolean {
159
+ const { parent } = element ;
160
+ return parent !== null && ( ! this . isElementVisible ( element ) || this . isAncestorNotVisible ( parent ) ) ;
161
+ }
115
162
}
0 commit comments