1+ import unittest
2+
13from django .test import TestCase
4+ from wagtail import VERSION as WAGTAIL_VERSION
25
36from wagtailgeowidget import app_settings , geocoders
4- from wagtailgeowidget .widgets import GeocoderField , GoogleMapsField , LeafletField
7+ from wagtailgeowidget .widgets import (
8+ GeocoderField ,
9+ GoogleMapsField ,
10+ GoogleMapsFieldAdapter ,
11+ LeafletField ,
12+ LeafletFieldAdapter ,
13+ )
514
615
716class GoogleMapsFieldTestCase (TestCase ):
8- def test_google_maps_field_contains_constuct (self ):
17+ def test_google_maps_field_contains_construct (self ):
918 widget = GoogleMapsField ()
1019 html = widget .render (
1120 "field" ,
@@ -20,33 +29,99 @@ def test_google_maps_field_contains_constuct(self):
2029 html ,
2130 )
2231
32+ def test_streamfield_widget_uses_empty_id_prefix (self ):
33+ """Test that StreamField widgets use empty id_prefix."""
2334
24- class LeafletFieldTestCase (TestCase ):
25- def test_leaflet_field_contains_constuct (self ):
26- widget = LeafletField ()
35+ widget = GoogleMapsField (srid = 4326 , id_prefix = "" )
36+
37+ self .assertEqual (widget .id_prefix , "" )
38+
39+ def test_fieldpanel_widget_includes_stimulus_attributes (self ):
40+ """Test that FieldPanel widgets (id_prefix='id_') include Stimulus controller attributes."""
41+
42+ widget = GoogleMapsField (srid = 4326 , id_prefix = "id_" )
2743 html = widget .render (
2844 "field" ,
29- "" ,
45+ "SRID=4326;POINT(18.0686 59.3293) " ,
3046 {
31- "id" : "X " ,
47+ "id" : "test-field " ,
3248 },
3349 )
3450
35- self .assertIn (
36- '<input type="hidden" name="field" id="X" data-controller="leaflet-field"' ,
37- html ,
51+ self .assertIn ('data-controller="google-maps-field"' , html )
52+ self .assertIn ("data-google-maps-field-options-value=" , html )
53+
54+ def test_streamfield_widget_excludes_stimulus_attributes (self ):
55+ """Test that StreamField widgets (id_prefix='') exclude Stimulus controller attributes."""
56+ widget = GoogleMapsField (srid = 4326 , id_prefix = "" )
57+
58+ html = widget .render (
59+ "field" ,
60+ "SRID=4326;POINT(18.0686 59.3293)" ,
61+ {
62+ "id" : "test-field" ,
63+ },
3864 )
3965
40- def test_leaflet_field_js_init_contains_construct (self ):
41- widget = LeafletField ()
66+ self .assertNotIn ('data-controller="google-maps-field"' , html )
67+ self .assertNotIn ("data-google-maps-field-options-value=" , html )
68+
69+ @unittest .skipIf (WAGTAIL_VERSION < (7 , 1 ), "Test only applicable for Wagtail 7.1+" )
70+ def test_telepath_adapter_js_args_structure (self ):
71+ """Test that the adapter returns correct js_args structure for Telepath."""
72+
73+ widget = GoogleMapsField (
74+ srid = 4326 ,
75+ address_field = "address" ,
76+ zoom_field = "zoom" ,
77+ )
78+ adapter = GoogleMapsFieldAdapter ()
79+
80+ result = adapter .js_args (widget )
81+
82+ self .assertEqual (len (result ), 2 )
83+
84+ self .assertIsInstance (result [0 ], str )
85+ self .assertIn ('<input type="hidden"' , result [0 ])
86+
87+ self .assertIsInstance (result [1 ], dict )
88+ options = result [1 ]
89+
90+ self .assertIn ("srid" , options )
91+ self .assertIn ("addressField" , options )
92+ self .assertIn ("zoomField" , options )
93+ self .assertIn ("defaultLocation" , options )
94+ self .assertIn ("zoom" , options )
95+ self .assertIn ("mapId" , options )
4296
97+ self .assertEqual (options ["srid" ], 4326 )
98+ self .assertEqual (options ["addressField" ], "address" )
99+ self .assertEqual (options ["zoomField" ], "zoom" )
100+
101+ def test_telepath_adapter_streamfield_excludes_stimulus_attributes (self ):
102+ """Test that HTML by adapter for StreamField widget has no Stimulus attributes."""
103+
104+ widget = GoogleMapsField (srid = 4326 , id_prefix = "" )
105+ adapter = GoogleMapsFieldAdapter ()
106+
107+ result = adapter .js_args (widget )
108+ html = result [0 ]
109+
110+ self .assertNotIn ("data-controller=" , html )
111+ self .assertNotIn ("data-google-maps-field-options-value=" , html )
112+
113+
114+ class LeafletFieldTestCase (TestCase ):
115+ def test_leaflet_field_contains_construct (self ):
116+ widget = LeafletField ()
43117 html = widget .render (
44118 "field" ,
45119 "" ,
46120 {
47121 "id" : "X" ,
48122 },
49123 )
124+
50125 self .assertIn (
51126 '<input type="hidden" name="field" id="X" data-controller="leaflet-field"' ,
52127 html ,
@@ -67,13 +142,94 @@ def test_value_are_parsed_properly(self):
67142 self .assertIn (escape ('"lat": "13.0"' ), html )
68143 self .assertIn (escape ('"lng": "12.0"' ), html )
69144
145+ def test_streamfield_widget_uses_empty_id_prefix (self ):
146+ """Test that StreamField widgets use empty id_prefix."""
147+
148+ widget = LeafletField (srid = 4326 , id_prefix = "" )
149+
150+ self .assertEqual (widget .id_prefix , "" )
151+
152+ def test_fieldpanel_widget_includes_stimulus_attributes (self ):
153+ """Test that FieldPanel widgets (id_prefix='id_') include Stimulus controller attributes."""
154+
155+ widget = LeafletField (srid = 4326 , id_prefix = "id_" )
156+ html = widget .render (
157+ "field" ,
158+ "SRID=4326;POINT(18.0686 59.3293)" ,
159+ {
160+ "id" : "test-field" ,
161+ },
162+ )
163+
164+ self .assertIn ('data-controller="leaflet-field"' , html )
165+ self .assertIn ("data-leaflet-field-options-value=" , html )
166+
167+ def test_streamfield_widget_excludes_stimulus_attributes (self ):
168+ """Test that StreamField widgets (id_prefix='') exclude Stimulus controller attributes."""
169+
170+ widget = LeafletField (srid = 4326 , id_prefix = "" )
171+
172+ html = widget .render (
173+ "field" ,
174+ "SRID=4326;POINT(18.0686 59.3293)" ,
175+ {
176+ "id" : "test-field" ,
177+ },
178+ )
179+
180+ self .assertNotIn ('data-controller="leaflet-field"' , html )
181+ self .assertNotIn ("data-leaflet-field-options-value=" , html )
182+
183+ @unittest .skipIf (WAGTAIL_VERSION < (7 , 1 ), "Test only applicable for Wagtail 7.1+" )
184+ def test_telepath_adapter_js_args_structure (self ):
185+ """Test that the adapter returns correct js_args structure for Telepath."""
186+
187+ widget = LeafletField (
188+ srid = 4326 ,
189+ address_field = "address" ,
190+ zoom_field = "zoom" ,
191+ )
192+ adapter = LeafletFieldAdapter ()
193+
194+ result = adapter .js_args (widget )
195+
196+ self .assertEqual (len (result ), 2 )
197+
198+ self .assertIsInstance (result [0 ], str )
199+ self .assertIn ('<input type="hidden"' , result [0 ])
200+
201+ self .assertIsInstance (result [1 ], dict )
202+ options = result [1 ]
203+
204+ self .assertIn ("srid" , options )
205+ self .assertIn ("addressField" , options )
206+ self .assertIn ("zoomField" , options )
207+ self .assertIn ("defaultLocation" , options )
208+ self .assertIn ("zoom" , options )
209+
210+ self .assertEqual (options ["srid" ], 4326 )
211+ self .assertEqual (options ["addressField" ], "address" )
212+ self .assertEqual (options ["zoomField" ], "zoom" )
213+
214+ def test_telepath_adapter_streamfield_excludes_stimulus_attributes (self ):
215+ """Test that HTML by adapter for StreamField widget has no Stimulus attributes."""
216+
217+ widget = LeafletField (srid = 4326 , id_prefix = "" )
218+ adapter = LeafletFieldAdapter ()
219+
220+ result = adapter .js_args (widget )
221+ html = result [0 ]
222+
223+ self .assertNotIn ("data-controller=" , html )
224+ self .assertNotIn ("data-leaflet-field-options-value=" , html )
225+
70226
71227class GeocoderFieldTestCase (TestCase ):
72228 def setUp (self ):
73229 app_settings .MAPBOX_ACCESS_TOKEN = None
74230 app_settings .MAPBOX_LANGUAGE = "en"
75231
76- def test_geocoder_field_contains_constuct (self ):
232+ def test_geocoder_field_contains_construct (self ):
77233 widget = GeocoderField ()
78234 html = widget .render (
79235 "field" ,
0 commit comments