-
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
10 changed files
with
162 additions
and
112 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 was deleted.
Oops, something went wrong.
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(), markerConstraints: 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: 10, lng: 5 } | ||
|
||
expect(instance.marker).toBe(null) | ||
instance.updateMarker(latlng) | ||
expect(instance.marker).toBeDefined() | ||
expect(input.value).toEqual(expect.stringContaining('5,10')) | ||
instance.updateMarker({ lat: 2, lng: 5 }) | ||
expect(input.value).toEqual(expect.stringContaining('5,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() | ||
}) | ||
}) |
2 changes: 1 addition & 1 deletion
2
adhocracy4/maps_react/templates/a4maps_react/map_choose_point_widget.html
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{% load react_maps_tags %} | ||
|
||
{% react_choose_point polygon=polygon point=point name=name %} | ||
{% react_choose_point polygon point name %} | ||
<input id="id_{{ name }}" type="hidden" name="{{ name }}" {% if point %}value="{{ point }}"{% endif %}> |
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 |
---|---|---|
@@ -1,11 +1,20 @@ | ||
import json | ||
|
||
from django import template | ||
from django.utils.html import format_html | ||
|
||
from adhocracy4.maps_react.utils import react_tag_factory | ||
from adhocracy4.maps_react.utils import get_map_settings | ||
|
||
register = template.Library() | ||
|
||
register.simple_tag( | ||
react_tag_factory("choose-point"), | ||
False, | ||
"react_choose_point", | ||
) | ||
|
||
@register.simple_tag() | ||
def react_choose_point(polygon, point, name): | ||
attributes = { | ||
"map": get_map_settings(polygon=polygon, point=point, name=name), | ||
} | ||
|
||
return format_html( | ||
'<div data-a4-widget="choose-point" ' 'data-attributes="{attributes}"></div>', | ||
attributes=json.dumps(attributes), | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
if (typeof window.URL.createObjectURL === 'undefined') { | ||
window.URL.createObjectURL = () => {} | ||
} |