Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Demonstrate #1485, #1487, #2662, #2664 in functional and unit tests
Browse files Browse the repository at this point in the history
- test additional cases _close_ to these bugs as well

for #1485
- show odd `@Html.CheckBox()`, `@Html.Hidden()` behaviour in unit tests
- show odd `@Html.TextBox()` behaviour in functional tests (templates)

for #1487
- show odd `@Html.Value()` behaviour in unit tests
- show odd `@Html.RadioButton()`, `@Html.TextArea()` behaviour in functional tests
- show lack of validation attributes for `@Html.RadioButton()`, `<select>` tag helper

for #2662
- show odd `@Html.RadioButton(string.Empty)` behaviour in functional tests

for #2664
- show failures with `@Html.ListBox()` in unit tests

nits:
- test `IHtmlHelper` methods, not extensions
- use `ViewData`, not `ViewBag` in `HtmlGeneration_FormController`
- name test methods a bit more consistently
- rename `HtmlHelperValueExtensionsTest` to `HtmlHelperValueTest`
  • Loading branch information
dougbu committed Jun 9, 2015
1 parent a679e87 commit cd138a9
Show file tree
Hide file tree
Showing 10 changed files with 953 additions and 72 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,76 @@ public void HiddenWithArgumentValueAndAttributes_UsesArgumentValue(object attrib
Assert.Equal(expected, result.ToString());
}

[Fact]
public void HiddenNotInTemplate_GetsValueFromPropertyOfViewDataEntry()
{
// Arrange
var expected = @"<input id=""HtmlEncode[[Prefix_Property1]]"" name=""HtmlEncode[[Prefix.Property1]]"" " +
@"type=""HtmlEncode[[hidden]]"" value=""HtmlEncode[[contained-view-data-value]]"" />";
var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNonNullModel());
helper.ViewData.Model.Property1 = "model-property1-value";
helper.ViewData["Prefix"] = new HiddenModel { Property1 = "contained-view-data-value" };

// Act
var html = helper.Hidden("Prefix.Property1", value: null, htmlAttributes: null);

// Assert
Assert.Equal(expected, html.ToString());
}

[Fact]
public void HiddenInTemplate_GetsValueFromPropertyOfViewDataEntry()
{
// Arrange
var expected = @"<input id=""HtmlEncode[[Prefix_Property1]]"" name=""HtmlEncode[[Prefix.Property1]]"" " +
@"type=""HtmlEncode[[hidden]]"" value=""HtmlEncode[[contained-view-data-value]]"" />";
var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNonNullModel());
helper.ViewData.TemplateInfo.HtmlFieldPrefix = "Prefix";
helper.ViewData.Model.Property1 = "model-property1-value";
helper.ViewData["Prefix"] = new HiddenModel { Property1 = "contained-view-data-value" };

// Act
var html = helper.Hidden("Property1", value: null, htmlAttributes: null);

// Assert
Assert.Equal(expected, html.ToString());
}

[Fact]
public void HiddenNotInTemplate_GetsValueFromViewDataEntry_EvenIfNull()
{
// Arrange
var expected = @"<input id=""HtmlEncode[[Property1]]"" name=""HtmlEncode[[Property1]]"" " +
@"type=""HtmlEncode[[hidden]]"" value="""" />";
var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNonNullModel());
helper.ViewData.Model.Property1 = "model-property1-value";
helper.ViewData["Property1"] = null;

// Act
var html = helper.Hidden("Property1", value: null, htmlAttributes: null);

// Assert
Assert.Equal(expected, html.ToString());
}

[Fact]
public void HiddenInTemplate_GetsValueFromViewDataEntry_EvenIfNull()
{
// Arrange
var expected = @"<input id=""HtmlEncode[[Prefix_Property1]]"" name=""HtmlEncode[[Prefix.Property1]]"" " +
@"type=""HtmlEncode[[hidden]]"" value="""" />";
var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNonNullModel());
helper.ViewData.TemplateInfo.HtmlFieldPrefix = "Prefix";
helper.ViewData.Model.Property1 = "model-property1-value";
helper.ViewData["Prefix.Property1"] = null;

// Act
var html = helper.Hidden("Property1", value: null, htmlAttributes: null);

// Assert
Assert.Equal(expected, html.ToString());
}

[Fact]
public void HiddenOverridesValueFromAttributesWithArgumentValue()
{
Expand Down Expand Up @@ -92,7 +162,7 @@ public void HiddenWithArgumentValueAndNullModel_UsesArgumentValue()
}

[Fact]
public void HiddenWithNullValueAndNullModel_GeneratesExpectedValue()
public void HiddenWithNonNullValue_GeneratesExpectedValue()
{
// Arrange
var expected = @"<input data-key=""HtmlEncode[[value]]"" id=""HtmlEncode[[Property1]]"" name=""HtmlEncode[[Property1]]"" type=""HtmlEncode[[hidden]]"" " +
Expand Down Expand Up @@ -155,13 +225,11 @@ public void HiddenUsesValueFromViewData_IfModelStateDoesNotHavePropertyAndExplic
}

