-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move zoom and coordinates as separate component
We don't have to render zoom/coordiantes if there is no change in maps movement. Added unit test to check render Signed-off-by: Vijayan Balasubramanian <[email protected]>
- Loading branch information
Showing
3 changed files
with
78 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React from 'react'; | ||
import { render, unmountComponentAtNode } from 'react-dom'; | ||
import { act } from 'react-dom/test-utils'; | ||
|
||
import { MapsFooter } from './mapsFooter'; | ||
import { LngLat } from 'maplibre-gl'; | ||
|
||
let container: Element | null; | ||
beforeEach(() => { | ||
// setup a DOM element as a render target | ||
container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
}); | ||
|
||
afterEach(() => { | ||
// cleanup on exiting | ||
unmountComponentAtNode(container!); | ||
container?.remove(); | ||
container = null; | ||
}); | ||
|
||
it('renders with or without coordinates', () => { | ||
act(() => { | ||
render(<MapsFooter zoom={3} />, container); | ||
}); | ||
expect(container?.textContent).toBe('zoom: 3'); | ||
|
||
act(() => { | ||
const coordinates: LngLat = new LngLat(-73.974912, 40.773654); | ||
render(<MapsFooter zoom={3} coordinates={coordinates} />, container); | ||
}); | ||
expect(container?.textContent).toBe('lat: 40.7737, lon: -73.9749, zoom: 3'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { EuiPanel } from '@elastic/eui'; | ||
import React, { memo } from 'react'; | ||
import { LngLat } from 'maplibre-gl'; | ||
|
||
const coordinatesRoundOffDigit = 4; | ||
interface MapFooterProps { | ||
coordinates?: LngLat; | ||
zoom: number; | ||
} | ||
|
||
// props are equal only if zoom and lat/lng are equal | ||
const propsComparator = (prevProps: MapFooterProps, nextProps: MapFooterProps) => { | ||
return ( | ||
nextProps.zoom === prevProps.zoom && | ||
nextProps.coordinates?.lng === prevProps.coordinates?.lng && | ||
nextProps.coordinates?.lat === prevProps.coordinates?.lat | ||
); | ||
}; | ||
|
||
export const MapsFooter = memo(({ coordinates, zoom }: MapFooterProps) => { | ||
return ( | ||
<EuiPanel | ||
hasShadow={false} | ||
hasBorder={false} | ||
color="transparent" | ||
className="zoombar" | ||
data-test-subj="mapStatusBar" | ||
> | ||
<small> | ||
{coordinates && | ||
`lat: ${coordinates.lat.toFixed( | ||
coordinatesRoundOffDigit | ||
)}, lon: ${coordinates.lng.toFixed(coordinatesRoundOffDigit)}, `} | ||
zoom: {zoom} | ||
</small> | ||
</EuiPanel> | ||
); | ||
}, propsComparator); |