Skip to content

Commit

Permalink
fix: New up renderer for markup matches (#1160)
Browse files Browse the repository at this point in the history
* fix: New up renderer for markup matches

* revert: Changes for editorconfig

* chore: Suppress xUnit1026
  • Loading branch information
linkdotnet authored Jul 23, 2023
1 parent ffd6fa7 commit 76fd92f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/bunit.web/Asserting/MarkupMatchesAssertExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Bunit.Diffing;
using Bunit.Extensions;
using Bunit.Rendering;
using Microsoft.Extensions.Logging;

namespace Bunit;

Expand Down Expand Up @@ -299,8 +300,13 @@ public static void MarkupMatches(this IRenderedFragment actual, RenderFragment e
if (expected is null)
throw new ArgumentNullException(nameof(expected));

var testContext = actual.Services.GetRequiredService<TestContextBase>();
var renderedFragment = (IRenderedFragment)testContext.RenderInsideRenderTree(expected);
// TODO: This will be obsolete with: https://github.com/bUnit-dev/bUnit/issues/1018
// As the renderer would be transient we don't have to new up an instance
using var renderer = new TestRenderer(
actual.Services.GetRequiredService<IRenderedComponentActivator>(),
actual.Services.GetRequiredService<TestServiceProvider>(),
actual.Services.GetRequiredService<ILoggerFactory>());
var renderedFragment = (IRenderedFragment)renderer.RenderFragment(expected);
MarkupMatches(actual, renderedFragment, userMessage);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
@if (Task != null)
{
@if (Task.IsCompleted)
{
<span>done</span>
}
else
{
<span>waiting</span>
}
}
@code {
[Parameter] public Task? Task { get; set; }

private Task? registeredTask;
private Task? delegatedTask;

protected override void OnParametersSet()
{
var task = Task;
if (task != registeredTask)
{
registeredTask = task;
delegatedTask = task == null ? null : DelegateTo(task);
RenderWhenDone();
}

base.OnParametersSet();
}

private async void RenderWhenDone()
{
var task = delegatedTask;
if (task != null)
{
_ = await Task.WhenAny(task).ConfigureAwait(false);

if (task == delegatedTask)
{
_ = InvokeAsync(StateHasChanged);
}
}
}

private static async Task<object?> DelegateTo(Task task)
{
await task;//.ConfigureAwait(false);
return null;
}
}
17 changes: 17 additions & 0 deletions tests/bunit.web.tests/Asserting/MarkupMatchesTests.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@inherits TestContext

@code {

[Fact]
public void MarkupMatchesShouldNotBeBlockedByRenderer()
{
Expand All @@ -15,4 +16,20 @@

cut.WaitForAssertion(() => cut.MarkupMatches(@<span>done</span>));
}

[SuppressMessage("Usage", "xUnit1026:Theory method does not use parameter")]
[Theory]
[Repeat(2)]
public void MarkupMatchesShouldNotBeBlockedByRendererComplex(int repeatCount)
{
var tcs = new TaskCompletionSource<object?>();

var cut = Render(@<InvokeAsyncInsideContinueWith Task="@tcs.Task"/> );

cut.MarkupMatches(@<span>waiting</span>);

tcs.SetResult(true);

cut.WaitForAssertion(() => cut.MarkupMatches(@<span>done</span>));
}
}

0 comments on commit 76fd92f

Please sign in to comment.