Skip to content

Enable input number component to support type='range' attribute #55583

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
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
ca712c1
Commit modifies the InputNumber component to allow the 'type' attribu…
MattyLeslie May 7, 2024
f9ceb16
Adding a test to the inputNumberTests to ensure the user defined type…
MattyLeslie May 7, 2024
525b346
Merge branch 'dotnet:main' into Enable-InputNumber-Component-to-Suppo…
MattyLeslie May 7, 2024
bfa7733
Using the TestRederer to extract attributes and assert the user-defin…
MattyLeslie May 7, 2024
ce3d8c0
Merge branch 'Enable-InputNumber-Component-to-Support-type='range'-At…
MattyLeslie May 7, 2024
8df498d
Retrieving frames with new methodology
MattyLeslie May 8, 2024
c7a76af
Isolating the correct element to extract attributes from
MattyLeslie May 9, 2024
ac09491
Only asserting the type attribute = range
MattyLeslie May 9, 2024
dd6b779
Isolating input element, atrributes and type attribute correctly
MattyLeslie May 14, 2024
7b2ee01
Improving methodology of handling render tree frames and isolating th…
MattyLeslie May 15, 2024
ebf82de
Ammendments to tests
MattyLeslie May 20, 2024
af90057
Merge branch 'dotnet:main' into Enable-InputNumber-Component-to-Suppo…
MattyLeslie May 28, 2024
d1f916e
Merge branch 'dotnet:main' into Enable-InputNumber-Component-to-Suppo…
MattyLeslie May 30, 2024
b1ab8e0
Update src/Components/Web/test/Forms/InputNumberTest.cs
MattyLeslie Jul 26, 2024
bf70742
Update src/Components/Web/test/Forms/InputNumberTest.cs
MattyLeslie Jul 26, 2024
abfb3e6
Update src/Components/Web/test/Forms/InputNumberTest.cs
MattyLeslie Jul 26, 2024
11aa774
re-adding missing tests
MattyLeslie Jul 26, 2024
6e7ca94
Using helper methods in tests.
MattyLeslie Jul 26, 2024
c717209
Update src/Components/Web/test/Forms/InputNumberTest.cs
MackinnonBuck Jul 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Components/Web/src/Forms/InputNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenElement(0, "input");
builder.AddAttribute(1, "step", _stepAttributeValue);
builder.AddMultipleAttributes(2, AdditionalAttributes);
builder.AddAttribute(3, "type", "number");
builder.AddAttribute(2, "type", "number");
builder.AddMultipleAttributes(3, AdditionalAttributes);
builder.AddAttributeIfNotNullOrEmpty(4, "name", NameAttributeValue);
builder.AddAttributeIfNotNullOrEmpty(5, "class", CssClass);
builder.AddAttribute(6, "value", CurrentValueAsString);
Expand Down
91 changes: 65 additions & 26 deletions src/Components/Web/test/Forms/InputNumberTest.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,93 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Components.Forms.Mapping;
using Microsoft.AspNetCore.Components.Infrastructure;
using Microsoft.AspNetCore.Components.RenderTree;
using Microsoft.AspNetCore.Components.Test.Helpers;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.AspNetCore.Components.Forms;

public class InputNumberTest
public class InputNumberTests
{
private TestRenderer _testRenderer;

public InputNumberTests()
{
var services = new ServiceCollection();
services.AddLogging();
_testRenderer = new TestRenderer(services.BuildServiceProvider());
}

[Fact]
public async Task ValidationErrorUsesDisplayAttributeName()
public async Task UserDefinedTypeAttributeOverridesDefault()
{
// Arrange
var model = new TestModel();
var rootComponent = new TestInputHostComponent<int, TestInputNumberComponent>
var hostComponent = new TestInputHostComponent<int, TestInputNumberComponent>
{
EditContext = new EditContext(model),
ValueExpression = () => model.SomeNumber,
AdditionalAttributes = new Dictionary<string, object>
{
{ "DisplayName", "Some number" }
}
{
{ "type", "range" } // User-defined 'type' attribute to override default
}
};
var fieldIdentifier = FieldIdentifier.Create(() => model.SomeNumber);
var inputComponent = await InputRenderer.RenderAndGetComponent(rootComponent);

// Act
await inputComponent.SetCurrentValueAsStringAsync("notANumber");
var componentId = await RenderAndGetTestInputNumberComponentIdAsync(hostComponent);

// Assert
var validationMessages = rootComponent.EditContext.GetValidationMessages(fieldIdentifier);
Assert.NotEmpty(validationMessages);
Assert.Contains("The Some number field must be a number.", validationMessages);
}
// Retrieve the render tree frames and extract attributes
var frames = _testRenderer.GetCurrentRenderTreeFrames(componentId);

[Fact]
public async Task InputElementIsAssignedSuccessfully()
{
// Arrange
var model = new TestModel();
var rootComponent = new TestInputHostComponent<int, TestInputNumberComponent>
// Debugging: Output the frames for inspection
foreach (var frame in frames.Array)
{
EditContext = new EditContext(model),
ValueExpression = () => model.SomeNumber,
};
Console.WriteLine($"Frame: {frame.FrameType}, {frame.ElementName}, {frame.AttributeName}, {frame.AttributeValue}");
}

// Act
var inputSelectComponent = await InputRenderer.RenderAndGetComponent(rootComponent);
bool inputElementFound = false;
var attributes = new Dictionary<string, object>();

for (int i = 0; i < frames.Count; i++)
{
var frame = frames.Array[i];
if (frame.FrameType == RenderTreeFrameType.Element && frame.ElementName == "input")
{
inputElementFound = true;
for (int j = i + 1; j < frames.Count; j++)
{
var attributeFrame = frames.Array[j];
if (attributeFrame.FrameType != RenderTreeFrameType.Attribute)
{
break;
}
attributes[attributeFrame.AttributeName] = attributeFrame.AttributeValue;
}
break;
}
}

// Assert
Assert.NotNull(inputSelectComponent.Element);
Assert.True(inputElementFound, "Input element was not found.");
Assert.True(attributes.ContainsKey("type"), "Type attribute was not found.");
Assert.Equal("range", attributes["type"]);
}

private async Task<int> RenderAndGetTestInputNumberComponentIdAsync(TestInputHostComponent<int, TestInputNumberComponent> hostComponent)
{
var componentId = _testRenderer.AssignRootComponentId(hostComponent);
await _testRenderer.RenderRootComponentAsync(componentId);
return FindTestInputNumberComponentId(_testRenderer.Batches.Single(), typeof(TestInputNumberComponent));
}

private static int FindTestInputNumberComponentId(CapturedBatch batch, Type componentType)
=> batch.ReferenceFrames
.Where(f => f.FrameType == RenderTreeFrameType.Component && f.Component.GetType() == componentType)
.Select(f => f.ComponentId)
.Single();

private class TestModel
{
public int SomeNumber { get; set; }
Expand Down