Skip to content

Commit

Permalink
Add overloads to InvokeAsync with a return value
Browse files Browse the repository at this point in the history
Closes #1296
  • Loading branch information
Jcparkyn committed Dec 1, 2023
1 parent 5976b86 commit 781133a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ All notable changes to **bUnit** will be documented in this file. The project ad

- When the `TestContext` was disposed, the Blazor Renderer itself didn't dispose components under test. By [@linkdotnet](https://github.com/linkdotnet).

### Added

- New overloads for `IRenderedFragmentBase.InvokeAsync` that allow retrieving the work item's return value. By [@jcparkyn](https://github.com/jcparkyn).

## [1.25.3] - 2023-11-14

- Upgrade all .NET 8 preview dependencies to .NET 8 stable.
Expand Down
30 changes: 30 additions & 0 deletions src/bunit.core/Extensions/RenderedFragmentInvokeAsyncExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,34 @@ public static Task InvokeAsync(this IRenderedFragmentBase renderedFragment, Func
return renderedFragment.Services.GetRequiredService<ITestRenderer>()
.Dispatcher.InvokeAsync(workItem);
}

/// <summary>
/// Invokes the given <paramref name="workItem"/> in the context of the associated <see cref="ITestRenderer"/>.
/// </summary>
/// <param name="renderedFragment">The rendered component whose dispatcher to invoke with.</param>
/// <param name="workItem">The work item to execute on the renderer's thread.</param>
/// <returns>A <see cref="Task"/> that will be completed when the action has finished executing, with the return value from <paramref name="workItem"/>.</returns>
public static Task<T> InvokeAsync<T>(this IRenderedFragmentBase renderedFragment, Func<T> workItem)
{
if (renderedFragment is null)
throw new ArgumentNullException(nameof(renderedFragment));

return renderedFragment.Services.GetRequiredService<ITestRenderer>()
.Dispatcher.InvokeAsync(workItem);
}

/// <summary>
/// Invokes the given <paramref name="workItem"/> in the context of the associated <see cref="ITestRenderer"/>.
/// </summary>
/// <param name="renderedFragment">The rendered component whose dispatcher to invoke with.</param>
/// <param name="workItem">The work item to execute on the renderer's thread.</param>
/// <returns>A <see cref="Task"/> that will be completed when the action has finished executing, with the return value from <paramref name="workItem"/>.</returns>
public static Task<T> InvokeAsync<T>(this IRenderedFragmentBase renderedFragment, Func<Task<T>> workItem)
{
if (renderedFragment is null)
throw new ArgumentNullException(nameof(renderedFragment));

return renderedFragment.Services.GetRequiredService<ITestRenderer>()
.Dispatcher.InvokeAsync(workItem);
}
}

0 comments on commit 781133a

Please sign in to comment.