Skip to content

Commit 6d253ed

Browse files
committed
Fix tests
1 parent 214def9 commit 6d253ed

File tree

9 files changed

+58
-29
lines changed

9 files changed

+58
-29
lines changed

packages/core/__tests__/context.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import { renderHook } from '@testing-library/react'
22
import type { Map } from 'leaflet'
33
import React, { StrictMode, type ReactNode } from 'react'
44

5-
import { CONTEXT_VERSION, LeafletContext, createLeafletContext, useLeafletContext } from '../src'
5+
import {
6+
CONTEXT_VERSION,
7+
LeafletContext,
8+
createLeafletContext,
9+
useLeafletContext,
10+
} from '../src'
611

712
export function createWrapper(context) {
813
return function Wrapper({ children }: { children: ReactNode }) {
@@ -28,7 +33,7 @@ describe('context', () => {
2833
const { result } = renderHook(() => useLeafletContext())
2934
return result.current
3035
}).toThrow(
31-
'No context provided: useLeafletContext() can only be used in a descendant of <MapContainer>'
36+
'No context provided: useLeafletContext() can only be used in a descendant of <MapContainer>',
3237
)
3338
})
3439

packages/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@react-leaflet/core",
3-
"version": "2.1.0",
3+
"version": "3.0.0-beta.1",
44
"description": "React Leaflet core",
55
"repository": {
66
"type": "git",

packages/core/src/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
export { useAttribution } from './attribution.js'
2-
export { type CircleMarkerProps, type CircleProps, updateCircle } from './circle.js'
2+
export {
3+
type CircleMarkerProps,
4+
type CircleProps,
5+
updateCircle,
6+
} from './circle.js'
37
export {
48
createContainerComponent,
59
createDivOverlayComponent,

packages/react-leaflet/__tests__/MapContainer.tsx

+31-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { render } from '@testing-library/react'
2-
import { type LatLngExpression, Map } from 'leaflet'
2+
import { type LatLngExpression, Map as LeafletMap } from 'leaflet'
33
import React, { StrictMode, useEffect, useRef } from 'react'
44

55
import { MapContainer, useMap } from '../src'
@@ -30,10 +30,15 @@ describe('MapContainer', () => {
3030

3131
describe('provides the Map instance', () => {
3232
test('with the useMap() hook', (done) => {
33+
let doneCalled = false
34+
3335
function TestChild() {
3436
const map = useMap()
35-
expect(map).toBeInstanceOf(Map)
36-
done()
37+
expect(map).toBeInstanceOf(LeafletMap)
38+
if (!doneCalled) {
39+
doneCalled = true
40+
done()
41+
}
3742
return null
3843
}
3944

@@ -49,10 +54,15 @@ describe('MapContainer', () => {
4954
})
5055

5156
test('in the ref function', (done) => {
57+
let doneCalled = false
58+
5259
const ref = (map) => {
53-
if (map !== null) {
54-
expect(map).toBeInstanceOf(Map)
55-
done()
60+
if (map != null) {
61+
expect(map).toBeInstanceOf(LeafletMap)
62+
if (!doneCalled) {
63+
doneCalled = true
64+
done()
65+
}
5666
}
5767
}
5868

@@ -64,14 +74,19 @@ describe('MapContainer', () => {
6474
})
6575

6676
test('in the ref object', (done) => {
77+
let doneCalled = false
78+
6779
function Wrapper() {
68-
const ref = useRef()
80+
const ref = useRef(undefined)
6981

7082
useEffect(() => {
7183
setTimeout(() => {
72-
if (ref.current !== null) {
73-
expect(ref.current).toBeInstanceOf(Map)
74-
done()
84+
if (ref.current != null) {
85+
expect(ref.current).toBeInstanceOf(LeafletMap)
86+
if (!doneCalled) {
87+
doneCalled = true
88+
done()
89+
}
7590
}
7691
}, 50)
7792
}, [])
@@ -86,12 +101,16 @@ describe('MapContainer', () => {
86101
test('sets center and zoom props', (done) => {
87102
const center: LatLngExpression = [1.2, 3.4]
88103
const zoom = 10
104+
let doneCalled = false
89105

90106
const ref = (map) => {
91-
if (map !== null) {
107+
if (map != null) {
92108
expect(map.getCenter()).toEqual({ lat: 1.2, lng: 3.4 })
93109
expect(map.getZoom()).toBe(zoom)
94-
done()
110+
if (!doneCalled) {
111+
doneCalled = true
112+
done()
113+
}
95114
}
96115
}
97116

packages/react-leaflet/__tests__/Pane.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe('Pane', () => {
102102
describe('supports refs', () => {
103103
test('as callback function', (done) => {
104104
const ref = (pane) => {
105-
if (pane !== null) {
105+
if (pane != null) {
106106
expect(pane).toBeInstanceOf(HTMLElement)
107107
done()
108108
}
@@ -126,7 +126,7 @@ describe('Pane', () => {
126126

127127
test('as object', (done) => {
128128
function Wrapper() {
129-
const ref = useRef()
129+
const ref = useRef(undefined)
130130

131131
useEffect(() => {
132132
setTimeout(() => {

packages/react-leaflet/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-leaflet",
3-
"version": "4.2.1",
3+
"version": "5.0.0-beta.1",
44
"description": "React components for Leaflet maps",
55
"repository": {
66
"type": "git",
@@ -35,7 +35,7 @@
3535
"prepublishOnly": "package-check"
3636
},
3737
"dependencies": {
38-
"@react-leaflet/core": "workspace:^2.1.0"
38+
"@react-leaflet/core": "workspace:^"
3939
},
4040
"peerDependencies": {
4141
"leaflet": "^1.9.0",

packages/react-leaflet/src/MapContainer.tsx

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
2-
type LeafletContextInterface,
32
LeafletContext,
3+
type LeafletContextInterface,
44
createLeafletContext,
55
} from '@react-leaflet/core'
66
import {
@@ -14,9 +14,9 @@ import React, {
1414
type ReactNode,
1515
type Ref,
1616
forwardRef,
17-
useCallback,
1817
useEffect,
1918
useImperativeHandle,
19+
useRef,
2020
useState,
2121
} from 'react'
2222

@@ -51,12 +51,13 @@ function MapContainerComponent<
5151
) {
5252
const [props] = useState({ className, id, style })
5353
const [context, setContext] = useState<LeafletContextInterface | null>(null)
54-
useImperativeHandle(forwardedRef, () => context?.map ?? undefined, [context])
54+
const mapInstanceRef = useRef<LeafletMap>(undefined)
55+
useImperativeHandle(forwardedRef, () => mapInstanceRef.current)
5556

56-
// biome-ignore lint/correctness/useExhaustiveDependencies: ref callback
57-
const mapRef = useCallback((node: HTMLDivElement | null) => {
58-
if (node !== null && context === null) {
57+
const mapRef = (node?: HTMLDivElement | null) => {
58+
if (node != null && !mapInstanceRef.current) {
5959
const map = new LeafletMap(node, options)
60+
mapInstanceRef.current = map
6061
if (center != null && zoom != null) {
6162
map.setView(center, zoom)
6263
} else if (bounds != null) {
@@ -67,7 +68,7 @@ function MapContainerComponent<
6768
}
6869
setContext(createLeafletContext(map))
6970
}
70-
}, [])
71+
}
7172

7273
useEffect(() => {
7374
return () => {

packages/react-leaflet/src/Pane.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
2-
type LeafletContextInterface,
32
LeafletContext,
3+
type LeafletContextInterface,
44
addClassName,
55
useLeafletContext,
66
} from '@react-leaflet/core'

pnpm-lock.yaml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)