11import React from 'react' ;
2- import { cleanup , render , screen } from '@testing-library/react-native' ;
2+ import {
3+ cleanup ,
4+ render ,
5+ screen ,
6+ waitForElementToBeRemoved ,
7+ } from '@testing-library/react-native' ;
38import ListWithFetch from '../src/components/ListWithFetch' ;
49import { server } from '../src/test/mocks/server' ;
510import { rest } from 'msw' ;
@@ -15,16 +20,19 @@ test('displays images from the server', async () => {
1520
1621 // Loader is initially visible
1722 expect ( screen . getByLabelText ( / l o a d e r / i) ) . toBeOnTheScreen ( ) ;
18-
23+ await waitForElementToBeRemoved ( ( ) => screen . getByLabelText ( / l o a d e r / i) , {
24+ timeout : 1500 ,
25+ } ) ;
1926 // Verify that users are fetched and rendered
2027 expect ( await screen . findAllByLabelText ( / u s e r - c o n t a i n e r / i) ) . toHaveLength ( 10 ) ;
2128
2229 // Verifying that the loader is no longer visible
2330 // There are 2 ways to verify that a component is not in the UI tree
24- // 1. Use getBy* methods and expect them to throw an error with a corresponding message
25- // 2. Use queryBy* methods and expect them to return null (See the next expect statement)
31+ // 1. Use waitForElementToBeRemoved to wait for the element to be removed from the DOM
32+ // 2. Use getBy* methods and expect them to throw an error with a corresponding message
33+ // 3. Use queryBy* methods and expect them to return null (See the next expect statement)
2634 expect ( ( ) => screen . getByLabelText ( / l o a d e r / i) ) . toThrow (
27- 'Unable to find an element with accessibilityLabel : /loader/i' ,
35+ 'Unable to find an element with accessibility label : /loader/i' ,
2836 ) ;
2937
3038 // Verifying that there are no errors
0 commit comments