|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
| 4 | +using Microsoft.AspNetCore.Components.Forms.Mapping; |
| 5 | +using Microsoft.AspNetCore.Components.Infrastructure; |
| 6 | +using Microsoft.AspNetCore.Components.RenderTree; |
| 7 | +using Microsoft.AspNetCore.Components.Test.Helpers; |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | + |
4 | 10 | namespace Microsoft.AspNetCore.Components.Forms;
|
5 | 11 |
|
6 | 12 | public class InputNumberTest
|
7 | 13 | {
|
| 14 | + private readonly TestRenderer _testRenderer; |
| 15 | + |
| 16 | + public InputNumberTest() |
| 17 | + { |
| 18 | + var services = new ServiceCollection(); |
| 19 | + services.AddLogging(); |
| 20 | + _testRenderer = new TestRenderer(services.BuildServiceProvider()); |
| 21 | + } |
| 22 | + |
8 | 23 | [Fact]
|
9 | 24 | public async Task ValidationErrorUsesDisplayAttributeName()
|
10 | 25 | {
|
@@ -49,6 +64,43 @@ public async Task InputElementIsAssignedSuccessfully()
|
49 | 64 | Assert.NotNull(inputSelectComponent.Element);
|
50 | 65 | }
|
51 | 66 |
|
| 67 | + [Fact] |
| 68 | + public async Task UserDefinedTypeAttributeOverridesDefault() |
| 69 | + { |
| 70 | + // Arrange |
| 71 | + var model = new TestModel(); |
| 72 | + var hostComponent = new TestInputHostComponent<int, TestInputNumberComponent> |
| 73 | + { |
| 74 | + EditContext = new EditContext(model), |
| 75 | + ValueExpression = () => model.SomeNumber, |
| 76 | + AdditionalAttributes = new Dictionary<string, object> |
| 77 | + { |
| 78 | + { "type", "range" } // User-defined 'type' attribute to override default |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + // Act |
| 83 | + var componentId = await RenderAndGetTestInputNumberComponentIdAsync(hostComponent); |
| 84 | + |
| 85 | + // Retrieve the render tree frames and extract attributes using helper methods |
| 86 | + var frames = _testRenderer.GetCurrentRenderTreeFrames(componentId); |
| 87 | + |
| 88 | + var typeAttributeFrame = frames.Array.Single(frame => |
| 89 | + frame.FrameType == RenderTreeFrameType.Attribute && |
| 90 | + frame.AttributeName == "type"); |
| 91 | + |
| 92 | + // Assert |
| 93 | + Assert.Equal("range", typeAttributeFrame.AttributeValue); |
| 94 | + } |
| 95 | + |
| 96 | + private async Task<int> RenderAndGetTestInputNumberComponentIdAsync(TestInputHostComponent<int, TestInputNumberComponent> hostComponent) |
| 97 | + { |
| 98 | + var hostComponentId = _testRenderer.AssignRootComponentId(hostComponent); |
| 99 | + await _testRenderer.RenderRootComponentAsync(hostComponentId); |
| 100 | + var batch = _testRenderer.Batches.Single(); |
| 101 | + return batch.GetComponentFrames<TestInputNumberComponent>().Single().ComponentId; |
| 102 | + } |
| 103 | + |
52 | 104 | private class TestModel
|
53 | 105 | {
|
54 | 106 | public int SomeNumber { get; set; }
|
|
0 commit comments