Skip to content

Commit

Permalink
chore: Added test for MarkupMatches to render fragment when renderer …
Browse files Browse the repository at this point in the history
…is in use (#1157)
  • Loading branch information
linkdotnet committed Jul 15, 2023
1 parent 4cb7458 commit ffd6fa7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
43 changes: 21 additions & 22 deletions src/bunit.web/Rendering/WebTestRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,33 @@
using Microsoft.JSInterop;
#endif

namespace Bunit.Rendering
namespace Bunit.Rendering;

/// <summary>
/// Represents a <see cref="ITestRenderer"/> that is used when rendering
/// Blazor components for the web.
/// </summary>
public class WebTestRenderer : TestRenderer
{
/// <summary>
/// Represents a <see cref="ITestRenderer"/> that is used when rendering
/// Blazor components for the web.
/// Initializes a new instance of the <see cref="WebTestRenderer"/> class.
/// </summary>
public class WebTestRenderer : TestRenderer
public WebTestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory)
: base(renderedComponentActivator, services, loggerFactory)
{
/// <summary>
/// Initializes a new instance of the <see cref="WebTestRenderer"/> class.
/// </summary>
public WebTestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory)
: base(renderedComponentActivator, services, loggerFactory)
{
#if NET5_0_OR_GREATER
ElementReferenceContext = new WebElementReferenceContext(services.GetRequiredService<IJSRuntime>());
ElementReferenceContext = new WebElementReferenceContext(services.GetRequiredService<IJSRuntime>());
#endif
}
}

#if NET5_0_OR_GREATER
/// <summary>
/// Initializes a new instance of the <see cref="WebTestRenderer"/> class.
/// </summary>
public WebTestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory, IComponentActivator componentActivator)
: base(renderedComponentActivator, services, loggerFactory, componentActivator)
{
ElementReferenceContext = new WebElementReferenceContext(services.GetRequiredService<IJSRuntime>());
}
#endif
/// <summary>
/// Initializes a new instance of the <see cref="WebTestRenderer"/> class.
/// </summary>
public WebTestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory, IComponentActivator componentActivator)
: base(renderedComponentActivator, services, loggerFactory, componentActivator)
{
ElementReferenceContext = new WebElementReferenceContext(services.GetRequiredService<IJSRuntime>());
}
}
#endif
}
12 changes: 12 additions & 0 deletions tests/bunit.testassets/SampleComponents/LoadingComponent.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<span>@text</span>
@code {
[Parameter]
public Task Task { get; set; }

private string text = "loading";
protected override async Task OnInitializedAsync()
{
await Task;
text = "done";
}
}
18 changes: 18 additions & 0 deletions tests/bunit.web.tests/Asserting/MarkupMatchesTests.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@using Bunit.TestAssets.SampleComponents
@inherits TestContext

@code {
[Fact]
public void MarkupMatchesShouldNotBeBlockedByRenderer()
{
var tcs = new TaskCompletionSource<object?>();

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

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

tcs.SetResult(true);

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

0 comments on commit ffd6fa7

Please sign in to comment.