Added tests for DropDownList and ListBox extensions

This commit is contained in:
Ajay Bhargav Baaskaran 2016-03-02 16:47:30 -08:00
parent 06289945d0
commit 5238a8f16e
3 changed files with 435 additions and 1 deletions

View File

@ -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<SelectListItem> BasicSelectList = new List<SelectListItem>
{
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 = "<select id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\">" +
"<option value=\"HtmlEncode[[0]]\">HtmlEncode[[Zero]]</option>" + Environment.NewLine +
"<option value=\"HtmlEncode[[1]]\">HtmlEncode[[One]]</option>" + Environment.NewLine +
"<option value=\"HtmlEncode[[2]]\">HtmlEncode[[Two]]</option>" + Environment.NewLine +
"<option selected=\"HtmlEncode[[selected]]\" value=\"HtmlEncode[[3]]\">HtmlEncode[[Three]]</option>" + Environment.NewLine +
"</select>";
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(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 = "<select id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\">" +
"<option value=\"\">HtmlEncode[[--select--]]</option>" + Environment.NewLine +
"<option value=\"HtmlEncode[[0]]\">HtmlEncode[[Zero]]</option>" + Environment.NewLine +
"<option selected=\"HtmlEncode[[selected]]\" value=\"HtmlEncode[[1]]\">HtmlEncode[[One]]</option>" + Environment.NewLine +
"<option value=\"HtmlEncode[[2]]\">HtmlEncode[[Two]]</option>" + Environment.NewLine +
"<option value=\"HtmlEncode[[3]]\">HtmlEncode[[Three]]</option>" + Environment.NewLine +
"</select>";
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(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 = "<select id=\"HtmlEncode[[Property2]]\" name=\"HtmlEncode[[Property2]]\">" +
"<option selected=\"HtmlEncode[[selected]]\" value=\"HtmlEncode[[4]]\">HtmlEncode[[Four]]</option>" + Environment.NewLine +
"<option value=\"HtmlEncode[[5]]\">HtmlEncode[[Five]]</option>" + Environment.NewLine +
"</select>";
var selectList = new List<SelectListItem>
{
new SelectListItem { Text = "Four", Value = "4" },
new SelectListItem { Text = "Five", Value = "5" },
};
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(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 = "<select id=\"HtmlEncode[[Property1]]\" Key=\"HtmlEncode[[Value]]\" name=\"HtmlEncode[[Property1]]\">" +
"<option selected=\"HtmlEncode[[selected]]\" value=\"HtmlEncode[[4]]\">HtmlEncode[[Four]]</option>" + Environment.NewLine +
"<option value=\"HtmlEncode[[5]]\">HtmlEncode[[Five]]</option>" + Environment.NewLine +
"</select>";
var selectList = new List<SelectListItem>
{
new SelectListItem { Text = "Four", Value = "4" },
new SelectListItem { Text = "Five", Value = "5" },
};
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(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 = "<select id=\"HtmlEncode[[Property2]]\" name=\"HtmlEncode[[Property2]]\">" +
"<option value=\"\">HtmlEncode[[--select--]]</option>" + Environment.NewLine +
"<option value=\"HtmlEncode[[4]]\">HtmlEncode[[Four]]</option>" + Environment.NewLine +
"<option selected=\"HtmlEncode[[selected]]\" value=\"HtmlEncode[[5]]\">HtmlEncode[[Five]]</option>" + Environment.NewLine +
"</select>";
var selectList = new List<SelectListItem>
{
new SelectListItem { Text = "Four", Value = "4" },
new SelectListItem { Text = "Five", Value = "5" },
};
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(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 = "<select id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\">" +
"<option selected=\"HtmlEncode[[selected]]\" value=\"HtmlEncode[[0]]\">HtmlEncode[[Zero]]</option>" + Environment.NewLine +
"<option value=\"HtmlEncode[[1]]\">HtmlEncode[[One]]</option>" + Environment.NewLine +
"<option value=\"HtmlEncode[[2]]\">HtmlEncode[[Two]]</option>" + Environment.NewLine +
"<option value=\"HtmlEncode[[3]]\">HtmlEncode[[Three]]</option>" + Environment.NewLine +
"</select>";
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(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 = "<select id=\"HtmlEncode[[Property2]]\" name=\"HtmlEncode[[Property2]]\">" +
"<option value=\"HtmlEncode[[4]]\">HtmlEncode[[Four]]</option>" + Environment.NewLine +
"<option selected=\"HtmlEncode[[selected]]\" value=\"HtmlEncode[[5]]\">HtmlEncode[[Five]]</option>" + Environment.NewLine +
"</select>";
var selectList = new List<SelectListItem>
{
new SelectListItem { Text = "Four", Value = "4" },
new SelectListItem { Text = "Five", Value = "5" },
};
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(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 = "<select id=\"HtmlEncode[[Property3_2_]]\" Key=\"HtmlEncode[[Value]]\" name=\"HtmlEncode[[Property3[2]]]\">" +
"<option selected=\"HtmlEncode[[selected]]\" value=\"HtmlEncode[[4]]\">HtmlEncode[[Four]]</option>" + Environment.NewLine +
"<option value=\"HtmlEncode[[5]]\">HtmlEncode[[Five]]</option>" + Environment.NewLine +
"</select>";
var selectList = new List<SelectListItem>
{
new SelectListItem { Text = "Four", Value = "4" },
new SelectListItem { Text = "Five", Value = "5" },
};
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property3 = new List<string> { "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 = "<select id=\"HtmlEncode[[Property2]]\" name=\"HtmlEncode[[Property2]]\">" +
"<option value=\"\">HtmlEncode[[--select--]]</option>" + Environment.NewLine +
"<option value=\"HtmlEncode[[4]]\">HtmlEncode[[Four]]</option>" + Environment.NewLine +
"<option selected=\"HtmlEncode[[selected]]\" value=\"HtmlEncode[[5]]\">HtmlEncode[[Five]]</option>" + Environment.NewLine +
"</select>";
var selectList = new List<SelectListItem>
{
new SelectListItem { Text = "Four", Value = "4" },
new SelectListItem { Text = "Five", Value = "5" },
};
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(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<string> Property3 { get; set; }
}
}
}

View File

@ -114,7 +114,6 @@ namespace Microsoft.AspNetCore.Mvc.Rendering
},
};
}
}
[Fact]

View File

@ -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<SelectListItem> BasicSelectList = new List<SelectListItem>
{
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 = "<select id=\"HtmlEncode[[Property1]]\" multiple=\"HtmlEncode[[multiple]]\" name=\"HtmlEncode[[Property1]]\">" +
"<option value=\"HtmlEncode[[0]]\">HtmlEncode[[Zero]]</option>" + Environment.NewLine +
"<option value=\"HtmlEncode[[1]]\">HtmlEncode[[One]]</option>" + Environment.NewLine +
"<option selected=\"HtmlEncode[[selected]]\" value=\"HtmlEncode[[2]]\">HtmlEncode[[Two]]</option>" + Environment.NewLine +
"<option value=\"HtmlEncode[[3]]\">HtmlEncode[[Three]]</option>" + Environment.NewLine +
"</select>";
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(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 = "<select id=\"HtmlEncode[[Property3]]\" multiple=\"HtmlEncode[[multiple]]\" name=\"HtmlEncode[[Property3]]\">" +
"<option selected=\"HtmlEncode[[selected]]\" value=\"HtmlEncode[[4]]\">HtmlEncode[[Four]]</option>" + Environment.NewLine +
"<option value=\"HtmlEncode[[5]]\">HtmlEncode[[Five]]</option>" + Environment.NewLine +
"</select>";
var selectList = new List<SelectListItem>
{
new SelectListItem { Text = "Four", Value = "4" },
new SelectListItem { Text = "Five", Value = "5" },
};
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property3 = new List<string> { "4" } };
// Act
var listBoxResult = helper.ListBox("Property3", selectList);
// Assert
Assert.Equal(expectedHtml, HtmlContentUtilities.HtmlContentToString(listBoxResult));
}
[Fact]
public void ListBox_UsesSpecifiedSelectExpressionAndListAndHtmlAttributes()
{
// Arrange
var expectedHtml = "<select id=\"HtmlEncode[[Property2]]\" Key=\"HtmlEncode[[Value]]\" multiple=\"HtmlEncode[[multiple]]\" name=\"HtmlEncode[[Property2]]\">" +
"<option value=\"HtmlEncode[[4]]\">HtmlEncode[[Four]]</option>" + Environment.NewLine +
"<option selected=\"HtmlEncode[[selected]]\" value=\"HtmlEncode[[5]]\">HtmlEncode[[Five]]</option>" + Environment.NewLine +
"</select>";
var selectList = new List<SelectListItem>
{
new SelectListItem { Text = "Four", Value = "4" },
new SelectListItem { Text = "Five", Value = "5" },
};
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData["Property2"] = new List<string> { "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 = "<select id=\"HtmlEncode[[Property1]]\" multiple=\"HtmlEncode[[multiple]]\" name=\"HtmlEncode[[Property1]]\">" +
"<option value=\"HtmlEncode[[0]]\">HtmlEncode[[Zero]]</option>" + Environment.NewLine +
"<option value=\"HtmlEncode[[1]]\">HtmlEncode[[One]]</option>" + Environment.NewLine +
"<option value=\"HtmlEncode[[2]]\">HtmlEncode[[Two]]</option>" + Environment.NewLine +
"<option value=\"HtmlEncode[[3]]\">HtmlEncode[[Three]]</option>" + Environment.NewLine +
"</select>";
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(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 = "<select id=\"HtmlEncode[[Property3]]\" multiple=\"HtmlEncode[[multiple]]\" name=\"HtmlEncode[[Property3]]\">" +
"<option selected=\"HtmlEncode[[selected]]\" value=\"HtmlEncode[[4]]\">HtmlEncode[[Four]]</option>" + Environment.NewLine +
"<option selected=\"HtmlEncode[[selected]]\" value=\"HtmlEncode[[5]]\">HtmlEncode[[Five]]</option>" + Environment.NewLine +
"</select>";
var selectList = new List<SelectListItem>
{
new SelectListItem { Text = "Four", Value = "4" },
new SelectListItem { Text = "Five", Value = "5" },
};
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property3 = new List<string> { "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 = "<select id=\"HtmlEncode[[Property3]]\" Key=\"HtmlEncode[[Value]]\" multiple=\"HtmlEncode[[multiple]]\" name=\"HtmlEncode[[Property3]]\">" +
"<option value=\"HtmlEncode[[4]]\">HtmlEncode[[Four]]</option>" + Environment.NewLine +
"<option value=\"HtmlEncode[[5]]\">HtmlEncode[[Five]]</option>" + Environment.NewLine +
"</select>";
var selectList = new List<SelectListItem>
{
new SelectListItem { Text = "Four", Value = "4" },
new SelectListItem { Text = "Five", Value = "5" },
};
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData["Property3"] = new List<string> { "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<string> Property3 { get; set; }
}
}
}