Skip to content

Commit b5bcbd2

Browse files
linkdotnetegil
authored andcommitted
feat: Enable RenderMode cascading to the Component
1 parent 3111b1d commit b5bcbd2

File tree

7 files changed

+139
-29
lines changed

7 files changed

+139
-29
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ All notable changes to **bUnit** will be documented in this file. The project ad
77
## [Unreleased]
88

99
### Added
10-
- Added support for `RendererInfo` (`.net9.0`).
10+
- Added support for `RendererInfo` and `AssignedRenderMode` (`.net9.0`).
1111

1212
## [1.36.0] - 2024-11-12
1313

src/bunit.core/Rendering/TestRenderer.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,27 @@ protected override IComponent ResolveComponentForRenderMode(Type componentType,
241241
}
242242
#endif
243243

244+
#if NET9_0_OR_GREATER
245+
/// <inheritdoc/>
246+
protected override IComponentRenderMode? GetComponentRenderMode(IComponent component)
247+
{
248+
ArgumentNullException.ThrowIfNull(component);
249+
250+
var renderModeAttribute = component.GetType()
251+
.GetCustomAttribute<RenderModeAttribute>();
252+
253+
if (renderModeAttribute is not null)
254+
{
255+
return renderModeAttribute.Mode;
256+
}
257+
258+
var parentComponentState = GetComponentState(component).ParentComponentState;
259+
return parentComponentState is not null
260+
? GetComponentRenderMode(parentComponentState.Component)
261+
: null;
262+
}
263+
#endif
264+
244265
/// <inheritdoc/>
245266
internal Task SetDirectParametersAsync(IRenderedFragmentBase renderedComponent, ParameterView parameters)
246267
{
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#if NET9_0_OR_GREATER
2+
using Bunit.TestAssets.RenderModes;
3+
4+
namespace Bunit.Rendering;
5+
6+
public class RenderModeTests : TestContext
7+
{
8+
[Fact(DisplayName = "TestRenderer provides RendererInfo")]
9+
public void Test001()
10+
{
11+
Renderer.SetRendererInfo(new RendererInfo("Server", true));
12+
var cut = RenderComponent<RendererInfoComponent>();
13+
14+
cut.MarkupMatches("""
15+
<p>Is interactive: True</p>
16+
<p>Rendermode: Server</p>
17+
""");
18+
}
19+
20+
[Fact(DisplayName = "Renderer throws exception if RendererInfo is not specified")]
21+
public void Test002()
22+
{
23+
Action act = () => RenderComponent<RendererInfoComponent>();
24+
25+
act.ShouldThrow<MissingRendererInfoException>();
26+
}
27+
28+
[Fact(DisplayName = "Renderer should set the RenderModeAttribute on the component")]
29+
public void Test003()
30+
{
31+
var cut = RenderComponent<ComponentWithServerRenderMode>();
32+
33+
cut.MarkupMatches("<div>Assigned render mode: InteractiveServerRenderMode</div>");
34+
}
35+
36+
[Fact(DisplayName = "The AssignedRenderMode is based on the RenderModeAttribute in the component hierarchy where parent component has no RenderMode")]
37+
public void Test004()
38+
{
39+
var cut = RenderComponent<ComponentWithoutRenderMode>(
40+
c => c.AddChildContent<ComponentWithWebAssemblyRenderMode>());
41+
42+
cut.MarkupMatches("""
43+
<div>Parent assigned render mode: </div>
44+
<div>Assigned render mode: InteractiveWebAssemblyRenderMode</div>
45+
""");
46+
}
47+
48+
[Fact(DisplayName = "Parent and child render mode is specified")]
49+
public void Test005()
50+
{
51+
var cut = RenderComponent<ComponentWithWebAssemblyRenderMode>(
52+
c => c.AddChildContent<ComponentWithServerRenderMode>());
53+
54+
cut.MarkupMatches("""
55+
<div>Parent assigned render mode: InteractiveWebAssemblyRenderMode</div>
56+
<div>Assigned render mode: InteractiveServerRenderMode</div>
57+
""");
58+
}
59+
60+
[Fact(DisplayName = "Parent and child render mode is not specified")]
61+
public void Test006()
62+
{
63+
var cut = RenderComponent<ComponentWithoutRenderMode>(
64+
c => c.AddChildContent<ComponentWithoutRenderMode>());
65+
66+
cut.MarkupMatches("""
67+
<div>Parent assigned render mode: </div>
68+
<div>Assigned render mode: </div>
69+
""");
70+
71+
}
72+
}
73+
#endif

tests/bunit.core.tests/Rendering/RendererInfoTests.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@{
2+
#if NET9_0_OR_GREATER
3+
}
4+
5+
@rendermode Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveServer
6+
<div>@(ChildContent is not null ? "Parent assigned" : "Assigned") render mode: @AssignedRenderMode?.GetType().Name</div>
7+
@ChildContent
8+
9+
@code {
10+
[Parameter] public RenderFragment? ChildContent { get; set; }
11+
}
12+
13+
@{
14+
#endif
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@{
2+
#if NET9_0_OR_GREATER
3+
}
4+
5+
@rendermode Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveWebAssembly
6+
<div>@(ChildContent is not null ? "Parent assigned" : "Assigned") render mode: @AssignedRenderMode?.GetType().Name</div>
7+
@ChildContent
8+
9+
@code {
10+
[Parameter] public RenderFragment? ChildContent { get; set; }
11+
}
12+
13+
@{
14+
#endif
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@{
2+
#if NET9_0_OR_GREATER
3+
}
4+
5+
<div>@(ChildContent is not null ? "Parent assigned" : "Assigned") render mode: @AssignedRenderMode?.GetType().Name</div>
6+
@ChildContent
7+
8+
@code {
9+
[Parameter] public RenderFragment? ChildContent { get; set; }
10+
}
11+
12+
@{
13+
#endif
14+
}

0 commit comments

Comments
 (0)