|
| 1 | +import { AssertionError, expect } from "@assertive-ts/core"; |
| 2 | +import { render } from "@testing-library/react-native"; |
| 3 | +import { |
| 4 | + View, |
| 5 | + TextInput, |
| 6 | +} from "react-native"; |
| 7 | + |
| 8 | +import { ElementAssertion } from "../../src/lib/ElementAssertion"; |
| 9 | + |
| 10 | +describe("[Unit] ElementAssertion.test.ts", () => { |
| 11 | + describe(".toBeDisabled", () => { |
| 12 | + context("when the element is TextInput", () => { |
| 13 | + context("and the element is not editable", () => { |
| 14 | + it("returns the assertion instance", () => { |
| 15 | + const element = render( |
| 16 | + <TextInput testID="id" editable={false} />, |
| 17 | + ); |
| 18 | + const test = new ElementAssertion(element.getByTestId("id")); |
| 19 | + expect(test.toBeDisabled()).toBe(test); |
| 20 | + expect(test.not.toBeEnabled()).toBeEqual(test); |
| 21 | + expect(() => test.toBeEnabled()) |
| 22 | + .toThrowError(AssertionError) |
| 23 | + .toHaveMessage("Expected element <TextInput ... /> to be enabled."); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + context("and the element is editable", () => { |
| 28 | + it("throws an error", () => { |
| 29 | + const reactElement = render(<TextInput editable={true} testID="id" />); |
| 30 | + const test = new ElementAssertion(reactElement.getByTestId("id")); |
| 31 | + |
| 32 | + expect(() => test.toBeDisabled()) |
| 33 | + .toThrowError(AssertionError) |
| 34 | + .toHaveMessage("Expected element <TextInput ... /> to be disabled."); |
| 35 | + expect(() => test.not.toBeEnabled()) |
| 36 | + .toThrowError(AssertionError) |
| 37 | + .toHaveMessage("Expected element <TextInput ... /> to NOT be enabled."); |
| 38 | + }); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + context("when the parent has property aria-disabled", () => { |
| 43 | + context("if parent aria-disabled = true", () => { |
| 44 | + it("returns assertion instance for parent and child element", () => { |
| 45 | + const element = render( |
| 46 | + <View aria-disabled={true} testID="parentId"> |
| 47 | + <View testID="childId"> |
| 48 | + <TextInput /> |
| 49 | + </View> |
| 50 | + </View>, |
| 51 | + ); |
| 52 | + |
| 53 | + const parent = new ElementAssertion(element.getByTestId("parentId")); |
| 54 | + const child = new ElementAssertion(element.getByTestId("childId")); |
| 55 | + expect(parent.toBeDisabled()).toBe(parent); |
| 56 | + expect(child.toBeDisabled()).toBe(child); |
| 57 | + expect(() => parent.toBeEnabled()) |
| 58 | + .toThrowError(AssertionError) |
| 59 | + .toHaveMessage("Expected element <View ... /> to be enabled."); |
| 60 | + expect(() => parent.not.toBeDisabled()) |
| 61 | + .toThrowError(AssertionError) |
| 62 | + .toHaveMessage("Expected element <View ... /> to NOT be disabled."); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + context("if parent aria-disabled = false", () => { |
| 67 | + it("throws an error for parent and child element", () => { |
| 68 | + const element = render( |
| 69 | + <View aria-disabled={false} testID="parentId"> |
| 70 | + <View testID="childId"> |
| 71 | + <TextInput /> |
| 72 | + </View> |
| 73 | + </View>, |
| 74 | + ); |
| 75 | + |
| 76 | + const parent = new ElementAssertion(element.getByTestId("parentId")); |
| 77 | + const child = new ElementAssertion(element.getByTestId("childId")); |
| 78 | + |
| 79 | + expect(parent.toBeEnabled()).toBeEqual(parent); |
| 80 | + expect(parent.not.toBeDisabled()).toBeEqual(parent); |
| 81 | + expect(() => parent.toBeDisabled()) |
| 82 | + .toThrowError(AssertionError) |
| 83 | + .toHaveMessage("Expected element <View ... /> to be disabled."); |
| 84 | + expect(() => parent.not.toBeEnabled()) |
| 85 | + .toThrowError(AssertionError) |
| 86 | + .toHaveMessage("Expected element <View ... /> to NOT be enabled."); |
| 87 | + expect(() => child.toBeDisabled()) |
| 88 | + .toThrowError(AssertionError) |
| 89 | + .toHaveMessage("Expected element <View ... /> to be disabled."); |
| 90 | + expect(() => child.not.toBeEnabled()) |
| 91 | + .toThrowError(AssertionError) |
| 92 | + .toHaveMessage("Expected element <View ... /> to NOT be enabled."); |
| 93 | + }); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + context("when the element contains property aria-disabled", () => { |
| 98 | + const element = render( |
| 99 | + <View testID="parentId"> |
| 100 | + <View aria-disabled={true} testID="childId"> |
| 101 | + <TextInput /> |
| 102 | + </View> |
| 103 | + </View>, |
| 104 | + ); |
| 105 | + |
| 106 | + const parent = new ElementAssertion(element.getByTestId("parentId")); |
| 107 | + const child = new ElementAssertion(element.getByTestId("childId")); |
| 108 | + |
| 109 | + context("if child contains aria-disabled = true", () => { |
| 110 | + it("returns assertion instance for child element", () => { |
| 111 | + expect(child.toBeDisabled()).toBe(child); |
| 112 | + expect(() => child.toBeEnabled()) |
| 113 | + .toThrowError(AssertionError) |
| 114 | + .toHaveMessage("Expected element <View ... /> to be enabled."); |
| 115 | + expect(() => child.not.toBeDisabled()) |
| 116 | + .toThrowError(AssertionError) |
| 117 | + .toHaveMessage("Expected element <View ... /> to NOT be disabled."); |
| 118 | + }); |
| 119 | + |
| 120 | + it("returns error for parent element", () => { |
| 121 | + expect(parent.toBeEnabled()).toBeEqual(parent); |
| 122 | + expect(() => parent.toBeDisabled()) |
| 123 | + .toThrowError(AssertionError) |
| 124 | + .toHaveMessage("Expected element <View ... /> to be disabled."); |
| 125 | + expect(() => parent.not.toBeEnabled()) |
| 126 | + .toThrowError(AssertionError) |
| 127 | + .toHaveMessage("Expected element <View ... /> to NOT be enabled."); |
| 128 | + }); |
| 129 | + }); |
| 130 | + }); |
| 131 | + }); |
| 132 | +}); |
0 commit comments