@@ -8,9 +8,13 @@ import { MapContainer, useMapEvents } from "react-leaflet";
88import ResetViewControl from "../src/ResetViewControl" ;
99
1010import type { ResetViewControlOptions } from "../src/ResetViewControl" ;
11+ import type { LatLngExpression , Map } from "leaflet" ;
1112
1213describe ( "ResetViewControl" , ( ) => {
1314 const mockHandleViewReset = jest . fn ( ) ;
15+ let mapInstance = null as Map | null
16+ const defaultMapCenter = [ - 96.8716348 , 32.8205866 ] as LatLngExpression ;
17+ const defaultMapZoom = 5
1418
1519 const ControlWrapper = ( { title, icon } : ResetViewControlOptions ) => {
1620 useMapEvents ( {
@@ -28,18 +32,30 @@ describe("ResetViewControl", () => {
2832 } ;
2933 const Map = ( { title, icon } : ResetViewControlOptions ) => {
3034 return (
31- < MapContainer zoom = { 5 } center = { [ - 96.8716348 , 32.8205866 ] } >
35+ < MapContainer zoom = { defaultMapZoom } center = { defaultMapCenter } ref = { map => mapInstance = map } >
3236 < ControlWrapper title = { title } icon = { icon } />
3337 </ MapContainer >
3438 ) ;
3539 } ;
3640
3741 test ( "can reset map view" , ( ) => {
3842 render ( < Map /> ) ;
43+ expect ( mapInstance ?. getCenter ( ) . lat ) . toBeCloseTo ( defaultMapCenter [ 0 ] , 1 ) ;
44+ expect ( mapInstance ?. getCenter ( ) . lng ) . toBeCloseTo ( defaultMapCenter [ 1 ] , 1 ) ;
45+ expect ( mapInstance ?. getZoom ( ) ) . toEqual ( defaultMapZoom )
46+
47+ mapInstance ?. setView ( [ 2 , 46 ] , 6 )
48+
49+ expect ( mapInstance ?. getCenter ( ) . lat ) . toBeCloseTo ( 2 , 1 ) ;
50+ expect ( mapInstance ?. getCenter ( ) . lng ) . toBeCloseTo ( 46 , 1 ) ;
51+ expect ( mapInstance ?. getZoom ( ) ) . toEqual ( 6 )
3952
40- userEvent . click ( screen . getByTitle ( / z o o m o u t / i) ) ;
4153 userEvent . click ( screen . getByTitle ( "Reset view" ) ) ;
4254
55+ expect ( mapInstance ?. getCenter ( ) . lat ) . toBeCloseTo ( defaultMapCenter [ 0 ] , 1 ) ;
56+ expect ( mapInstance ?. getCenter ( ) . lng ) . toBeCloseTo ( defaultMapCenter [ 1 ] , 1 ) ;
57+ expect ( mapInstance ?. getZoom ( ) ) . toEqual ( defaultMapZoom )
58+
4359 expect ( mockHandleViewReset ) . toHaveBeenCalledTimes ( 2 ) ;
4460 } ) ;
4561
0 commit comments