Add test to demonstrate current component attribute parsing inconsistencies

This commit is contained in:
Steve Sanderson 2018-02-20 15:28:56 +00:00
parent 78a19c07e9
commit 87fc15cc23
2 changed files with 42 additions and 0 deletions

View File

@ -374,6 +374,40 @@ namespace Microsoft.AspNetCore.Blazor.Build.Test
frame => AssertFrame.Component<TestComponent>(frame, 0));
}
[Fact]
public void CanPassParametersToComponents()
{
// Arrange/Act
var testComponentTypeName = typeof(TestComponent).FullName.Replace('+', '.');
var testObjectTypeName = typeof(SomeType).FullName.Replace('+', '.');
var component = CompileToComponent($"<c:{testComponentTypeName}" +
$" IntProperty=123" +
$" StringProperty=\"My string\"" +
$" ObjectProperty=@(new {testObjectTypeName}()) />");
var frames = GetRenderTree(component);
// Assert
// TODO: Fix this.
// * Currently the attribute names are lowercased if they were
// parsed by AngleSharp as HTML, and left in their original case if they
// were parsed by the Razor compiler as a C# expression. They should all
// retain their original case when the target element represents a component.
// * Similarly, unquoted values are interpreted as strings if they were parsed
// by AngleSharp (e.g., intproperty=123 passes a string). The values should
// always be treated as C# expressions if the target represents a component.
// This problem will probably go away on its own when we have new component
// tooling.
Assert.Collection(frames,
frame => AssertFrame.Component<TestComponent>(frame, 0),
frame => AssertFrame.Attribute(frame, "intproperty", "123", 1),
frame => AssertFrame.Attribute(frame, "stringproperty", "My string", 2),
frame =>
{
AssertFrame.Attribute(frame, "ObjectProperty");
Assert.IsType<SomeType>(frame.AttributeValue);
});
}
[Fact]
public void ComponentsDoNotHaveLayoutAttributeByDefault()
{
@ -602,5 +636,7 @@ namespace Microsoft.AspNetCore.Blazor.Build.Test
public interface ITestInterface { }
public class TestBaseClass : BlazorComponent { }
public class SomeType { }
}
}

View File

@ -52,6 +52,12 @@ namespace Microsoft.AspNetCore.Blazor.Test.Helpers
Assert.Equal(attributeEventHandlerValue, frame.AttributeValue);
}
public static void Attribute(RenderTreeFrame frame, string attributeName, object attributeValue, int? sequence = null)
{
AssertFrame.Attribute(frame, attributeName, sequence);
Assert.Equal(attributeValue, frame.AttributeValue);
}
public static void Component<T>(RenderTreeFrame frame, int? sequence = null) where T : IComponent
{
Assert.Equal(RenderTreeFrameType.Component, frame.FrameType);