public void ReturnsCorrectly()
        {
            string value = "Value One";

            var viewModel = new CheckBoxListItemViewModel();

            var htmlHelper = HtmlHelperGenerator.CreateHtmlHelper( viewModel );

            var result = htmlHelper.CheckBoxListItemFor( x => x.SelectedCheckBoxValues, value );

            string expectedId = string.Format( "SelectedCheckBoxValues_{0}", value.Replace( ' ', '_' ) );
            string expectedResult = string.Format( "<input name=\"SelectedCheckBoxValues.CheckBoxList\" type=\"hidden\" value=\"\" /><input id=\"{0}\" name=\"SelectedCheckBoxValues\" type=\"checkbox\" value=\"{1}\" />", expectedId, value );
            Assert.AreEqual( expectedResult, result.ToHtmlString() );
        }
        public void WithHtmlAttributesDictionaryReturnsCorrectly()
        {
            string value = "Value One";
            string attributeName = "AttributeName";
            string attributeValue = "AttributeValue";

            var htmlAttributes = new Dictionary<string, object>();
            htmlAttributes.Add( attributeName, attributeValue );

            var viewModel = new CheckBoxListItemViewModel();

            HtmlHelper<CheckBoxListItemViewModel> htmlHelper = HtmlHelperGenerator.CreateHtmlHelper( viewModel );

            var result = htmlHelper.CheckBoxListItemFor( x => x.SelectedCheckBoxValues, value, htmlAttributes );

            string expectedId = string.Format( "SelectedCheckBoxValues_{0}", value.Replace( ' ', '_' ) );
            string expectedResult = string.Format( "<input name=\"SelectedCheckBoxValues.CheckBoxList\" type=\"hidden\" value=\"\" /><input {0}=\"{1}\" id=\"{2}\" name=\"SelectedCheckBoxValues\" type=\"checkbox\" value=\"{3}\" />", attributeName, attributeValue, expectedId, value );
            Assert.AreEqual( expectedResult, result.ToHtmlString() );
        }
        public void MultipleCallsOnlyRendersOneHiddenInputReturnsCorrectly()
        {
            string valueOne = "Value One";
            string valueTwo = "Value Two";

            var viewModel = new CheckBoxListItemViewModel();

            var htmlHelper = HtmlHelperGenerator.CreateHtmlHelper( viewModel );

            var firstResult = htmlHelper.CheckBoxListItemFor( x => x.SelectedCheckBoxValues, valueOne );
            var secondResult = htmlHelper.CheckBoxListItemFor( x => x.SelectedCheckBoxValues, valueTwo );

            string expectedFirstId = string.Format( "SelectedCheckBoxValues_{0}", valueOne.Replace( ' ', '_' ) );
            string expectedFirstResult = string.Format( "<input name=\"SelectedCheckBoxValues.CheckBoxList\" type=\"hidden\" value=\"\" /><input id=\"{0}\" name=\"SelectedCheckBoxValues\" type=\"checkbox\" value=\"{1}\" />", expectedFirstId, valueOne );
            Assert.AreEqual( expectedFirstResult, firstResult.ToHtmlString() );

            string expectedSecondId = string.Format( "SelectedCheckBoxValues_{0}", valueTwo.Replace( ' ', '_' ) );
            string expectedSecondResult = string.Format( "<input id=\"{0}\" name=\"SelectedCheckBoxValues\" type=\"checkbox\" value=\"{1}\" />", expectedSecondId, valueTwo );
            Assert.AreEqual( expectedSecondResult, secondResult.ToHtmlString() );
        }
        public void WithValueNullThrowsArgumentNullException()
        {
            string value = null;

            var viewModel = new CheckBoxListItemViewModel();

            var htmlHelper = HtmlHelperGenerator.CreateHtmlHelper( viewModel );

            htmlHelper.CheckBoxListItemFor( x => x.SelectedCheckBoxValues, value );
        }
        public void WithRequiredAnnotationAndUnobtrusiveValidationEnabledReturnsCorrectly()
        {
            string value = "Value One";

            var viewModel = new CheckBoxListItemViewModel();

            ModelState selectedCheckBoxValuesModelState = new ModelState()
            {
                Value = new ValueProviderResult( value, value, CultureInfo.InvariantCulture )
            };

            HtmlHelper<CheckBoxListItemViewModel> htmlHelper = HtmlHelperGenerator.CreateHtmlHelper( viewModel, true );

            var result = htmlHelper.CheckBoxListItemFor( x => x.RequiredSelectedCheckBoxValues, value );

            string expectedId = string.Format( "RequiredSelectedCheckBoxValues_{0}", value.Replace( ' ', '_' ) );
            string expectedResult = string.Format( "<input name=\"RequiredSelectedCheckBoxValues.CheckBoxList\" type=\"hidden\" value=\"\" /><input data-val=\"true\" data-val-required=\"The RequiredSelectedCheckBoxValues field is required.\" id=\"{0}\" name=\"RequiredSelectedCheckBoxValues\" type=\"checkbox\" value=\"{1}\" />", expectedId, value );
            Assert.AreEqual( expectedResult, result.ToHtmlString() );
        }
        public void WithModelStateValueDoesNotContainValueAndModelValueContainsValueReturnsCorrectly()
        {
            string value = "Value";

            var viewModel = new CheckBoxListItemViewModel();
            viewModel.SelectedCheckBoxValues = new string[] { value, "Other Value" };

            string[] modelStateValue = new string[] { "ThisValue", "OtherValue" };

            ModelState modelState = new ModelState();
            modelState.Value = new ValueProviderResult( modelStateValue, string.Join( ",", modelStateValue ), CultureInfo.InvariantCulture );

            var htmlHelper = HtmlHelperGenerator.CreateHtmlHelper<CheckBoxListItemViewModel>( viewModel );
            ( ( HtmlHelper )htmlHelper ).ViewData.ModelState.Add( "SelectedCheckBoxValues", modelState );

            var result = htmlHelper.CheckBoxListItemFor( x => x.SelectedCheckBoxValues, value );

            string expectedId = string.Format( "SelectedCheckBoxValues_{0}", value.Replace( ' ', '_' ) );
            string expectedResult = string.Format( "<input name=\"SelectedCheckBoxValues.CheckBoxList\" type=\"hidden\" value=\"\" /><input id=\"{0}\" name=\"SelectedCheckBoxValues\" type=\"checkbox\" value=\"{1}\" />", expectedId, value );
            Assert.AreEqual( expectedResult, result.ToHtmlString() );
        }
        public void WithModelStateErrorReturnsCorrectly()
        {
            string value = "Value One";

            var viewModel = new CheckBoxListItemViewModel();

            HtmlHelper<CheckBoxListItemViewModel> htmlHelper = HtmlHelperGenerator.CreateHtmlHelper( viewModel );
            ( ( HtmlHelper )htmlHelper ).ViewData.ModelState.AddModelError( "SelectedCheckBoxValues", "ErrorMessage" );

            var result = htmlHelper.CheckBoxListItemFor( x => x.SelectedCheckBoxValues, value );

            string expectedId = string.Format( "SelectedCheckBoxValues_{0}", value.Replace( ' ', '_' ) );
            string expectedResult = string.Format( "<input name=\"SelectedCheckBoxValues.CheckBoxList\" type=\"hidden\" value=\"\" /><input class=\"input-validation-error\" id=\"{0}\" name=\"SelectedCheckBoxValues\" type=\"checkbox\" value=\"{1}\" />", expectedId, value );
            Assert.AreEqual( expectedResult, result.ToHtmlString() );
        }