| 1 |
from django.forms import Form, MultipleChoiceField, MultipleHiddenInput |
| 2 |
from django.utils.datastructures import MultiValueDict |
| 3 |
|
| 4 |
from .base import WidgetTest |
| 5 |
|
| 6 |
|
| 7 |
class MultipleHiddenInputTest(WidgetTest): |
| 8 |
widget = MultipleHiddenInput() |
| 9 |
|
| 10 |
def test_render_single(self): |
| 11 |
self.check_html( |
| 12 |
self.widget, |
| 13 |
"email", |
| 14 |
["[email protected]"], |
| 15 |
html='<input type="hidden" name="email" value="[email protected]">', |
| 16 |
) |
| 17 |
|
| 18 |
def test_render_multiple(self): |
| 19 |
self.check_html( |
| 20 |
self.widget, |
| 21 |
"email", |
| 22 |
["[email protected]", "[email protected]"], |
| 23 |
html=( |
| 24 |
'<input type="hidden" name="email" value="[email protected]">\n' |
| 25 |
'<input type="hidden" name="email" value="[email protected]">' |
| 26 |
), |
| 27 |
) |
| 28 |
|
| 29 |
def test_render_attrs(self): |
| 30 |
self.check_html( |
| 31 |
self.widget, |
| 32 |
"email", |
| 33 |
["[email protected]"], |
| 34 |
attrs={"class": "fun"}, |
| 35 |
html=( |
| 36 |
'<input type="hidden" name="email" value="[email protected]" ' |
| 37 |
'class="fun">' |
| 38 |
), |
| 39 |
) |
| 40 |
|
| 41 |
def test_render_attrs_multiple(self): |
| 42 |
self.check_html( |
| 43 |
self.widget, |
| 44 |
"email", |
| 45 |
["[email protected]", "[email protected]"], |
| 46 |
attrs={"class": "fun"}, |
| 47 |
html=( |
| 48 |
'<input type="hidden" name="email" value="[email protected]" ' |
| 49 |
'class="fun">\n' |
| 50 |
'<input type="hidden" name="email" value="[email protected]" class="fun">' |
| 51 |
), |
| 52 |
) |
| 53 |
|
| 54 |
def test_render_attrs_constructor(self): |
| 55 |
widget = MultipleHiddenInput(attrs={"class": "fun"}) |
| 56 |
self.check_html(widget, "email", [], "") |
| 57 |
self.check_html( |
| 58 |
widget, |
| 59 |
"email", |
| 60 |
["[email protected]"], |
| 61 |
html=( |
| 62 |
'<input type="hidden" class="fun" value="[email protected]" name="email">' |
| 63 |
), |
| 64 |
) |
| 65 |
self.check_html( |
| 66 |
widget, |
| 67 |
"email", |
| 68 |
["[email protected]", "[email protected]"], |
| 69 |
html=( |
| 70 |
'<input type="hidden" class="fun" value="[email protected]" ' |
| 71 |
'name="email">\n' |
| 72 |
'<input type="hidden" class="fun" value="[email protected]" ' |
| 73 |
'name="email">' |
| 74 |
), |
| 75 |
) |
| 76 |
self.check_html( |
| 77 |
widget, |
| 78 |
"email", |
| 79 |
["[email protected]"], |
| 80 |
attrs={"class": "special"}, |
| 81 |
html=( |
| 82 |
'<input type="hidden" class="special" value="[email protected]" ' |
| 83 |
'name="email">' |
| 84 |
), |
| 85 |
) |
| 86 |
|
| 87 |
def test_render_empty(self): |
| 88 |
self.check_html(self.widget, "email", [], "") |
| 89 |
|
| 90 |
def test_render_none(self): |
| 91 |
self.check_html(self.widget, "email", None, "") |
| 92 |
|
| 93 |
def test_render_increment_id(self): |
| 94 |
""" |
| 95 |
Each input should get a separate ID. |
| 96 |
""" |
| 97 |
self.check_html( |
| 98 |
self.widget, |
| 99 |
"letters", |
| 100 |
["a", "b", "c"], |
| 101 |
attrs={"id": "hideme"}, |
| 102 |
html=( |
| 103 |
'<input type="hidden" name="letters" value="a" id="hideme_0">\n' |
| 104 |
'<input type="hidden" name="letters" value="b" id="hideme_1">\n' |
| 105 |
'<input type="hidden" name="letters" value="c" id="hideme_2">' |
| 106 |
), |
| 107 |
) |
| 108 |
|
| 109 |
def test_fieldset(self): |
| 110 |
class TestForm(Form): |
| 111 |
template_name = "forms_tests/use_fieldset.html" |
| 112 |
composers = MultipleChoiceField( |
| 113 |
choices=[("J", "John Lennon"), ("P", "Paul McCartney")], |
| 114 |
widget=MultipleHiddenInput, |
| 115 |
) |
| 116 |
|
| 117 |
form = TestForm(MultiValueDict({"composers": ["J", "P"]})) |
| 118 |
self.assertIs(self.widget.use_fieldset, False) |
| 119 |
self.assertHTMLEqual( |
| 120 |
'<input type="hidden" name="composers" value="J" id="id_composers_0">' |
| 121 |
'<input type="hidden" name="composers" value="P" id="id_composers_1">', |
| 122 |
form.render(), |
| 123 |
) |