Skip to content

Commit

Permalink
fix: Add ComponentParameterCollectionBuilder overloaded methods (#1467)
Browse files Browse the repository at this point in the history
* ComponentParameterCollectionBuilder: Add overloaded methods

* CHANGELOG.md: Updated

---------

Co-authored-by: Pawel Krzyzak <[email protected]>
  • Loading branch information
Qwertyluk and Pawel Krzyzak authored May 28, 2024
1 parent 04aa5fe commit 4ed4747
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ All notable changes to **bUnit** will be documented in this file. The project ad

## [Unreleased]

### Added

- New overloads for `ComponentParameterCollectionBuilder.Add` that allow passing arguments for asynchronous callback parameters. Reported by [springy76](https://github.com/springy76). By [@Qwertyluk](https://github.com/Qwertyluk).

## [1.28.9] - 2024-04-19

### Fixed
Expand Down
42 changes: 42 additions & 0 deletions src/bunit/ComponentParameterCollectionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,26 @@ public ComponentParameterCollectionBuilder<TComponent> Add(Expression<Func<TComp
public ComponentParameterCollectionBuilder<TComponent> Add(Expression<Func<TComponent, EventCallback?>> parameterSelector, Func<Task> callback)
=> Add(parameterSelector, EventCallback.Factory.Create(callback?.Target!, callback!));

/// <summary>
/// Adds a component parameter for an <see cref="EventCallback"/> parameter selected with <paramref name="parameterSelector"/>,
/// where the <paramref name="callback"/> is used as value.
/// </summary>
/// <param name="parameterSelector">A lambda function that selects the parameter.</param>
/// <param name="callback">The callback to pass to the <see cref="EventCallback"/>.</param>
/// <returns>This <see cref="ComponentParameterCollectionBuilder{TComponent}"/>.</returns>
public ComponentParameterCollectionBuilder<TComponent> Add(Expression<Func<TComponent, EventCallback>> parameterSelector, Func<object, Task> callback)
=> Add(parameterSelector, EventCallback.Factory.Create(callback?.Target!, callback!));

/// <summary>
/// Adds a component parameter for a nullable <see cref="EventCallback"/> parameter selected with <paramref name="parameterSelector"/>,
/// where the <paramref name="callback"/> is used as value.
/// </summary>
/// <param name="parameterSelector">A lambda function that selects the parameter.</param>
/// <param name="callback">The callback to pass to the <see cref="EventCallback"/>.</param>
/// <returns>This <see cref="ComponentParameterCollectionBuilder{TComponent}"/>.</returns>
public ComponentParameterCollectionBuilder<TComponent> Add(Expression<Func<TComponent, EventCallback?>> parameterSelector, Func<object, Task> callback)
=> Add(parameterSelector, EventCallback.Factory.Create(callback?.Target!, callback!));

/// <summary>
/// Adds a component parameter for an <see cref="EventCallback{TValue}"/> parameter selected with <paramref name="parameterSelector"/>,
/// where the <paramref name="callback"/> is used as value.
Expand Down Expand Up @@ -233,6 +253,28 @@ public ComponentParameterCollectionBuilder<TComponent> Add<TValue>(Expression<Fu
public ComponentParameterCollectionBuilder<TComponent> Add<TValue>(Expression<Func<TComponent, EventCallback<TValue>?>> parameterSelector, Func<Task> callback)
=> Add(parameterSelector, EventCallback.Factory.Create<TValue>(callback?.Target!, callback!));

/// <summary>
/// Adds a component parameter for an <see cref="EventCallback{TValue}"/> parameter selected with <paramref name="parameterSelector"/>,
/// where the <paramref name="callback"/> is used as value.
/// </summary>
/// <param name="parameterSelector">A lambda function that selects the parameter.</param>
/// <param name="callback">The callback to pass to the <see cref="EventCallback"/>.</param>
/// <typeparam name="TValue">The value returned in the <see cref="EventCallback{TValue}"/>.</typeparam>
/// <returns>This <see cref="ComponentParameterCollectionBuilder{TComponent}"/>.</returns>
public ComponentParameterCollectionBuilder<TComponent> Add<TValue>(Expression<Func<TComponent, EventCallback<TValue>>> parameterSelector, Func<TValue, Task> callback)
=> Add(parameterSelector, EventCallback.Factory.Create<TValue>(callback?.Target!, callback!));

/// <summary>
/// Adds a component parameter for a nullable <see cref="EventCallback{TValue}"/> parameter selected with <paramref name="parameterSelector"/>,
/// where the <paramref name="callback"/> is used as value.
/// </summary>
/// <param name="parameterSelector">A lambda function that selects the parameter.</param>
/// <param name="callback">The callback to pass to the <see cref="EventCallback"/>.</param>
/// <typeparam name="TValue">The value returned in the <see cref="EventCallback{TValue}"/>.</typeparam>
/// <returns>This <see cref="ComponentParameterCollectionBuilder{TComponent}"/>.</returns>
public ComponentParameterCollectionBuilder<TComponent> Add<TValue>(Expression<Func<TComponent, EventCallback<TValue>?>> parameterSelector, Func<TValue, Task> callback)
=> Add(parameterSelector, EventCallback.Factory.Create<TValue>(callback?.Target!, callback!));

/// <summary>
/// Adds a ChildContent <see cref="RenderFragment"/> type parameter with the <paramref name="childContent"/> as value.
///
Expand Down
31 changes: 29 additions & 2 deletions tests/bunit.tests/ComponentParameterCollectionBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq.Expressions;
using Bunit.Rendering;

namespace Bunit;

Expand Down Expand Up @@ -115,7 +114,7 @@ public void Test010()
[Fact(DisplayName = "Null to EventCallback throws")]
public void Test011()
{
Should.Throw<ArgumentNullException>(() => Builder.Add(x => x.EC, null!));
Should.Throw<ArgumentNullException>(() => Builder.Add(x => x.EC, (Func<Task>)null!));
}

[Fact(DisplayName = "Null for EventCallback? with parameter selector")]
Expand Down Expand Up @@ -225,6 +224,34 @@ public async Task Test025()
await VerifyEventCallbackAsync<EventArgs>("NullableECWithArgs");
}

[Fact(DisplayName = "Func<object, Task> to EventCallback with parameter selector")]
public async Task Test026()
{
Builder.Add(x => x.EC, _ => { EventCallbackCalled = true; return Task.CompletedTask; });
await VerifyEventCallbackAsync("EC");
}

[Fact(DisplayName = "Func<object, Task> to EventCallback? with parameter selector")]
public async Task Test027()
{
Builder.Add(x => x.NullableEC, _ => { EventCallbackCalled = true; return Task.CompletedTask; });
await VerifyEventCallbackAsync("NullableEC");
}

[Fact(DisplayName = "Func<T, Task> to EventCallback<T> with parameter selector")]
public async Task Test028()
{
Builder.Add(x => x.ECWithArgs, _ => { EventCallbackCalled = true; return Task.CompletedTask; });
await VerifyEventCallbackAsync<EventArgs>("ECWithArgs");
}

[Fact(DisplayName = "Func<T, Task> to EventCallback<T>? with parameter selector")]
public async Task Test029()
{
Builder.Add(x => x.NullableECWithArgs, _ => { EventCallbackCalled = true; return Task.CompletedTask; });
await VerifyEventCallbackAsync<EventArgs>("NullableECWithArgs");
}

[Fact(DisplayName = "ChildContent can be passed as RenderFragment")]
public void Test030()
{
Expand Down

0 comments on commit 4ed4747

Please sign in to comment.