|
| 1 | +import re |
| 2 | +from time import sleep |
| 3 | +import json |
| 4 | +import pytest |
| 5 | +from playwright.sync_api import expect |
| 6 | + |
| 7 | +from django.urls import path |
| 8 | + |
| 9 | +from .utils import get_javascript_catalog, ContextMixin |
| 10 | +from formset.views import FormCollectionView |
| 11 | +from testapp.forms.regioncollection import RegionCollection |
| 12 | + |
| 13 | +# class DemoFormView(ContextMixin, FormView): |
| 14 | +# template_name = 'testapp/native-form.html' #'../../formset/templates/formset/default/widgets/leaflet.html' |
| 15 | +# success_url = '/success' |
| 16 | + |
| 17 | +# urlpatterns = [ |
| 18 | +# path('regioncollection', DemoFormView.as_view( |
| 19 | +# form_class=MapForm, |
| 20 | +# extra_context={'click_actions': 'submit -> proceed', 'force_submission': True}, |
| 21 | +# ), name='regioncollection'), |
| 22 | +# get_javascript_catalog(), |
| 23 | +# ] |
| 24 | + |
| 25 | +class FormCollectionView(ContextMixin, FormCollectionView): |
| 26 | + success_url='/success' |
| 27 | + |
| 28 | +urlpatterns = [ |
| 29 | + path('regioncollection', FormCollectionView.as_view( |
| 30 | + collection_class=RegionCollection, |
| 31 | + template_name='testapp/form-collection.html', |
| 32 | + extra_context={'click_actions': 'submit -> proceed', 'force_submission': True}, |
| 33 | + ), name='regioncollection'), |
| 34 | + get_javascript_catalog(), |
| 35 | +] |
| 36 | + |
| 37 | +@pytest.mark.urls(__name__) |
| 38 | +@pytest.mark.parametrize('viewname', ['regioncollection']) |
| 39 | +def test_elements_visible(page, viewname): |
| 40 | + django_leafletclient = page.locator('django-formset div[is="django-leafletclient"]') |
| 41 | + expect(django_leafletclient).to_be_visible() |
| 42 | + |
| 43 | + leaflet_map = page.locator('#id_regionmaps0regionmapgeometry-map') |
| 44 | + expect(leaflet_map).to_be_visible() |
| 45 | + |
| 46 | + input_field = page.locator('input[name="caption"]') |
| 47 | + expect(input_field).to_be_visible() |
| 48 | + |
| 49 | + id_field = page.locator('input[name="id"]') |
| 50 | + expect(id_field).not_to_be_visible() |
| 51 | + |
| 52 | +@pytest.mark.urls(__name__) |
| 53 | +@pytest.mark.parametrize('viewname', ['regioncollection']) |
| 54 | +def test_submit_region_collection(page, mocker, viewname): |
| 55 | + page.screenshot(path="start.png") |
| 56 | + spy = mocker.spy(FormCollectionView, 'post') |
| 57 | + |
| 58 | + collection_name= page.locator('input[id="id_region.name"]') |
| 59 | + expect(collection_name).to_be_visible() |
| 60 | + collection_name.fill('Regions') |
| 61 | + |
| 62 | + page.click('a.leaflet-draw-draw-rectangle') |
| 63 | + rectangle_button = page.locator('.leaflet-draw-draw-rectangle.leaflet-draw-toolbar-button-enabled') |
| 64 | + expect(rectangle_button).to_have_class(re.compile("enabled")) |
| 65 | + page.mouse.move(500,200) |
| 66 | + page.mouse.down() |
| 67 | + page.mouse.move(550, 250) |
| 68 | + page.mouse.up() |
| 69 | + page.screenshot(path="marker.png") |
| 70 | + |
| 71 | + caption = page.locator('input[id="id_regionmaps.0.regionmap.caption"]') |
| 72 | + expect(caption).to_be_visible() |
| 73 | + caption.fill('Region') |
| 74 | + page.screenshot(path="caption.png") |
| 75 | + |
| 76 | + submit_button = page.locator('button:has-text("Submit")') |
| 77 | + expect(submit_button).to_be_visible() |
| 78 | + expect(submit_button).to_be_enabled() |
| 79 | + submit_button.click() |
| 80 | + request = json.loads(spy.call_args.args[1].body) |
| 81 | + expected = { |
| 82 | + 'formset_data': { |
| 83 | + 'regionmaps': [{ |
| 84 | + 'regionmap': { |
| 85 | + 'geometry': '{"type":"Polygon","coordinates":[[[6.807404,50.74167],[6.807404,50.785102],[6.876068,50.785102],[6.876068,50.74167],[6.807404,50.74167]]]}', |
| 86 | + 'caption': 'Region', |
| 87 | + 'id': '' |
| 88 | + }}], |
| 89 | + 'region': { |
| 90 | + 'name': 'Regions' |
| 91 | + }}} |
| 92 | + |
| 93 | + assert "Polygon" in request["formset_data"]["regionmaps"][0]["regionmap"]["geometry"] |
| 94 | + assert request == expected |
| 95 | + sleep(0.2) |
| 96 | + spy.assert_called() |
| 97 | + assert spy.spy_return.status_code == 200 |
0 commit comments