|
1 | 1 | 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, ReactElement } from "react"; |
3 | 4 | import {
|
4 | 5 | View,
|
5 | 6 | TextInput,
|
6 | 7 | Text,
|
| 8 | + Modal, |
| 9 | + Button, |
7 | 10 | } from "react-native";
|
8 | 11 |
|
9 | 12 | import { ElementAssertion } from "../../src/lib/ElementAssertion";
|
10 | 13 |
|
| 14 | +function SimpleToggleText(): ReactElement { |
| 15 | + const [isVisible, setIsVisible] = useState(true); |
| 16 | + |
| 17 | + const handleToggle = useCallback((): void => { |
| 18 | + setIsVisible(prev => !prev); |
| 19 | + }, []); |
| 20 | + |
| 21 | + return ( |
| 22 | + <View> |
| 23 | + <Text style={{ display: isVisible ? "flex" : "none" }}> |
| 24 | + {"Toggle me!"} |
| 25 | + </Text> |
| 26 | + <Button |
| 27 | + title="Toggle Text" |
| 28 | + onPress={handleToggle} |
| 29 | + /> |
| 30 | + </View> |
| 31 | + ); |
| 32 | +} |
| 33 | + |
11 | 34 | describe("[Unit] ElementAssertion.test.ts", () => {
|
12 | 35 | describe(".toBeDisabled", () => {
|
13 | 36 | context("when the element is TextInput", () => {
|
@@ -160,4 +183,132 @@ describe("[Unit] ElementAssertion.test.ts", () => {
|
160 | 183 | });
|
161 | 184 | });
|
162 | 185 | });
|
| 186 | + |
| 187 | + describe (".toBeVisible", () => { |
| 188 | + context("when the modal is visible", () => { |
| 189 | + it("returns the assertion instance", () => { |
| 190 | + const { getByTestId } = render( |
| 191 | + <Modal testID="id" visible={true} />, |
| 192 | + ); |
| 193 | + const test = new ElementAssertion(getByTestId("id")); |
| 194 | + |
| 195 | + expect(test.toBeVisible()).toBe(test); |
| 196 | + expect(() => test.not.toBeVisible()) |
| 197 | + .toThrowError(AssertionError) |
| 198 | + .toHaveMessage("Expected element <Modal ... /> NOT to be visible."); |
| 199 | + }); |
| 200 | + }); |
| 201 | + |
| 202 | + context("when the element contains 'display' property", () => { |
| 203 | + context("and display = none", () => { |
| 204 | + it("throws an error", () => { |
| 205 | + const { getByText, getByRole } = render( |
| 206 | + <SimpleToggleText />, |
| 207 | + ); |
| 208 | + const textElement = new ElementAssertion(getByText("Toggle me!")); |
| 209 | + |
| 210 | + expect(textElement.toBeVisible()).toBeEqual(textElement); |
| 211 | + |
| 212 | + const toggleButton = getByRole("button", { name: "Toggle Text" }); |
| 213 | + fireEvent.press(toggleButton); |
| 214 | + |
| 215 | + expect(textElement.not.toBeVisible()).toBeEqual(textElement); |
| 216 | + }); |
| 217 | + }); |
| 218 | + |
| 219 | + context("and display = flex", () => { |
| 220 | + it("returns the assertion instance", () => { |
| 221 | + const { getByTestId } = render( |
| 222 | + <View testID="id" style={{ display: "flex" }} />, |
| 223 | + ); |
| 224 | + const test = new ElementAssertion(getByTestId("id")); |
| 225 | + |
| 226 | + expect(test.toBeVisible()).toBe(test); |
| 227 | + expect(() => test.not.toBeVisible()) |
| 228 | + .toThrowError(AssertionError) |
| 229 | + .toHaveMessage("Expected element <View ... /> NOT to be visible."); |
| 230 | + }); |
| 231 | + }); |
| 232 | + }); |
| 233 | + |
| 234 | + context("when the element contains 'accessibilityElementsHidden' property", () => { |
| 235 | + it("returns the assertion instance", () => { |
| 236 | + const { getByTestId } = render( |
| 237 | + <View testID="id" accessibilityElementsHidden={false} />, |
| 238 | + ); |
| 239 | + const test = new ElementAssertion(getByTestId("id")); |
| 240 | + |
| 241 | + expect(test.toBeVisible()).toBe(test); |
| 242 | + expect(() => test.not.toBeVisible()) |
| 243 | + .toThrowError(AssertionError) |
| 244 | + .toHaveMessage("Expected element <View ... /> NOT to be visible."); |
| 245 | + }); |
| 246 | + }); |
| 247 | + |
| 248 | + context("when the element contains 'importantForAccessibility' property", () => { |
| 249 | + it("returns the assertion instance", () => { |
| 250 | + const { getByTestId } = render( |
| 251 | + <View testID="id" importantForAccessibility={"yes"} />, |
| 252 | + ); |
| 253 | + const test = new ElementAssertion(getByTestId("id")); |
| 254 | + |
| 255 | + expect(test.toBeVisible()).toBe(test); |
| 256 | + expect(() => test.not.toBeVisible()) |
| 257 | + .toThrowError(AssertionError) |
| 258 | + .toHaveMessage("Expected element <View ... /> NOT to be visible."); |
| 259 | + }); |
| 260 | + }); |
| 261 | + |
| 262 | + context("when the parent element contains 'opacity' property", () => { |
| 263 | + context("and parent opacity = 0", () => { |
| 264 | + const { getByTestId } = render( |
| 265 | + <View testID="parentId" style={{ opacity: 0 }} > |
| 266 | + <View testID="childId" style={{ opacity: 1 }} /> |
| 267 | + </View>, |
| 268 | + ); |
| 269 | + |
| 270 | + const parent = new ElementAssertion(getByTestId("parentId")); |
| 271 | + const child = new ElementAssertion(getByTestId("childId")); |
| 272 | + |
| 273 | + it("returns assertion instance for NOT visible elements", () => { |
| 274 | + expect(parent.not.toBeVisible()).toBeEqual(parent); |
| 275 | + expect(child.not.toBeVisible()).toBeEqual(child); |
| 276 | + }); |
| 277 | + |
| 278 | + it("throws an error for visible elements", () => { |
| 279 | + expect(() => parent.toBeVisible()) |
| 280 | + .toThrowError(AssertionError) |
| 281 | + .toHaveMessage("Expected element <View ... /> to be visible."); |
| 282 | + expect(() => child.toBeVisible()) |
| 283 | + .toThrowError(AssertionError) |
| 284 | + .toHaveMessage("Expected element <View ... /> to be visible."); |
| 285 | + }); |
| 286 | + }); |
| 287 | + |
| 288 | + context("and child opacity = 0", () => { |
| 289 | + const { getByTestId } = render( |
| 290 | + <View testID="parentId" style={{ opacity: 1 }} > |
| 291 | + <View testID="childId" style={{ opacity: 0 }} /> |
| 292 | + </View>, |
| 293 | + ); |
| 294 | + |
| 295 | + const parent = new ElementAssertion(getByTestId("parentId")); |
| 296 | + const child = new ElementAssertion(getByTestId("childId")); |
| 297 | + |
| 298 | + it("returns assertion instance for visible parent and NOT visible child", () => { |
| 299 | + expect(parent.toBeVisible()).toBeEqual(parent); |
| 300 | + expect(child.not.toBeVisible()).toBeEqual(child); |
| 301 | + }); |
| 302 | + |
| 303 | + it("throws an error for NOT visible parent and visible child", () => { |
| 304 | + expect(() => parent.not.toBeVisible()) |
| 305 | + .toThrowError(AssertionError) |
| 306 | + .toHaveMessage("Expected element <View ... /> NOT to be visible."); |
| 307 | + expect(() => child.toBeVisible()) |
| 308 | + .toThrowError(AssertionError) |
| 309 | + .toHaveMessage("Expected element <View ... /> to be visible."); |
| 310 | + }); |
| 311 | + }); |
| 312 | + }); |
| 313 | + }); |
163 | 314 | });
|
0 commit comments