1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Web.WebPages.Html;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.WebPages.Test
{
public class RadioButtonTest
{
[Fact]
public void RadioButtonWithEmptyNameThrows()
{
// Arrange
HtmlHelper helper = HtmlHelperFactory.Create();
// Act and assert
Assert.ThrowsArgumentNullOrEmptyString(() => helper.RadioButton(null, null), "name");
Assert.ThrowsArgumentNullOrEmptyString(() => helper.RadioButton(String.Empty, null), "name");
}
[Fact]
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());
}
[Fact]
public void RadioButtonWithObjectAttributes()
{
// Arrange
HtmlHelper helper = HtmlHelperFactory.Create();
// Act
var html = helper.RadioButton("foo", "bar", new { attr = "attr-value" });
// Assert
Assert.Equal(@"<input attr=""attr-value"" id=""foo"" name=""foo"" type=""radio"" value=""bar"" />",
html.ToHtmlString());
}
[Fact]
public void RadioButtonWithDictionaryAttributes()
{
// Arrange
HtmlHelper helper = HtmlHelperFactory.Create();
// Act
var html = helper.RadioButton("foo", "bar", new Dictionary<string, object> { { "attr", "attr-value" } });
// Assert
Assert.Equal(@"<input attr=""attr-value"" id=""foo"" name=""foo"" type=""radio"" value=""bar"" />",
html.ToHtmlString());
}
[Fact]
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());
}
[Fact]
public void RadioButtonUsesModelStateToRemoveChecked()
{
// Arrange
var modelState = new ModelStateDictionary();
modelState.SetModelValue("foo", "not-a-bar");
HtmlHelper helper = HtmlHelperFactory.Create(modelState);
// Act
var html = helper.RadioButton("foo", "bar", new { @checked = "checked" });
// Assert
Assert.Equal(@"<input id=""foo"" name=""foo"" type=""radio"" value=""bar"" />",
html.ToHtmlString());
}
[Fact]
public void RadioButtonWithoutModelStateDoesNotAffectChecked()
{
// Arrange
HtmlHelper helper = HtmlHelperFactory.Create();
// Act
var html = helper.RadioButton("foo", "bar", new { @checked = "checked" });
// Assert
Assert.Equal(@"<input checked=""checked"" id=""foo"" name=""foo"" type=""radio"" value=""bar"" />",
html.ToHtmlString());
}
[Fact]
public void RadioButtonWithNonStringModelValue()
{
// Arrange
var modelState = new ModelStateDictionary();
modelState.SetModelValue("foo", new List<double>());
HtmlHelper helper = HtmlHelperFactory.Create(modelState);
// Act
var html = helper.RadioButton("foo", "bar");
// Assert
Assert.Equal(@"<input id=""foo"" name=""foo"" type=""radio"" value=""bar"" />",
html.ToHtmlString());
}
[Fact]
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());
}
[Fact]
public void RadioButtonWithExplicitChecked()
{
// Arrange
var modelState = new ModelStateDictionary();
modelState.SetModelValue("foo", "bar");
HtmlHelper helper = HtmlHelperFactory.Create(modelState);
// Act
var html = helper.RadioButton("foo", "not-bar", true);
// Assert
Assert.Equal(@"<input checked=""checked"" id=""foo"" name=""foo"" type=""radio"" value=""not-bar"" />",
html.ToHtmlString());
}
[Fact]
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());
}
[Fact]
public void RadioButtonAddsUnobtrusiveValidationAttributes()
{
// Arrange
const string fieldName = "name";
var modelStateDictionary = new ModelStateDictionary();
var validationHelper = new ValidationHelper(new Mock<HttpContextBase>().Object, modelStateDictionary);
HtmlHelper helper = HtmlHelperFactory.Create(modelStateDictionary, validationHelper);
// Act
validationHelper.RequireField(fieldName, "Please specify a valid Name.");
validationHelper.Add(fieldName, Validator.StringLength(30, errorMessage: "Name cannot exceed {0} characters"));
var html = helper.RadioButton(fieldName, value: 8, htmlAttributes: new Dictionary<string, object> { { "data-some-val", "5" } });
// Assert
Assert.Equal(@"<input data-some-val=""5"" data-val=""true"" data-val-length=""Name cannot exceed 30 characters"" data-val-length-max=""30"" data-val-required=""Please specify a valid Name."" id=""name"" name=""name"" type=""radio"" value=""8"" />",
html.ToString());
}
}
}
|