|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.ComponentModel; |
| 5 | +using System.ComponentModel.DataAnnotations; |
| 6 | +using Microsoft.AspNetCore.Components.Rendering; |
| 7 | +using Microsoft.AspNetCore.Components.Test.Helpers; |
| 8 | + |
| 9 | +namespace Microsoft.AspNetCore.Components.Forms; |
| 10 | + |
| 11 | +public class DisplayNameTest |
| 12 | +{ |
| 13 | + [Fact] |
| 14 | + public async Task ThrowsIfNoForParameterProvided() |
| 15 | + { |
| 16 | + // Arrange |
| 17 | + var rootComponent = new TestHostComponent |
| 18 | + { |
| 19 | + InnerContent = builder => |
| 20 | + { |
| 21 | + builder.OpenComponent<DisplayName<string>>(0); |
| 22 | + builder.CloseComponent(); |
| 23 | + } |
| 24 | + }; |
| 25 | + |
| 26 | + var testRenderer = new TestRenderer(); |
| 27 | + var componentId = testRenderer.AssignRootComponentId(rootComponent); |
| 28 | + |
| 29 | + // Act & Assert |
| 30 | + var ex = await Assert.ThrowsAsync<InvalidOperationException>( |
| 31 | + async () => await testRenderer.RenderRootComponentAsync(componentId)); |
| 32 | + Assert.Contains("For", ex.Message); |
| 33 | + Assert.Contains("parameter", ex.Message); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public async Task DisplaysPropertyNameWhenNoAttributePresent() |
| 38 | + { |
| 39 | + // Arrange |
| 40 | + var model = new TestModel(); |
| 41 | + var rootComponent = new TestHostComponent |
| 42 | + { |
| 43 | + InnerContent = builder => |
| 44 | + { |
| 45 | + builder.OpenComponent<DisplayName<string>>(0); |
| 46 | + builder.AddComponentParameter(1, "For", (System.Linq.Expressions.Expression<Func<string>>)(() => model.PlainProperty)); |
| 47 | + builder.CloseComponent(); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + // Act |
| 52 | + var output = await RenderAndGetOutput(rootComponent); |
| 53 | + |
| 54 | + // Assert |
| 55 | + Assert.Equal("PlainProperty", output); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public async Task DisplaysDisplayAttributeName() |
| 60 | + { |
| 61 | + // Arrange |
| 62 | + var model = new TestModel(); |
| 63 | + var rootComponent = new TestHostComponent |
| 64 | + { |
| 65 | + InnerContent = builder => |
| 66 | + { |
| 67 | + builder.OpenComponent<DisplayName<string>>(0); |
| 68 | + builder.AddComponentParameter(1, "For", (System.Linq.Expressions.Expression<Func<string>>)(() => model.PropertyWithDisplayAttribute)); |
| 69 | + builder.CloseComponent(); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + // Act |
| 74 | + var output = await RenderAndGetOutput(rootComponent); |
| 75 | + |
| 76 | + // Assert |
| 77 | + Assert.Equal("Custom Display Name", output); |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public async Task DisplaysDisplayNameAttributeName() |
| 82 | + { |
| 83 | + // Arrange |
| 84 | + var model = new TestModel(); |
| 85 | + var rootComponent = new TestHostComponent |
| 86 | + { |
| 87 | + InnerContent = builder => |
| 88 | + { |
| 89 | + builder.OpenComponent<DisplayName<string>>(0); |
| 90 | + builder.AddComponentParameter(1, "For", (System.Linq.Expressions.Expression<Func<string>>)(() => model.PropertyWithDisplayNameAttribute)); |
| 91 | + builder.CloseComponent(); |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + // Act |
| 96 | + var output = await RenderAndGetOutput(rootComponent); |
| 97 | + |
| 98 | + // Assert |
| 99 | + Assert.Equal("Custom DisplayName", output); |
| 100 | + } |
| 101 | + |
| 102 | + [Fact] |
| 103 | + public async Task DisplayAttributeTakesPrecedenceOverDisplayNameAttribute() |
| 104 | + { |
| 105 | + // Arrange |
| 106 | + var model = new TestModel(); |
| 107 | + var rootComponent = new TestHostComponent |
| 108 | + { |
| 109 | + InnerContent = builder => |
| 110 | + { |
| 111 | + builder.OpenComponent<DisplayName<string>>(0); |
| 112 | + builder.AddComponentParameter(1, "For", (System.Linq.Expressions.Expression<Func<string>>)(() => model.PropertyWithBothAttributes)); |
| 113 | + builder.CloseComponent(); |
| 114 | + } |
| 115 | + }; |
| 116 | + |
| 117 | + // Act |
| 118 | + var output = await RenderAndGetOutput(rootComponent); |
| 119 | + |
| 120 | + // Assert |
| 121 | + // DisplayAttribute should take precedence per MVC conventions |
| 122 | + Assert.Equal("Display Takes Precedence", output); |
| 123 | + } |
| 124 | + |
| 125 | + [Fact] |
| 126 | + public async Task WorksWithDifferentPropertyTypes() |
| 127 | + { |
| 128 | + // Arrange |
| 129 | + var model = new TestModel(); |
| 130 | + var intComponent = new TestHostComponent |
| 131 | + { |
| 132 | + InnerContent = builder => |
| 133 | + { |
| 134 | + builder.OpenComponent<DisplayName<int>>(0); |
| 135 | + builder.AddComponentParameter(1, "For", (System.Linq.Expressions.Expression<Func<int>>)(() => model.IntProperty)); |
| 136 | + builder.CloseComponent(); |
| 137 | + } |
| 138 | + }; |
| 139 | + var dateComponent = new TestHostComponent |
| 140 | + { |
| 141 | + InnerContent = builder => |
| 142 | + { |
| 143 | + builder.OpenComponent<DisplayName<DateTime>>(0); |
| 144 | + builder.AddComponentParameter(1, "For", (System.Linq.Expressions.Expression<Func<DateTime>>)(() => model.DateProperty)); |
| 145 | + builder.CloseComponent(); |
| 146 | + } |
| 147 | + }; |
| 148 | + |
| 149 | + // Act |
| 150 | + var intOutput = await RenderAndGetOutput(intComponent); |
| 151 | + var dateOutput = await RenderAndGetOutput(dateComponent); |
| 152 | + |
| 153 | + // Assert |
| 154 | + Assert.Equal("Integer Value", intOutput); |
| 155 | + Assert.Equal("Date Value", dateOutput); |
| 156 | + } |
| 157 | + |
| 158 | + [Fact] |
| 159 | + public async Task SupportsLocalizationWithResourceType() |
| 160 | + { |
| 161 | + var model = new TestModel(); |
| 162 | + var rootComponent = new TestHostComponent |
| 163 | + { |
| 164 | + InnerContent = builder => |
| 165 | + { |
| 166 | + builder.OpenComponent<DisplayName<string>>(0); |
| 167 | + builder.AddComponentParameter(1, "For", (System.Linq.Expressions.Expression<Func<string>>)(() => model.PropertyWithResourceBasedDisplay)); |
| 168 | + builder.CloseComponent(); |
| 169 | + } |
| 170 | + }; |
| 171 | + |
| 172 | + var output = await RenderAndGetOutput(rootComponent); |
| 173 | + Assert.Equal("Localized Display Name", output); |
| 174 | + } |
| 175 | + |
| 176 | + private static async Task<string> RenderAndGetOutput(TestHostComponent rootComponent) |
| 177 | + { |
| 178 | + var testRenderer = new TestRenderer(); |
| 179 | + var componentId = testRenderer.AssignRootComponentId(rootComponent); |
| 180 | + await testRenderer.RenderRootComponentAsync(componentId); |
| 181 | + |
| 182 | + var batch = testRenderer.Batches.Single(); |
| 183 | + var displayLabelComponentFrame = batch.ReferenceFrames |
| 184 | + .First(f => f.FrameType == RenderTree.RenderTreeFrameType.Component && |
| 185 | + f.Component is DisplayName<string> or DisplayName<int> or DisplayName<DateTime>); |
| 186 | + |
| 187 | + // Find the text content frame within the component |
| 188 | + var textFrame = batch.ReferenceFrames |
| 189 | + .First(f => f.FrameType == RenderTree.RenderTreeFrameType.Text); |
| 190 | + |
| 191 | + return textFrame.TextContent; |
| 192 | + } |
| 193 | + |
| 194 | + private class TestHostComponent : ComponentBase |
| 195 | + { |
| 196 | + public RenderFragment InnerContent { get; set; } |
| 197 | + |
| 198 | + protected override void BuildRenderTree(RenderTreeBuilder builder) |
| 199 | + { |
| 200 | + InnerContent(builder); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + private class TestModel |
| 205 | + { |
| 206 | + public string PlainProperty { get; set; } = string.Empty; |
| 207 | + |
| 208 | + [Display(Name = "Custom Display Name")] |
| 209 | + public string PropertyWithDisplayAttribute { get; set; } = string.Empty; |
| 210 | + |
| 211 | + [DisplayName("Custom DisplayName")] |
| 212 | + public string PropertyWithDisplayNameAttribute { get; set; } = string.Empty; |
| 213 | + |
| 214 | + [Display(Name = "Display Takes Precedence")] |
| 215 | + [DisplayName("This Should Not Be Used")] |
| 216 | + public string PropertyWithBothAttributes { get; set; } = string.Empty; |
| 217 | + |
| 218 | + [Display(Name = "Integer Value")] |
| 219 | + public int IntProperty { get; set; } |
| 220 | + |
| 221 | + [Display(Name = "Date Value")] |
| 222 | + public DateTime DateProperty { get; set; } |
| 223 | + |
| 224 | + [Display(Name = nameof(TestResources.LocalizedDisplayName), ResourceType = typeof(TestResources))] |
| 225 | + public string PropertyWithResourceBasedDisplay { get; set; } = string.Empty; |
| 226 | + } |
| 227 | + |
| 228 | + public static class TestResources |
| 229 | + { |
| 230 | + public static string LocalizedDisplayName => "Localized Display Name"; |
| 231 | + } |
| 232 | +} |
0 commit comments