diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperDropDownListExtensionsTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperDropDownListExtensionsTest.cs new file mode 100644 index 0000000000..d3de94321f --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperDropDownListExtensionsTest.cs @@ -0,0 +1,257 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using Microsoft.AspNetCore.Mvc.ModelBinding; +using Microsoft.AspNetCore.Mvc.Rendering; +using Microsoft.AspNetCore.Mvc.TestCommon; +using Microsoft.AspNetCore.Mvc.ViewFeatures; +using Xunit; + +namespace Microsoft.AspNetCore.Mvc.Core +{ + public class HtmlHelperDropDownListExtensionsTest + { + private static readonly List BasicSelectList = new List + { + new SelectListItem { Text = "Zero", Value = "0"}, + new SelectListItem { Text = "One", Value = "1"}, + new SelectListItem { Text = "Two", Value = "2"}, + new SelectListItem { Text = "Three", Value = "3"}, + }; + + [Fact] + public void DropDownList_FindsSelectList() + { + // Arrange + var expectedHtml = ""; + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.ModelState.SetModelValue("Property1", 3, "3"); + helper.ViewData["Property1"] = BasicSelectList; + + // Act + var dropDownListResult = helper.DropDownList("Property1"); + + // Assert + Assert.Equal(expectedHtml, HtmlContentUtilities.HtmlContentToString(dropDownListResult)); + } + + [Fact] + public void DropDownList_FindsSelectList_UsesOptionLabel() + { + // Arrange + var expectedHtml = ""; + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.ModelState.SetModelValue("Property1", 1, "1"); + helper.ViewData["Property1"] = BasicSelectList; + + // Act + var dropDownListResult = helper.DropDownList("Property1", "--select--"); + + // Assert + Assert.Equal(expectedHtml, HtmlContentUtilities.HtmlContentToString(dropDownListResult)); + } + + [Fact] + public void DropDownList_UsesSpecifiedExpressionAndSelectList() + { + // Arrange + var expectedHtml = ""; + var selectList = new List + { + new SelectListItem { Text = "Four", Value = "4" }, + new SelectListItem { Text = "Five", Value = "5" }, + }; + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.Model = new TestModel { Property2 = "4" }; + + // Act + var dropDownListResult = helper.DropDownList("Property2", selectList); + + // Assert + Assert.Equal(expectedHtml, HtmlContentUtilities.HtmlContentToString(dropDownListResult)); + } + + [Fact] + public void DropDownList_UsesSpecifiedExpressionAndSelectListAndHtmlAttributes() + { + // Arrange + var expectedHtml = ""; + var selectList = new List + { + new SelectListItem { Text = "Four", Value = "4" }, + new SelectListItem { Text = "Five", Value = "5" }, + }; + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.ModelState.SetModelValue("Property1", 4, "4"); + helper.ViewData["Property1"] = BasicSelectList; + + // Act + var dropDownListResult = helper.DropDownList("Property1", selectList, new { Key = "Value", name = "CustomName" }); + + // Assert + Assert.Equal(expectedHtml, HtmlContentUtilities.HtmlContentToString(dropDownListResult)); + } + + [Fact] + public void DropDownList_UsesExpressionAndSpecifiedSelectListAndOptionLabel() + { + // Arrange + var expectedHtml = ""; + var selectList = new List + { + new SelectListItem { Text = "Four", Value = "4" }, + new SelectListItem { Text = "Five", Value = "5" }, + }; + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.Model = new TestModel { Property2 = "5" }; + + // Act + var dropDownListResult = helper.DropDownList("Property2", selectList, "--select--"); + + // Assert + Assert.Equal(expectedHtml, HtmlContentUtilities.HtmlContentToString(dropDownListResult)); + } + + [Fact] + public void DropDownListFor_NullSelectListFindsListFromViewData() + { + // Arrange + var expectedHtml = ""; + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData["Property1"] = BasicSelectList; + helper.ViewData.Model = new TestModel { Property1 = 0 }; + + // Act + var dropDownListForResult = helper.DropDownListFor(m => m.Property1, selectList: null); + + // Assert + Assert.Equal(expectedHtml, HtmlContentUtilities.HtmlContentToString(dropDownListForResult)); + } + + [Fact] + public void DropDownListFor_UsesSpecifiedExpressionAndSelectList() + { + // Arrange + var expectedHtml = ""; + var selectList = new List + { + new SelectListItem { Text = "Four", Value = "4" }, + new SelectListItem { Text = "Five", Value = "5" }, + }; + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData["Property1"] = BasicSelectList; + helper.ViewData.Model = new TestModel { Property2 = "5" }; + + // Act + var dropDownListForResult = helper.DropDownListFor(m => m.Property2, selectList); + + // Assert + Assert.Equal(expectedHtml, HtmlContentUtilities.HtmlContentToString(dropDownListForResult)); + } + + [Fact] + public void DropDownListFor_UsesSpecifiedExpressionAndSelectListAndHtmlAttributes() + { + // Arrange + var expectedHtml = ""; + var selectList = new List + { + new SelectListItem { Text = "Four", Value = "4" }, + new SelectListItem { Text = "Five", Value = "5" }, + }; + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.Model = new TestModel { Property3 = new List { "0", "2", "4" } }; + + // Act + var dropDownListForResult = helper.DropDownListFor(m => m.Property3[2], selectList, new { Key = "Value", name = "CustomName" }); + + // Assert + Assert.Equal(expectedHtml, HtmlContentUtilities.HtmlContentToString(dropDownListForResult)); + } + + [Fact] + public void DropDownListFor_UsesSpecifiedSelectListAndOptionLabel() + { + // Arrange + var expectedHtml = ""; + var selectList = new List + { + new SelectListItem { Text = "Four", Value = "4" }, + new SelectListItem { Text = "Five", Value = "5" }, + }; + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.Model = new TestModel { Property2 = "5" }; + + // Act + var dropDownListForResult = helper.DropDownListFor(m => m.Property2, selectList, "--select--"); + + // Assert + Assert.Equal(expectedHtml, HtmlContentUtilities.HtmlContentToString(dropDownListForResult)); + } + + private class TestModel + { + public int Property1 { get; set; } + + public string Property2 { get; set; } + + public List Property3 { get; set; } + } + } +} diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperFormTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperFormTest.cs index c7ba1cf768..8091aa225a 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperFormTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperFormTest.cs @@ -114,7 +114,6 @@ namespace Microsoft.AspNetCore.Mvc.Rendering }, }; } - } [Fact] diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperListBoxExtensionsTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperListBoxExtensionsTest.cs new file mode 100644 index 0000000000..92c8fe0896 --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperListBoxExtensionsTest.cs @@ -0,0 +1,178 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using Microsoft.AspNetCore.Mvc.ModelBinding; +using Microsoft.AspNetCore.Mvc.Rendering; +using Microsoft.AspNetCore.Mvc.TestCommon; +using Microsoft.AspNetCore.Mvc.ViewFeatures; +using Xunit; + +namespace Microsoft.AspNetCore.Mvc.Core +{ + public class HtmlHelperListBoxExtensionsTest + { + private static readonly List BasicSelectList = new List + { + new SelectListItem { Text = "Zero", Value = "0"}, + new SelectListItem { Text = "One", Value = "1"}, + new SelectListItem { Text = "Two", Value = "2"}, + new SelectListItem { Text = "Three", Value = "3"}, + }; + + [Fact] + public void ListBox_FindsSelectList() + { + // Arrange + var expectedHtml = ""; + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.ModelState.SetModelValue("Property1", 2, "2"); + helper.ViewData["Property1"] = BasicSelectList; + + // Act + var listBoxResult = helper.ListBox("Property1"); + + // Assert + Assert.Equal(expectedHtml, HtmlContentUtilities.HtmlContentToString(listBoxResult)); + } + + [Fact] + public void ListBox_UsesSpecifiedExpressionAndSelectList() + { + // Arrange + var expectedHtml = ""; + var selectList = new List + { + new SelectListItem { Text = "Four", Value = "4" }, + new SelectListItem { Text = "Five", Value = "5" }, + }; + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.Model = new TestModel { Property3 = new List { "4" } }; + + // Act + var listBoxResult = helper.ListBox("Property3", selectList); + + // Assert + Assert.Equal(expectedHtml, HtmlContentUtilities.HtmlContentToString(listBoxResult)); + } + + [Fact] + public void ListBox_UsesSpecifiedSelectExpressionAndListAndHtmlAttributes() + { + // Arrange + var expectedHtml = ""; + var selectList = new List + { + new SelectListItem { Text = "Four", Value = "4" }, + new SelectListItem { Text = "Five", Value = "5" }, + }; + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData["Property2"] = new List { "1", "2", "5" }; + + // Act + var listBoxResult = helper.ListBox("Property2", selectList, new { Key = "Value", name = "CustomName" }); + + // Assert + Assert.Equal(expectedHtml, HtmlContentUtilities.HtmlContentToString(listBoxResult)); + } + + [Fact] + public void ListBoxFor_NullSelectListFindsListFromViewData() + { + // Arrange + var expectedHtml = ""; + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData["Property1"] = BasicSelectList; + + // Act + var listBoxForResult = helper.ListBoxFor(m => m.Property1, null); + + // Assert + Assert.Equal(expectedHtml, HtmlContentUtilities.HtmlContentToString(listBoxForResult)); + } + + [Fact] + public void ListBoxFor_UsesSpecifiedExpressionAndSelectList() + { + // Arrange + var expectedHtml = ""; + var selectList = new List + { + new SelectListItem { Text = "Four", Value = "4" }, + new SelectListItem { Text = "Five", Value = "5" }, + }; + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.Model = new TestModel { Property3 = new List { "0", "4", "5" } }; + + // Act + var listBoxForResult = helper.ListBoxFor(m => m.Property3, selectList); + + // Assert + Assert.Equal(expectedHtml, HtmlContentUtilities.HtmlContentToString(listBoxForResult)); + } + + [Fact] + public void ListBoxFor_UsesSpecifiedExpressionAndSelectListAndHtmlAttributes() + { + // Arrange + var expectedHtml = ""; + var selectList = new List + { + new SelectListItem { Text = "Four", Value = "4" }, + new SelectListItem { Text = "Five", Value = "5" }, + }; + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData["Property3"] = new List { "0", "2" }; + + // Act + var listBoxForResult = helper.ListBoxFor(m => m.Property3, selectList, new { Key = "Value", name = "CustomName" }); + + // Assert + Assert.Equal(expectedHtml, HtmlContentUtilities.HtmlContentToString(listBoxForResult)); + } + + private class TestModel + { + public int Property1 { get; set; } + + public string Property2 { get; set; } + + public List Property3 { get; set; } + } + } +}