@@ -3,6 +3,10 @@ import { render, screen, waitFor, fireEvent } from '@testing-library/react';
33import userEvent from '@testing-library/user-event' ;
44import { MapComponent } from './index' ;
55
6+ const mockToast = vi . hoisted ( ( ) => ( {
7+ error : vi . fn ( ) ,
8+ } ) ) ;
9+
610const mockMapRef = {
711 getCenter : vi . fn ( ( ) => ( { lng : 13.4 , lat : 52.5 } ) ) ,
812 getZoom : vi . fn ( ( ) => 10 ) ,
@@ -45,6 +49,22 @@ vi.mock('react-map-gl/maplibre', () => ({
4549 NavigationControl : vi . fn ( ( ) => (
4650 < div data-testid = "navigation-control" > Nav</ div >
4751 ) ) ,
52+ GeolocateControl : vi . fn ( ( { onError } ) => (
53+ < div data-testid = "geolocate-control" >
54+ < button
55+ data-testid = "trigger-geolocate-error"
56+ onClick = { ( ) => onError ?.( { PERMISSION_DENIED : false } ) }
57+ >
58+ Trigger Error
59+ </ button >
60+ < button
61+ data-testid = "trigger-geolocate-permission-denied"
62+ onClick = { ( ) => onError ?.( { PERMISSION_DENIED : true } ) }
63+ >
64+ Trigger Permission Denied
65+ </ button >
66+ </ div >
67+ ) ) ,
4868 useMap : vi . fn ( ( ) => ( { current : mockMapRef } ) ) ,
4969} ) ) ;
5070
@@ -212,6 +232,10 @@ vi.mock('@/utils/heightgraph', () => ({
212232 buildHeightgraphData : vi . fn ( ( ) => [ ] ) ,
213233} ) ) ;
214234
235+ vi . mock ( 'sonner' , ( ) => ( {
236+ toast : mockToast ,
237+ } ) ) ;
238+
215239describe ( 'MapComponent' , ( ) => {
216240 let localStorageMock : Record < string , string > ;
217241
@@ -247,6 +271,11 @@ describe('MapComponent', () => {
247271 expect ( screen . getByTestId ( 'navigation-control' ) ) . toBeInTheDocument ( ) ;
248272 } ) ;
249273
274+ it ( 'should render geolocate control' , ( ) => {
275+ render ( < MapComponent /> ) ;
276+ expect ( screen . getByTestId ( 'geolocate-control' ) ) . toBeInTheDocument ( ) ;
277+ } ) ;
278+
250279 it ( 'should render draw control' , ( ) => {
251280 render ( < MapComponent /> ) ;
252281 expect ( screen . getByTestId ( 'draw-control' ) ) . toBeInTheDocument ( ) ;
@@ -388,4 +417,30 @@ describe('MapComponent', () => {
388417 expect ( screen . queryByTestId ( 'map-info-popup' ) ) . not . toBeInTheDocument ( ) ;
389418 } ) ;
390419 } ) ;
420+
421+ describe ( 'GeolocateControl error handling' , ( ) => {
422+ it ( 'should show default error toast when geolocate fails' , async ( ) => {
423+ const user = userEvent . setup ( ) ;
424+ render ( < MapComponent /> ) ;
425+
426+ await user . click ( screen . getByTestId ( 'trigger-geolocate-error' ) ) ;
427+
428+ expect ( mockToast . error ) . toHaveBeenCalledWith (
429+ "We couldn't get your location. Please try again."
430+ ) ;
431+ } ) ;
432+
433+ it ( 'should show permission denied error toast when location permission is denied' , async ( ) => {
434+ const user = userEvent . setup ( ) ;
435+ render ( < MapComponent /> ) ;
436+
437+ await user . click (
438+ screen . getByTestId ( 'trigger-geolocate-permission-denied' )
439+ ) ;
440+
441+ expect ( mockToast . error ) . toHaveBeenCalledWith (
442+ "We couldn't get your location. Please check your browser settings and allow location access."
443+ ) ;
444+ } ) ;
445+ } ) ;
391446} ) ;
0 commit comments