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 } from "react" ;
3
4
import {
4
5
View ,
5
6
TextInput ,
6
7
Text ,
7
8
Modal ,
9
+ Button ,
8
10
} from "react-native" ;
9
11
10
12
import { ElementAssertion } from "../../src/lib/ElementAssertion" ;
11
13
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
+
12
38
describe ( "[Unit] ElementAssertion.test.ts" , ( ) => {
13
39
describe ( ".toBeDisabled" , ( ) => {
14
40
context ( "when the element is TextInput" , ( ) => {
@@ -178,16 +204,34 @@ describe("[Unit] ElementAssertion.test.ts", () => {
178
204
} ) ;
179
205
180
206
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" ) ) ;
186
213
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
+ } ) ;
191
235
} ) ;
192
236
} ) ;
193
237
@@ -220,7 +264,7 @@ describe("[Unit] ElementAssertion.test.ts", () => {
220
264
} ) ;
221
265
222
266
context ( "when the parent element contains 'opacity' property" , ( ) => {
223
- context ( "if parent opacity = 0" , ( ) => {
267
+ context ( "and parent opacity = 0" , ( ) => {
224
268
const element = render (
225
269
< View testID = "parentId" style = { { opacity : 0 } } >
226
270
< View testID = "childId" style = { { opacity : 1 } } />
@@ -245,7 +289,7 @@ describe("[Unit] ElementAssertion.test.ts", () => {
245
289
} ) ;
246
290
} ) ;
247
291
248
- context ( "if child opacity = 0" , ( ) => {
292
+ context ( "and child opacity = 0" , ( ) => {
249
293
const element = render (
250
294
< View testID = "parentId" style = { { opacity : 1 } } >
251
295
< View testID = "childId" style = { { opacity : 0 } } />
@@ -255,12 +299,12 @@ describe("[Unit] ElementAssertion.test.ts", () => {
255
299
const parent = new ElementAssertion ( element . getByTestId ( "parentId" ) ) ;
256
300
const child = new ElementAssertion ( element . getByTestId ( "childId" ) ) ;
257
301
258
- it ( "returns assertion instance for NOT visible elements " , ( ) => {
302
+ it ( "returns assertion instance for visible parent and NOT visible child " , ( ) => {
259
303
expect ( parent . toBeVisible ( ) ) . toBeEqual ( parent ) ;
260
304
expect ( child . not . toBeVisible ( ) ) . toBeEqual ( child ) ;
261
305
} ) ;
262
306
263
- it ( "throws an error for visible elements " , ( ) => {
307
+ it ( "throws an error for NOT visible parent and visible child " , ( ) => {
264
308
expect ( ( ) => parent . not . toBeVisible ( ) )
265
309
. toThrowError ( AssertionError )
266
310
. toHaveMessage ( "Expected element <View ... /> NOT to be visible." ) ;
0 commit comments