Skip to content

Commit 22e54c9

Browse files
Add toBeVisible matcher
1 parent aed8344 commit 22e54c9

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

packages/native/src/lib/ElementAssertion.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,33 @@ export class ElementAssertion extends Assertion<ReactTestInstance> {
9393
});
9494
}
9595

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+
96123
private isElementDisabled(element: ReactTestInstance): boolean {
97124
const { type } = element;
98125
const elementType = type.toString();
@@ -112,4 +139,24 @@ export class ElementAssertion extends Assertion<ReactTestInstance> {
112139
const { parent } = element;
113140
return parent !== null && (this.isElementDisabled(element) || this.isAncestorDisabled(parent));
114141
}
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+
}
115162
}

0 commit comments

Comments
 (0)