-
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 python tests for react leaflet map
- Loading branch information
Showing
3 changed files
with
109 additions
and
0 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
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 |
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,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 |
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