|
| 1 | +namespace Bunit; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Provides extension methods for waiting on components within a rendered Blazor component. |
| 5 | +/// </summary> |
| 6 | +public static partial class RenderedComponentWaitForHelperExtensions |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Waits until the specified component is rendered in the DOM. |
| 10 | + /// </summary> |
| 11 | + /// <param name="renderedComponent">The rendered component to find the component in.</param> |
| 12 | + /// <param name="timeout">The maximum time to wait for the element to appear.</param> |
| 13 | + /// <typeparam name="TComponent">The target component type to wait for.</typeparam> |
| 14 | + /// <returns>See <see cref="IRenderedComponent{TComponent}"/>.</returns> |
| 15 | + public static IRenderedComponent<TComponent> WaitForComponent<TComponent>( |
| 16 | + this IRenderedComponent<IComponent> renderedComponent, |
| 17 | + TimeSpan? timeout = null) |
| 18 | + where TComponent : IComponent |
| 19 | + { |
| 20 | + renderedComponent.WaitForState(renderedComponent.HasComponent<TComponent>, timeout); |
| 21 | + return renderedComponent.FindComponent<TComponent>(); |
| 22 | + } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Waits until the specified number of components are rendered in the DOM and returns their instances. |
| 26 | + /// </summary> |
| 27 | + /// <param name="renderedComponent">The rendered component in which to search for instances of the specified component.</param> |
| 28 | + /// <param name="matchComponentCount">The minimum amount component instances to wait for.</param> |
| 29 | + /// <param name="timeout">The maximum time to wait for the components to appear. Defaults to no specific timeout if not provided.</param> |
| 30 | + /// <typeparam name="TComponent">The target component type to wait for.</typeparam> |
| 31 | + /// <returns>A read-only collection of <see cref="IRenderedComponent{TComponent}"/> instances representing the found components.</returns> |
| 32 | + public static IReadOnlyCollection<IRenderedComponent<TComponent>> WaitForComponents<TComponent>( |
| 33 | + this IRenderedComponent<IComponent> renderedComponent, |
| 34 | + int matchComponentCount, |
| 35 | + TimeSpan? timeout = null) |
| 36 | + where TComponent : IComponent |
| 37 | + { |
| 38 | + renderedComponent.WaitForState( |
| 39 | + () => renderedComponent.FindComponents<TComponent>().Count == matchComponentCount, |
| 40 | + timeout); |
| 41 | + return renderedComponent.FindComponents<TComponent>(); |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Waits until the specified component is rendered in the DOM and returns all instances of that component when the first instance is found. |
| 46 | + /// </summary> |
| 47 | + /// <param name="renderedComponent">The rendered component in which to search for instances of the specified component.</param> |
| 48 | + /// <param name="timeout">The maximum time to wait for the components to appear. Defaults to no specific timeout if not provided.</param> |
| 49 | + /// <typeparam name="TComponent">The target component type to wait for.</typeparam> |
| 50 | + /// <returns>A read-only collection of <see cref="IRenderedComponent{TComponent}"/> instances representing the found components.</returns> |
| 51 | + public static IReadOnlyCollection<IRenderedComponent<TComponent>> WaitForComponents<TComponent>( |
| 52 | + this IRenderedComponent<IComponent> renderedComponent, |
| 53 | + TimeSpan? timeout = null) |
| 54 | + where TComponent : IComponent |
| 55 | + => renderedComponent.WaitForComponents<TComponent>(1, timeout); |
| 56 | +} |
0 commit comments