Skip to content
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

fix: New up renderer for markup matches #1160

Merged
merged 3 commits into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI you can use ActivatorUtilities.CreateInstance to create a new instance while automatically supplying existing services. Makes it less brittle if new services are needed in the future, too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting one. I would still prefer the explicit new but a nice addition in any case.
Thanks

var renderedFragment = (IRenderedFragment)renderer.RenderFragment(expected);
egil marked this conversation as resolved.
Show resolved Hide resolved
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>));
}
}
Loading