From 8edd78f7ea0824310cf75e3c27523e3836ae8164 Mon Sep 17 00:00:00 2001 From: Qwertyluk <64921645+Qwertyluk@users.noreply.github.com> Date: Tue, 28 May 2024 10:26:07 +0200 Subject: [PATCH] fix: Add ComponentParameterCollectionBuilder overloaded methods (#1467) * ComponentParameterCollectionBuilder: Add overloaded methods * CHANGELOG.md: Updated --------- Co-authored-by: Pawel Krzyzak --- CHANGELOG.md | 4 ++ .../ComponentParameterCollectionBuilder.cs | 42 +++++++++++++++++++ ...omponentParameterCollectionBuilderTests.cs | 31 +++++++++++++- 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 298508092..b3925f0d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ All notable changes to **bUnit** will be documented in this file. The project ad - CI build changes to force running verification on x64 based AMD CPUs. +### 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() {