Skip to content

Commit

Permalink
Change TestContext registration in services and elements to TestConte…
Browse files Browse the repository at this point in the history
…xtBase, added additional smoke tests for handlers triggers
  • Loading branch information
egil committed Nov 23, 2020
1 parent 2249acc commit a7d098c
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 31 deletions.
9 changes: 5 additions & 4 deletions src/bunit.web/Asserting/MarkupMatchesAssertExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using AngleSharp.Dom;
using Bunit.Asserting;
using Bunit.Diffing;
using Bunit.Extensions;
using Bunit.Rendering;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -301,8 +302,8 @@ public static void MarkupMatches(this IRenderedFragment actual, RenderFragment e
if (expected is null)
throw new ArgumentNullException(nameof(expected));

var testContext = actual.Services.GetRequiredService<TestContext>();
var renderedFragment = testContext.Render(expected);
var testContext = actual.Services.GetRequiredService<TestContextBase>();
var renderedFragment = testContext.RenderInsideRenderTree(expected);
MarkupMatches(actual, renderedFragment, userMessage);
}

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

var testContext = actual.GetTestContext() ?? new TestContext();
var renderedFragment = testContext.Render(expected);
var renderedFragment = testContext.RenderInsideRenderTree(expected);
MarkupMatches(actual, renderedFragment, userMessage);
}

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

var testContext = actual.GetTestContext() ?? new TestContext();
var renderedFragment = testContext.Render(expected);
var renderedFragment = testContext.RenderInsideRenderTree(expected);
MarkupMatches(actual, renderedFragment, userMessage);
}

Expand Down
6 changes: 3 additions & 3 deletions src/bunit.web/Extensions/Internal/AngleSharpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public static IEnumerable<INode> AsEnumerable(this INode node)
/// </summary>
/// <param name="node"></param>
/// <returns>The <see cref="TestContext"/> or null if not found.</returns>
public static TestContext? GetTestContext(this INode? node)
public static TestContextBase? GetTestContext(this INode? node)
{
return node?.Owner.Context.GetService<TestContext>();
return node?.Owner.Context.GetService<TestContextBase>();
}

