Esempio n. 1
0
        public void CheckBoxForWithInvalidBooleanThrows()
        {
            // Arrange
            var expected = "String was not recognized as a valid Boolean.";
            var helper   = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetTestModelViewData());

            // Act & Assert
            // "Property2" in ViewData isn't a valid boolean
            var ex = Assert.Throws <FormatException>(() => helper.CheckBoxFor(m => m.Property2));

            Assert.Equal(expected, ex.Message);
        }
Esempio n. 2
0
        public void CheckBoxWithInvalidBooleanThrows()
        {
            // Arrange
            var expected = "String was not recognized as a valid Boolean.";
            var helper   = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetTestModelViewData());

            // Act & Assert
            var ex = Assert.Throws <FormatException>(
                () => helper.CheckBox("Property2", isChecked: null, htmlAttributes: null));

            Assert.Equal(expected, ex.Message);
        }
Esempio n. 3
0
        public void CheckBoxCheckedWithOnlyName()
        {
            // Arrange
            var expected = @"<input checked=""checked"" id=""Property1"" name=""Property1"" type=""checkbox"" " +
                           @"value=""true"" /><input name=""Property1"" type=""hidden"" value=""false"" />";
            var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetTestModelViewData());

            // Act
            var html = helper.CheckBox("Property1", isChecked: true, htmlAttributes: null);

            // Assert
            Assert.Equal(expected, html.ToString());
        }
Esempio n. 4
0
        public void CheckBoxForOverridesCalculatedParametersWithValuesFromHtmlAttributes()
        {
            // Arrange
            var expected = @"<input checked=""checked"" id=""Property3"" name=""Property3"" type=""checkbox"" " +
                           @"value=""false"" /><input name=""Property3"" type=""hidden"" value=""false"" />";
            var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetTestModelViewData());

            // Act
            var html = helper.CheckBoxFor(m => m.Property3, new { @checked = "checked", value = "false" });

            // Assert
            Assert.Equal(expected, html.ToString());
        }
Esempio n. 5
0
        public void CheckboxForWithComplexExpressionsUsesValuesFromViewDataDictionary()
        {
            // Arrange
            var expected = @"<input checked=""checked"" id=""ComplexProperty_Property1"" name=""ComplexProperty." +
                           @"Property1"" type=""checkbox"" value=""true"" /><input name=""ComplexProperty.Property1"" " +
                           @"type=""hidden"" value=""false"" />";
            var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetModelWithValidationViewData());

            // Act
            var html = helper.CheckBoxFor(m => m.ComplexProperty.Property1, htmlAttributes: null);

            // Assert
            Assert.Equal(expected, html.ToString());
        }
Esempio n. 6
0
        public void CheckBoxGeneratesUnobtrusiveValidationAttributes()
        {
            // Arrange
            var expected = @"<input data-val=""true"" data-val-required=""The Name field is required."" id=""Name""" +
                           @" name=""Name"" type=""checkbox"" value=""true"" />" +
                           @"<input name=""Name"" type=""hidden"" value=""false"" />";
            var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetModelWithValidationViewData());

            // Act
            var html = helper.CheckBox("Name", isChecked: null, htmlAttributes: null);

            // Assert
            Assert.Equal(expected, html.ToString());
        }
Esempio n. 7
0
        public void CheckBoxExplicitParametersOverrideDictionary_ForValueInModel()
        {
            // Arrange
            var expected = @"<input checked=""checked"" id=""Property3"" name=""Property3"" type=""checkbox"" " +
                           @"value=""false"" /><input name=""Property3"" type=""hidden"" value=""false"" />";
            var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetTestModelViewData());

            // Act
            var html = helper.CheckBox("Property3",
                                       isChecked: true,
                                       htmlAttributes: new { @checked = "unchecked", value = "false" });

            // Assert
            Assert.Equal(expected, html.ToString());
        }
Esempio n. 8
0
        public void CheckBoxForWithObjectAttributeWithUnderscores()
        {
            // Arrange
            var expected = @"<input Property1-Property3=""Property3ObjValue"" checked=""checked"" id=""Property1"" " +
                           @"name=""Property1"" type=""checkbox"" value=""true"" />" +
                           @"<input name=""Property1"" type=""hidden"" value=""false"" />";
            var helper         = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetTestModelViewData());
            var htmlAttributes = new { Property1_Property3 = "Property3ObjValue" };

            // Act
            var html = helper.CheckBoxFor(m => m.Property1, htmlAttributes);

            // Assert
            Assert.Equal(expected, html.ToString());
        }
Esempio n. 9
0
        public void CheckBoxUsesAttemptedValueFromModelState()
        {
            // Arrange
            var expected = @"<input id=""Property1"" name=""Property1"" type=""checkbox"" value=""true"" />" +
                           @"<input name=""Property1"" type=""hidden"" value=""false"" />";
            var valueProviderResult = new ValueProviderResult("false", "false", CultureInfo.InvariantCulture);
            var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetTestModelViewData());

            helper.ViewData.ModelState.SetModelValue("Property1", valueProviderResult);

            // Act
            var html = helper.CheckBox("Property1", isChecked: null, htmlAttributes: null);

            // Assert
            Assert.Equal(expected, html.ToString());
        }
Esempio n. 10
0
        public void CheckBoxForWithAttributeDictionary()
        {
            // Arrange
            var expected = @"<input Property3=""Property3Value"" checked=""checked"" id=""Property1"" " +
                           @"name=""Property1"" type=""checkbox"" value=""true"" /><input name=""Property1"" " +
                           @"type=""hidden"" value=""false"" />";
            var helper     = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetTestModelViewData());
            var attributes = new Dictionary <string, object> {
                { "Property3", "Property3Value" }
            };

            // Act
            var html = helper.CheckBoxFor(m => m.Property1, attributes);

            // Assert
            Assert.Equal(expected, html.ToString());
        }
Esempio n. 11
0
        public void CheckBoxForGeneratesUnobtrusiveValidationAttributes()
        {
            // Arrange
            var expected = @"<input data-val=""true"" data-val-required=""The Name field is required."" id=""Name""" +
                           @" name=""Name"" type=""checkbox"" value=""true"" />" +
                           @"<input name=""Name"" type=""hidden"" value=""false"" />";
            var metadataProvider   = new DataAnnotationsModelMetadataProvider();
            var viewDataDictionary = new ViewDataDictionary <ModelWithValidation>(metadataProvider)
            {
                Model = new ModelWithValidation()
            };
            var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(viewDataDictionary);

            // Act
            var html = helper.CheckBoxFor(m => m.Name, htmlAttributes: null);

            // Assert
            Assert.Equal(expected, html.ToString());
        }