Skip to content

Commit 429a422

Browse files
Add tests for toBeVisible
1 parent a7b2c0b commit 429a422

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

packages/native/test/lib/ElementAssertion.test.tsx

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
View,
55
TextInput,
66
Text,
7+
Modal,
78
} from "react-native";
89

910
import { ElementAssertion } from "../../src/lib/ElementAssertion";
@@ -160,4 +161,114 @@ describe("[Unit] ElementAssertion.test.ts", () => {
160161
});
161162
});
162163
});
164+
165+
describe (".toBeVisible", () => {
166+
context("when the modal is visible", () => {
167+
it("returns the assertion instance", () => {
168+
const element = render(
169+
<Modal testID="id" visible={true} />,
170+
);
171+
const test = new ElementAssertion(element.getByTestId("id"));
172+
173+
expect(test.toBeVisible()).toBe(test);
174+
expect(() => test.not.toBeVisible())
175+
.toThrowError(AssertionError)
176+
.toHaveMessage("Expected element <Modal ... /> NOT to be visible.");
177+
});
178+
});
179+
180+
context("when the element contains 'display' property", () => {
181+
it("returns the assertion instance", () => {
182+
const element = render(
183+
<View testID="id" style={{ display: "flex" }} />,
184+
);
185+
const test = new ElementAssertion(element.getByTestId("id"));
186+
187+
expect(test.toBeVisible()).toBe(test);
188+
expect(() => test.not.toBeVisible())
189+
.toThrowError(AssertionError)
190+
.toHaveMessage("Expected element <View ... /> NOT to be visible.");
191+
});
192+
});
193+
194+
context("when the element contains 'accessibilityElementsHidden' property", () => {
195+
it("returns the assertion instance", () => {
196+
const element = render(
197+
<View testID="id" accessibilityElementsHidden={false} />,
198+
);
199+
const test = new ElementAssertion(element.getByTestId("id"));
200+
201+
expect(test.toBeVisible()).toBe(test);
202+
expect(() => test.not.toBeVisible())
203+
.toThrowError(AssertionError)
204+
.toHaveMessage("Expected element <View ... /> NOT to be visible.");
205+
});
206+
});
207+
208+
context("when the element contains 'importantForAccessibility' property", () => {
209+
it("returns the assertion instance", () => {
210+
const element = render(
211+
<View testID="id" importantForAccessibility={"yes"} />,
212+
);
213+
const test = new ElementAssertion(element.getByTestId("id"));
214+
215+
expect(test.toBeVisible()).toBe(test);
216+
expect(() => test.not.toBeVisible())
217+
.toThrowError(AssertionError)
218+
.toHaveMessage("Expected element <View ... /> NOT to be visible.");
219+
});
220+
});
221+
222+
context("when the parent element contains 'opacity' property", () => {
223+
context("if parent opacity = 0", () => {
224+
const element = render(
225+
<View testID="parentId" style={{ opacity: 0 }} >
226+
<View testID="childId" style={{ opacity: 1 }} />
227+
</View>,
228+
);
229+
230+
const parent = new ElementAssertion(element.getByTestId("parentId"));
231+
const child = new ElementAssertion(element.getByTestId("childId"));
232+
233+
it("returns assertion instance for NOT visible elements", () => {
234+
expect(parent.not.toBeVisible()).toBeEqual(parent);
235+
expect(child.not.toBeVisible()).toBeEqual(child);
236+
});
237+
238+
it("throws an error for visible elements", () => {
239+
expect(() => parent.toBeVisible())
240+
.toThrowError(AssertionError)
241+
.toHaveMessage("Expected element <View ... /> to be visible.");
242+
expect(() => child.toBeVisible())
243+
.toThrowError(AssertionError)
244+
.toHaveMessage("Expected element <View ... /> to be visible.");
245+
});
246+
});
247+
248+
context("if child opacity = 0", () => {
249+
const element = render(
250+
<View testID="parentId" style={{ opacity: 1 }} >
251+
<View testID="childId" style={{ opacity: 0 }} />
252+
</View>,
253+
);
254+
255+
const parent = new ElementAssertion(element.getByTestId("parentId"));
256+
const child = new ElementAssertion(element.getByTestId("childId"));
257+
258+
it("returns assertion instance for NOT visible elements", () => {
259+
expect(parent.toBeVisible()).toBeEqual(parent);
260+
expect(child.not.toBeVisible()).toBeEqual(child);
261+
});
262+
263+
it("throws an error for visible elements", () => {
264+
expect(() => parent.not.toBeVisible())
265+
.toThrowError(AssertionError)
266+
.toHaveMessage("Expected element <View ... /> NOT to be visible.");
267+
expect(() => child.toBeVisible())
268+
.toThrowError(AssertionError)
269+
.toHaveMessage("Expected element <View ... /> to be visible.");
270+
});
271+
});
272+
});
273+
});
163274
});

0 commit comments

Comments
 (0)