/// <summary>
Expand All @@ -82,7 +82,7 @@ public static IEnumerable<INode> AsEnumerable(this INode node)
/// </summary>
/// <param name="nodes"></param>
/// <returns>The <see cref="TestContext"/> or null if not found.</returns>
public static TestContext? GetTestContext(this INodeList nodes)
public static TestContextBase? GetTestContext(this INodeList nodes)
{
return nodes?.Length > 0 ? nodes[0].GetTestContext() : null;
}
Expand Down
8 changes: 7 additions & 1 deletion src/bunit.web/Extensions/TestServiceProviderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net.Http;
using Bunit.Diffing;
using Bunit.JSInterop;
using Bunit.Rendering;
using Bunit.TestDoubles;
using Microsoft.AspNetCore.Authorization;
Expand All @@ -21,7 +22,7 @@ public static class TestServiceProviderExtensions
/// <summary>
/// Registers the default services required by the web <see cref="TestContext"/>.
/// </summary>
public static IServiceCollection AddDefaultTestContextServices(this IServiceCollection services)
public static IServiceCollection AddDefaultTestContextServices(this IServiceCollection services, TestContextBase testContext, BunitJSInterop jsInterop)
{
// Placeholders and defaults for common Blazor services
services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance);
Expand All @@ -31,7 +32,12 @@ public static IServiceCollection AddDefaultTestContextServices(this IServiceColl
services.AddSingleton<HttpClient, PlaceholderHttpClient>();
services.AddSingleton<IStringLocalizer, PlaceholderStringLocalization>();

// bUnits fake JSInterop
jsInterop.AddBuiltInJSRuntimeInvocationHandlers();
services.AddSingleton<IJSRuntime>(jsInterop.JSRuntime);

// bUnit specific services
services.AddSingleton<TestContextBase>(testContext);
services.AddSingleton<ITestRenderer, WebTestRenderer>();
services.AddSingleton<HtmlComparer>();
services.AddSingleton<BunitHtmlParser>();
Expand Down
5 changes: 1 addition & 4 deletions src/bunit.web/RazorTesting/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ private IReadOnlyList<FragmentBase> TestData
/// </summary>
public Fixture()
{
JSInterop.AddBuiltInJSRuntimeInvocationHandlers();
Services.AddSingleton<IJSRuntime>(JSInterop.JSRuntime);
Services.AddDefaultTestContextServices();
Services.AddDefaultTestContextServices(this, JSInterop);
}

/// <summary>
Expand Down Expand Up @@ -170,7 +168,6 @@ private static IRenderedComponent<TComponent> TryCastTo<TComponent>(IRenderedFra
/// <inheritdoc/>
protected override Task Run()
{
Services.AddDefaultTestContextServices();
return base.Run(this);
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/bunit.web/RazorTesting/SnapshotTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ public class SnapshotTest : RazorTestBase
/// </summary>
public SnapshotTest()
{
JSInterop.AddBuiltInJSRuntimeInvocationHandlers();
Services.AddSingleton<IJSRuntime>(JSInterop.JSRuntime);
Services.AddDefaultTestContextServices();
Services.AddDefaultTestContextServices(this, JSInterop);
}

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

private void VerifySnapshot(string inputHtml, string expectedHtml)
{
using var parser = new BunitHtmlParser();
var parser = Services.GetRequiredService<BunitHtmlParser>();
var inputNodes = parser.Parse(inputHtml);
var expectedNodes = parser.Parse(expectedHtml);

Expand Down
2 changes: 1 addition & 1 deletion src/bunit.web/Rendering/BunitHtmlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public BunitHtmlParser() : this(Configuration.Default.WithCss().With(new HtmlCom
/// Creates an instance of the parser with a AngleSharp context
/// with the <paramref name="testRenderer"/> registered.
/// </summary>
public BunitHtmlParser(ITestRenderer testRenderer, HtmlComparer htmlComparer, TestContext testContext)
public BunitHtmlParser(ITestRenderer testRenderer, HtmlComparer htmlComparer, TestContextBase testContext)
: this(Configuration.Default.WithCss()
.With(testRenderer ?? throw new ArgumentNullException(nameof(testRenderer)))
.With(htmlComparer ?? throw new ArgumentNullException(nameof(htmlComparer)))
Expand Down
5 changes: 1 addition & 4 deletions src/bunit.web/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ public class TestContext : TestContextBase
/// </summary>
public TestContext()
{
Services.AddSingleton(this);
JSInterop.AddBuiltInJSRuntimeInvocationHandlers();
Services.AddSingleton(JSInterop.JSRuntime);
Services.AddDefaultTestContextServices();
Services.AddDefaultTestContextServices(this, JSInterop);
}

/// <summary>
Expand Down
11 changes: 1 addition & 10 deletions tests/bunit.core.tests/Rendering/TestRendererTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,8 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
}
}

public class TestRendererTest
public class TestRendererTest : TestContext
{
private TestServiceProvider Services { get; }

public TestRendererTest()
{
Services = new TestServiceProvider();
Services.AddDefaultTestContextServices();
Services.AddSingleton<ITestRenderer, TestRenderer>();
}

[Fact(DisplayName = "RenderFragment re-throws exception from component")]
public void Test004()
{
Expand Down
15 changes: 15 additions & 0 deletions tests/bunit.web.tests/RazorTesting/FixtureTest.razor
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,18 @@
}
}
</Fixture>

<Fixture Test="Test030" Description="Can raise events from markup rendered with Fixture">
<ComponentUnderTest>
<ClickCounter></ClickCounter>
</ComponentUnderTest>
@code
{
void Test030(Fixture f)
{
f.GetComponentUnderTest()
.Find("button")
.Click();
}
}
</Fixture>
8 changes: 8 additions & 0 deletions tests/bunit.web.tests/TestContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ public void Test033()
.ShouldBe("FOO");
}

[Fact(DisplayName = "Can raise events from markup rendered with TestContext")]
public void Test040()
{
RenderComponent<ClickCounter>()
.Find("button")
.Click();
}

class ReceivesCascadinValue : ComponentBase
{
[CascadingParameter] public string? Value { get; set; }
Expand Down

0 comments on commit a7d098c

Please sign in to comment.