@@ -8,9 +8,13 @@ import { MapContainer, useMapEvents } from "react-leaflet";
8
8
import ResetViewControl from "../src/ResetViewControl" ;
9
9
10
10
import type { ResetViewControlOptions } from "../src/ResetViewControl" ;
11
+ import type { LatLngExpression , Map } from "leaflet" ;
11
12
12
13
describe ( "ResetViewControl" , ( ) => {
13
14
const mockHandleViewReset = jest . fn ( ) ;
15
+ let mapInstance = null as Map | null
16
+ const defaultMapCenter = [ - 96.8716348 , 32.8205866 ] as LatLngExpression ;
17
+ const defaultMapZoom = 5
14
18
15
19
const ControlWrapper = ( { title, icon } : ResetViewControlOptions ) => {
16
20
useMapEvents ( {
@@ -28,18 +32,30 @@ describe("ResetViewControl", () => {
28
32
} ;
29
33
const Map = ( { title, icon } : ResetViewControlOptions ) => {
30
34
return (
31
- < MapContainer zoom = { 5 } center = { [ - 96.8716348 , 32.8205866 ] } >
35
+ < MapContainer zoom = { defaultMapZoom } center = { defaultMapCenter } ref = { map => mapInstance = map } >
32
36
< ControlWrapper title = { title } icon = { icon } />
33
37
</ MapContainer >
34
38
) ;
35
39
} ;
36
40
37
41
test ( "can reset map view" , ( ) => {
38
42
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 )
39
52
40
- userEvent . click ( screen . getByTitle ( / z o o m o u t / i) ) ;
41
53
userEvent . click ( screen . getByTitle ( "Reset view" ) ) ;
42
54
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
+
43
59
expect ( mockHandleViewReset ) . toHaveBeenCalledTimes ( 2 ) ;
44
60
} ) ;
45
61
0 commit comments