-
-
Notifications
You must be signed in to change notification settings - Fork 116
Add InvokeAsync overload #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fb20acd
Add InvokeAsync overloads
JeroenBos 14d3b80
Add tests
JeroenBos b48b297
Make methods on IRenderedComponentBase`1 awaitable
JeroenBos 4a481d0
Revert "Make methods on IRenderedComponentBase`1 awaitable"
JeroenBos ed65320
Refactored InvokeAsync and Render* methods out of IRenderedComponentB…
egil 62b9650
Update src/bunit.core/Extensions/RenderedComponentInvokeAsyncExtensio…
egil d15adc9
update to docs
egil c99427e
Update CHANGELOG.md
egil 8d24220
Update CHANGELOG.md
egil 05a0632
Update CHANGELOG.md
egil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/bunit.core.tests/Extensions/RenderedComponentInvokeAsyncExtensionsTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using Bunit.TestAssets.SampleComponents; | ||
| using Shouldly; | ||
| using Xunit; | ||
|
|
||
| namespace Bunit.Extensions | ||
| { | ||
| public class RenderedComponentInvokeAsyncExtensionsTest : TestContext | ||
egil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| [Fact(DisplayName = "Dispatcher awaits Task-returning callback")] | ||
| public async Task Test003() | ||
| { | ||
| // Arrange | ||
| var cut = RenderComponent<Simple1>(); | ||
| bool delegateFinished = false; | ||
|
|
||
| async Task Callback() | ||
| { | ||
| await Task.Delay(10); | ||
| delegateFinished = true; | ||
| } | ||
|
|
||
| // Act | ||
| await cut.InvokeAsync(Callback); | ||
|
|
||
| // Assert | ||
| delegateFinished.ShouldBeTrue(); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Dispatcher does not await void-returning callback")] | ||
| public async Task Test004() | ||
| { | ||
| // Arrange | ||
| var cut = RenderComponent<Simple1>(); | ||
| bool delegateFinished = false; | ||
|
|
||
| async void Callback() | ||
| { | ||
| await Task.Delay(10); | ||
| delegateFinished = true; | ||
| } | ||
|
|
||
| // Act | ||
| await cut.InvokeAsync(Callback); | ||
|
|
||
| // Assert | ||
| delegateFinished.ShouldBeFalse(); | ||
| } | ||
| } | ||
| } | ||
44 changes: 44 additions & 0 deletions
44
src/bunit.core/Extensions/RenderedComponentInvokeAsyncExtension.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| using System; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using Bunit.Rendering; | ||
| using Microsoft.AspNetCore.Components; | ||
|
|
||
| namespace Bunit | ||
| { | ||
| /// <summary> | ||
| /// InvokeAsync extensions methods on <see cref="IRenderedComponentBase{TComponent}"/>. | ||
| /// </summary> | ||
| public static class RenderedComponentInvokeAsyncExtensions | ||
| { | ||
| /// <summary> | ||
| /// Invokes the given <paramref name="callback"/> in the context of the associated <see cref="ITestRenderer"/>. | ||
| /// </summary> | ||
| /// <param name="renderedComponent">The rendered component whose dispatcher to invoke with.</param> | ||
| /// <param name="callback"></param> | ||
| /// <returns>A <see cref="Task"/> that will be completed when the action has finished executing or is suspended by an asynchronous operation.</returns> | ||
| public static Task InvokeAsync<TComponent>(this IRenderedComponentBase<TComponent> renderedComponent, Action callback) | ||
| where TComponent : IComponent | ||
| { | ||
| if (renderedComponent is null) | ||
| throw new ArgumentNullException(nameof(renderedComponent)); | ||
|
|
||
| return renderedComponent.Renderer.Dispatcher.InvokeAsync(callback); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Invokes the given <paramref name="callback"/> in the context of the associated <see cref="ITestRenderer"/>. | ||
| /// </summary> | ||
| /// <param name="renderedComponent">The rendered component whose dispatcher to invoke with.</param> | ||
| /// <param name="callback"></param> | ||
| /// <returns>A <see cref="Task"/> that will be completed when the action has finished executing.</returns> | ||
| public static Task InvokeAsync<TComponent>(this IRenderedComponentBase<TComponent> renderedComponent, Func<Task> callback) | ||
| where TComponent : IComponent | ||
| { | ||
| if (renderedComponent is null) | ||
| throw new ArgumentNullException(nameof(renderedComponent)); | ||
|
|
||
| return renderedComponent.Renderer.Dispatcher.InvokeAsync(callback); | ||
| } | ||
| } | ||
| } |
87 changes: 87 additions & 0 deletions
87
src/bunit.core/Extensions/RenderedComponentRenderExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Bunit.Rendering; | ||
| using Microsoft.AspNetCore.Components; | ||
|
|
||
| namespace Bunit | ||
| { | ||
| /// <summary> | ||
| /// Re-render extension methods, optionally with new parameters, for <see cref="IRenderedComponentBase{TComponent}"/>. | ||
| /// </summary> | ||
| public static class RenderedComponentRenderExtensions | ||
| { | ||
| /// <summary> | ||
| /// Render the component under test again. | ||
| /// </summary> | ||
| /// <param name="renderedComponent">The rendered component to re-render.</param> | ||
| public static void Render<TComponent>(this IRenderedComponentBase<TComponent> renderedComponent) where TComponent : IComponent | ||
| => SetParametersAndRender(renderedComponent, ParameterView.Empty); | ||
|
|
||
| /// <summary> | ||
| /// Render the component under test again with the provided <paramref name="parameters"/>. | ||
| /// </summary> | ||
| /// <param name="renderedComponent">The rendered component to re-render with new parameters</param> | ||
| /// <param name="parameters">Parameters to pass to the component upon rendered</param> | ||
| public static void SetParametersAndRender<TComponent>(this IRenderedComponentBase<TComponent> renderedComponent, ParameterView parameters) | ||
| where TComponent : IComponent | ||
| { | ||
| if (renderedComponent is null) | ||
| throw new ArgumentNullException(nameof(renderedComponent)); | ||
|
|
||
| renderedComponent.InvokeAsync(() => | ||
| { | ||
| renderedComponent.Instance.SetParametersAsync(parameters); | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Render the component under test again with the provided <paramref name="parameters"/>. | ||
| /// </summary> | ||
| /// <param name="renderedComponent">The rendered component to re-render with new parameters</param> | ||
| /// <param name="parameters">Parameters to pass to the component upon rendered</param> | ||
| public static void SetParametersAndRender<TComponent>(this IRenderedComponentBase<TComponent> renderedComponent, params ComponentParameter[] parameters) where TComponent : IComponent | ||
| { | ||
| if (renderedComponent is null) | ||
| throw new ArgumentNullException(nameof(renderedComponent)); | ||
| SetParametersAndRender(renderedComponent, ToParameterView(parameters)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Render the component under test again with the provided parameters from the <paramref name="parameterBuilder"/>. | ||
| /// </summary> | ||
| /// <param name="renderedComponent">The rendered component to re-render with new parameters</param> | ||
| /// <param name="parameterBuilder">An action that receives a <see cref="ComponentParameterBuilder{TComponent}"/>.</param> | ||
| public static void SetParametersAndRender<TComponent>(this IRenderedComponentBase<TComponent> renderedComponent, Action<ComponentParameterBuilder<TComponent>> parameterBuilder) | ||
| where TComponent : IComponent | ||
| { | ||
| if (renderedComponent is null) | ||
| throw new ArgumentNullException(nameof(renderedComponent)); | ||
| if (parameterBuilder is null) | ||
| throw new ArgumentNullException(nameof(parameterBuilder)); | ||
|
|
||
| var builder = new ComponentParameterBuilder<TComponent>(); | ||
| parameterBuilder(builder); | ||
|
|
||
| SetParametersAndRender(renderedComponent, ToParameterView(builder.Build())); | ||
| } | ||
|
|
||
|
|
||
| private static ParameterView ToParameterView(IReadOnlyList<ComponentParameter> parameters) | ||
| { | ||
| var parameterView = ParameterView.Empty; | ||
| if (parameters.Any()) | ||
| { | ||
| var paramDict = new Dictionary<string, object?>(); | ||
| foreach (var param in parameters) | ||
| { | ||
| if (param.IsCascadingValue) | ||
| throw new InvalidOperationException($"You cannot provide a new cascading value through the {nameof(SetParametersAndRender)} method."); | ||
| paramDict.Add(param.Name!, param.Value); | ||
| } | ||
| parameterView = ParameterView.FromDictionary(paramDict); | ||
| } | ||
| return parameterView; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.