-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Use NullDispatchTestRenderer for detached rendering
- Loading branch information
1 parent
dced948
commit 411d054
Showing
8 changed files
with
153 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,50 @@ | ||
namespace Bunit.Rendering; | ||
|
||
/// <summary> | ||
/// A dispatcher that directly invokes the work item on the current thread. | ||
/// </summary> | ||
internal sealed class NullDispatcher : Dispatcher | ||
{ | ||
public override bool CheckAccess() => true; | ||
|
||
public override Task InvokeAsync(Action workItem) | ||
{ | ||
if (workItem is null) | ||
{ | ||
throw new ArgumentNullException(nameof(workItem)); | ||
} | ||
|
||
workItem(); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public override Task InvokeAsync(Func<Task> workItem) | ||
{ | ||
if (workItem is null) | ||
{ | ||
throw new ArgumentNullException(nameof(workItem)); | ||
} | ||
|
||
return workItem(); | ||
} | ||
|
||
public override Task<TResult> InvokeAsync<TResult>(Func<TResult> workItem) | ||
{ | ||
if (workItem is null) | ||
{ | ||
throw new ArgumentNullException(nameof(workItem)); | ||
} | ||
|
||
return Task.FromResult(workItem()); | ||
} | ||
|
||
public override Task<TResult> InvokeAsync<TResult>(Func<Task<TResult>> workItem) | ||
{ | ||
if (workItem is null) | ||
{ | ||
throw new ArgumentNullException(nameof(workItem)); | ||
} | ||
|
||
return workItem(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/bunit.web/Rendering/Internal/NullDispatcherTestRenderer.cs
This file contains 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,23 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Bunit.Rendering; | ||
|
||
/// <summary> | ||
/// A renderer that uses a <see cref="NullDispatcher"/> to invoke work items. | ||
/// </summary> | ||
internal sealed class NullDispatcherTestRenderer : TestRenderer | ||
{ | ||
public override Dispatcher Dispatcher { get; } = new NullDispatcher(); | ||
|
||
public NullDispatcherTestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory) | ||
: base(renderedComponentActivator, services, loggerFactory) | ||
{ | ||
} | ||
|
||
#if NET5_0_OR_GREATER | ||
public NullDispatcherTestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory, IComponentActivator componentActivator) | ||
: base(renderedComponentActivator, services, loggerFactory, componentActivator) | ||
{ | ||
} | ||
#endif | ||
} |
This file contains 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
35 changes: 35 additions & 0 deletions
35
tests/bunit.testassets/SampleComponents/InvokeAsyncInsideContinueWith.razor
This file contains 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,35 @@ | ||
@if (Task != null) | ||
{ | ||
@if (Task.IsCompleted) | ||
{ | ||
<span>done</span> | ||
} | ||
else | ||
{ | ||
<span>waiting</span> | ||
} | ||
} | ||
@code { | ||
[Parameter] public Task? Task { get; set; } | ||
|
||
private Task? registeredTask; | ||
|
||
protected override void OnParametersSet() | ||
{ | ||
var task = Task; | ||
if (task != registeredTask) | ||
{ | ||
registeredTask = task; | ||
|
||
_ = task?.ContinueWith((t, o) => | ||
{ | ||
if (t == Task) | ||
{ | ||
_ = InvokeAsync(StateHasChanged); | ||
} | ||
}, null); | ||
} | ||
|
||
base.OnParametersSet(); | ||
} | ||
} |
This file contains 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,20 @@ | ||
@using Bunit.TestAssets.SampleComponents | ||
@inherits TestContext | ||
|
||
@code { | ||
#if NET5_0_OR_GREATER | ||
[Fact] | ||
public void MarkupMatchesShouldNotBeBlockedByRenderer() | ||
{ | ||
var tcs = new TaskCompletionSource(); | ||
|
||
var cut = Render(@<InvokeAsyncInsideContinueWith Task="@tcs.Task"/>); | ||
|
||
cut.MarkupMatches(@<span>waiting</span>); | ||
|
||
tcs.SetCanceled(); | ||
|
||
cut.WaitForAssertion(() => cut.MarkupMatches(@<span>done</span>)); | ||
} | ||
#endif | ||
} |