|
1 | 1 | import pytest |
2 | 2 | from django.forms import Form |
3 | 3 | from django.forms.widgets import CheckboxSelectMultiple, Select |
| 4 | +from django.http import QueryDict |
4 | 5 | from pytest_django.asserts import assertHTMLEqual |
5 | 6 |
|
6 | 7 | from manage_breast_screening.nhsuk_forms.fields.choice_fields import ( |
@@ -197,6 +198,9 @@ class TestForm(Form): |
197 | 198 | hint="Pick any number", |
198 | 199 | widget=CheckboxSelectMultiple, |
199 | 200 | ) |
| 201 | + checkbox_field_with_exclusive_option = MultipleChoiceField( |
| 202 | + label="Abc", choices=(("a", "A"), ("b", "B")), exclusive_choices={"b"} |
| 203 | + ) |
200 | 204 | details = CharField(label="Abc", initial="") |
201 | 205 |
|
202 | 206 | return TestForm |
@@ -261,6 +265,44 @@ def test_renders_nhs_checkboxes_with_conditional_html(self, form_class): |
261 | 265 | """, |
262 | 266 | ) |
263 | 267 |
|
| 268 | + def test_renders_exclusive_options(self, form_class): |
| 269 | + form = form_class() |
| 270 | + |
| 271 | + assertHTMLEqual( |
| 272 | + form["checkbox_field_with_exclusive_option"].as_field_group(), |
| 273 | + """ |
| 274 | + <div class="nhsuk-form-group"> |
| 275 | + <fieldset class="nhsuk-fieldset"> |
| 276 | + <legend class="nhsuk-fieldset__legend nhsuk-fieldset__legend--m"> |
| 277 | + Abc |
| 278 | + </legend> |
| 279 | + <div class="nhsuk-checkboxes" data-module="nhsuk-checkboxes"> |
| 280 | + <div class="nhsuk-checkboxes__item"> |
| 281 | + <input class="nhsuk-checkboxes__input" id="id_checkbox_field_with_exclusive_option" name="checkbox_field_with_exclusive_option" type="checkbox" value="a"> |
| 282 | + <label class="nhsuk-label nhsuk-checkboxes__label" for="id_checkbox_field_with_exclusive_option">A</label> |
| 283 | + </div> |
| 284 | + <div class="nhsuk-checkboxes__item"> |
| 285 | + <input class="nhsuk-checkboxes__input" id="id_checkbox_field_with_exclusive_option-2" name="checkbox_field_with_exclusive_option" type="checkbox" value="b" data-checkbox-exclusive> |
| 286 | + <label class="nhsuk-label nhsuk-checkboxes__label" for="id_checkbox_field_with_exclusive_option-2">B</label> |
| 287 | + </div> |
| 288 | + </div> |
| 289 | + </fieldset> |
| 290 | + </div> |
| 291 | + """, |
| 292 | + ) |
| 293 | + |
| 294 | + def test_exclusive_options_are_validated(self, form_class): |
| 295 | + form = form_class( |
| 296 | + QueryDict( |
| 297 | + "checkbox_field=a&details=abc&checkbox_field_with_exclusive_option=a&checkbox_field_with_exclusive_option=b" |
| 298 | + ) |
| 299 | + ) |
| 300 | + assert form.errors == { |
| 301 | + "checkbox_field_with_exclusive_option": [ |
| 302 | + 'Unselect "B" in order to select other options' |
| 303 | + ] |
| 304 | + } |
| 305 | + |
264 | 306 | def test_renders_without_fieldset(self, form_class): |
265 | 307 | class TestForm(Form): |
266 | 308 | checkbox_field = MultipleChoiceField( |
|
0 commit comments