Skip to content

Commit

Permalink
react_maps: write python tests for react leaflet map
Browse files Browse the repository at this point in the history
  • Loading branch information
vellip authored and goapunk committed Feb 14, 2024
1 parent 2c59289 commit f6e827a
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tests/maps_react/test_templatetags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import json

import pytest
from django.test import override_settings
from django.utils.html import format_html

from adhocracy4.test.helpers import render_template


@override_settings(A4_MAP_BASEURL="https://{s}.tile.openstreetmap.org/")
@override_settings(A4_MAP_ATTRIBUTION='<a href="example.com">attribution</a>')
@pytest.mark.django_db
def test_react_maps_without_point(rf, area_settings):
request = rf.get("/")
attrs = {
"map": {
"attribution": '<a href="example.com">attribution</a>',
"useVectorMap": 0,
"baseUrl": "https://{s}.tile.openstreetmap.org/",
"polygon": area_settings.polygon,
"name": "test",
}
}
expected = format_html(
'<div data-a4-widget="choose-point" data-attributes="{attributes}"></div>',
attributes=json.dumps(attrs),
)

context = {"request": request, "polygon": area_settings.polygon}
template = '{% load react_maps_tags %}{% react_choose_point polygon=polygon point=point name="test" %}'
result = render_template(template, context)
assert result == expected


@override_settings(A4_MAP_BASEURL="https://{s}.tile.openstreetmap.org/")
@override_settings(A4_MAP_ATTRIBUTION='<a href="example.com">attribution</a>')
@pytest.mark.django_db
def test_react_maps_with_point(rf, area_settings):
request = rf.get("/")
point = {"test": [1, 2]}

attrs = {
"map": {
"attribution": '<a href="example.com">attribution</a>',
"useVectorMap": 0,
"baseUrl": "https://{s}.tile.openstreetmap.org/",
"polygon": area_settings.polygon,
"point": point,
"name": "test",
}
}
expected = format_html(
'<div data-a4-widget="choose-point" data-attributes="{attributes}"></div>',
attributes=json.dumps(attrs),
)

context = {"request": request, "polygon": area_settings.polygon, "point": point}
template = '{% load react_maps_tags %}{% react_choose_point polygon=polygon point=point name="test" %}'
result = render_template(template, context)
assert result == expected
48 changes: 48 additions & 0 deletions tests/maps_react/test_widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import json
from unittest.mock import patch

import pytest
from django.core.exceptions import ImproperlyConfigured
from django.test import override_settings
from django.utils.html import format_html

from adhocracy4.maps_react import widgets


@override_settings(A4_MAP_BASEURL="https://{s}.tile.openstreetmap.org/")
@override_settings(A4_MAP_ATTRIBUTION='<a href="example.com">attribution</a>')
@pytest.mark.django_db
def test_choose_point_widget_throws(area_settings):
widget = widgets.MapChoosePointWidget(area_settings.polygon)
with pytest.raises(ImproperlyConfigured):
widget.render("test_filter", "test_val1", attrs={"id": "test_id"})


@override_settings(A4_MAP_BASEURL="https://{s}.tile.openstreetmap.org/")
@override_settings(A4_MAP_ATTRIBUTION='<a href="example.com">attribution</a>')
@pytest.mark.django_db
def test_choose_point_widget(area_settings):
widget = widgets.MapChoosePointWidget(area_settings.polygon)
with patch("django.contrib.staticfiles.finders.find") as mock_find:
mock_find.return_value = "path/to/a4maps_choose_point.js"
html = widget.render("test_filter", "test_val1", attrs={"id": "test_id"})
attrs = {
"map": {
"attribution": '<a href="example.com">attribution</a>',
"useVectorMap": 0,
"baseUrl": "https://{s}.tile.openstreetmap.org/",
"polygon": area_settings.polygon,
"point": "test_val1",
"name": "test_filter",
}
}
expected = format_html(
"""
<div data-a4-widget="choose-point" data-attributes="{attributes}"></div>
<input id="id_test_filter" type="hidden" name="test_filter" value="test_val1">
""",
attributes=json.dumps(attrs),
)
assert widget.polygon == area_settings.polygon
assert html == expected
1 change: 1 addition & 0 deletions tests/project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"adhocracy4.comments",
"adhocracy4.comments_async",
"adhocracy4.maps",
"adhocracy4.maps_react",
"adhocracy4.actions",
"adhocracy4.follows",
"adhocracy4.filters",
Expand Down

0 comments on commit f6e827a

Please sign in to comment.