Skip to content

Commit

Permalink
refactor: Remove FakeRouter
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Oct 13, 2024
1 parent 97cff02 commit 720cce1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 39 deletions.
5 changes: 1 addition & 4 deletions src/bunit.web/Extensions/TestServiceProviderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Bunit.Diffing;
using Bunit.Rendering;
using Bunit.TestDoubles;
using Bunit.TestDoubles.Router;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Routing;
Expand Down Expand Up @@ -46,9 +45,7 @@ public static IServiceCollection AddDefaultTestContextServices(this IServiceColl
services.AddSingleton<FakeWebAssemblyHostEnvironment>();
services.AddSingleton<IWebAssemblyHostEnvironment>(s => s.GetRequiredService<FakeWebAssemblyHostEnvironment>());

// bUnits fake Router
services.AddSingleton<FakeRouter>();

services.AddSingleton<ComponentRouteParameterService>();
services.AddSingleton<ComponentRegistry>();

#if NET8_0_OR_GREATER
Expand Down
24 changes: 2 additions & 22 deletions src/bunit.web/TestContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Bunit.Extensions;
using Bunit.Rendering;
using Bunit.TestDoubles.Router;
using Microsoft.Extensions.Logging;

namespace Bunit;
Expand All @@ -10,8 +9,6 @@ namespace Bunit;
/// </summary>
public class TestContext : TestContextBase
{
private FakeRouter? router;

/// <summary>
/// Gets bUnits JSInterop, that allows setting up handlers for <see cref="IJSRuntime.InvokeAsync{TValue}(string, object[])"/> invocations
/// that components under tests will issue during testing. It also makes it possible to verify that the invocations has happened as expected.
Expand Down Expand Up @@ -67,14 +64,8 @@ public virtual IRenderedComponent<TComponent> RenderComponent<TComponent>(Action
/// <param name="renderFragment">The render fragment to render.</param>
/// <returns>The <see cref="IRenderedComponent{TComponent}"/>.</returns>
public virtual IRenderedComponent<TComponent> Render<TComponent>(RenderFragment renderFragment)
where TComponent : IComponent
{
// There has to be a better way of having this global thing initialized
// We can't do it in the ctor because we would "materialize" the container, and it would
// throw if the user tries to add a service after the ctor has run.
router ??= Services.GetService<FakeRouter>();
return (IRenderedComponent<TComponent>)this.RenderInsideRenderTree<TComponent>(renderFragment);
}
where TComponent : IComponent =>
(IRenderedComponent<TComponent>)this.RenderInsideRenderTree<TComponent>(renderFragment);

/// <summary>
/// Renders the <paramref name="renderFragment"/> and returns it as a <see cref="IRenderedFragment"/>.
Expand All @@ -84,17 +75,6 @@ public virtual IRenderedComponent<TComponent> Render<TComponent>(RenderFragment
public virtual IRenderedFragment Render(RenderFragment renderFragment)
=> (IRenderedFragment)this.RenderInsideRenderTree(renderFragment);

/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (disposing)
{
router?.Dispose();
}

base.Dispose(disposing);
}

/// <summary>
/// Dummy method required to allow Blazor's compiler to generate
/// C# from .razor files.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
using System.Globalization;
using System.Reflection;
using Bunit.Rendering;
using Microsoft.AspNetCore.Components.Routing;

namespace Bunit.TestDoubles.Router;
namespace Bunit.TestDoubles;

internal sealed class FakeRouter : IDisposable
/// <summary>
/// This is an internal service that is used to update components with route parameters.
/// It is not meant to be used outside bUnit internal classes.
/// </summary>
public sealed class ComponentRouteParameterService
{
private readonly NavigationManager navigationManager;
private readonly ComponentRegistry componentRegistry;

public FakeRouter(NavigationManager navigationManager, ComponentRegistry componentRegistry)
/// <summary>
/// Initializes a new instance of the <see cref="ComponentRouteParameterService"/> class.
/// </summary>
public ComponentRouteParameterService(ComponentRegistry componentRegistry)
{
this.navigationManager = navigationManager;
this.componentRegistry = componentRegistry;
navigationManager.LocationChanged += UpdatePageParameters;
}

public void Dispose() => navigationManager.LocationChanged -= UpdatePageParameters;

private void UpdatePageParameters(object? sender, LocationChangedEventArgs e)
/// <summary>
/// Triggers the components to update their parameters based on the route parameters.
/// </summary>
public void UpdateComponentsWithRouteParameters(Uri uri)
{
var uri = new Uri(e.Location);
_ = uri ?? throw new ArgumentNullException(nameof(uri));

var relativeUri = uri.PathAndQuery;

foreach (var instance in componentRegistry.Components)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Bunit.Rendering;
using Bunit.TestDoubles.Router;
using Microsoft.AspNetCore.Components.Routing;

namespace Bunit.TestDoubles;
Expand All @@ -13,6 +12,7 @@ namespace Bunit.TestDoubles;
public sealed class FakeNavigationManager : NavigationManager
{
private readonly TestContextBase testContextBase;
private readonly ComponentRouteParameterService componentRouteParameterService;
private readonly Stack<NavigationHistory> history = new();

/// <summary>
Expand All @@ -28,9 +28,10 @@ public sealed class FakeNavigationManager : NavigationManager
/// Initializes a new instance of the <see cref="FakeNavigationManager"/> class.
/// </summary>
[SuppressMessage("Minor Code Smell", "S1075:URIs should not be hardcoded", Justification = "By design. Fake navigation manager defaults to local host as base URI.")]
public FakeNavigationManager(TestContextBase testContextBase)
public FakeNavigationManager(TestContextBase testContextBase, ComponentRouteParameterService componentRouteParameterService)
{
this.testContextBase = testContextBase;
this.componentRouteParameterService = componentRouteParameterService;
Initialize("http://localhost/", "http://localhost/");
}

Expand Down Expand Up @@ -65,6 +66,8 @@ protected override void NavigateToCore(string uri, bool forceLoad)
{
BaseUri = GetBaseUri(absoluteUri);
}

componentRouteParameterService.UpdateComponentsWithRouteParameters(absoluteUri);
});
}
#endif
Expand Down Expand Up @@ -131,6 +134,8 @@ protected override void NavigateToCore(string uri, NavigationOptions options)
{
BaseUri = GetBaseUri(absoluteUri);
}

componentRouteParameterService.UpdateComponentsWithRouteParameters(absoluteUri);
});
}
#endif
Expand Down

0 comments on commit 720cce1

Please sign in to comment.