Skip to content

Commit

Permalink
feat: Enable RenderMode cascading to the Component
Browse files Browse the repository at this point in the history
linkdotnet committed Nov 1, 2024
1 parent 05010c2 commit 1514e0c
Showing 7 changed files with 139 additions and 29 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ All notable changes to **bUnit** will be documented in this file. The project ad
## [Unreleased]

### Added
- Added support for `RendererInfo` (`.net9.0`).
- Added support for `RendererInfo` and `AssignedRenderMode` (`.net9.0`).

## [1.34.0] - 2024-11-01

21 changes: 21 additions & 0 deletions src/bunit.core/Rendering/TestRenderer.cs
Original file line number Diff line number Diff line change
@@ -241,6 +241,27 @@ protected override IComponent ResolveComponentForRenderMode(Type componentType,
}
#endif

#if NET9_0_OR_GREATER
/// <inheritdoc/>
protected override IComponentRenderMode? GetComponentRenderMode(IComponent component)
{
ArgumentNullException.ThrowIfNull(component);

var renderModeAttribute = component.GetType()
.GetCustomAttribute<RenderModeAttribute>();

if (renderModeAttribute is not null)
{
return renderModeAttribute.Mode;
}

var parentComponentState = GetComponentState(component).ParentComponentState;
return parentComponentState is not null
? GetComponentRenderMode(parentComponentState.Component)
: null;
}
#endif

/// <inheritdoc/>
internal Task SetDirectParametersAsync(IRenderedFragmentBase renderedComponent, ParameterView parameters)
{
73 changes: 73 additions & 0 deletions tests/bunit.core.tests/Rendering/RenderModeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#if NET9_0_OR_GREATER
using Bunit.TestAssets.RenderModes;

namespace Bunit.Rendering;

public class RenderModeTests : TestContext
{
[Fact(DisplayName = "TestRenderer provides RendererInfo")]
public void Test001()
{
Renderer.SetRendererInfo(new RendererInfo("Server", true));
var cut = RenderComponent<RendererInfoComponent>();

cut.MarkupMatches("""
<p>Is interactive: True</p>
<p>Rendermode: Server</p>
""");
}

[Fact(DisplayName = "Renderer throws exception if RendererInfo is not specified")]
public void Test002()
{
Action act = () => RenderComponent<RendererInfoComponent>();

act.ShouldThrow<MissingRendererInfoException>();
}

[Fact(DisplayName = "Renderer should set the RenderModeAttribute on the component")]
public void Test003()
{
var cut = RenderComponent<ComponentWithServerRenderMode>();

cut.MarkupMatches("<div>Assigned render mode: InteractiveServerRenderMode</div>");
}

[Fact(DisplayName = "The AssignedRenderMode is based on the RenderModeAttribute in the component hierarchy where parent component has no RenderMode")]
public void Test004()
{
var cut = RenderComponent<ComponentWithoutRenderMode>(
c => c.AddChildContent<ComponentWithWebAssemblyRenderMode>());

cut.MarkupMatches("""
<div>Parent assigned render mode: </div>
<div>Assigned render mode: InteractiveWebAssemblyRenderMode</div>
""");
}

[Fact(DisplayName = "Parent and child render mode is specified")]
public void Test005()
{
var cut = RenderComponent<ComponentWithWebAssemblyRenderMode>(
c => c.AddChildContent<ComponentWithServerRenderMode>());

cut.MarkupMatches("""
<div>Parent assigned render mode: InteractiveWebAssemblyRenderMode</div>
<div>Assigned render mode: InteractiveServerRenderMode</div>
""");
}

[Fact(DisplayName = "Parent and child render mode is not specified")]
public void Test006()
{
var cut = RenderComponent<ComponentWithoutRenderMode>(
c => c.AddChildContent<ComponentWithoutRenderMode>());

cut.MarkupMatches("""
<div>Parent assigned render mode: </div>
<div>Assigned render mode: </div>
""");

}
}
#endif
28 changes: 0 additions & 28 deletions tests/bunit.core.tests/Rendering/RendererInfoTests.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@{
#if NET9_0_OR_GREATER
}

@rendermode Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveServer
<div>@(ChildContent is not null ? "Parent assigned" : "Assigned") render mode: @AssignedRenderMode?.GetType().Name</div>
@ChildContent

@code {
[Parameter] public RenderFragment? ChildContent { get; set; }
}

@{
#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@{
#if NET9_0_OR_GREATER
}

@rendermode Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveWebAssembly
<div>@(ChildContent is not null ? "Parent assigned" : "Assigned") render mode: @AssignedRenderMode?.GetType().Name</div>
@ChildContent

@code {
[Parameter] public RenderFragment? ChildContent { get; set; }
}

@{
#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@{
#if NET9_0_OR_GREATER
}

<div>@(ChildContent is not null ? "Parent assigned" : "Assigned") render mode: @AssignedRenderMode?.GetType().Name</div>
@ChildContent

@code {
[Parameter] public RenderFragment? ChildContent { get; set; }
}

@{
#endif
}

0 comments on commit 1514e0c

Please sign in to comment.