diff --git a/docs/site/docs/interaction/index.md b/docs/site/docs/interaction/index.md index 96e06fdde..9967c90ea 100644 --- a/docs/site/docs/interaction/index.md +++ b/docs/site/docs/interaction/index.md @@ -11,3 +11,4 @@ This section covers the various ways to interact with a component under test, e. - **:** This covers how to manually trigger a render cycle for a component under test. - **:** This covers how to await one or more asynchronous changes to the state of a component under test before continuing the test. - **:** This covers how to dispose components and their children. +- **:** This covers the different render modes and their interaction with bUnit. diff --git a/docs/site/docs/interaction/render-modes.md b/docs/site/docs/interaction/render-modes.md new file mode 100644 index 000000000..bb13bf0de --- /dev/null +++ b/docs/site/docs/interaction/render-modes.md @@ -0,0 +1,62 @@ +--- +uid: render-modes +title: Support for render modes and renderer info +--- + +# Support for render modes and the renderer info +Render modes in Blazor Web Apps determine the hosting model and interactivity of components. The render mode for example can be applied to a component using the `@rendermode` directive. The [`RendererInfo`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.rendererinfo?view=aspnetcore-9.0) allows the application to determine the interactivity and location of the component. For more details, check out the [Blazor render modes](https://learn.microsoft.com/en-us/aspnet/core/blazor/components/render-modes?view=aspnetcore-9.0) documentation. + +## Setting the render mode for a component under test +Setting the render mode can be done via the method. The `SetAssignedRenderMode` method takes an [`IComponentRenderMode`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.icomponentrendermode?view=aspnetcore-9.0) object as a parameter. Normally this is one of the following types: + * [`InteractiveAutoRenderMode`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.web.interactiveautorendermode?view=aspnetcore-9.0) + * [`InteractiveServerRendeMode`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.web.interactiveserverrendermode?view=aspnetcore-9.0) + * [`InteractiveWebAssemblyRenderMode`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.web.interactivewebassemblyrendermode?view=aspnetcore-9.0) + +For ease of use the [`RenderMode`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.web.rendermode?view=aspnetcore-9.0) class defines all three of them. + + +> [!NOTE] +> Strictly speaking, this feature is available since `net8.0`. With `net9.0` Blazor exposes the information on `ComponentBase` via the `AssignedRenderMode` property. + +This methods emulates the behavior of the `@rendermode` directive in a test environment. The following example shows how to set the render mode for a component under test: + +```razor +@inherits TestContext + +@code { + [Fact] + public void WhenRenderModeNonInteractive_DisableButton() + { + // Arrange + var cut = RenderComponent(ps => ps.SetAssignedRenderMode(RenderMode.InteractiveServer)); +``` + +## Setting the `RendererInfo` for a component under test +To set the `RendererInfo` for a component under test, use the `SetRendererInfo` method on the `TestContext` class. The `SetRendererInfo` method takes an optional `RendererInfo` object as a parameter. A component might check if interactivity is given to enable a button: + +```razor + +``` + +In the test, you can set the `RendererInfo` to enabl or disable the button: + +```csharp +@inherits TestContext + +@code { + [Fact] + public void WhenRenderModeNonInteractive_DisableButton() + { + // Arrange + SetRendererInfo(new RendererInfo("Server", false)); + + // Act + var cut = RenderComponent(); + + // Assert + cut.Find("button").Attributes["disabled"].ShouldBe("disabled"); + } +} +``` \ No newline at end of file diff --git a/src/bunit.core/TestContextBase.cs b/src/bunit.core/TestContextBase.cs index 9c2fa1e8a..d9e3edce8 100644 --- a/src/bunit.core/TestContextBase.cs +++ b/src/bunit.core/TestContextBase.cs @@ -118,4 +118,14 @@ public void DisposeComponents() { Renderer.DisposeComponents(); } + +#if NET9_0_OR_GREATER + /// + /// Sets the for the renderer. + /// + public void SetRendererInfo(RendererInfo? rendererInfo) + { + Renderer.SetRendererInfo(rendererInfo); + } +#endif } diff --git a/tests/bunit.core.tests/Rendering/RenderModeTests.razor b/tests/bunit.core.tests/Rendering/RenderModeTests.razor index e389650f3..0bded05f6 100644 --- a/tests/bunit.core.tests/Rendering/RenderModeTests.razor +++ b/tests/bunit.core.tests/Rendering/RenderModeTests.razor @@ -7,7 +7,7 @@ [Fact(DisplayName = "TestRenderer provides RendererInfo")] public void Test001() { - Renderer.SetRendererInfo(new RendererInfo("Server", true)); + SetRendererInfo(new RendererInfo("Server", true)); var cut = RenderComponent(); cut.MarkupMatches(