|
| 1 | +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.AspNet.Mvc.ModelBinding; |
| 8 | +using Microsoft.AspNet.Mvc.Razor; |
| 9 | +using Microsoft.AspNet.Mvc.Rendering; |
| 10 | +using Microsoft.AspNet.Razor.Runtime.TagHelpers; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace Microsoft.AspNet.Mvc.TagHelpers |
| 14 | +{ |
| 15 | + public class TextAreaTagHelperTest |
| 16 | + { |
| 17 | + // Model (List<Model> or Model instance), container type (Model or NestModel), model accessor, |
| 18 | + // property path, expected content. |
| 19 | + public static TheoryData<object, Type, Func<object>, string, string> TestDataSet |
| 20 | + { |
| 21 | + get |
| 22 | + { |
| 23 | + var modelWithNull = new Model |
| 24 | + { |
| 25 | + NestedModel = new NestedModel |
| 26 | + { |
| 27 | + Text = null, |
| 28 | + }, |
| 29 | + Text = null, |
| 30 | + }; |
| 31 | + var modelWithText = new Model |
| 32 | + { |
| 33 | + NestedModel = new NestedModel |
| 34 | + { |
| 35 | + Text = "inner text", |
| 36 | + }, |
| 37 | + Text = "outer text", |
| 38 | + }; |
| 39 | + var models = new List<Model> |
| 40 | + { |
| 41 | + modelWithNull, |
| 42 | + modelWithText, |
| 43 | + }; |
| 44 | + |
| 45 | + return new TheoryData<object, Type, Func<object>, string, string> |
| 46 | + { |
| 47 | + { null, typeof(Model), () => null, "Text", |
| 48 | + Environment.NewLine }, |
| 49 | + |
| 50 | + { modelWithNull, typeof(Model), () => modelWithNull.Text, "Text", |
| 51 | + Environment.NewLine }, |
| 52 | + { modelWithText, typeof(Model), () => modelWithText.Text, "Text", |
| 53 | + Environment.NewLine + "outer text" }, |
| 54 | + |
| 55 | + { modelWithNull, typeof(NestedModel), () => modelWithNull.NestedModel.Text, "NestedModel.Text", |
| 56 | + Environment.NewLine }, |
| 57 | + { modelWithText, typeof(NestedModel), () => modelWithText.NestedModel.Text, "NestedModel.Text", |
| 58 | + Environment.NewLine + "inner text" }, |
| 59 | + |
| 60 | + // Top-level indexing does not work end-to-end due to code generation issue #1345. |
| 61 | + // TODO: Remove above comment when #1345 is fixed. |
| 62 | + { models, typeof(Model), () => models[0].Text, "[0].Text", |
| 63 | + Environment.NewLine }, |
| 64 | + { models, typeof(Model), () => models[1].Text, "[1].Text", |
| 65 | + Environment.NewLine + "outer text" }, |
| 66 | + |
| 67 | + { models, typeof(NestedModel), () => models[0].NestedModel.Text, "[0].NestedModel.Text", |
| 68 | + Environment.NewLine }, |
| 69 | + { models, typeof(NestedModel), () => models[1].NestedModel.Text, "[1].NestedModel.Text", |
| 70 | + Environment.NewLine + "inner text" }, |
| 71 | + }; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + [Theory] |
| 76 | + [MemberData(nameof(TestDataSet))] |
| 77 | + public async Task Process_GeneratesExpectedOutput( |
| 78 | + object model, |
| 79 | + Type containerType, |
| 80 | + Func<object> modelAccessor, |
| 81 | + string propertyPath, |
| 82 | + string expectedContent) |
| 83 | + { |
| 84 | + // Arrange |
| 85 | + var expectedAttributes = new Dictionary<string, string> |
| 86 | + { |
| 87 | + { "class", "form-control" }, |
| 88 | + { "id", propertyPath }, |
| 89 | + { "name", propertyPath }, |
| 90 | + { "valid", "from validation attributes" }, |
| 91 | + }; |
| 92 | + var expectedTagName = "textarea"; |
| 93 | + |
| 94 | + var metadataProvider = new DataAnnotationsModelMetadataProvider(); |
| 95 | + |
| 96 | + // Property name is either nameof(Model.Text) or nameof(NestedModel.Text). |
| 97 | + var metadata = metadataProvider.GetMetadataForProperty(modelAccessor, containerType, propertyName: "Text"); |
| 98 | + var modelExpression = new ModelExpression(propertyPath, metadata); |
| 99 | + var tagHelper = new TextAreaTagHelper |
| 100 | + { |
| 101 | + For = modelExpression, |
| 102 | + }; |
| 103 | + |
| 104 | + var tagHelperContext = new TagHelperContext(new Dictionary<string, object>()); |
| 105 | + var htmlAttributes = new Dictionary<string, string> |
| 106 | + { |
| 107 | + { "class", "form-control" }, |
| 108 | + }; |
| 109 | + var output = new TagHelperOutput("original tag name", htmlAttributes, "original content") |
| 110 | + { |
| 111 | + SelfClosing = true, |
| 112 | + }; |
| 113 | + |
| 114 | + var htmlGenerator = new TestableHtmlGenerator(metadataProvider) |
| 115 | + { |
| 116 | + ValidationAttributes = |
| 117 | + { |
| 118 | + { "valid", "from validation attributes" }, |
| 119 | + } |
| 120 | + }; |
| 121 | + var viewContext = TestableHtmlGenerator.GetViewContext(model, htmlGenerator, metadataProvider); |
| 122 | + var activator = new DefaultTagHelperActivator(); |
| 123 | + activator.Activate(tagHelper, viewContext); |
| 124 | + |
| 125 | + // Act |
| 126 | + await tagHelper.ProcessAsync(tagHelperContext, output); |
| 127 | + |
| 128 | + // Assert |
| 129 | + Assert.Equal(expectedAttributes, output.Attributes); |
| 130 | + Assert.Equal(expectedContent, output.Content); |
| 131 | + Assert.False(output.SelfClosing); |
| 132 | + Assert.Equal(expectedTagName, output.TagName); |
| 133 | + } |
| 134 | + |
| 135 | + [Fact] |
| 136 | + public async Task TagHelper_LeavesOutputUnchanged_IfForNotBound() |
| 137 | + { |
| 138 | + // Arrange |
| 139 | + var expectedAttributes = new Dictionary<string, string> |
| 140 | + { |
| 141 | + { "class", "form-control" }, |
| 142 | + }; |
| 143 | + var expectedContent = "original content"; |
| 144 | + var expectedTagName = "original tag name"; |
| 145 | + |
| 146 | + var metadataProvider = new DataAnnotationsModelMetadataProvider(); |
| 147 | + var metadata = metadataProvider.GetMetadataForProperty( |
| 148 | + modelAccessor: () => null, |
| 149 | + containerType: typeof(Model), |
| 150 | + propertyName: nameof(Model.Text)); |
| 151 | + var modelExpression = new ModelExpression(nameof(Model.Text), metadata); |
| 152 | + var tagHelper = new TextAreaTagHelper(); |
| 153 | + |
| 154 | + var tagHelperContext = new TagHelperContext(new Dictionary<string, object>()); |
| 155 | + var output = new TagHelperOutput(expectedTagName, expectedAttributes, expectedContent) |
| 156 | + { |
| 157 | + SelfClosing = true, |
| 158 | + }; |
| 159 | + |
| 160 | + var htmlGenerator = new TestableHtmlGenerator(metadataProvider) |
| 161 | + { |
| 162 | + ValidationAttributes = |
| 163 | + { |
| 164 | + { "valid", "from validation attributes" }, |
| 165 | + } |
| 166 | + }; |
| 167 | + Model model = null; |
| 168 | + var viewContext = TestableHtmlGenerator.GetViewContext(model, htmlGenerator, metadataProvider); |
| 169 | + var activator = new DefaultTagHelperActivator(); |
| 170 | + activator.Activate(tagHelper, viewContext); |
| 171 | + |
| 172 | + // Act |
| 173 | + await tagHelper.ProcessAsync(tagHelperContext, output); |
| 174 | + |
| 175 | + // Assert |
| 176 | + Assert.Equal(expectedAttributes, output.Attributes); |
| 177 | + Assert.Equal(expectedContent, output.Content); |
| 178 | + Assert.True(output.SelfClosing); |
| 179 | + Assert.Equal(expectedTagName, output.TagName); |
| 180 | + } |
| 181 | + |
| 182 | + private class Model |
| 183 | + { |
| 184 | + public string Text { get; set; } |
| 185 | + |
| 186 | + public NestedModel NestedModel { get; set; } |
| 187 | + } |
| 188 | + |
| 189 | + private class NestedModel |
| 190 | + { |
| 191 | + public string Text { get; set; } |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments