Skip to content

Commit 4ed4747

Browse files
QwertylukPawel Krzyzak
and
Pawel Krzyzak
authored
fix: Add ComponentParameterCollectionBuilder overloaded methods (#1467)
* ComponentParameterCollectionBuilder: Add overloaded methods * CHANGELOG.md: Updated --------- Co-authored-by: Pawel Krzyzak <Pawel.Krzyzak@krolldiscovery.com>
1 parent 04aa5fe commit 4ed4747

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ All notable changes to **bUnit** will be documented in this file. The project ad
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- 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).
12+
913
## [1.28.9] - 2024-04-19
1014

1115
### Fixed

src/bunit/ComponentParameterCollectionBuilder.cs

+42
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,26 @@ public ComponentParameterCollectionBuilder<TComponent> Add(Expression<Func<TComp
167167
public ComponentParameterCollectionBuilder<TComponent> Add(Expression<Func<TComponent, EventCallback?>> parameterSelector, Func<Task> callback)
168168
=> Add(parameterSelector, EventCallback.Factory.Create(callback?.Target!, callback!));
169169

170+
/// <summary>
171+
/// Adds a component parameter for an <see cref="EventCallback"/> parameter selected with <paramref name="parameterSelector"/>,
172+
/// where the <paramref name="callback"/> is used as value.
173+
/// </summary>
174+
/// <param name="parameterSelector">A lambda function that selects the parameter.</param>
175+
/// <param name="callback">The callback to pass to the <see cref="EventCallback"/>.</param>
176+
/// <returns>This <see cref="ComponentParameterCollectionBuilder{TComponent}"/>.</returns>
177+
public ComponentParameterCollectionBuilder<TComponent> Add(Expression<Func<TComponent, EventCallback>> parameterSelector, Func<object, Task> callback)
178+
=> Add(parameterSelector, EventCallback.Factory.Create(callback?.Target!, callback!));
179+
180+
/// <summary>
181+
/// Adds a component parameter for a nullable <see cref="EventCallback"/> parameter selected with <paramref name="parameterSelector"/>,
182+
/// where the <paramref name="callback"/> is used as value.
183+
/// </summary>
184+
/// <param name="parameterSelector">A lambda function that selects the parameter.</param>
185+
/// <param name="callback">The callback to pass to the <see cref="EventCallback"/>.</param>
186+
/// <returns>This <see cref="ComponentParameterCollectionBuilder{TComponent}"/>.</returns>
187+
public ComponentParameterCollectionBuilder<TComponent> Add(Expression<Func<TComponent, EventCallback?>> parameterSelector, Func<object, Task> callback)
188+
=> Add(parameterSelector, EventCallback.Factory.Create(callback?.Target!, callback!));
189+
170190
/// <summary>
171191
/// Adds a component parameter for an <see cref="EventCallback{TValue}"/> parameter selected with <paramref name="parameterSelector"/>,
172192
/// where the <paramref name="callback"/> is used as value.
@@ -233,6 +253,28 @@ public ComponentParameterCollectionBuilder<TComponent> Add<TValue>(Expression<Fu
233253
public ComponentParameterCollectionBuilder<TComponent> Add<TValue>(Expression<Func<TComponent, EventCallback<TValue>?>> parameterSelector, Func<Task> callback)
234254
=> Add(parameterSelector, EventCallback.Factory.Create<TValue>(callback?.Target!, callback!));
235255

256+
/// <summary>
257+
/// Adds a component parameter for an <see cref="EventCallback{TValue}"/> parameter selected with <paramref name="parameterSelector"/>,
258+
/// where the <paramref name="callback"/> is used as value.
259+
/// </summary>
260+
/// <param name="parameterSelector">A lambda function that selects the parameter.</param>
261+
/// <param name="callback">The callback to pass to the <see cref="EventCallback"/>.</param>
262+
/// <typeparam name="TValue">The value returned in the <see cref="EventCallback{TValue}"/>.</typeparam>
263+
/// <returns>This <see cref="ComponentParameterCollectionBuilder{TComponent}"/>.</returns>
264+
public ComponentParameterCollectionBuilder<TComponent> Add<TValue>(Expression<Func<TComponent, EventCallback<TValue>>> parameterSelector, Func<TValue, Task> callback)
265+
=> Add(parameterSelector, EventCallback.Factory.Create<TValue>(callback?.Target!, callback!));
266+
267+
/// <summary>
268+
/// Adds a component parameter for a nullable <see cref="EventCallback{TValue}"/> parameter selected with <paramref name="parameterSelector"/>,
269+
/// where the <paramref name="callback"/> is used as value.
270+
/// </summary>
271+
/// <param name="parameterSelector">A lambda function that selects the parameter.</param>
272+
/// <param name="callback">The callback to pass to the <see cref="EventCallback"/>.</param>
273+
/// <typeparam name="TValue">The value returned in the <see cref="EventCallback{TValue}"/>.</typeparam>
274+
/// <returns>This <see cref="ComponentParameterCollectionBuilder{TComponent}"/>.</returns>
275+
public ComponentParameterCollectionBuilder<TComponent> Add<TValue>(Expression<Func<TComponent, EventCallback<TValue>?>> parameterSelector, Func<TValue, Task> callback)
276+
=> Add(parameterSelector, EventCallback.Factory.Create<TValue>(callback?.Target!, callback!));
277+
236278
/// <summary>
237279
/// Adds a ChildContent <see cref="RenderFragment"/> type parameter with the <paramref name="childContent"/> as value.
238280
///

tests/bunit.tests/ComponentParameterCollectionBuilderTests.cs

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Linq.Expressions;
2-
using Bunit.Rendering;
32

43
namespace Bunit;
54

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

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

227+
[Fact(DisplayName = "Func<object, Task> to EventCallback with parameter selector")]
228+
public async Task Test026()
229+
{
230+
Builder.Add(x => x.EC, _ => { EventCallbackCalled = true; return Task.CompletedTask; });
231+
await VerifyEventCallbackAsync("EC");
232+
}
233+
234+
[Fact(DisplayName = "Func<object, Task> to EventCallback? with parameter selector")]
235+
public async Task Test027()
236+
{
237+
Builder.Add(x => x.NullableEC, _ => { EventCallbackCalled = true; return Task.CompletedTask; });
238+
await VerifyEventCallbackAsync("NullableEC");
239+
}
240+
241+
[Fact(DisplayName = "Func<T, Task> to EventCallback<T> with parameter selector")]
242+
public async Task Test028()
243+
{
244+
Builder.Add(x => x.ECWithArgs, _ => { EventCallbackCalled = true; return Task.CompletedTask; });
245+
await VerifyEventCallbackAsync<EventArgs>("ECWithArgs");
246+
}
247+
248+
[Fact(DisplayName = "Func<T, Task> to EventCallback<T>? with parameter selector")]
249+
public async Task Test029()
250+
{
251+
Builder.Add(x => x.NullableECWithArgs, _ => { EventCallbackCalled = true; return Task.CompletedTask; });
252+
await VerifyEventCallbackAsync<EventArgs>("NullableECWithArgs");
253+
}
254+
228255
[Fact(DisplayName = "ChildContent can be passed as RenderFragment")]
229256
public void Test030()
230257
{

0 commit comments

Comments
 (0)