Skip to content

Commit 82fa78b

Browse files
(CR): Address Code Review comments
1 parent f8258d2 commit 82fa78b

File tree

1 file changed

+58
-14
lines changed

1 file changed

+58
-14
lines changed

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

+58-14
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
11
import { AssertionError, expect } from "@assertive-ts/core";
2-
import { render } from "@testing-library/react-native";
2+
import { fireEvent, render } from "@testing-library/react-native";
3+
import { useState, useCallback } from "react";
34
import {
45
View,
56
TextInput,
67
Text,
78
Modal,
9+
Button,
810
} from "react-native";
911

1012
import { ElementAssertion } from "../../src/lib/ElementAssertion";
1113

14+
const SimpleToggleText: React.FC = () => {
15+
const [isVisible, setIsVisible] = useState(true);
16+
17+
const handleToggle = useCallback((): void => {
18+
setIsVisible((prev: boolean) => !prev);
19+
}, []);
20+
21+
return (
22+
<View>
23+
<Text
24+
testID="textElement"
25+
style={{ display: isVisible ? "flex" : "none" }}
26+
>
27+
{"Toggle me!"}
28+
</Text>
29+
<Button
30+
testID="toggleButton"
31+
title="Toggle Text"
32+
onPress={handleToggle}
33+
/>
34+
</View>
35+
);
36+
};
37+
1238
describe("[Unit] ElementAssertion.test.ts", () => {
1339
describe(".toBeDisabled", () => {
1440
context("when the element is TextInput", () => {
@@ -178,16 +204,34 @@ describe("[Unit] ElementAssertion.test.ts", () => {
178204
});
179205

180206
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"));
207+
context("and display = none", () => {
208+
it("throws an error", () => {
209+
const element = render(
210+
<SimpleToggleText />,
211+
);
212+
const textElement = new ElementAssertion(element.getByTestId("textElement"));
186213

187-
expect(test.toBeVisible()).toBe(test);
188-
expect(() => test.not.toBeVisible())
189-
.toThrowError(AssertionError)
190-
.toHaveMessage("Expected element <View ... /> NOT to be visible.");
214+
expect(textElement.toBeVisible()).toBeEqual(textElement);
215+
216+
const toggleButton = element.getByTestId("toggleButton");
217+
fireEvent.press(toggleButton);
218+
219+
expect(textElement.not.toBeVisible()).toBeEqual(textElement);
220+
});
221+
});
222+
223+
context("and display = flex", () => {
224+
it("returns the assertion instance", () => {
225+
const element = render(
226+
<View testID="id" style={{ display: "flex" }} />,
227+
);
228+
const test = new ElementAssertion(element.getByTestId("id"));
229+
230+
expect(test.toBeVisible()).toBe(test);
231+
expect(() => test.not.toBeVisible())
232+
.toThrowError(AssertionError)
233+
.toHaveMessage("Expected element <View ... /> NOT to be visible.");
234+
});
191235
});
192236
});
193237

@@ -220,7 +264,7 @@ describe("[Unit] ElementAssertion.test.ts", () => {
220264
});
221265

222266
context("when the parent element contains 'opacity' property", () => {
223-
context("if parent opacity = 0", () => {
267+
context("and parent opacity = 0", () => {
224268
const element = render(
225269
<View testID="parentId" style={{ opacity: 0 }} >
226270
<View testID="childId" style={{ opacity: 1 }} />
@@ -245,7 +289,7 @@ describe("[Unit] ElementAssertion.test.ts", () => {
245289
});
246290
});
247291

248-
context("if child opacity = 0", () => {
292+
context("and child opacity = 0", () => {
249293
const element = render(
250294
<View testID="parentId" style={{ opacity: 1 }} >
251295
<View testID="childId" style={{ opacity: 0 }} />
@@ -255,12 +299,12 @@ describe("[Unit] ElementAssertion.test.ts", () => {
255299
const parent = new ElementAssertion(element.getByTestId("parentId"));
256300
const child = new ElementAssertion(element.getByTestId("childId"));
257301

258-
it("returns assertion instance for NOT visible elements", () => {
302+
it("returns assertion instance for visible parent and NOT visible child", () => {
259303
expect(parent.toBeVisible()).toBeEqual(parent);
260304
expect(child.not.toBeVisible()).toBeEqual(child);
261305
});
262306

263-
it("throws an error for visible elements", () => {
307+
it("throws an error for NOT visible parent and visible child", () => {
264308
expect(() => parent.not.toBeVisible())
265309
.toThrowError(AssertionError)
266310
.toHaveMessage("Expected element <View ... /> NOT to be visible.");

0 commit comments

Comments
 (0)