[Fact]
public void HiddenUsesPropertyValue_IfModelStateAndViewDataDoNotHavePropertyAndExplicitValueIsNull()
public void HiddenNotInTemplate_GetsModelValue_IfModelStateAndViewDataEmpty()
{
// Arrange
var expected = @"<input id=""HtmlEncode[[Property1]]"" name=""HtmlEncode[[Property1]]"" type=""HtmlEncode[[hidden]]"" value=""HtmlEncode[[property-value]]"" />";
var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues());
helper.ViewData.ModelState.Clear();
helper.ViewData.Clear();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNonNullModel());
helper.ViewData.Model.Property1 = "property-value";

// Act
Expand All @@ -171,15 +239,29 @@ public void HiddenUsesPropertyValue_IfModelStateAndViewDataDoNotHavePropertyAndE
Assert.Equal(expected, result.ToString());
}

[Fact(Skip = "#1485, unable to get Model value.")]
public void HiddenInTemplate_GetsModelValue_IfModelStateAndViewDataEmpty()
{
// Arrange
var expected = @"<input id=""HtmlEncode[[Prefix_Property1]]"" name=""HtmlEncode[[Prefix.Property1]]"" " +
@"type=""HtmlEncode[[hidden]]"" value=""HtmlEncode[[property-value]]"" />";
var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNonNullModel());
helper.ViewData.TemplateInfo.HtmlFieldPrefix = "Prefix";
helper.ViewData.Model.Property1 = "property-value";

// Act
var html = helper.Hidden("Property1", value: null, htmlAttributes: new { value = "attribute-value" });

// Assert
Assert.Equal(expected, html.ToString());
}

[Fact]
public void HiddenDoesNotUsesAttributeValue()
public void HiddenNotInTemplate_DoesNotUseAttributeValue()
{
// Arrange
var expected = @"<input id=""HtmlEncode[[Property1]]"" name=""HtmlEncode[[Property1]]"" type=""HtmlEncode[[hidden]]"" value="""" />";
var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues());
helper.ViewData.ModelState.Clear();
helper.ViewData.Clear();
helper.ViewData.Model.Property1 = null;
var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNonNullModel());

// Act
var result = helper.Hidden("Property1", value: null, htmlAttributes: new { value = "attribute-value" });
Expand All @@ -189,7 +271,23 @@ public void HiddenDoesNotUsesAttributeValue()
}

[Fact]
public void HiddenReturnsEmptyValue_IfPropertyIsNotFound()
public void HiddenInTemplate_DoesNotUseAttributeValue()
{
// Arrange
var expected = @"<input id=""HtmlEncode[[Prefix_Property1]]"" name=""HtmlEncode[[Prefix.Property1]]"" " +
@"type=""HtmlEncode[[hidden]]"" value="""" />";
var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNonNullModel());
helper.ViewData.TemplateInfo.HtmlFieldPrefix = "Prefix";

// Act
var html = helper.Hidden("Property1", value: null, htmlAttributes: new { value = "attribute-value" });

// Assert
Assert.Equal(expected, html.ToString());
}

[Fact]
public void HiddenNotInTemplate_GetsEmptyValue_IfPropertyIsNotFound()
{
// Arrange
var expected = @"<input baz=""HtmlEncode[[BazValue]]"" id=""HtmlEncode[[keyNotFound]]"" name=""HtmlEncode[[keyNotFound]]"" type=""HtmlEncode[[hidden]]"" " +
Expand All @@ -205,7 +303,23 @@ public void HiddenReturnsEmptyValue_IfPropertyIsNotFound()
}

