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 200 201 202 203 204 205 206 207 208 209 210 211
|
// 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 TextAreaExtensionsTest
{
[Fact]
public void TextAreaWithEmptyNameThrows()
{
// Arrange
HtmlHelper helper = HtmlHelperFactory.Create();
// Act and assert
Assert.ThrowsArgument(() => helper.TextArea(null), "name", "Value cannot be null or an empty string.");
// Act and assert
Assert.ThrowsArgument(() => helper.TextArea(String.Empty), "name", "Value cannot be null or an empty string.");
}
[Fact]
public void TextAreaWithDefaultRowsAndCols()
{
// Arrange
HtmlHelper helper = HtmlHelperFactory.Create();
// Act
var html = helper.TextArea("foo");
// Assert
Assert.Equal(@"<textarea cols=""20"" id=""foo"" name=""foo"" rows=""2""></textarea>", html.ToHtmlString());
}
[Fact]
public void TextAreaWithZeroRowsAndColumns()
{
// Arrange
HtmlHelper helper = HtmlHelperFactory.Create();
// Act
var html = helper.TextArea("foo", null, 0, 0, null);
// Assert
Assert.Equal(@"<textarea id=""foo"" name=""foo""></textarea>", html.ToHtmlString());
}
[Fact]
public void TextAreaWithNonZeroRowsAndColumns()
{
// Arrange
HtmlHelper helper = HtmlHelperFactory.Create();
// Act
var html = helper.TextArea("foo", null, 4, 10, null);
// Assert
Assert.Equal(@"<textarea cols=""10"" id=""foo"" name=""foo"" rows=""4""></textarea>", html.ToHtmlString());
}
[Fact]
public void TextAreaWithObjectAttributes()
{
// Arrange
ModelStateDictionary modelState = new ModelStateDictionary();
modelState.SetModelValue("foo", "foo-value");
HtmlHelper helper = HtmlHelperFactory.Create(modelState);
// Act
var html = helper.TextArea("foo", new { attr = "value", cols = 6 });
// Assert
Assert.Equal(@"<textarea attr=""value"" cols=""6"" id=""foo"" name=""foo"" rows=""2"">foo-value</textarea>", html.ToHtmlString());
}
[Fact]
public void TextAreaWithExplicitValue()
{
// Arrange
ModelStateDictionary modelState = new ModelStateDictionary();
modelState.SetModelValue("foo", "explicit-foo-value");
HtmlHelper helper = HtmlHelperFactory.Create(modelState);
// Act
var html = helper.TextArea("foo", "explicit-foo-value", new { attr = "attr-value", cols = 6 });
// Assert
Assert.Equal(@"<textarea attr=""attr-value"" cols=""6"" id=""foo"" name=""foo"" rows=""2"">explicit-foo-value</textarea>",
html.ToHtmlString());
}
[Fact]
public void TextAreaWithDictionaryAttributes()
{
// Arrange
ModelStateDictionary modelState = new ModelStateDictionary();
modelState.SetModelValue("foo", "explicit-foo-value");
HtmlHelper helper = HtmlHelperFactory.Create(modelState);
var attributes = new Dictionary<string, object>() { { "attr", "attr-val" }, { "rows", 15 }, { "cols", 12 } };
// Act
var html = helper.TextArea("foo", attributes);
// Assert
Assert.Equal(@"<textarea attr=""attr-val"" cols=""12"" id=""foo"" name=""foo"" rows=""15"">explicit-foo-value</textarea>",
html.ToHtmlString());
}
[Fact]
public void TextAreaWithNoValueAndObjectAttributes()
{
// Arrange
HtmlHelper helper = HtmlHelperFactory.Create();
var attributes = new Dictionary<string, object>() { { "attr", "attr-val" }, { "rows", 15 }, { "cols", 12 } };
// Act
var html = helper.TextArea("foo", attributes);
// Assert
Assert.Equal(@"<textarea attr=""attr-val"" cols=""12"" id=""foo"" name=""foo"" rows=""15""></textarea>",
html.ToHtmlString());
}
[Fact]
public void TextAreaWithNullValue()
{
// Arrange
ModelStateDictionary modelState = new ModelStateDictionary();
modelState.SetModelValue("foo", "explicit-foo-value");
HtmlHelper helper = HtmlHelperFactory.Create(modelState);
var attributes = new Dictionary<string, object>() { { "attr", "attr-val" }, { "rows", 15 }, { "cols", 12 } };
// Act
var html = helper.TextArea("foo", null, attributes);
// Assert
Assert.Equal(@"<textarea attr=""attr-val"" cols=""12"" id=""foo"" name=""foo"" rows=""15"">explicit-foo-value</textarea>",
html.ToHtmlString());
}
[Fact]
public void TextAreaWithError()
{
// Arrange
ModelStateDictionary modelState = new ModelStateDictionary();
modelState.AddError("foo", "some error");
HtmlHelper helper = HtmlHelperFactory.Create(modelState);
// Act
var html = helper.TextArea("foo", String.Empty);
// Assert
Assert.Equal(@"<textarea class=""input-validation-error"" cols=""20"" id=""foo"" name=""foo"" rows=""2""></textarea>",
html.ToHtmlString());
}
[Fact]
public void TextAreaWithErrorAndCustomCssClass()
{
// Arrange
ModelStateDictionary modelState = new ModelStateDictionary();
modelState.AddError("foo", "some error");
HtmlHelper helper = HtmlHelperFactory.Create(modelState);
// Act
var html = helper.TextArea("foo", String.Empty, new { @class = "my-css" });
// Assert
Assert.Equal(@"<textarea class=""input-validation-error my-css"" cols=""20"" id=""foo"" name=""foo"" rows=""2""></textarea>",
html.ToHtmlString());
}
// [Fact]
// Cant test this in multi-threaded
public void TextAreaWithCustomErrorClass()
{
// Arrange
ModelStateDictionary modelState = new ModelStateDictionary();
modelState.AddError("foo", "some error");
HtmlHelper.ValidationInputCssClassName = "custom-input-validation-error";
HtmlHelper helper = HtmlHelperFactory.Create(modelState);
// Act
var html = helper.TextArea("foo", String.Empty, new { @class = "my-css" });
// Assert
Assert.Equal(@"<textarea class=""custom-input-validation-error my-css"" cols=""20"" id=""foo"" name=""foo"" rows=""2""></textarea>",
html.ToHtmlString());
}
[Fact]
public void TextAreaAddsUnobtrusiveValidationAttributes()
{
// 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.TextArea(fieldName, htmlAttributes: new Dictionary<string, object> { { "data-some-val", "5" } });
// Assert
Assert.Equal(@"<textarea cols=""20"" 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"" rows=""2""></textarea>",
html.ToString());
}
}
}
|