def test_DictCharWidget_renders_with_initial_when_no_value(self): """Widgets should use initial value if rendered without value.""" names = [factory.make_name()] initials = [factory.make_name()] labels = [factory.make_name()] mock_widget = Mock() mock_widget.configure_mock(**{"render.return_value": ""}) widget = DictCharWidget([mock_widget, widgets.TextInput], names, initials, labels, skip_check=True) widget.render('foo', []) self.assertThat( mock_widget.render, MockCalledOnceWith(ANY, initials[0], ANY))
def test_DictCharWidget_renders_fieldset_with_label_and_field_names(self): names = [factory.make_string(), factory.make_string()] initials = [] labels = [factory.make_string(), factory.make_string()] values = [factory.make_string(), factory.make_string()] widget = DictCharWidget( [widgets.TextInput, widgets.TextInput, widgets.CheckboxInput], names, initials, labels, skip_check=True, ) name = factory.make_string() html_widget = fromstring("<root>" + widget.render(name, values) + "</root>") widget_names = XPath("fieldset/input/@name")(html_widget) widget_labels = XPath("fieldset/label/text()")(html_widget) widget_values = XPath("fieldset/input/@value")(html_widget) expected_names = [ "%s_%s" % (name, widget_name) for widget_name in names ] self.assertEqual( [expected_names, labels, values], [widget_names, widget_labels, widget_values], )
def test_DictCharWidget_renders_with_empty_string_as_input_data(self): names = [factory.make_string(), factory.make_string()] initials = [] labels = [factory.make_string(), factory.make_string()] widget = DictCharWidget( [widgets.TextInput, widgets.TextInput, widgets.CheckboxInput], names, initials, labels, skip_check=True) name = factory.make_string() html_widget = fromstring( '<root>' + widget.render(name, '') + '</root>') widget_names = XPath('fieldset/input/@name')(html_widget) widget_labels = XPath('fieldset/label/text()')(html_widget) expected_names = [ "%s_%s" % (name, widget_name) for widget_name in names] self.assertEqual( [expected_names, labels], [widget_names, widget_labels])
def test_DictCharWidget_renders_with_empty_string_as_input_data(self): names = [factory.getRandomString(), factory.getRandomString()] labels = [factory.getRandomString(), factory.getRandomString()] widget = DictCharWidget( [widgets.TextInput, widgets.TextInput, widgets.CheckboxInput], names, labels, skip_check=True) name = factory.getRandomString() self.assertEqual( '<fieldset>' '<label>%s</label>' '<input type="text" name="%s" />' '<label>%s</label>' '<input type="text" name="%s" />' '</fieldset>' % ( labels[0], '%s_%s' % (name, names[0]), labels[1], '%s_%s' % (name, names[1]), ), widget.render(name, ''))
def test_DictCharWidget_renders_fieldset_with_label_and_field_names(self): names = [factory.getRandomString(), factory.getRandomString()] labels = [factory.getRandomString(), factory.getRandomString()] values = [factory.getRandomString(), factory.getRandomString()] widget = DictCharWidget( [widgets.TextInput, widgets.TextInput, widgets.CheckboxInput], names, labels, skip_check=True) name = factory.getRandomString() self.assertEqual( '<fieldset>' '<label>%s</label>' '<input type="text" name="%s" value="%s" />' '<label>%s</label>' '<input type="text" name="%s" value="%s" />' '</fieldset>' % ( labels[0], '%s_%s' % (name, names[0]), values[0], labels[1], '%s_%s' % (name, names[1]), values[1], ), widget.render(name, values))
def test_DictCharWidget_renders_fieldset_with_label_and_field_names(self): names = [factory.getRandomString(), factory.getRandomString()] labels = [factory.getRandomString(), factory.getRandomString()] values = [factory.getRandomString(), factory.getRandomString()] widget = DictCharWidget( [widgets.TextInput, widgets.TextInput, widgets.CheckboxInput], names, labels, skip_check=True) name = factory.getRandomString() html_widget = fromstring('<root>' + widget.render(name, values) + '</root>') widget_names = XPath('fieldset/input/@name')(html_widget) widget_labels = XPath('fieldset/label/text()')(html_widget) widget_values = XPath('fieldset/input/@value')(html_widget) expected_names = [ "%s_%s" % (name, widget_name) for widget_name in names ] self.assertEqual([expected_names, labels, values], [widget_names, widget_labels, widget_values])
def test_empty_DictCharWidget_renders_as_empty_string(self): widget = DictCharWidget( [widgets.CheckboxInput], [], [], skip_check=True) self.assertEqual('', widget.render(factory.getRandomString(), ''))
def test_empty_DictCharWidget_renders_as_empty_string(self): widget = DictCharWidget([widgets.CheckboxInput], [], [], [], skip_check=True) self.assertEqual("", widget.render(factory.make_string(), ""))
def test_empty_DictCharWidget_renders_as_empty_string(self): widget = DictCharWidget([widgets.CheckboxInput], [], [], skip_check=True) self.assertEqual('', widget.render(factory.getRandomString(), ''))