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 (
@@ -74,3 +81,72 @@ class TestForm(Form):
7481 </div>
7582 """ ,
7683 )
84+
85+ def test_checkbox_field_with_choice_hints (self ):
86+ """Test that choice hints are rendered correctly for checkbox fields"""
87+ form = TestMultipleChoiceForm ()
88+ bound_field = form ["field" ]
89+
90+ # Add hints for specific choices
91+ bound_field .add_hint_for_choice ("a" , "This is hint for option A" )
92+ bound_field .add_hint_for_choice ("b" , "This is hint for option B" )
93+
94+ rendered_html = bound_field .as_field_group ()
95+
96+ # Verify the hints are rendered
97+ self .assertIn ('This is hint for option A' , rendered_html )
98+ self .assertIn ('This is hint for option B' , rendered_html )
99+ self .assertIn ('aria-describedby="id_field_0-item-hint"' , rendered_html )
100+ self .assertIn ('aria-describedby="id_field_1-item-hint"' , rendered_html )
101+
102+ def test_get_hint_for_choice_returns_correct_hint (self ):
103+ """Test that get_hint_for_choice returns the correct hint text"""
104+ form = TestMultipleChoiceForm ()
105+ bound_field = form ["field" ]
106+
107+ # Add a hint
108+ bound_field .add_hint_for_choice ("a" , "Hint for A" )
109+
110+ # Verify the hint can be retrieved
111+ self .assertEqual (bound_field .get_hint_for_choice ("a" ), "Hint for A" )
112+
113+ def test_get_hint_for_choice_returns_none_when_no_hint (self ):
114+ """Test that get_hint_for_choice returns None when no hint is set"""
115+ form = TestMultipleChoiceForm ()
116+ bound_field = form ["field" ]
117+
118+ # No hint added, should return None
119+ self .assertIsNone (bound_field .get_hint_for_choice ("a" ))
120+
121+ def test_checkbox_field_renders_without_hints_when_none_added (self ):
122+ """Test that checkbox field renders correctly when no hints are added"""
123+ form = TestMultipleChoiceForm ()
124+ rendered_html = form ["field" ].as_field_group ()
125+
126+ # Should render without hint elements
127+ self .assertNotIn ('nhsuk-checkboxes__hint' , rendered_html )
128+
129+ def test_radio_field_with_choice_hints (self ):
130+ """Test that choice hints are rendered correctly for radio fields"""
131+ form = TestForm ()
132+ bound_field = form ["field" ]
133+
134+ # Add hints for specific choices
135+ bound_field .add_hint_for_choice ("a" , "This is hint for option A" )
136+ bound_field .add_hint_for_choice ("b" , "This is hint for option B" )
137+
138+ rendered_html = bound_field .as_field_group ()
139+
140+ # Verify the hints are rendered
141+ self .assertIn ('This is hint for option A' , rendered_html )
142+ self .assertIn ('This is hint for option B' , rendered_html )
143+ self .assertIn ('aria-describedby="id_field-item-hint"' , rendered_html )
144+ self .assertIn ('aria-describedby="id_field-2-item-hint"' , rendered_html )
145+
146+ def test_radio_field_renders_without_hints_when_none_added (self ):
147+ """Test that radio field renders correctly when no hints are added"""
148+ form = TestForm ()
149+ rendered_html = form ["field" ].as_field_group ()
150+
151+ # Should render without hint elements for individual items
152+ self .assertNotIn ('nhsuk-radios__hint' , rendered_html )
0 commit comments