1
1
import { AssertionError , expect } from "@assertive-ts/core" ;
2
2
import { fireEvent , render } from "@testing-library/react-native" ;
3
- import { useState , useCallback } from "react" ;
3
+ import { useState , useCallback , ReactElement } from "react" ;
4
4
import {
5
5
View ,
6
6
TextInput ,
@@ -11,29 +11,27 @@ import {
11
11
12
12
import { ElementAssertion } from "../../src/lib/ElementAssertion" ;
13
13
14
- const SimpleToggleText : React . FC = ( ) => {
14
+ function SimpleToggleText ( ) : ReactElement {
15
15
const [ isVisible , setIsVisible ] = useState ( true ) ;
16
16
17
17
const handleToggle = useCallback ( ( ) : void => {
18
- setIsVisible ( ( prev : boolean ) => ! prev ) ;
18
+ setIsVisible ( prev => ! prev ) ;
19
19
} , [ ] ) ;
20
20
21
21
return (
22
22
< View >
23
23
< Text
24
- testID = "textElement"
25
24
style = { { display : isVisible ? "flex" : "none" } }
26
25
>
27
26
{ "Toggle me!" }
28
27
</ Text >
29
28
< Button
30
- testID = "toggleButton"
31
29
title = "Toggle Text"
32
30
onPress = { handleToggle }
33
31
/>
34
32
</ View >
35
33
) ;
36
- } ;
34
+ }
37
35
38
36
describe ( "[Unit] ElementAssertion.test.ts" , ( ) => {
39
37
describe ( ".toBeDisabled" , ( ) => {
@@ -191,10 +189,10 @@ describe("[Unit] ElementAssertion.test.ts", () => {
191
189
describe ( ".toBeVisible" , ( ) => {
192
190
context ( "when the modal is visible" , ( ) => {
193
191
it ( "returns the assertion instance" , ( ) => {
194
- const element = render (
192
+ const { getByTestId } = render (
195
193
< Modal testID = "id" visible = { true } /> ,
196
194
) ;
197
- const test = new ElementAssertion ( element . getByTestId ( "id" ) ) ;
195
+ const test = new ElementAssertion ( getByTestId ( "id" ) ) ;
198
196
199
197
expect ( test . toBeVisible ( ) ) . toBe ( test ) ;
200
198
expect ( ( ) => test . not . toBeVisible ( ) )
@@ -206,14 +204,14 @@ describe("[Unit] ElementAssertion.test.ts", () => {
206
204
context ( "when the element contains 'display' property" , ( ) => {
207
205
context ( "and display = none" , ( ) => {
208
206
it ( "throws an error" , ( ) => {
209
- const element = render (
207
+ const { getByText , getByRole } = render (
210
208
< SimpleToggleText /> ,
211
209
) ;
212
- const textElement = new ElementAssertion ( element . getByTestId ( "textElement ") ) ;
210
+ const textElement = new ElementAssertion ( getByText ( "Toggle me! ") ) ;
213
211
214
212
expect ( textElement . toBeVisible ( ) ) . toBeEqual ( textElement ) ;
215
213
216
- const toggleButton = element . getByTestId ( "toggleButton" ) ;
214
+ const toggleButton = getByRole ( "button" , { name : "Toggle Text" } ) ;
217
215
fireEvent . press ( toggleButton ) ;
218
216
219
217
expect ( textElement . not . toBeVisible ( ) ) . toBeEqual ( textElement ) ;
@@ -222,10 +220,10 @@ describe("[Unit] ElementAssertion.test.ts", () => {
222
220
223
221
context ( "and display = flex" , ( ) => {
224
222
it ( "returns the assertion instance" , ( ) => {
225
- const element = render (
223
+ const { getByTestId } = render (
226
224
< View testID = "id" style = { { display : "flex" } } /> ,
227
225
) ;
228
- const test = new ElementAssertion ( element . getByTestId ( "id" ) ) ;
226
+ const test = new ElementAssertion ( getByTestId ( "id" ) ) ;
229
227
230
228
expect ( test . toBeVisible ( ) ) . toBe ( test ) ;
231
229
expect ( ( ) => test . not . toBeVisible ( ) )
@@ -237,10 +235,10 @@ describe("[Unit] ElementAssertion.test.ts", () => {
237
235
238
236
context ( "when the element contains 'accessibilityElementsHidden' property" , ( ) => {
239
237
it ( "returns the assertion instance" , ( ) => {
240
- const element = render (
238
+ const { getByTestId } = render (
241
239
< View testID = "id" accessibilityElementsHidden = { false } /> ,
242
240
) ;
243
- const test = new ElementAssertion ( element . getByTestId ( "id" ) ) ;
241
+ const test = new ElementAssertion ( getByTestId ( "id" ) ) ;
244
242
245
243
expect ( test . toBeVisible ( ) ) . toBe ( test ) ;
246
244
expect ( ( ) => test . not . toBeVisible ( ) )
@@ -251,10 +249,10 @@ describe("[Unit] ElementAssertion.test.ts", () => {
251
249
252
250
context ( "when the element contains 'importantForAccessibility' property" , ( ) => {
253
251
it ( "returns the assertion instance" , ( ) => {
254
- const element = render (
252
+ const { getByTestId } = render (
255
253
< View testID = "id" importantForAccessibility = { "yes" } /> ,
256
254
) ;
257
- const test = new ElementAssertion ( element . getByTestId ( "id" ) ) ;
255
+ const test = new ElementAssertion ( getByTestId ( "id" ) ) ;
258
256
259
257
expect ( test . toBeVisible ( ) ) . toBe ( test ) ;
260
258
expect ( ( ) => test . not . toBeVisible ( ) )
@@ -265,14 +263,14 @@ describe("[Unit] ElementAssertion.test.ts", () => {
265
263
266
264
context ( "when the parent element contains 'opacity' property" , ( ) => {
267
265
context ( "and parent opacity = 0" , ( ) => {
268
- const element = render (
266
+ const { getByTestId } = render (
269
267
< View testID = "parentId" style = { { opacity : 0 } } >
270
268
< View testID = "childId" style = { { opacity : 1 } } />
271
269
</ View > ,
272
270
) ;
273
271
274
- const parent = new ElementAssertion ( element . getByTestId ( "parentId" ) ) ;
275
- const child = new ElementAssertion ( element . getByTestId ( "childId" ) ) ;
272
+ const parent = new ElementAssertion ( getByTestId ( "parentId" ) ) ;
273
+ const child = new ElementAssertion ( getByTestId ( "childId" ) ) ;
276
274
277
275
it ( "returns assertion instance for NOT visible elements" , ( ) => {
278
276
expect ( parent . not . toBeVisible ( ) ) . toBeEqual ( parent ) ;
@@ -290,14 +288,14 @@ describe("[Unit] ElementAssertion.test.ts", () => {
290
288
} ) ;
291
289
292
290
context ( "and child opacity = 0" , ( ) => {
293
- const element = render (
291
+ const { getByTestId } = render (
294
292
< View testID = "parentId" style = { { opacity : 1 } } >
295
293
< View testID = "childId" style = { { opacity : 0 } } />
296
294
</ View > ,
297
295
) ;
298
296
299
- const parent = new ElementAssertion ( element . getByTestId ( "parentId" ) ) ;
300
- const child = new ElementAssertion ( element . getByTestId ( "childId" ) ) ;
297
+ const parent = new ElementAssertion ( getByTestId ( "parentId" ) ) ;
298
+ const child = new ElementAssertion ( getByTestId ( "childId" ) ) ;
301
299
302
300
it ( "returns assertion instance for visible parent and NOT visible child" , ( ) => {
303
301
expect ( parent . toBeVisible ( ) ) . toBeEqual ( parent ) ;
0 commit comments