コード例 #1
0
        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()
                );
        }
コード例 #2
0
        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()
                );
        }
コード例 #3
0
        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());
        }
コード例 #4
0
        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()
                );
        }
コード例 #5
0
        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());
        }
コード例 #6
0
        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());
        }
コード例 #7
0
        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()
                );
        }
コード例 #8
0
        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());
        }
コード例 #9
0
        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());
        }
コード例 #10
0
        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());
        }
コード例 #11
0
        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());
        }
コード例 #12
0
        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()
                );
        }
コード例 #13
0
        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()
                );
        }
コード例 #14
0
        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 &lt;1&gt;</li>
<li>foo error &lt;2&gt;</li>
<li>bar error &lt;1&gt;</li>
<li>bar error &lt;2&gt;</li>
</ul></div>"
                         , html.ToHtmlString());
        }
コード例 #15
0
        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());
        }
コード例 #16
0
        /// <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);
        }
コード例 #17
0
        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 &lt;1&gt;</li>
<li>foo error &lt;2&gt;</li>
<li>bar error &lt;1&gt;</li>
<li>bar error &lt;2&gt;</li>
</ul></div>"
                         , html.ToHtmlString());
        }
コード例 #18
0
        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());
        }
コード例 #19
0
        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());
        }
コード例 #20
0
        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));
        }
コード例 #21
0
        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 &lt;1&gt;</li>" + Environment.NewLine
                + "<li>foo error &lt;2&gt;</li>" + Environment.NewLine
                + "<li>bar error &lt;1&gt;</li>" + Environment.NewLine
                + "<li>bar error &lt;2&gt;</li>" + Environment.NewLine
                + "</ul></div>",
                html.ToHtmlString());
        }
コード例 #22
0
        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());
        }
コード例 #23
0
        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());
        }
コード例 #24
0
        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());
        }
コード例 #25
0
        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());
        }
コード例 #26
0
        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());
        }
コード例 #27
0
        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());
        }
コード例 #28
0
        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()
                );
        }
コード例 #29
0
        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()
                );
        }
コード例 #30
0
        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()
                );
        }