You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/site/docs/interaction/awaiting-async-state.md
+38-2Lines changed: 38 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ A test can fail if a component performs asynchronous renders. This may be due to
9
9
10
10
You need to handle this specifically in your tests because tests execute in the test framework's synchronization context and the test renderer executes renders in its own synchronization context. If you do not, you will likely experience tests that sometimes pass and sometimes fail.
11
11
12
-
bUnit comes with two methods that help to deal with this issue: the [`WaitForState()`](xref:Bunit.RenderedComponentWaitForHelperExtensions.WaitForState``1(Bunit.IRenderedComponent{``0},System.Func{System.Boolean},System.Nullable{System.TimeSpan})) method covered on this page, and the [`WaitForAssertion()`](xref:Bunit.RenderedComponentWaitForHelperExtensions.WaitForAssertion``1(Bunit.IRenderedComponent{``0},System.Action,System.Nullable{System.TimeSpan})) method covered on the <xref:async-assertion> page.
12
+
bUnit comes with several methods that help to deal with this issue: the [`WaitForState()`](xref:Bunit.RenderedComponentWaitForHelperExtensions.WaitForState``1(Bunit.IRenderedComponent{``0},System.Func{System.Boolean},System.Nullable{System.TimeSpan})) method, the [`WaitForAssertion()`](xref:Bunit.RenderedComponentWaitForHelperExtensions.WaitForAssertion``1(Bunit.IRenderedComponent{``0},System.Action,System.Nullable{System.TimeSpan})) method covered on the <xref:async-assertion> page, and the component-specific waiting methods [`WaitForComponent()`](xref:Bunit.RenderedComponentWaitForHelperExtensions.WaitForComponent``1(Bunit.IRenderedComponent{Microsoft.AspNetCore.Components.IComponent},System.Nullable{System.TimeSpan})) and [`WaitForComponents()`](xref:Bunit.RenderedComponentWaitForHelperExtensions.WaitForComponents``1(Bunit.IRenderedComponent{Microsoft.AspNetCore.Components.IComponent},System.Nullable{System.TimeSpan})) that are covered later on this page.
13
13
14
14
Let's start by taking a look at the `WaitForState` method in more detail.
15
15
@@ -48,6 +48,42 @@ If the timeout is reached, a <xref:Bunit.Extensions.WaitForHelpers.WaitForFailed
48
48
49
49
> The state predicate did not pass before the timeout period passed.
50
50
51
-
## Debugging code that uses `WaitForState`, `WaitForAssertion`, or `WaitForElement`
51
+
## Waiting for components using `WaitForComponent` and `WaitForComponents`
52
+
53
+
bUnit provides specialized methods for waiting for child components to appear in the render tree. These methods are useful when testing scenarios where components are rendered asynchronously based on some state change or data loading.
54
+
55
+
### Waiting for a single component
56
+
57
+
The [`WaitForComponent<TComponent>(TimeSpan?)`](xref:Bunit.RenderedComponentWaitForHelperExtensions.WaitForComponent``1(Bunit.IRenderedComponent{Microsoft.AspNetCore.Components.IComponent},System.Nullable{System.TimeSpan})) method waits until the specified component type is rendered in the DOM and returns the first instance found.
58
+
59
+
Consider the following `<AsyncComponentLoader>` component that loads child components asynchronously:
The [`WaitForComponents<TComponent>(TimeSpan?)`](xref:Bunit.RenderedComponentWaitForHelperExtensions.WaitForComponents``1(Bunit.IRenderedComponent{Microsoft.AspNetCore.Components.IComponent},System.Nullable{System.TimeSpan})) method waits until at least one instance of the specified component type is rendered in the DOM and returns all instances found:
The [`WaitForComponents<TComponent>(int, TimeSpan?)`](xref:Bunit.RenderedComponentWaitForHelperExtensions.WaitForComponents``1(Bunit.IRenderedComponent{Microsoft.AspNetCore.Components.IComponent},System.Int32,System.Nullable{System.TimeSpan})) overload waits until at least the specified number of component instances are rendered:
> This method waits for **at least** the specified number of components, not exactly that number. If more components are rendered than requested, the method will still succeed and return all found instances.
81
+
82
+
> [!NOTE]
83
+
> These component wait methods use the same underlying mechanism as `WaitForState()` and will retry their checks every time the component under test renders.
84
+
85
+
If the timeout is reached, a <xref:Bunit.Extensions.WaitForHelpers.WaitForFailedException> exception is thrown with an appropriate error message indicating that the expected component(s) were not found within the timeout period.
86
+
87
+
## Debugging code that uses `WaitForState`, `WaitForAssertion`, `WaitForComponent`, `WaitForComponents`, or `WaitForElement`
52
88
53
89
When `bUnit` detects that a debugger is attached (`Debugger.IsAttached`), it will automatically disable the timeout functionality of the "wait for" methods.
0 commit comments