diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b9933ce1..1473b6fbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/bunit.core/Rendering/TestRenderer.cs b/src/bunit.core/Rendering/TestRenderer.cs index 34d2377f7..df41b817a 100644 --- a/src/bunit.core/Rendering/TestRenderer.cs +++ b/src/bunit.core/Rendering/TestRenderer.cs @@ -241,6 +241,27 @@ protected override IComponent ResolveComponentForRenderMode(Type componentType, } #endif +#if NET9_0_OR_GREATER + /// + protected override IComponentRenderMode? GetComponentRenderMode(IComponent component) + { + ArgumentNullException.ThrowIfNull(component); + + var renderModeAttribute = component.GetType() + .GetCustomAttribute(); + + if (renderModeAttribute is not null) + { + return renderModeAttribute.Mode; + } + + var parentComponentState = GetComponentState(component).ParentComponentState; + return parentComponentState is not null + ? GetComponentRenderMode(parentComponentState.Component) + : null; + } +#endif + /// internal Task SetDirectParametersAsync(IRenderedFragmentBase renderedComponent, ParameterView parameters) { diff --git a/tests/bunit.core.tests/Rendering/RenderModeTests.cs b/tests/bunit.core.tests/Rendering/RenderModeTests.cs new file mode 100644 index 000000000..352da9dec --- /dev/null +++ b/tests/bunit.core.tests/Rendering/RenderModeTests.cs @@ -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(); + + cut.MarkupMatches(""" +

Is interactive: True

+

Rendermode: Server

+ """); + } + + [Fact(DisplayName = "Renderer throws exception if RendererInfo is not specified")] + public void Test002() + { + Action act = () => RenderComponent(); + + act.ShouldThrow(); + } + + [Fact(DisplayName = "Renderer should set the RenderModeAttribute on the component")] + public void Test003() + { + var cut = RenderComponent(); + + cut.MarkupMatches("
Assigned render mode: InteractiveServerRenderMode
"); + } + + [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( + c => c.AddChildContent()); + + cut.MarkupMatches(""" +
Parent assigned render mode:
+
Assigned render mode: InteractiveWebAssemblyRenderMode
+ """); + } + + [Fact(DisplayName = "Parent and child render mode is specified")] + public void Test005() + { + var cut = RenderComponent( + c => c.AddChildContent()); + + cut.MarkupMatches(""" +
Parent assigned render mode: InteractiveWebAssemblyRenderMode
+
Assigned render mode: InteractiveServerRenderMode
+ """); + } + + [Fact(DisplayName = "Parent and child render mode is not specified")] + public void Test006() + { + var cut = RenderComponent( + c => c.AddChildContent()); + + cut.MarkupMatches(""" +
Parent assigned render mode:
+
Assigned render mode:
+ """); + + } +} +#endif \ No newline at end of file diff --git a/tests/bunit.core.tests/Rendering/RendererInfoTests.cs b/tests/bunit.core.tests/Rendering/RendererInfoTests.cs deleted file mode 100644 index cdccb3b58..000000000 --- a/tests/bunit.core.tests/Rendering/RendererInfoTests.cs +++ /dev/null @@ -1,28 +0,0 @@ -#if NET9_0_OR_GREATER -using Bunit.TestAssets.RenderModes; - -namespace Bunit.Rendering; - -public class RendererInfoTests : TestContext -{ - [Fact(DisplayName = "TestRenderer provides RendererInfo")] - public void Test001() - { - Renderer.SetRendererInfo(new RendererInfo("Server", true)); - var cut = RenderComponent(); - - cut.MarkupMatches(""" -

Is interactive: True

-

Rendermode: Server

- """); - } - - [Fact(DisplayName = "Renderer throws exception if RendererInfo is not specified")] - public void Test002() - { - Action act = () => RenderComponent(); - - act.ShouldThrow(); - } -} -#endif \ No newline at end of file diff --git a/tests/bunit.testassets/RenderModes/ComponentWithServerRenderMode.razor b/tests/bunit.testassets/RenderModes/ComponentWithServerRenderMode.razor new file mode 100644 index 000000000..3ce1d35a0 --- /dev/null +++ b/tests/bunit.testassets/RenderModes/ComponentWithServerRenderMode.razor @@ -0,0 +1,15 @@ +@{ +#if NET9_0_OR_GREATER +} + +@rendermode Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveServer +
@(ChildContent is not null ? "Parent assigned" : "Assigned") render mode: @AssignedRenderMode?.GetType().Name
+@ChildContent + +@code { + [Parameter] public RenderFragment? ChildContent { get; set; } +} + +@{ +#endif +} \ No newline at end of file diff --git a/tests/bunit.testassets/RenderModes/ComponentWithWebAssemblyRenderMode.razor b/tests/bunit.testassets/RenderModes/ComponentWithWebAssemblyRenderMode.razor new file mode 100644 index 000000000..af0888317 --- /dev/null +++ b/tests/bunit.testassets/RenderModes/ComponentWithWebAssemblyRenderMode.razor @@ -0,0 +1,15 @@ +@{ +#if NET9_0_OR_GREATER +} + +@rendermode Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveWebAssembly +
@(ChildContent is not null ? "Parent assigned" : "Assigned") render mode: @AssignedRenderMode?.GetType().Name
+@ChildContent + +@code { + [Parameter] public RenderFragment? ChildContent { get; set; } +} + +@{ +#endif +} \ No newline at end of file diff --git a/tests/bunit.testassets/RenderModes/ComponentWithoutRenderMode.razor b/tests/bunit.testassets/RenderModes/ComponentWithoutRenderMode.razor new file mode 100644 index 000000000..6d5f7f9b7 --- /dev/null +++ b/tests/bunit.testassets/RenderModes/ComponentWithoutRenderMode.razor @@ -0,0 +1,14 @@ +@{ +#if NET9_0_OR_GREATER +} + +
@(ChildContent is not null ? "Parent assigned" : "Assigned") render mode: @AssignedRenderMode?.GetType().Name
+@ChildContent + +@code { + [Parameter] public RenderFragment? ChildContent { get; set; } +} + +@{ +#endif +} \ No newline at end of file