From ffd6fa7e931011e6080b1ae70f897013c6e01b8e Mon Sep 17 00:00:00 2001 From: Steven Giesel Date: Sat, 15 Jul 2023 13:26:10 +0200 Subject: [PATCH] chore: Added test for MarkupMatches to render fragment when renderer is in use (#1157) --- src/bunit.web/Rendering/WebTestRenderer.cs | 43 +++++++++---------- .../SampleComponents/LoadingComponent.razor | 12 ++++++ .../Asserting/MarkupMatchesTests.razor | 18 ++++++++ 3 files changed, 51 insertions(+), 22 deletions(-) create mode 100644 tests/bunit.testassets/SampleComponents/LoadingComponent.razor create mode 100644 tests/bunit.web.tests/Asserting/MarkupMatchesTests.razor diff --git a/src/bunit.web/Rendering/WebTestRenderer.cs b/src/bunit.web/Rendering/WebTestRenderer.cs index 2249abf8e..0e710b50a 100644 --- a/src/bunit.web/Rendering/WebTestRenderer.cs +++ b/src/bunit.web/Rendering/WebTestRenderer.cs @@ -7,34 +7,33 @@ using Microsoft.JSInterop; #endif -namespace Bunit.Rendering +namespace Bunit.Rendering; + +/// +/// Represents a that is used when rendering +/// Blazor components for the web. +/// +public class WebTestRenderer : TestRenderer { /// - /// Represents a that is used when rendering - /// Blazor components for the web. + /// Initializes a new instance of the class. /// - public class WebTestRenderer : TestRenderer + public WebTestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory) + : base(renderedComponentActivator, services, loggerFactory) { - /// - /// Initializes a new instance of the class. - /// - public WebTestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory) - : base(renderedComponentActivator, services, loggerFactory) - { #if NET5_0_OR_GREATER - ElementReferenceContext = new WebElementReferenceContext(services.GetRequiredService()); + ElementReferenceContext = new WebElementReferenceContext(services.GetRequiredService()); #endif - } + } #if NET5_0_OR_GREATER - /// - /// Initializes a new instance of the class. - /// - public WebTestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory, IComponentActivator componentActivator) - : base(renderedComponentActivator, services, loggerFactory, componentActivator) - { - ElementReferenceContext = new WebElementReferenceContext(services.GetRequiredService()); - } -#endif + /// + /// Initializes a new instance of the class. + /// + public WebTestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory, IComponentActivator componentActivator) + : base(renderedComponentActivator, services, loggerFactory, componentActivator) + { + ElementReferenceContext = new WebElementReferenceContext(services.GetRequiredService()); } -} +#endif +} \ No newline at end of file diff --git a/tests/bunit.testassets/SampleComponents/LoadingComponent.razor b/tests/bunit.testassets/SampleComponents/LoadingComponent.razor new file mode 100644 index 000000000..35c9bf469 --- /dev/null +++ b/tests/bunit.testassets/SampleComponents/LoadingComponent.razor @@ -0,0 +1,12 @@ +@text +@code { + [Parameter] + public Task Task { get; set; } + + private string text = "loading"; + protected override async Task OnInitializedAsync() + { + await Task; + text = "done"; + } +} diff --git a/tests/bunit.web.tests/Asserting/MarkupMatchesTests.razor b/tests/bunit.web.tests/Asserting/MarkupMatchesTests.razor new file mode 100644 index 000000000..3596c1a60 --- /dev/null +++ b/tests/bunit.web.tests/Asserting/MarkupMatchesTests.razor @@ -0,0 +1,18 @@ +@using Bunit.TestAssets.SampleComponents +@inherits TestContext + +@code { + [Fact] + public void MarkupMatchesShouldNotBeBlockedByRenderer() + { + var tcs = new TaskCompletionSource(); + + var cut = Render(@ ); + + cut.MarkupMatches(@loading); + + tcs.SetResult(true); + + cut.WaitForAssertion(() => cut.MarkupMatches(@done)); + } +}