Skip to content

Commit ffd6fa7

Browse files
authored
chore: Added test for MarkupMatches to render fragment when renderer is in use (#1157)
1 parent 4cb7458 commit ffd6fa7

3 files changed

Lines changed: 51 additions & 22 deletions

File tree

src/bunit.web/Rendering/WebTestRenderer.cs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,33 @@
77
using Microsoft.JSInterop;
88
#endif
99

10-
namespace Bunit.Rendering
10+
namespace Bunit.Rendering;
11+
12+
/// <summary>
13+
/// Represents a <see cref="ITestRenderer"/> that is used when rendering
14+
/// Blazor components for the web.
15+
/// </summary>
16+
public class WebTestRenderer : TestRenderer
1117
{
1218
/// <summary>
13-
/// Represents a <see cref="ITestRenderer"/> that is used when rendering
14-
/// Blazor components for the web.
19+
/// Initializes a new instance of the <see cref="WebTestRenderer"/> class.
1520
/// </summary>
16-
public class WebTestRenderer : TestRenderer
21+
public WebTestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory)
22+
: base(renderedComponentActivator, services, loggerFactory)
1723
{
18-
/// <summary>
19-
/// Initializes a new instance of the <see cref="WebTestRenderer"/> class.
20-
/// </summary>
21-
public WebTestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory)
22-
: base(renderedComponentActivator, services, loggerFactory)
23-
{
2424
#if NET5_0_OR_GREATER
25-
ElementReferenceContext = new WebElementReferenceContext(services.GetRequiredService<IJSRuntime>());
25+
ElementReferenceContext = new WebElementReferenceContext(services.GetRequiredService<IJSRuntime>());
2626
#endif
27-
}
27+
}
2828

2929
#if NET5_0_OR_GREATER
30-
/// <summary>
31-
/// Initializes a new instance of the <see cref="WebTestRenderer"/> class.
32-
/// </summary>
33-
public WebTestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory, IComponentActivator componentActivator)
34-
: base(renderedComponentActivator, services, loggerFactory, componentActivator)
35-
{
36-
ElementReferenceContext = new WebElementReferenceContext(services.GetRequiredService<IJSRuntime>());
37-
}
38-
#endif
30+
/// <summary>
31+
/// Initializes a new instance of the <see cref="WebTestRenderer"/> class.
32+
/// </summary>
33+
public WebTestRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory, IComponentActivator componentActivator)
34+
: base(renderedComponentActivator, services, loggerFactory, componentActivator)
35+
{
36+
ElementReferenceContext = new WebElementReferenceContext(services.GetRequiredService<IJSRuntime>());
3937
}
40-
}
38+
#endif
39+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<span>@text</span>
2+
@code {
3+
[Parameter]
4+
public Task Task { get; set; }
5+
6+
private string text = "loading";
7+
protected override async Task OnInitializedAsync()
8+
{
9+
await Task;
10+
text = "done";
11+
}
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@using Bunit.TestAssets.SampleComponents
2+
@inherits TestContext
3+
4+
@code {
5+
[Fact]
6+
public void MarkupMatchesShouldNotBeBlockedByRenderer()
7+
{
8+
var tcs = new TaskCompletionSource<object?>();
9+
10+
var cut = Render(@<LoadingComponent Task="@tcs.Task"/> );
11+
12+
cut.MarkupMatches(@<span>loading</span>);
13+
14+
tcs.SetResult(true);
15+
16+
cut.WaitForAssertion(() => cut.MarkupMatches(@<span>done</span>));
17+
}
18+
}

0 commit comments

Comments
 (0)