diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f8c1f7f4..00cfabaf0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/bunit/ComponentParameterCollectionBuilder.cs b/src/bunit/ComponentParameterCollectionBuilder.cs index f1bbf142a..7c7ee8cc2 100644 --- a/src/bunit/ComponentParameterCollectionBuilder.cs +++ b/src/bunit/ComponentParameterCollectionBuilder.cs @@ -167,6 +167,26 @@ public ComponentParameterCollectionBuilder Add(Expression Add(Expression> parameterSelector, Func callback) => Add(parameterSelector, EventCallback.Factory.Create(callback?.Target!, callback!)); + /// + /// Adds a component parameter for an parameter selected with , + /// where the is used as value. + /// + /// A lambda function that selects the parameter. + /// The callback to pass to the . + /// This . + public ComponentParameterCollectionBuilder Add(Expression> parameterSelector, Func callback) + => Add(parameterSelector, EventCallback.Factory.Create(callback?.Target!, callback!)); + + /// + /// Adds a component parameter for a nullable parameter selected with , + /// where the is used as value. + /// + /// A lambda function that selects the parameter. + /// The callback to pass to the . + /// This . + public ComponentParameterCollectionBuilder Add(Expression> parameterSelector, Func callback) + => Add(parameterSelector, EventCallback.Factory.Create(callback?.Target!, callback!)); + /// /// Adds a component parameter for an parameter selected with , /// where the is used as value. @@ -233,6 +253,28 @@ public ComponentParameterCollectionBuilder Add(Expression Add(Expression?>> parameterSelector, Func callback) => Add(parameterSelector, EventCallback.Factory.Create(callback?.Target!, callback!)); + /// + /// Adds a component parameter for an parameter selected with , + /// where the is used as value. + /// + /// A lambda function that selects the parameter. + /// The callback to pass to the . + /// The value returned in the . + /// This . + public ComponentParameterCollectionBuilder Add(Expression>> parameterSelector, Func callback) + => Add(parameterSelector, EventCallback.Factory.Create(callback?.Target!, callback!)); + + /// + /// Adds a component parameter for a nullable parameter selected with , + /// where the is used as value. + /// + /// A lambda function that selects the parameter. + /// The callback to pass to the . + /// The value returned in the . + /// This . + public ComponentParameterCollectionBuilder Add(Expression?>> parameterSelector, Func callback) + => Add(parameterSelector, EventCallback.Factory.Create(callback?.Target!, callback!)); + /// /// Adds a ChildContent type parameter with the as value. /// diff --git a/tests/bunit.tests/ComponentParameterCollectionBuilderTests.cs b/tests/bunit.tests/ComponentParameterCollectionBuilderTests.cs index 2a8332954..960d7f738 100644 --- a/tests/bunit.tests/ComponentParameterCollectionBuilderTests.cs +++ b/tests/bunit.tests/ComponentParameterCollectionBuilderTests.cs @@ -1,5 +1,4 @@ using System.Linq.Expressions; -using Bunit.Rendering; namespace Bunit; @@ -115,7 +114,7 @@ public void Test010() [Fact(DisplayName = "Null to EventCallback throws")] public void Test011() { - Should.Throw(() => Builder.Add(x => x.EC, null!)); + Should.Throw(() => Builder.Add(x => x.EC, (Func)null!)); } [Fact(DisplayName = "Null for EventCallback? with parameter selector")] @@ -225,6 +224,34 @@ public async Task Test025() await VerifyEventCallbackAsync("NullableECWithArgs"); } + [Fact(DisplayName = "Func 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 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 to EventCallback with parameter selector")] + public async Task Test028() + { + Builder.Add(x => x.ECWithArgs, _ => { EventCallbackCalled = true; return Task.CompletedTask; }); + await VerifyEventCallbackAsync("ECWithArgs"); + } + + [Fact(DisplayName = "Func to EventCallback? with parameter selector")] + public async Task Test029() + { + Builder.Add(x => x.NullableECWithArgs, _ => { EventCallbackCalled = true; return Task.CompletedTask; }); + await VerifyEventCallbackAsync("NullableECWithArgs"); + } + [Fact(DisplayName = "ChildContent can be passed as RenderFragment")] public void Test030() {