11import React from 'react' ;
2- import { render , fireEvent , act } from '@testing-library/react-native' ;
2+ import { render , fireEvent , act , waitFor } from '@testing-library/react-native' ;
33
44import UserLocation from '../../src/components/UserLocation' ;
5+ import Annotation from '../../src/components/Annotation' ;
56import { ShapeSource } from '../../src/components/ShapeSource' ;
67import CircleLayer from '../../src/components/CircleLayer' ;
78import locationManager from '../../src/modules/location/locationManager' ;
@@ -20,7 +21,7 @@ const position = {
2021} ;
2122
2223describe ( 'UserLocation' , ( ) => {
23- describe ( 'render ' , ( ) => {
24+ describe ( 'renderUL ' , ( ) => {
2425 jest . spyOn ( locationManager , 'start' ) . mockImplementation ( jest . fn ( ) ) ;
2526 jest
2627 . spyOn ( locationManager , 'getLastKnownLocation' )
@@ -38,8 +39,10 @@ describe('UserLocation', () => {
3839 } ) ;
3940
4041 test ( 'renders with CircleLayers by default' , async ( ) => {
41- const { UNSAFE_getAllByType } = await render ( < UserLocation /> ) ;
42- await act ( async ( ) => { } )
42+ const { UNSAFE_getAllByType, UNSAFE_queryByType } = render ( < UserLocation /> ) ;
43+ await waitFor ( ( ) => {
44+ expect ( ( ) => UNSAFE_queryByType ( UserLocation ) ) . not . toThrow ( ) ;
45+ } ) ;
4346
4447 const shapeSource = UNSAFE_getAllByType ( ShapeSource ) ;
4548 const circleLayer = UNSAFE_getAllByType ( CircleLayer ) ;
@@ -52,7 +55,9 @@ describe('UserLocation', () => {
5255 const { UNSAFE_queryByType } = await render (
5356 < UserLocation visible = { false } /> ,
5457 ) ;
55- await act ( async ( ) => { } )
58+ await waitFor ( ( ) => {
59+ expect ( ( ) => UNSAFE_queryByType ( UserLocation ) ) . not . toThrow ( ) ;
60+ } ) ;
5661
5762 const shapeSource = UNSAFE_queryByType ( ShapeSource ) ;
5863 const circleLayer = UNSAFE_queryByType ( CircleLayer ) ;
@@ -72,11 +77,14 @@ describe('UserLocation', () => {
7277 } ,
7378 } ;
7479
75- const { UNSAFE_queryByType, UNSAFE_queryAllByType } = await render ( < UserLocation >
76- < CircleLayer key = 'testUserLocationCircle' { ...circleLayerProps } />
77- </ UserLocation > )
78- await act ( async ( ) => {
79- } )
80+ const { UNSAFE_queryByType, UNSAFE_queryAllByType } = render (
81+ < UserLocation >
82+ < CircleLayer key = 'testUserLocationCircle' { ...circleLayerProps } />
83+ </ UserLocation >
84+ )
85+ await waitFor ( ( ) => {
86+ expect ( ( ) => UNSAFE_queryByType ( UserLocation ) ) . not . toThrow ( ) ;
87+ } ) ;
8088
8189 const shapeSource = UNSAFE_queryByType ( ShapeSource ) ;
8290 const circleLayer = UNSAFE_queryAllByType ( CircleLayer ) ;
@@ -88,10 +96,14 @@ describe('UserLocation', () => {
8896 expect ( circleLayer [ 0 ] . props . style ) . toEqual ( circleLayerProps . style ) ;
8997 } ) ;
9098
91- test ( 'calls onUpdate callback when new location is received' , ( ) => {
99+ test ( 'calls onUpdate callback when new location is received' , async ( ) => {
92100 const onUpdateCallback = jest . fn ( ) ;
93101
94- render ( < UserLocation onUpdate = { onUpdateCallback } /> ) ;
102+ const { UNSAFE_getByType } = render ( < UserLocation onUpdate = { onUpdateCallback } /> ) ;
103+
104+ await waitFor ( ( ) => {
105+ expect ( ( ) => UNSAFE_getByType ( UserLocation ) ) . not . toThrow ( ) ;
106+ } ) ;
95107
96108 act ( ( ) => {
97109 locationManager . _onUpdate ( {
@@ -111,16 +123,49 @@ describe('UserLocation', () => {
111123 expect ( onUpdateCallback ) . toHaveBeenCalled ( ) ;
112124 } ) ;
113125
114- test ( 'calls onPress callback when location icon is pressed' , ( ) => {
126+ test ( 'calls onPress callback when location icon is pressed' , async ( ) => {
115127 const onPressCallback = jest . fn ( ) ;
116128
117- const { UNSAFE_queryByType } = render (
129+ const { UNSAFE_getByType } = render (
118130 < UserLocation onPress = { onPressCallback } /> ,
119131 ) ;
120132
121- const shapeSource = UNSAFE_queryByType ( ShapeSource ) ;
122- fireEvent ( shapeSource , 'onPress' ) ;
123- fireEvent ( shapeSource , 'onPress' ) ;
133+ await waitFor ( ( ) => {
134+ expect ( ( ) => UNSAFE_getByType ( UserLocation ) ) . not . toThrow ( ) ;
135+ } ) ;
136+ const ul = UNSAFE_getByType ( UserLocation ) ;
137+ fireEvent ( ul , 'onPress' ) ;
138+ fireEvent ( ul , 'onPress' ) ;
139+ expect ( onPressCallback ) . toHaveBeenCalledTimes ( 2 ) ;
140+ } ) ;
141+
142+ test ( 'calls onPress callback when location icon is pressed and locations hav been received' , async ( ) => {
143+ const onPressCallback = jest . fn ( ) ;
144+
145+ const { rerender, UNSAFE_queryByType } = render (
146+ < UserLocation onPress = { onPressCallback } visible = { false } /> ,
147+ ) ;
148+
149+ await waitFor ( ( ) => {
150+ expect ( ( ) => UNSAFE_queryByType ( UserLocation ) ) . not . toThrow ( ) ;
151+ } ) ;
152+ const ul = UNSAFE_queryByType ( UserLocation ) ;
153+
154+ const lastKnownLocation = [ 4.1036916 , 51.5462244 ] ;
155+ locationManager . _lastKnownLocation = lastKnownLocation
156+
157+ expect ( locationManager . start ) . toHaveBeenCalledTimes ( 0 ) ;
158+ expect ( locationManager . _isListening ) . toStrictEqual ( false ) ;
159+
160+ await act ( async ( ) => {
161+ rerender ( < UserLocation onPress = { onPressCallback } visible = { true } /> ) ;
162+ } ) ;
163+ expect ( locationManager . start ) . toHaveBeenCalledTimes ( 1 ) ;
164+ expect ( ul . instance . locationManagerRunning ) . toStrictEqual ( true ) ;
165+
166+ const annotation = UNSAFE_queryByType ( Annotation ) ;
167+ fireEvent ( annotation , 'onPress' ) ;
168+ fireEvent ( annotation , 'onPress' ) ;
124169 expect ( onPressCallback ) . toHaveBeenCalledTimes ( 2 ) ;
125170 } ) ;
126171
0 commit comments