11from django .test import TestCase
22from django .forms import Form
33
4- from ...choice_field import ChoiceField
4+ from ...choice_field import ChoiceField , MultipleChoiceField
55
66class TestForm (Form ):
77 field = ChoiceField (
@@ -11,6 +11,13 @@ class TestForm(Form):
1111 hint = "Pick either one" ,
1212 )
1313
14+ class TestMultipleChoiceForm (Form ):
15+ field = MultipleChoiceField (
16+ label = "Select options" ,
17+ choices = (("a" , "Option A" ), ("b" , "Option B" ), ("c" , "Option C" )),
18+ hint = "Select all that apply" ,
19+ )
20+
1421class TestChoiceField (TestCase ):
1522 def test_renders_nhs_radios (self ):
1623 self .assertHTMLEqual (
@@ -38,3 +45,72 @@ def test_renders_nhs_radios(self):
3845 </div>
3946 """ ,
4047 )
48+
49+ def test_checkbox_field_with_choice_hints (self ):
50+ """Test that choice hints are rendered correctly for checkbox fields"""
51+ form = TestMultipleChoiceForm ()
52+ bound_field = form ["field" ]
53+
54+ # Add hints for specific choices
55+ bound_field .add_hint_for_choice ("a" , "This is hint for option A" )
56+ bound_field .add_hint_for_choice ("b" , "This is hint for option B" )
57+
58+ rendered_html = bound_field .as_field_group ()
59+
60+ # Verify the hints are rendered
61+ self .assertIn ('This is hint for option A' , rendered_html )
62+ self .assertIn ('This is hint for option B' , rendered_html )
63+ self .assertIn ('aria-describedby="id_field_0-item-hint"' , rendered_html )
64+ self .assertIn ('aria-describedby="id_field_1-item-hint"' , rendered_html )
65+
66+ def test_get_hint_for_choice_returns_correct_hint (self ):
67+ """Test that get_hint_for_choice returns the correct hint text"""
68+ form = TestMultipleChoiceForm ()
69+ bound_field = form ["field" ]
70+
71+ # Add a hint
72+ bound_field .add_hint_for_choice ("a" , "Hint for A" )
73+
74+ # Verify the hint can be retrieved
75+ self .assertEqual (bound_field .get_hint_for_choice ("a" ), "Hint for A" )
76+
77+ def test_get_hint_for_choice_returns_none_when_no_hint (self ):
78+ """Test that get_hint_for_choice returns None when no hint is set"""
79+ form = TestMultipleChoiceForm ()
80+ bound_field = form ["field" ]
81+
82+ # No hint added, should return None
83+ self .assertIsNone (bound_field .get_hint_for_choice ("a" ))
84+
85+ def test_checkbox_field_renders_without_hints_when_none_added (self ):
86+ """Test that checkbox field renders correctly when no hints are added"""
87+ form = TestMultipleChoiceForm ()
88+ rendered_html = form ["field" ].as_field_group ()
89+
90+ # Should render without hint elements
91+ self .assertNotIn ('nhsuk-checkboxes__hint' , rendered_html )
92+
93+ def test_radio_field_with_choice_hints (self ):
94+ """Test that choice hints are rendered correctly for radio fields"""
95+ form = TestForm ()
96+ bound_field = form ["field" ]
97+
98+ # Add hints for specific choices
99+ bound_field .add_hint_for_choice ("a" , "This is hint for option A" )
100+ bound_field .add_hint_for_choice ("b" , "This is hint for option B" )
101+
102+ rendered_html = bound_field .as_field_group ()
103+
104+ # Verify the hints are rendered
105+ self .assertIn ('This is hint for option A' , rendered_html )
106+ self .assertIn ('This is hint for option B' , rendered_html )
107+ self .assertIn ('aria-describedby="id_field-item-hint"' , rendered_html )
108+ self .assertIn ('aria-describedby="id_field-2-item-hint"' , rendered_html )
109+
110+ def test_radio_field_renders_without_hints_when_none_added (self ):
111+ """Test that radio field renders correctly when no hints are added"""
112+ form = TestForm ()
113+ rendered_html = form ["field" ].as_field_group ()
114+
115+ # Should render without hint elements for individual items
116+ self .assertNotIn ('nhsuk-radios__hint' , rendered_html )
0 commit comments