Skip to content

Commit a7d098c

Browse files
committed
Change TestContext registration in services and elements to TestContextBase, added additional smoke tests for handlers triggers
1 parent 2249acc commit a7d098c

10 files changed

Lines changed: 44 additions & 31 deletions

File tree

src/bunit.web/Asserting/MarkupMatchesAssertExtensions.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using AngleSharp.Dom;
33
using Bunit.Asserting;
44
using Bunit.Diffing;
5+
using Bunit.Extensions;
56
using Bunit.Rendering;
67
using Microsoft.AspNetCore.Components;
78
using Microsoft.Extensions.DependencyInjection;
@@ -301,8 +302,8 @@ public static void MarkupMatches(this IRenderedFragment actual, RenderFragment e
301302
if (expected is null)
302303
throw new ArgumentNullException(nameof(expected));
303304

304-
var testContext = actual.Services.GetRequiredService<TestContext>();
305-
var renderedFragment = testContext.Render(expected);
305+
var testContext = actual.Services.GetRequiredService<TestContextBase>();
306+
var renderedFragment = testContext.RenderInsideRenderTree(expected);
306307
MarkupMatches(actual, renderedFragment, userMessage);
307308
}
308309

@@ -323,7 +324,7 @@ public static void MarkupMatches(this INode actual, RenderFragment expected, str
323324
throw new ArgumentNullException(nameof(expected));
324325

325326
var testContext = actual.GetTestContext() ?? new TestContext();
326-
var renderedFragment = testContext.Render(expected);
327+
var renderedFragment = testContext.RenderInsideRenderTree(expected);
327328
MarkupMatches(actual, renderedFragment, userMessage);
328329
}
329330

@@ -344,7 +345,7 @@ public static void MarkupMatches(this INodeList actual, RenderFragment expected,
344345
throw new ArgumentNullException(nameof(expected));
345346

346347
var testContext = actual.GetTestContext() ?? new TestContext();
347-
var renderedFragment = testContext.Render(expected);
348+
var renderedFragment = testContext.RenderInsideRenderTree(expected);
348349
MarkupMatches(actual, renderedFragment, userMessage);
349350
}
350351

src/bunit.web/Extensions/Internal/AngleSharpExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ public static IEnumerable<INode> AsEnumerable(this INode node)
7171
/// </summary>
7272
/// <param name="node"></param>
7373
/// <returns>The <see cref="TestContext"/> or null if not found.</returns>
74-
public static TestContext? GetTestContext(this INode? node)
74+
public static TestContextBase? GetTestContext(this INode? node)
7575
{
76-
return node?.Owner.Context.GetService<TestContext>();
76+
return node?.Owner.Context.GetService<TestContextBase>();
7777
}
7878

7979
/// <summary>
@@ -82,7 +82,7 @@ public static IEnumerable<INode> AsEnumerable(this INode node)
8282
/// </summary>
8383
/// <param name="nodes"></param>
8484
/// <returns>The <see cref="TestContext"/> or null if not found.</returns>
85-
public static TestContext? GetTestContext(this INodeList nodes)
85+
public static TestContextBase? GetTestContext(this INodeList nodes)
8686
{
8787
return nodes?.Length > 0 ? nodes[0].GetTestContext() : null;
8888
}

