public void HiddenWithModelValueOverwritesAttributeValue() { // Arrange var model = new ModelStateDictionary(); model.SetModelValue("foo", "fooValue"); HtmlHelper helper = HtmlHelperFactory.Create(model); // Act var html = helper.Hidden("foo", null, new { value = "barValue" }); // Assert Assert.Equal( @"<input id=""foo"" name=""foo"" type=""hidden"" value=""fooValue"" />", html.ToHtmlString() ); }
public void TextBoxWithImplicitValueAndAttributesObject() { // Arrange var modelState = new ModelStateDictionary(); modelState.SetModelValue("foo", "fooModelValue"); HtmlHelper helper = HtmlHelperFactory.Create(modelState); // Act var html = helper.TextBox("foo", null, _attributesObject); // Assert Assert.Equal( @"<input baz=""BazValue"" id=""foo"" name=""foo"" type=""text"" value=""fooModelValue"" />", html.ToHtmlString() ); }
public void ListBoxWithNonStringExplicitValue() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.ListBox("foo", null, GetSelectList(), new List <int>(), new { attr = "attr-val" }); // Assert Assert.Equal( "<select attr=\"attr-val\" id=\"foo\" name=\"foo\">" + Environment.NewLine + "<option value=\"A\">Alpha</option>" + Environment.NewLine + "<option value=\"B\">Bravo</option>" + Environment.NewLine + "<option value=\"C\">Charlie</option>" + Environment.NewLine + "</select>", html.ToHtmlString()); }
public void HiddenWithImplicitValueAndAttributesObject() { // Arrange var model = new ModelStateDictionary(); model.SetModelValue("foo", "bar"); HtmlHelper helper = HtmlHelperFactory.Create(model); // Act var html = helper.Hidden("foo", null, new { attr = "attr-val" }); // Assert Assert.Equal( @"<input attr=""attr-val"" id=""foo"" name=""foo"" type=""hidden"" value=""bar"" />", html.ToHtmlString() ); }
public void ListBoxWithMultiSelectAndMultipleExplicitValues() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.ListBox("foo", GetSelectList(), new[] { "A", "C" }, 4, true); // Assert Assert.Equal( "<select id=\"foo\" multiple=\"multiple\" name=\"foo\" size=\"4\">" + Environment.NewLine + "<option selected=\"selected\" value=\"A\">Alpha</option>" + Environment.NewLine + "<option value=\"B\">Bravo</option>" + Environment.NewLine + "<option selected=\"selected\" value=\"C\">Charlie</option>" + Environment.NewLine + "</select>", html.ToHtmlString()); }
public void DropDownListWithAttributes() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.DropDownList("foo", GetSelectList(), new { attr = "attr-val", attr2 = "attr-val2" }); // Assert Assert.Equal( "<select attr=\"attr-val\" attr2=\"attr-val2\" id=\"foo\" name=\"foo\">" + Environment.NewLine + "<option value=\"A\">Alpha</option>" + Environment.NewLine + "<option value=\"B\">Bravo</option>" + Environment.NewLine + "<option value=\"C\">Charlie</option>" + Environment.NewLine + "</select>", html.ToHtmlString()); }
public void ValidationMessageAllowsEmptyModelName() { // Arrange ModelStateDictionary dictionary = new ModelStateDictionary(); dictionary.AddError("test", "some error text"); HtmlHelper htmlHelper = HtmlHelperFactory.Create(dictionary); // Act var html = htmlHelper.ValidationMessage("test"); // Assert Assert.Equal( "<span class=\"field-validation-error\" data-valmsg-for=\"test\" data-valmsg-replace=\"true\">some error text</span>", html.ToHtmlString() ); }
public void ListBoxWithNoSelectedItem() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.ListBox("foo", GetSelectList()); // Assert Assert.Equal( "<select id=\"foo\" name=\"foo\">" + Environment.NewLine + "<option value=\"A\">Alpha</option>" + Environment.NewLine + "<option value=\"B\">Bravo</option>" + Environment.NewLine + "<option value=\"C\">Charlie</option>" + Environment.NewLine + "</select>", html.ToHtmlString()); }
public void ListBoxWithMultiSelectAndMultipleExplicitValues() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.ListBox("foo", GetSelectList(), new[] { "A", "C" }, 4, true); // Assert Assert.Equal( @"<select id=""foo"" multiple=""multiple"" name=""foo"" size=""4""> <option selected=""selected"" value=""A"">Alpha</option> <option value=""B"">Bravo</option> <option selected=""selected"" value=""C"">Charlie</option> </select>", html.ToHtmlString()); }
public void ListBoxWithAttributes() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.ListBox("foo", GetSelectList(), new { attr = "attr-val", attr2 = "attr-val2" }); // Assert Assert.Equal( @"<select attr=""attr-val"" attr2=""attr-val2"" id=""foo"" name=""foo""> <option value=""A"">Alpha</option> <option value=""B"">Bravo</option> <option value=""C"">Charlie</option> </select>", html.ToHtmlString()); }
public void ListBoxWithNoSelectedItem() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.ListBox("foo", GetSelectList()); // Assert Assert.Equal( @"<select id=""foo"" name=""foo""> <option value=""A"">Alpha</option> <option value=""B"">Bravo</option> <option value=""C"">Charlie</option> </select>", html.ToHtmlString()); }
public void RadioButtonWithNonStringValue() { // Arrange var modelState = new ModelStateDictionary(); modelState.SetModelValue("foo", "bar"); HtmlHelper helper = HtmlHelperFactory.Create(modelState); // Act var html = helper.RadioButton("foo", 2.53); // Assert Assert.Equal( @"<input id=""foo"" name=""foo"" type=""radio"" value=""2.53"" />", html.ToHtmlString() ); }
public void RadioButtonUsesModelStateToAssignChecked() { // Arrange var modelState = new ModelStateDictionary(); modelState.SetModelValue("foo", "bar"); HtmlHelper helper = HtmlHelperFactory.Create(modelState); // Act var html = helper.RadioButton("foo", "bar"); // Assert Assert.Equal( @"<input checked=""checked"" id=""foo"" name=""foo"" type=""radio"" value=""bar"" />", html.ToHtmlString() ); }
public void ValidationSummary() { // Arrange HtmlHelper htmlHelper = HtmlHelperFactory.Create(GetModelStateWithErrors()); // Act var html = htmlHelper.ValidationSummary(); // Assert Assert.Equal(@"<div class=""validation-summary-errors"" data-valmsg-summary=""true""><ul> <li>foo error <1></li> <li>foo error <2></li> <li>bar error <1></li> <li>bar error <2></li> </ul></div>" , html.ToHtmlString()); }
public void DropDownWithNonStringExplicitValue() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.DropDownList("foo", null, GetSelectList(), new List <int>(), new { attr = "attr-val" }); // Assert Assert.Equal( @"<select attr=""attr-val"" id=""foo"" name=""foo""> <option value=""A"">Alpha</option> <option value=""B"">Bravo</option> <option value=""C"">Charlie</option> </select>", html.ToHtmlString()); }
/// <summary> /// Will invoke a helper with overload that accepts custom attribute with a name containing /// and underscore as an anonymous object, and will then assert that the resulted html /// has the attribute name underscore correctly transformed to a dash /// </summary> /// <param name="helperInvocation"></param> internal static void AssertHelperTransformsAttributesUnderscoresToDashs( Func <HtmlHelper, object, IHtmlString> helperInvocation ) { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); const string expected = @"data-name=""value"""; const string unexpected = @"data_name=""value"""; var attributes = new { data_name = "value" }; // Act var htmlString = helperInvocation(helper, attributes).ToHtmlString(); // Assert Assert.DoesNotContain(unexpected, htmlString); Assert.Contains(expected, htmlString); }
public void ValidationSummaryWithObjectProperties() { // Arrange HtmlHelper htmlHelper = HtmlHelperFactory.Create(GetModelStateWithErrors()); // Act var html = htmlHelper.ValidationSummary(new { attr = "attr-value", @class = "my-class" }); // Assert Assert.Equal(@"<div attr=""attr-value"" class=""validation-summary-errors my-class"" data-valmsg-summary=""true""><ul> <li>foo error <1></li> <li>foo error <2></li> <li>bar error <1></li> <li>bar error <2></li> </ul></div>" , html.ToHtmlString()); }
public void RadioButtonWithDefaultArguments() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.RadioButton("foo", "bar", true); // Assert Assert.Equal(@"<input checked=""checked"" id=""foo"" name=""foo"" type=""radio"" value=""bar"" />", html.ToHtmlString()); html = helper.RadioButton("foo", "bar", false); // Assert Assert.Equal(@"<input id=""foo"" name=""foo"" type=""radio"" value=""bar"" />", html.ToHtmlString()); }
public void TextAreaWithDictionaryAttributes() { // Arrange ModelStateDictionary modelState = new ModelStateDictionary(); modelState.SetModelValue("foo", "explicit-foo-value"); HtmlHelper helper = HtmlHelperFactory.Create(modelState); var attributes = new Dictionary <string, object>() { { "attr", "attr-val" }, { "rows", 15 }, { "cols", 12 } }; // Act var html = helper.TextArea("foo", attributes); // Assert Assert.Equal(@"<textarea attr=""attr-val"" cols=""12"" id=""foo"" name=""foo"" rows=""15"">explicit-foo-value</textarea>", html.ToHtmlString()); }
public void ValidationSummaryWithAttributesFromAnonymousObject_WithUnderscoreInName_TransformsUnderscoresToDashs() { // Arrange var modelState = new ModelStateDictionary(); modelState.AddFormError("error"); HtmlHelper htmlHelper = HtmlHelperFactory.Create(modelState); HtmlHelperTest.AssertHelperTransformsAttributesUnderscoresToDashs((helper, attributes) => htmlHelper.ValidationSummary(attributes)); HtmlHelperTest.AssertHelperTransformsAttributesUnderscoresToDashs((helper, attributes) => htmlHelper.ValidationSummary("foo", attributes)); HtmlHelperTest.AssertHelperTransformsAttributesUnderscoresToDashs((helper, attributes) => htmlHelper.ValidationSummary("foo", true, attributes)); }
public void ValidationSummary() { // Arrange HtmlHelper htmlHelper = HtmlHelperFactory.Create(GetModelStateWithErrors()); // Act var html = htmlHelper.ValidationSummary(); // Assert Assert.Equal( "<div class=\"validation-summary-errors\" data-valmsg-summary=\"true\"><ul>" + Environment.NewLine + "<li>foo error <1></li>" + Environment.NewLine + "<li>foo error <2></li>" + Environment.NewLine + "<li>bar error <1></li>" + Environment.NewLine + "<li>bar error <2></li>" + Environment.NewLine + "</ul></div>", html.ToHtmlString()); }
public void ListBoxWithDotReplacementForId() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.ListBox("foo.bar", "Select One", GetSelectList()); // Assert Assert.Equal( @"<select id=""foo_bar"" name=""foo.bar""> <option value="""">Select One</option> <option value=""A"">Alpha</option> <option value=""B"">Bravo</option> <option value=""C"">Charlie</option> </select>", html.ToHtmlString()); }
public void ListBoxWithObjectDictionaryAndTitle() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.ListBox("foo", "Select One", GetSelectList(), new { @class = "my-class" }); // Assert Assert.Equal( @"<select class=""my-class"" id=""foo"" name=""foo""> <option value="""">Select One</option> <option value=""A"">Alpha</option> <option value=""B"">Bravo</option> <option value=""C"">Charlie</option> </select>", html.ToHtmlString()); }
public void DropDownListWithDefaultOption() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.DropDownList("foo", "select-one", GetSelectList()); // Assert Assert.Equal( @"<select id=""foo"" name=""foo""> <option value="""">select-one</option> <option value=""A"">Alpha</option> <option value=""B"">Bravo</option> <option value=""C"">Charlie</option> </select>", html.ToHtmlString()); }
public void ListBoxWithDotReplacementForId() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.ListBox("foo.bar", "Select One", GetSelectList()); // Assert Assert.Equal( "<select id=\"foo_bar\" name=\"foo.bar\">" + Environment.NewLine + "<option value=\"\">Select One</option>" + Environment.NewLine + "<option value=\"A\">Alpha</option>" + Environment.NewLine + "<option value=\"B\">Bravo</option>" + Environment.NewLine + "<option value=\"C\">Charlie</option>" + Environment.NewLine + "</select>", html.ToHtmlString()); }
public void ListBoxWithObjectDictionaryAndTitle() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.ListBox("foo", "Select One", GetSelectList(), new { @class = "my-class" }); // Assert Assert.Equal( "<select class=\"my-class\" id=\"foo\" name=\"foo\">" + Environment.NewLine + "<option value=\"\">Select One</option>" + Environment.NewLine + "<option value=\"A\">Alpha</option>" + Environment.NewLine + "<option value=\"B\">Bravo</option>" + Environment.NewLine + "<option value=\"C\">Charlie</option>" + Environment.NewLine + "</select>", html.ToHtmlString()); }
public void DropDownListWithDefaultOption() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.DropDownList("foo", "select-one", GetSelectList()); // Assert Assert.Equal( "<select id=\"foo\" name=\"foo\">" + Environment.NewLine + "<option value=\"\">select-one</option>" + Environment.NewLine + "<option value=\"A\">Alpha</option>" + Environment.NewLine + "<option value=\"B\">Bravo</option>" + Environment.NewLine + "<option value=\"C\">Charlie</option>" + Environment.NewLine + "</select>", html.ToHtmlString()); }
public void CheckBoxUsesCustomErrorClass() { // Arrange var modelState = new ModelStateDictionary(); modelState.AddError("foo", "error"); HtmlHelper.ValidationInputCssClassName = "my-error-class"; HtmlHelper helper = HtmlHelperFactory.Create(modelState); // Act var html = helper.CheckBox("foo", true, new { @class = "my-class" }); // Assert Assert.Equal( @"<input checked=""checked"" class=""my-error-class my-class"" id=""foo"" name=""foo"" type=""checkbox"" />", html.ToHtmlString() ); }
public void TextAreaWithCustomErrorClass() { // Arrange ModelStateDictionary modelState = new ModelStateDictionary(); modelState.AddError("foo", "some error"); HtmlHelper.ValidationInputCssClassName = "custom-input-validation-error"; HtmlHelper helper = HtmlHelperFactory.Create(modelState); // Act var html = helper.TextArea("foo", String.Empty, new { @class = "my-css" }); // Assert Assert.Equal( @"<textarea class=""custom-input-validation-error my-css"" cols=""20"" id=""foo"" name=""foo"" rows=""2""></textarea>", html.ToHtmlString() ); }
public void RadioButtonOverwritesImplicitAttributes() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.RadioButton( "foo", "foo-value", new { value = "bazValue", type = "fooType", name = "bar" } ); // Assert Assert.Equal( @"<input id=""foo"" name=""foo"" type=""fooType"" value=""foo-value"" />", html.ToHtmlString() ); }