-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
react_maps: write js tests for react leaflet map
- Loading branch information
Showing
8 changed files
with
179 additions
and
4 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
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
60 changes: 60 additions & 0 deletions
60
adhocracy4/maps_react/static/a4maps_react/__tests__/AddMarkerControl.jest.js
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,60 @@ | ||
import L from 'leaflet' | ||
import { AddMarkerControlClass } from '../AddMarkerControl' | ||
import { polygonData } from './Map.jest' | ||
import { jest } from '@jest/globals' | ||
|
||
describe('AddMarkerControlClass', () => { | ||
const polygonGeoJSON = L.geoJSON(polygonData) | ||
const map = { on: jest.fn(), off: jest.fn(), addLayer: jest.fn(), constraints: polygonGeoJSON } | ||
const point = JSON.stringify({ | ||
type: 'Feature', | ||
properties: {}, | ||
geometry: { | ||
type: 'Point', | ||
coordinates: [5, 5] | ||
} | ||
}) | ||
|
||
it('sets a marker', () => { | ||
const input = document.createElement('input') | ||
const instance = new AddMarkerControlClass({ input }) | ||
instance.map = map | ||
|
||
const latlng = { lat: 5, lng: 5 } | ||
|
||
expect(instance.marker).toBe(null) | ||
instance.updateMarker(latlng) | ||
expect(instance.marker).toBeDefined() | ||
expect(input.value).toEqual(expect.stringContaining('5,5')) | ||
instance.updateMarker({ lat: 2, lng: 2 }) | ||
expect(input.value).toEqual(expect.stringContaining('2,2')) | ||
}) | ||
|
||
it('does not set a marker when outside', () => { | ||
const input = document.createElement('input') | ||
const instance = new AddMarkerControlClass({ input }) | ||
instance.map = map | ||
const latlng = { lat: 15, lng: 15 } | ||
expect(instance.marker).toBe(null) | ||
instance.updateMarker(latlng) | ||
expect(instance.marker).toBe(null) | ||
expect(input.value).toEqual('') | ||
}) | ||
|
||
it('updates on drag', () => { | ||
const input = document.createElement('input') | ||
const instance = new AddMarkerControlClass({ input, point }) | ||
instance.map = map | ||
expect(instance.oldCoords).toStrictEqual([5, 5]) | ||
const newCoords = { lat: 10, lng: 10 } | ||
|
||
const e = { target: { getLatLng: () => newCoords, setLatLng: jest.fn() } } | ||
instance.onDragend(e) | ||
expect(instance.oldCoords).toStrictEqual(newCoords) | ||
|
||
const e2 = { target: { getLatLng: () => ({ lat: 15, lng: 15 }), setLatLng: jest.fn() } } | ||
instance.onDragend(e2) | ||
expect(e2.target.setLatLng).toHaveBeenCalledWith(newCoords) | ||
expect(instance.oldCoords).toStrictEqual(newCoords) | ||
}) | ||
}) |
66 changes: 66 additions & 0 deletions
66
adhocracy4/maps_react/static/a4maps_react/__tests__/Map.jest.jsx
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,66 @@ | ||
import React from 'react' | ||
import { render, screen } from '@testing-library/react' | ||
import Map from '../Map' | ||
|
||
export const polygonData = { | ||
type: 'Feature', | ||
properties: {}, | ||
geometry: { | ||
type: 'Polygon', | ||
coordinates: [ | ||
[ | ||
[0, 0], | ||
[10, 0], | ||
[10, 10], | ||
[0, 10] | ||
] | ||
] | ||
} | ||
} | ||
|
||
jest.mock('react-leaflet', () => { | ||
const ActualReactLeaflet = jest.requireActual('react-leaflet') | ||
const React = require('react') | ||
|
||
const MapContainer = React.forwardRef((props, ref) => ( | ||
<div data-testid="map"> | ||
<ActualReactLeaflet.MapContainer ref={ref} {...props} /> | ||
</div> | ||
)) | ||
MapContainer.displayName = 'MapContainer' | ||
|
||
const GeoJSON = React.forwardRef((props, ref) => ( | ||
<div data-testid="geojson"><ActualReactLeaflet.GeoJSON ref={ref} props={props} /></div> | ||
)) | ||
GeoJSON.displayName = 'GeoJSON' | ||
|
||
return { | ||
__esModule: true, | ||
...ActualReactLeaflet, | ||
GeoJSON, | ||
MapContainer | ||
} | ||
}) | ||
|
||
describe('Map component tests', () => { | ||
test('component renders', () => { | ||
render(<Map />) | ||
const mapNode = screen.getByTestId('map') | ||
|
||
expect(mapNode).toBeTruthy() | ||
}) | ||
|
||
test('renders map with GeoJSON when polygon prop is provided', () => { | ||
render(<Map polygon={polygonData} />) | ||
const geoJsonNode = screen.getByTestId('geojson') | ||
|
||
expect(geoJsonNode).toBeTruthy() | ||
}) | ||
|
||
test('does not render GeoJSON when polygon prop is not provided', () => { | ||
render(<Map />) | ||
const geoJsonNode = screen.queryByTestId('geojson') | ||
|
||
expect(geoJsonNode).toBeFalsy() | ||
}) | ||
}) |
38 changes: 38 additions & 0 deletions
38
adhocracy4/maps_react/static/a4maps_react/__tests__/ZoomControl.jest.js
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,38 @@ | ||
import { createLeafletElement } from '../ZoomControl' | ||
import L from 'leaflet' | ||
|
||
describe('ZoomControl', () => { | ||
it('createLeafletElement should return a zoom control', () => { | ||
const zoomControl = createLeafletElement({}) | ||
expect(zoomControl).toBeInstanceOf(L.Control.Zoom) | ||
|
||
const map = { | ||
_zoom: 5, | ||
getMinZoom: jest.fn(() => 3), | ||
getMaxZoom: jest.fn(() => 10), | ||
getZoom: jest.fn(), | ||
on: jest.fn(), | ||
off: jest.fn() | ||
} | ||
L.DomUtil.addClass = jest.fn() | ||
L.DomUtil.removeClass = jest.fn() | ||
zoomControl._map = map | ||
|
||
const onAdd = jest.spyOn(L.Control.Zoom.prototype, 'onAdd') | ||
const onRemove = jest.spyOn(L.Control.Zoom.prototype, 'onRemove') | ||
|
||
zoomControl.onAdd(map) | ||
|
||
expect(map.on).toHaveBeenCalledWith('zoom', expect.any(Function)) | ||
expect(onAdd).toHaveBeenCalledWith(map) | ||
expect(map.getMinZoom).toHaveBeenCalled() | ||
expect(map.getMaxZoom).toHaveBeenCalled() | ||
expect(L.DomUtil.addClass).toHaveBeenCalledTimes(0) | ||
expect(L.DomUtil.removeClass).toHaveBeenCalledTimes(2) | ||
|
||
zoomControl.onRemove(map) | ||
|
||
expect(map.off).toHaveBeenCalledWith('zoom', expect.any(Function)) | ||
expect(onRemove).toHaveBeenCalledWith(map) | ||
}) | ||
}) |
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,3 @@ | ||
if (typeof window.URL.createObjectURL === 'undefined') { | ||
window.URL.createObjectURL = () => {} | ||
} |