src/bunit.web/Extensions/TestServiceProviderExtensions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Net.Http;
22
using Bunit.Diffing;
3+
using Bunit.JSInterop;
34
using Bunit.Rendering;
45
using Bunit.TestDoubles;
56
using Microsoft.AspNetCore.Authorization;
@@ -21,7 +22,7 @@ public static class TestServiceProviderExtensions
2122
/// <summary>
2223
/// Registers the default services required by the web <see cref="TestContext"/>.
2324
/// </summary>
24-
public static IServiceCollection AddDefaultTestContextServices(this IServiceCollection services)
25+
public static IServiceCollection AddDefaultTestContextServices(this IServiceCollection services, TestContextBase testContext, BunitJSInterop jsInterop)
2526
{
2627
// Placeholders and defaults for common Blazor services
2728
services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance);
@@ -31,7 +32,12 @@ public static IServiceCollection AddDefaultTestContextServices(this IServiceColl
3132
services.AddSingleton<HttpClient, PlaceholderHttpClient>();
3233
services.AddSingleton<IStringLocalizer, PlaceholderStringLocalization>();
3334

35+
// bUnits fake JSInterop
36+
jsInterop.AddBuiltInJSRuntimeInvocationHandlers();
37+
services.AddSingleton<IJSRuntime>(jsInterop.JSRuntime);
38+
3439
// bUnit specific services
40+
services.AddSingleton<TestContextBase>(testContext);
3541
services.AddSingleton<ITestRenderer, WebTestRenderer>();
3642
services.AddSingleton<HtmlComparer>();
3743
services.AddSingleton<BunitHtmlParser>();

src/bunit.web/RazorTesting/Fixture.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ private IReadOnlyList<FragmentBase> TestData
4242
/// </summary>
4343
public Fixture()
4444
{
45-
JSInterop.AddBuiltInJSRuntimeInvocationHandlers();
46-
Services.AddSingleton<IJSRuntime>(JSInterop.JSRuntime);
47-
Services.AddDefaultTestContextServices();
45+
Services.AddDefaultTestContextServices(this, JSInterop);
4846
}
4947

5048
/// <summary>
@@ -170,7 +168,6 @@ private static IRenderedComponent<TComponent> TryCastTo<TComponent>(IRenderedFra
170168
/// <inheritdoc/>
171169
protected override Task Run()
172170
{
173-
Services.AddDefaultTestContextServices();
174171
return base.Run(this);
175172
}
176173
}

src/bunit.web/RazorTesting/SnapshotTest.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ public class SnapshotTest : RazorTestBase
5353
/// </summary>
5454
public SnapshotTest()
5555
{
56-
JSInterop.AddBuiltInJSRuntimeInvocationHandlers();
57-
Services.AddSingleton<IJSRuntime>(JSInterop.JSRuntime);
58-
Services.AddDefaultTestContextServices();
56+
Services.AddDefaultTestContextServices(this, JSInterop);
5957
}
6058

6159
/// <inheritdoc/>
@@ -79,7 +77,7 @@ protected override async Task Run()
7977

8078
private void VerifySnapshot(string inputHtml, string expectedHtml)
8179
{
82-
using var parser = new BunitHtmlParser();
80+
var parser = Services.GetRequiredService<BunitHtmlParser>();
8381
var inputNodes = parser.Parse(inputHtml);
8482
var expectedNodes = parser.Parse(expectedHtml);
8583

src/bunit.web/Rendering/BunitHtmlParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public BunitHtmlParser() : this(Configuration.Default.WithCss().With(new HtmlCom
3636
/// Creates an instance of the parser with a AngleSharp context
3737
/// with the <paramref name="testRenderer"/> registered.
3838
/// </summary>
39-
public BunitHtmlParser(ITestRenderer testRenderer, HtmlComparer htmlComparer, TestContext testContext)
39+
public BunitHtmlParser(ITestRenderer testRenderer, HtmlComparer htmlComparer, TestContextBase testContext)
4040
: this(Configuration.Default.WithCss()
4141
.With(testRenderer ?? throw new ArgumentNullException(nameof(testRenderer)))
4242
.With(htmlComparer ?? throw new ArgumentNullException(nameof(htmlComparer)))

src/bunit.web/TestContext.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ public class TestContext : TestContextBase
2323
/// </summary>
2424
public TestContext()
2525
{
26-
Services.AddSingleton(this);
27-
JSInterop.AddBuiltInJSRuntimeInvocationHandlers();
28-
Services.AddSingleton(JSInterop.JSRuntime);
29-
Services.AddDefaultTestContextServices();
26+
Services.AddDefaultTestContextServices(this, JSInterop);
3027
}
3128

3229
/// <summary>

tests/bunit.core.tests/Rendering/TestRendererTest.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,8 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
7373
}
7474
}
7575

76-
public class TestRendererTest
76+
public class TestRendererTest : TestContext
7777
{
78-
private TestServiceProvider Services { get; }
79-
80-
public TestRendererTest()
81-
{
82-
Services = new TestServiceProvider();
83-
Services.AddDefaultTestContextServices();
84-
Services.AddSingleton<ITestRenderer, TestRenderer>();
85-
}
86-
8778
[Fact(DisplayName = "RenderFragment re-throws exception from component")]
8879
public void Test004()
8980
{

tests/bunit.web.tests/RazorTesting/FixtureTest.razor

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,18 @@
199199
}
200200
}
201201
</Fixture>
202+
203+
<Fixture Test="Test030" Description="Can raise events from markup rendered with Fixture">
204+
<ComponentUnderTest>
205+
<ClickCounter></ClickCounter>
206+
</ComponentUnderTest>
207+
@code
208+
{
209+
void Test030(Fixture f)
210+
{
211+
f.GetComponentUnderTest()
212+
.Find("button")
213+
.Click();
214+
}
215+
}
216+
</Fixture>

tests/bunit.web.tests/TestContextTest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ public void Test033()
8686
.ShouldBe("FOO");
8787
}
8888

89+
[Fact(DisplayName = "Can raise events from markup rendered with TestContext")]
90+
public void Test040()
91+
{
92+
RenderComponent<ClickCounter>()
93+
.Find("button")
94+
.Click();
95+
}
96+
8997
class ReceivesCascadinValue : ComponentBase
9098
{
9199
[CascadingParameter] public string? Value { get; set; }

0 commit comments

Comments
 (0)