Skip to content

Commit b5f8768

Browse files
committed
refactor: Remove FakeRouter
1 parent cb98b69 commit b5f8768

File tree

4 files changed

+26
-39
lines changed

4 files changed

+26
-39
lines changed

src/bunit.web/Extensions/TestServiceProviderExtensions.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Bunit.Diffing;
22
using Bunit.Rendering;
33
using Bunit.TestDoubles;
4-
using Bunit.TestDoubles.Router;
54
using Microsoft.AspNetCore.Authorization;
65
using Microsoft.AspNetCore.Components.Authorization;
76
using Microsoft.AspNetCore.Components.Routing;
@@ -46,9 +45,7 @@ public static IServiceCollection AddDefaultTestContextServices(this IServiceColl
4645
services.AddSingleton<FakeWebAssemblyHostEnvironment>();
4746
services.AddSingleton<IWebAssemblyHostEnvironment>(s => s.GetRequiredService<FakeWebAssemblyHostEnvironment>());
4847

49-
// bUnits fake Router
50-
services.AddSingleton<FakeRouter>();
51-
48+
services.AddSingleton<ComponentRouteParameterService>();
5249
services.AddSingleton<ComponentRegistry>();
5350

5451
#if NET8_0_OR_GREATER

src/bunit.web/TestContext.cs

+2-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Bunit.Extensions;
22
using Bunit.Rendering;
3-
using Bunit.TestDoubles.Router;
43
using Microsoft.Extensions.Logging;
54

65
namespace Bunit;
@@ -10,8 +9,6 @@ namespace Bunit;
109
/// </summary>
1110
public class TestContext : TestContextBase
1211
{
13-
private FakeRouter? router;
14-
1512
/// <summary>
1613
/// Gets bUnits JSInterop, that allows setting up handlers for <see cref="IJSRuntime.InvokeAsync{TValue}(string, object[])"/> invocations
1714
/// that components under tests will issue during testing. It also makes it possible to verify that the invocations has happened as expected.
@@ -67,14 +64,8 @@ public virtual IRenderedComponent<TComponent> RenderComponent<TComponent>(Action
6764
/// <param name="renderFragment">The render fragment to render.</param>
6865
/// <returns>The <see cref="IRenderedComponent{TComponent}"/>.</returns>
6966
public virtual IRenderedComponent<TComponent> Render<TComponent>(RenderFragment renderFragment)
70-
where TComponent : IComponent
71-
{
72-
// There has to be a better way of having this global thing initialized
73-
// We can't do it in the ctor because we would "materialize" the container, and it would
74-
// throw if the user tries to add a service after the ctor has run.
75-
router ??= Services.GetService<FakeRouter>();
76-
return (IRenderedComponent<TComponent>)this.RenderInsideRenderTree<TComponent>(renderFragment);
77-
}
67+
where TComponent : IComponent =>
68+
(IRenderedComponent<TComponent>)this.RenderInsideRenderTree<TComponent>(renderFragment);
7869

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

87-
/// <inheritdoc/>
88-
protected override void Dispose(bool disposing)
89-
{
90-
if (disposing)
91-
{
92-
router?.Dispose();
93-
}
94-
95-
base.Dispose(disposing);
96-
}
97-
9878
/// <summary>
9979
/// Dummy method required to allow Blazor's compiler to generate
10080
/// C# from .razor files.

src/bunit.web/TestDoubles/Router/FakeRouter.cs renamed to src/bunit.web/TestDoubles/NavigationManager/ComponentRouteParameterService.cs

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
using System.Globalization;
22
using System.Reflection;
33
using Bunit.Rendering;
4-
using Microsoft.AspNetCore.Components.Routing;
54

6-
namespace Bunit.TestDoubles.Router;
5+
namespace Bunit.TestDoubles;
76

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

13-
public FakeRouter(NavigationManager navigationManager, ComponentRegistry componentRegistry)
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="ComponentRouteParameterService"/> class.
17+
/// </summary>
18+
public ComponentRouteParameterService(ComponentRegistry componentRegistry)
1419
{
15-
this.navigationManager = navigationManager;
1620
this.componentRegistry = componentRegistry;
17-
navigationManager.LocationChanged += UpdatePageParameters;
1821
}
1922

20-
public void Dispose() => navigationManager.LocationChanged -= UpdatePageParameters;
21-
22-
private void UpdatePageParameters(object? sender, LocationChangedEventArgs e)
23+
/// <summary>
24+
/// Triggers the components to update their parameters based on the route parameters.
25+
/// </summary>
26+
public void UpdateComponentsWithRouteParameters(Uri uri)
2327
{
24-
var uri = new Uri(e.Location);
28+
_ = uri ?? throw new ArgumentNullException(nameof(uri));
29+
2530
var relativeUri = uri.PathAndQuery;
2631

2732
foreach (var instance in componentRegistry.Components)

src/bunit.web/TestDoubles/NavigationManager/FakeNavigationManager.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Bunit.Rendering;
2-
using Bunit.TestDoubles.Router;
32
using Microsoft.AspNetCore.Components.Routing;
43

54
namespace Bunit.TestDoubles;
@@ -13,6 +12,7 @@ namespace Bunit.TestDoubles;
1312
public sealed class FakeNavigationManager : NavigationManager
1413
{
1514
private readonly TestContextBase testContextBase;
15+
private readonly ComponentRouteParameterService componentRouteParameterService;
1616
private readonly Stack<NavigationHistory> history = new();
1717

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

@@ -65,6 +66,8 @@ protected override void NavigateToCore(string uri, bool forceLoad)
6566
{
6667
BaseUri = GetBaseUri(absoluteUri);
6768
}
69+
70+
componentRouteParameterService.UpdateComponentsWithRouteParameters(absoluteUri);
6871
});
6972
}
7073
#endif
@@ -131,6 +134,8 @@ protected override void NavigateToCore(string uri, NavigationOptions options)
131134
{
132135
BaseUri = GetBaseUri(absoluteUri);
133136
}
137+
138+
componentRouteParameterService.UpdateComponentsWithRouteParameters(absoluteUri);
134139
});
135140
}
136141
#endif

0 commit comments

Comments
 (0)