Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulLeCam committed May 1, 2024
1 parent 214def9 commit 6d253ed
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 29 deletions.
9 changes: 7 additions & 2 deletions packages/core/__tests__/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { renderHook } from '@testing-library/react'
import type { Map } from 'leaflet'
import React, { StrictMode, type ReactNode } from 'react'

import { CONTEXT_VERSION, LeafletContext, createLeafletContext, useLeafletContext } from '../src'
import {
CONTEXT_VERSION,
LeafletContext,
createLeafletContext,
useLeafletContext,
} from '../src'

export function createWrapper(context) {
return function Wrapper({ children }: { children: ReactNode }) {
Expand All @@ -28,7 +33,7 @@ describe('context', () => {
const { result } = renderHook(() => useLeafletContext())
return result.current
}).toThrow(
'No context provided: useLeafletContext() can only be used in a descendant of <MapContainer>'
'No context provided: useLeafletContext() can only be used in a descendant of <MapContainer>',
)
})

Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@react-leaflet/core",
"version": "2.1.0",
"version": "3.0.0-beta.1",
"description": "React Leaflet core",
"repository": {
"type": "git",
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export { useAttribution } from './attribution.js'
export { type CircleMarkerProps, type CircleProps, updateCircle } from './circle.js'
export {
type CircleMarkerProps,
type CircleProps,
updateCircle,
} from './circle.js'
export {
createContainerComponent,
createDivOverlayComponent,
Expand Down
43 changes: 31 additions & 12 deletions packages/react-leaflet/__tests__/MapContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { render } from '@testing-library/react'
import { type LatLngExpression, Map } from 'leaflet'
import { type LatLngExpression, Map as LeafletMap } from 'leaflet'
import React, { StrictMode, useEffect, useRef } from 'react'

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

describe('provides the Map instance', () => {
test('with the useMap() hook', (done) => {
let doneCalled = false

function TestChild() {
const map = useMap()
expect(map).toBeInstanceOf(Map)
done()
expect(map).toBeInstanceOf(LeafletMap)
if (!doneCalled) {
doneCalled = true
done()
}
return null
}

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

test('in the ref function', (done) => {
let doneCalled = false

const ref = (map) => {
if (map !== null) {
expect(map).toBeInstanceOf(Map)
done()
if (map != null) {
expect(map).toBeInstanceOf(LeafletMap)
if (!doneCalled) {
doneCalled = true
done()
}
}
}

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

test('in the ref object', (done) => {
let doneCalled = false

function Wrapper() {
const ref = useRef()
const ref = useRef(undefined)

useEffect(() => {
setTimeout(() => {
if (ref.current !== null) {
expect(ref.current).toBeInstanceOf(Map)
done()
if (ref.current != null) {
expect(ref.current).toBeInstanceOf(LeafletMap)
if (!doneCalled) {
doneCalled = true
done()
}
}
}, 50)
}, [])
Expand All @@ -86,12 +101,16 @@ describe('MapContainer', () => {
test('sets center and zoom props', (done) => {
const center: LatLngExpression = [1.2, 3.4]
const zoom = 10
let doneCalled = false

const ref = (map) => {
if (map !== null) {
if (map != null) {
expect(map.getCenter()).toEqual({ lat: 1.2, lng: 3.4 })
expect(map.getZoom()).toBe(zoom)
done()
if (!doneCalled) {
doneCalled = true
done()
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/react-leaflet/__tests__/Pane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('Pane', () => {
describe('supports refs', () => {
test('as callback function', (done) => {
const ref = (pane) => {
if (pane !== null) {
if (pane != null) {
expect(pane).toBeInstanceOf(HTMLElement)
done()
}
Expand All @@ -126,7 +126,7 @@ describe('Pane', () => {

test('as object', (done) => {
function Wrapper() {
const ref = useRef()
const ref = useRef(undefined)

useEffect(() => {
setTimeout(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/react-leaflet/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-leaflet",
"version": "4.2.1",
"version": "5.0.0-beta.1",
"description": "React components for Leaflet maps",
"repository": {
"type": "git",
Expand Down Expand Up @@ -35,7 +35,7 @@
"prepublishOnly": "package-check"
},
"dependencies": {
"@react-leaflet/core": "workspace:^2.1.0"
"@react-leaflet/core": "workspace:^"
},
"peerDependencies": {
"leaflet": "^1.9.0",
Expand Down
15 changes: 8 additions & 7 deletions packages/react-leaflet/src/MapContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
type LeafletContextInterface,
LeafletContext,
type LeafletContextInterface,
createLeafletContext,
} from '@react-leaflet/core'
import {
Expand All @@ -14,9 +14,9 @@ import React, {
type ReactNode,
type Ref,
forwardRef,
useCallback,
useEffect,
useImperativeHandle,
useRef,
useState,
} from 'react'

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

// biome-ignore lint/correctness/useExhaustiveDependencies: ref callback
const mapRef = useCallback((node: HTMLDivElement | null) => {
if (node !== null && context === null) {
const mapRef = (node?: HTMLDivElement | null) => {
if (node != null && !mapInstanceRef.current) {
const map = new LeafletMap(node, options)
mapInstanceRef.current = map
if (center != null && zoom != null) {
map.setView(center, zoom)
} else if (bounds != null) {
Expand All @@ -67,7 +68,7 @@ function MapContainerComponent<
}
setContext(createLeafletContext(map))
}
}, [])
}

useEffect(() => {
return () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-leaflet/src/Pane.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
type LeafletContextInterface,
LeafletContext,
type LeafletContextInterface,
addClassName,
useLeafletContext,
} from '@react-leaflet/core'
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6d253ed

Please sign in to comment.