Skip to content

Commit f2c7a29

Browse files
committed
react_maps: write python tests for react leaflet map
1 parent b01a56d commit f2c7a29

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

tests/maps_react/test_templatetags.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import json
2+
3+
import pytest
4+
from django.test import override_settings
5+
from django.utils.html import format_html
6+
7+
from adhocracy4.test.helpers import render_template
8+
9+
10+
@override_settings(A4_MAP_BASEURL="https://{s}.tile.openstreetmap.org/")
11+
@override_settings(A4_MAP_ATTRIBUTION='<a href="example.com">attribution</a>')
12+
@pytest.mark.django_db
13+
def test_react_maps_without_point(rf, area_settings):
14+
request = rf.get("/")
15+
attrs = {
16+
"map": {
17+
"attribution": '<a href="example.com">attribution</a>',
18+
"useVectorMap": 0,
19+
"baseUrl": "https://{s}.tile.openstreetmap.org/",
20+
"polygon": area_settings.polygon,
21+
"name": "test",
22+
}
23+
}
24+
expected = format_html(
25+
'<div data-a4-widget="choose-point" data-attributes="{attributes}"></div>',
26+
attributes=json.dumps(attrs),
27+
)
28+
29+
context = {"request": request, "polygon": area_settings.polygon}
30+
template = '{% load react_maps_tags %}{% react_choose_point polygon=polygon point=point name="test" %}'
31+
result = render_template(template, context)
32+
assert result == expected
33+
34+
35+
@override_settings(A4_MAP_BASEURL="https://{s}.tile.openstreetmap.org/")
36+
@override_settings(A4_MAP_ATTRIBUTION='<a href="example.com">attribution</a>')
37+
@pytest.mark.django_db
38+
def test_react_maps_with_point(rf, area_settings):
39+
request = rf.get("/")
40+
point = {"test": [1, 2]}
41+
42+
attrs = {
43+
"map": {
44+
"attribution": '<a href="example.com">attribution</a>',
45+
"useVectorMap": 0,
46+
"baseUrl": "https://{s}.tile.openstreetmap.org/",
47+
"polygon": area_settings.polygon,
48+
"point": point,
49+
"name": "test",
50+
}
51+
}
52+
expected = format_html(
53+
'<div data-a4-widget="choose-point" data-attributes="{attributes}"></div>',
54+
attributes=json.dumps(attrs),
55+
)
56+
57+
context = {"request": request, "polygon": area_settings.polygon, "point": point}
58+
template = '{% load react_maps_tags %}{% react_choose_point polygon=polygon point=point name="test" %}'
59+
result = render_template(template, context)
60+
assert result == expected

tests/maps_react/test_widget.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import json
2+
from unittest.mock import patch
3+
4+
import pytest
5+
from django.core.exceptions import ImproperlyConfigured
6+
from django.test import override_settings
7+
from django.utils.html import format_html
8+
9+
from adhocracy4.maps_react import widgets
10+
11+
12+
@override_settings(A4_MAP_BASEURL="https://{s}.tile.openstreetmap.org/")
13+
@override_settings(A4_MAP_ATTRIBUTION='<a href="example.com">attribution</a>')
14+
@pytest.mark.django_db
15+
def test_choose_point_widget_throws(area_settings):
16+
widget = widgets.MapChoosePointWidget(area_settings.polygon)
17+
with pytest.raises(ImproperlyConfigured):
18+
widget.render("test_filter", "test_val1", attrs={"id": "test_id"})
19+
20+
21+
@override_settings(A4_MAP_BASEURL="https://{s}.tile.openstreetmap.org/")
22+
@override_settings(A4_MAP_ATTRIBUTION='<a href="example.com">attribution</a>')
23+
@pytest.mark.django_db
24+
def test_choose_point_widget(area_settings):
25+
widget = widgets.MapChoosePointWidget(area_settings.polygon)
26+
with patch("django.contrib.staticfiles.finders.find") as mock_find:
27+
mock_find.return_value = "path/to/a4maps_choose_point.js"
28+
html = widget.render("test_filter", "test_val1", attrs={"id": "test_id"})
29+
attrs = {
30+
"map": {
31+
"attribution": '<a href="example.com">attribution</a>',
32+
"useVectorMap": 0,
33+
"baseUrl": "https://{s}.tile.openstreetmap.org/",
34+
"polygon": area_settings.polygon,
35+
"point": "test_val1",
36+
"name": "test_filter",
37+
}
38+
}
39+
expected = format_html(
40+
"""
41+
42+
<div data-a4-widget="choose-point" data-attributes="{attributes}"></div>
43+
<input id="id_test_filter" type="hidden" name="test_filter" value="test_val1">
44+
""",
45+
attributes=json.dumps(attrs),
46+
)
47+
assert widget.polygon == area_settings.polygon
48+
assert html == expected

tests/project/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"adhocracy4.comments",
3636
"adhocracy4.comments_async",
3737
"adhocracy4.maps",
38+
"adhocracy4.maps_react",
3839
"adhocracy4.actions",
3940
"adhocracy4.follows",
4041
"adhocracy4.filters",

0 commit comments

Comments
 (0)