From 2e1468f9e3685364ffd83096b172d5da6384393b Mon Sep 17 00:00:00 2001 From: Steven Giesel Date: Wed, 19 Jul 2023 10:55:21 +0200 Subject: [PATCH 1/3] fix: New up renderer for markup matches --- .../MarkupMatchesAssertExtensions.cs | 10 +++- tests/.editorconfig | 2 +- .../InvokeAsyncInsideContinueWith.razor | 50 +++++++++++++++++++ .../Asserting/MarkupMatchesTests.razor | 16 ++++++ 4 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 tests/bunit.testassets/SampleComponents/InvokeAsyncInsideContinueWith.razor diff --git a/src/bunit.web/Asserting/MarkupMatchesAssertExtensions.cs b/src/bunit.web/Asserting/MarkupMatchesAssertExtensions.cs index 4d7c2fe5e..a52b1e002 100644 --- a/src/bunit.web/Asserting/MarkupMatchesAssertExtensions.cs +++ b/src/bunit.web/Asserting/MarkupMatchesAssertExtensions.cs @@ -3,6 +3,7 @@ using Bunit.Diffing; using Bunit.Extensions; using Bunit.Rendering; +using Microsoft.Extensions.Logging; namespace Bunit; @@ -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(); - 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(), + actual.Services.GetRequiredService(), + actual.Services.GetRequiredService()); + var renderedFragment = (IRenderedFragment)renderer.RenderFragment(expected); MarkupMatches(actual, renderedFragment, userMessage); } diff --git a/tests/.editorconfig b/tests/.editorconfig index 1e6052f0b..b1ae7b54f 100644 --- a/tests/.editorconfig +++ b/tests/.editorconfig @@ -7,7 +7,7 @@ ########################################## # Code Analyzers Rules ########################################## -[*.{cs,csx,cake}] +[*.{cs,csx,cake,razor}] # AsyncFixer # http://www.asyncfixer.com diff --git a/tests/bunit.testassets/SampleComponents/InvokeAsyncInsideContinueWith.razor b/tests/bunit.testassets/SampleComponents/InvokeAsyncInsideContinueWith.razor new file mode 100644 index 000000000..9be4161b2 --- /dev/null +++ b/tests/bunit.testassets/SampleComponents/InvokeAsyncInsideContinueWith.razor @@ -0,0 +1,50 @@ +@if (Task != null) +{ + @if (Task.IsCompleted) + { + done + } + else + { + waiting + } +} +@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 DelegateTo(Task task) + { + await task;//.ConfigureAwait(false); + return null; + } +} diff --git a/tests/bunit.web.tests/Asserting/MarkupMatchesTests.razor b/tests/bunit.web.tests/Asserting/MarkupMatchesTests.razor index 3596c1a60..4f33df09a 100644 --- a/tests/bunit.web.tests/Asserting/MarkupMatchesTests.razor +++ b/tests/bunit.web.tests/Asserting/MarkupMatchesTests.razor @@ -2,6 +2,7 @@ @inherits TestContext @code { + [Fact] public void MarkupMatchesShouldNotBeBlockedByRenderer() { @@ -15,4 +16,19 @@ cut.WaitForAssertion(() => cut.MarkupMatches(@done)); } + + [Theory] + [Repeat(2)] + public void MarkupMatchesShouldNotBeBlockedByRendererComplex(int repeatCount) + { + var tcs = new TaskCompletionSource(); + + var cut = Render(@ ); + + cut.MarkupMatches(@waiting); + + tcs.SetResult(true); + + cut.WaitForAssertion(() => cut.MarkupMatches(@done)); + } } From 3c04b77877ac092dad966ec8f6fc24a5edabda12 Mon Sep 17 00:00:00 2001 From: Steven Giesel Date: Thu, 20 Jul 2023 19:55:51 +0200 Subject: [PATCH 2/3] revert: Changes for editorconfig --- tests/.editorconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/.editorconfig b/tests/.editorconfig index b1ae7b54f..1e6052f0b 100644 --- a/tests/.editorconfig +++ b/tests/.editorconfig @@ -7,7 +7,7 @@ ########################################## # Code Analyzers Rules ########################################## -[*.{cs,csx,cake,razor}] +[*.{cs,csx,cake}] # AsyncFixer # http://www.asyncfixer.com From 784749d31fa0a9c690be8d6402a9211c2f82f62f Mon Sep 17 00:00:00 2001 From: Steven Giesel Date: Sun, 23 Jul 2023 15:29:52 +0200 Subject: [PATCH 3/3] chore: Suppress xUnit1026 --- tests/bunit.web.tests/Asserting/MarkupMatchesTests.razor | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/bunit.web.tests/Asserting/MarkupMatchesTests.razor b/tests/bunit.web.tests/Asserting/MarkupMatchesTests.razor index 4f33df09a..5c22485fc 100644 --- a/tests/bunit.web.tests/Asserting/MarkupMatchesTests.razor +++ b/tests/bunit.web.tests/Asserting/MarkupMatchesTests.razor @@ -17,6 +17,7 @@ cut.WaitForAssertion(() => cut.MarkupMatches(@done)); } + [SuppressMessage("Usage", "xUnit1026:Theory method does not use parameter")] [Theory] [Repeat(2)] public void MarkupMatchesShouldNotBeBlockedByRendererComplex(int repeatCount)