Skip to content

Commit 0ee079d

Browse files
committed
Docs: verify component state
1 parent 75fa1eb commit 0ee079d

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

docs/site/docs/verification/verify-component-state.md

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,48 @@ uid: verify-component-state
33
title: Verifying the State of a Component Under Test
44
---
55

6-
# Verifying the State of a Component Under Test
6+
# Verifying the State of a Component
77

8-
Describe IRenderedComponent interface
8+
Calling [`RenderComponent<TComponent>`()](xref:Bunit.TestContext.RenderComponent``1(System.Action{Bunit.ComponentParameterBuilder{``0}})) on a <xref:Bunit.TestContext> or calling <xref:Bunit.Fixture.GetComponentUnderTest``1> on a <xref:Bunit.Fixture> returns an instance of the <xref:Bunit.IRenderedComponent`1> type.
99

10-
Show Instance prop
10+
The <xref:Bunit.IRenderedComponent`1> type makes it possible to inspect the instance of the component under test (`TComponent`), and trigger re-renders explicitly.
1111

12-
Warn against setting props directly
12+
> [!NOTE]
13+
> Since <xref:Bunit.IRenderedComponent`1> inherits from <xref:Bunit.IRenderedFragment>, all the markup verification techniques covered on the <xref:verify-markup> page also applies to the <xref:Bunit.IRenderedComponent`1> type.
1314
14-
FindComponent/FindComponents
15+
## Inspecting the Component Under Test
16+
17+
The <xref:Bunit.IRenderedComponentBase`1.Instance> property on the <xref:Bunit.IRenderedComponent`1> type provides access to the component under test. For example:
18+
19+
```csharp
20+
using var ctx = new TestContext();
21+
22+
IRenderedComponent<Alert> cut = ctx.RenderComponent<Alert>();
23+
24+
Alert alert = cut.Instance;
25+
26+
// Assert against <Alert /> instance
27+
```
28+
29+
> [!WARNING]
30+
> While it is possible to set `[Parameter]` and `[CascadingParameter]` properties directly through the <xref:Bunit.IRenderedComponentBase`1.Instance> property on the <xref:Bunit.IRenderedComponent`1> type, doing so does not implicitly trigger a render and the component life-cycle methods are not called.
31+
>
32+
> The correct approach is to set parameters through the [`SetParametersAndRender()`](xref:Bunit.IRenderedComponentBase`1.SetParametersAndRender(Bunit.Rendering.ComponentParameter[])) methods. See the <xref:trigger-renders> page for more on this.
33+
34+
## Finding Components in the Render Tree
35+
36+
To get the instance of components nested inside the component under test, use the
37+
[`FindComponent<TComponent>()`](xref:Bunit.RenderedFragmentExtensions.FindComponent``1(Bunit.IRenderedFragment)) and [`FindComponents<TComponent>()`](xref:Bunit.RenderedFragmentExtensions.FindComponents``1(Bunit.IRenderedFragment)) methods on the <xref:Bunit.IRenderedComponent`1> type. Suppose we have a `<TodoList>` component with `<Task>` components nested inside for each task in the todo list, then the `<Task>` components can be found like this:
38+
39+
```csharp
40+
using var ctx = new TestContext();
41+
var cut = ctx.RenderComponent<TodoList>(parameter => parameter
42+
.Add(p => p.Tasks, new [] { "Task 1", "Task 2" })
43+
);
44+
45+
var tasks = cut.FindComponents<Task>();
46+
47+
Assert.Equal(2, tasks.Count);
48+
```
49+
50+
Both the [`FindComponent<TComponent>()`](xref:Bunit.RenderedFragmentExtensions.FindComponent``1(Bunit.IRenderedFragment)) and [`FindComponents<TComponent>()`](xref:Bunit.RenderedFragmentExtensions.FindComponents``1(Bunit.IRenderedFragment)) methods performs a **depth-first search** of the render tree, with the first method returning only the first found matching component, while latter returning all matching components in the render tree.

0 commit comments

Comments
 (0)