-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add DisplayName to inputs #24029
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add DisplayName to inputs #24029
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
390eb4c
Add DisplayName
zHaytam 76f771a
Add InputNumber and InputDate tests
zHaytam 9b10b96
Add InputSelect validation test
zHaytam 0ed2a54
Refactor code to use InputRenderer and shared TestInputHostComponent
zHaytam d9667a9
Apply suggestions from code review
zHaytam 614b57a
Add license and DisplayName usage
zHaytam 0f3bb43
Regenerate reference source
zHaytam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System; | ||
zHaytam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.AspNetCore.Components.Forms | ||
| { | ||
| public class InputDateTest | ||
| { | ||
| [Fact] | ||
| public async Task ValidationErrorUsesDisplayAttributeName() | ||
| { | ||
| // Arrange | ||
| var model = new TestModel(); | ||
| var rootComponent = new TestInputHostComponent<DateTime, TestInputDateComponent> | ||
| { | ||
| EditContext = new EditContext(model), | ||
| ValueExpression = () => model.DateProperty, | ||
| AdditionalAttributes = new Dictionary<string, object> | ||
| { | ||
| { "DisplayName", "Date property" } | ||
| } | ||
| }; | ||
| var fieldIdentifier = FieldIdentifier.Create(() => model.DateProperty); | ||
| var inputComponent = await InputRenderer.RenderAndGetComponent(rootComponent); | ||
|
|
||
| // Act | ||
| await inputComponent.SetCurrentValueAsStringAsync("invalidDate"); | ||
|
|
||
| // Assert | ||
| var validationMessages = rootComponent.EditContext.GetValidationMessages(fieldIdentifier); | ||
| Assert.NotEmpty(validationMessages); | ||
| Assert.Contains("The Date property field must be a date.", validationMessages); | ||
| } | ||
|
|
||
| private class TestModel | ||
| { | ||
| public DateTime DateProperty { get; set; } | ||
| } | ||
|
|
||
| private class TestInputDateComponent : InputDate<DateTime> | ||
| { | ||
| public async Task SetCurrentValueAsStringAsync(string value) | ||
| { | ||
| // This is equivalent to the subclass writing to CurrentValueAsString | ||
| // (e.g., from @bind), except to simplify the test code there's an InvokeAsync | ||
| // here. In production code it wouldn't normally be required because @bind | ||
| // calls run on the sync context anyway. | ||
| await InvokeAsync(() => { base.CurrentValueAsString = value; }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System.Collections.Generic; | ||
zHaytam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.AspNetCore.Components.Forms | ||
| { | ||
| public class InputNumberTest | ||
| { | ||
| [Fact] | ||
| public async Task ValidationErrorUsesDisplayAttributeName() | ||
| { | ||
| // Arrange | ||
| var model = new TestModel(); | ||
| var rootComponent = new TestInputHostComponent<int, TestInputNumberComponent> | ||
| { | ||
| EditContext = new EditContext(model), | ||
| ValueExpression = () => model.SomeNumber, | ||
| AdditionalAttributes = new Dictionary<string, object> | ||
| { | ||
| { "DisplayName", "Some number" } | ||
| } | ||
| }; | ||
| var fieldIdentifier = FieldIdentifier.Create(() => model.SomeNumber); | ||
| var inputComponent = await InputRenderer.RenderAndGetComponent(rootComponent); | ||
|
|
||
| // Act | ||
| await inputComponent.SetCurrentValueAsStringAsync("notANumber"); | ||
|
|
||
| // Assert | ||
| var validationMessages = rootComponent.EditContext.GetValidationMessages(fieldIdentifier); | ||
| Assert.NotEmpty(validationMessages); | ||
| Assert.Contains("The Some number field must be a number.", validationMessages); | ||
| } | ||
|
|
||
| private class TestModel | ||
| { | ||
| public int SomeNumber { get; set; } | ||
| } | ||
|
|
||
| private class TestInputNumberComponent : InputNumber<int> | ||
| { | ||
| public async Task SetCurrentValueAsStringAsync(string value) | ||
| { | ||
| // This is equivalent to the subclass writing to CurrentValueAsString | ||
| // (e.g., from @bind), except to simplify the test code there's an InvokeAsync | ||
| // here. In production code it wouldn't normally be required because @bind | ||
| // calls run on the sync context anyway. | ||
| await InvokeAsync(() => { base.CurrentValueAsString = value; }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System.Linq; | ||
zHaytam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Components.RenderTree; | ||
| using Microsoft.AspNetCore.Components.Test.Helpers; | ||
|
|
||
| namespace Microsoft.AspNetCore.Components.Forms | ||
| { | ||
| internal static class InputRenderer | ||
| { | ||
| public static async Task<TComponent> RenderAndGetComponent<TValue, TComponent>(TestInputHostComponent<TValue, TComponent> hostComponent) | ||
| where TComponent : InputBase<TValue> | ||
| { | ||
| var testRenderer = new TestRenderer(); | ||
| var componentId = testRenderer.AssignRootComponentId(hostComponent); | ||
| await testRenderer.RenderRootComponentAsync(componentId); | ||
| return FindComponent<TComponent>(testRenderer.Batches.Single()); | ||
| } | ||
|
|
||
| private static TComponent FindComponent<TComponent>(CapturedBatch batch) | ||
| => batch.ReferenceFrames | ||
| .Where(f => f.FrameType == RenderTreeFrameType.Component) | ||
| .Select(f => f.Component) | ||
| .OfType<TComponent>() | ||
| .Single(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.