[Fact]
public void HiddenWithPrefix_GeneratesExpectedValue()
public void HiddenInTemplate_GetsEmptyValue_IfPropertyIsNotFound()
{
// Arrange
var expected = @"<input id=""HtmlEncode[[Prefix_keyNotFound]]"" name=""HtmlEncode[[Prefix.keyNotFound]]"" " +
@"type=""HtmlEncode[[hidden]]"" value="""" />";
var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues());
helper.ViewData.TemplateInfo.HtmlFieldPrefix = "Prefix";

// Act
var html = helper.Hidden("keyNotFound", value: null, htmlAttributes: null);

// Assert
Assert.Equal(expected, html.ToString());
}

[Fact]
public void HiddenInTemplate_WithExplicitValue_GeneratesExpectedValue()
{
// Arrange
var expected = @"<input id=""HtmlEncode[[MyPrefix_Property1]]"" name=""HtmlEncode[[MyPrefix.Property1]]"" type=""HtmlEncode[[hidden]]"" " +
Expand All @@ -221,7 +335,7 @@ public void HiddenWithPrefix_GeneratesExpectedValue()
}

[Fact]
public void HiddenWithPrefixAndEmptyName_GeneratesExpectedValue()
public void HiddenInTemplate_WithExplicitValueAndEmptyName_GeneratesExpectedValue()
{
// Arrange
var expected = @"<input id=""HtmlEncode[[MyPrefix]]"" name=""HtmlEncode[[MyPrefix]]"" type=""HtmlEncode[[hidden]]"" value=""HtmlEncode[[fooValue]]"" />";
Expand All @@ -236,7 +350,7 @@ public void HiddenWithPrefixAndEmptyName_GeneratesExpectedValue()
}

[Fact]
public void HiddenUsesPrefixName_ToLookupPropertyValueInModelState()
public void HiddenInTemplate_UsesPrefixName_ToLookupPropertyValueInModelState()
{
// Arrange
var expected = @"<input id=""HtmlEncode[[MyPrefix$Property1]]"" name=""HtmlEncode[[MyPrefix.Property1]]"" type=""HtmlEncode[[hidden]]"" " +
Expand All @@ -258,7 +372,7 @@ public void HiddenUsesPrefixName_ToLookupPropertyValueInModelState()
}

[Fact]
public void HiddenUsesPrefixName_ToLookupPropertyValueInViewData()
public void HiddenInTemplate_UsesPrefixNameToLookupPropertyValueInViewData()
{
// Arrange
var expected = @"<input id=""HtmlEncode[[MyPrefix$Property1]]"" name=""HtmlEncode[[MyPrefix.Property1]]"" type=""HtmlEncode[[hidden]]"" " +
Expand Down Expand Up @@ -509,7 +623,7 @@ public void HiddenForWithAttributesDictionaryAndNullModel_GeneratesExpectedValue

// This test ensures that specifying a the prefix does not affect the expression result.
[Fact]
public void HiddenForWithPrefix_GeneratesExpectedValue()
public void HiddenForInTemplate_GeneratesExpectedValue()
{
// Arrange
var expected = @"<input id=""HtmlEncode[[MyPrefix_Property1]]"" name=""HtmlEncode[[MyPrefix.Property1]]"" type=""HtmlEncode[[hidden]]"" " +
Expand All @@ -519,14 +633,14 @@ public void HiddenForWithPrefix_GeneratesExpectedValue()
helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";

// Act
var result = helper.HiddenFor(m => m.Property1);
var result = helper.HiddenFor(m => m.Property1, htmlAttributes: null);

// Assert
Assert.Equal(expected, result.ToString());
}

[Fact]
public void HiddenForWithPrefix_UsesPrefixWhenLookingUpModelStateValues()
public void HiddenForInTemplate_UsesPrefixWhenLookingUpModelStateValues()
{
// Arrange
var expected = @"<input id=""HtmlEncode[[MyPrefix$Property1]]"" name=""HtmlEncode[[MyPrefix.Property1]]"" type=""HtmlEncode[[hidden]]"" " +
Expand All @@ -542,7 +656,7 @@ public void HiddenForWithPrefix_UsesPrefixWhenLookingUpModelStateValues()
helper.ViewData.ModelState.Add("MyPrefix$Property1", GetModelState("modelstate-with-iddotreplacement"));

// Act
var result = helper.HiddenFor(m => m.Property1);
var result = helper.HiddenFor(m => m.Property1, htmlAttributes: null);

// Assert
Assert.Equal(expected, result.ToString());
Expand Down Expand Up @@ -671,7 +785,7 @@ public static TheoryData HiddenFor_UsesModelStateValueForComplexExpressionsData

[Theory]
[MemberData(nameof(HiddenFor_UsesModelStateValueForComplexExpressionsData))]
public void HiddenFor_UsesModelStateValueForComplexExpressions(
public void HiddenForInTemplate_UsesModelStateValueForComplexExpressions(
Expression<Func<HiddenModel, string>> expression,
string expected)
{
Expand Down Expand Up @@ -718,13 +832,20 @@ private static ViewDataDictionary<HiddenModel> GetViewDataWithNullModelAndNonNul
};
}

private static ViewDataDictionary<HiddenModel> GetViewDataWithModelStateAndModelAndViewDataValues()
private static ViewDataDictionary<HiddenModel> GetViewDataWithNonNullModel()
{
var viewData = new ViewDataDictionary<HiddenModel>(new EmptyModelMetadataProvider())
{
Model = new HiddenModel(),
["Property1"] = "view-data-val",
};

return viewData;
}

private static ViewDataDictionary<HiddenModel> GetViewDataWithModelStateAndModelAndViewDataValues()
{
var viewData = GetViewDataWithNonNullModel();
viewData["Property1"] = "view-data-val";
viewData.ModelState.Add("Property1", GetModelState("ModelStateValue"));

return viewData;
Expand Down

0 comments on commit cd138a9

Please sign in to comment.