Skip to content

Commit be813da

Browse files
committed
refactor: Remove RenderedComponentActivator
1 parent 846b0c5 commit be813da

11 files changed

+57
-180
lines changed

src/bunit/Asserting/MarkupMatchesAssertExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,6 @@ public static void MarkupMatches(this RenderedFragment actual, RenderFragment ex
275275
// TODO: This will be obsolete with: https://github.com/bUnit-dev/bUnit/issues/1018
276276
// As the renderer would be transient we don't have to new up an instance
277277
using var renderer = new BunitRenderer(
278-
actual.Services.GetRequiredService<IRenderedComponentActivator>(),
279278
actual.Services.GetRequiredService<TestServiceProvider>(),
280279
actual.Services.GetRequiredService<ILoggerFactory>());
281280
var renderedFragment = renderer.RenderFragment(expected);

src/bunit/ComponentParameterCollectionBuilder.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System.Linq.Expressions;
22
using System.Reflection;
3-
using System.Runtime.CompilerServices;
43
using Bunit.Extensions;
5-
using Bunit.Rendering;
64

75
namespace Bunit;
86

src/bunit/Extensions/BunitRendererExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static RenderedComponent<TComponent> Render<TComponent>(this BunitRendere
2424
if (resultBase is RenderedComponent<TComponent> result)
2525
return result;
2626

27-
throw new InvalidOperationException($"The renderer did not produce the expected type. Is the test renderer using the expected {nameof(IRenderedComponentActivator)}?");
27+
throw new InvalidOperationException($"The renderer did not produce the expected type.");
2828
}
2929

3030
/// <summary>
@@ -45,6 +45,6 @@ public static RenderedComponent<TComponent> Render<TComponent>(this BunitRendere
4545
if (resultBase is RenderedComponent<TComponent> result)
4646
return result;
4747

48-
throw new InvalidOperationException($"The renderer did not produce the expected type. Is the test renderer using the expected {nameof(IRenderedComponentActivator)}?");
48+
throw new InvalidOperationException($"The renderer did not produce the expected type.");
4949
}
5050
}

src/bunit/Extensions/RenderedComponentRenderExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using Bunit.Rendering;
21
using System.Runtime.ExceptionServices;
32

43
namespace Bunit;

src/bunit/Extensions/TestServiceProviderExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public static IServiceCollection AddDefaultTestContextServices(this IServiceColl
5050
services.AddSingleton(testContext);
5151
services.AddSingleton<HtmlComparer>();
5252
services.AddSingleton<BunitHtmlParser>();
53-
services.AddSingleton<IRenderedComponentActivator, RenderedComponentActivator>();
5453

5554
services.AddMemoryCache();
5655

src/bunit/Rendering/BunitRenderer.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace Bunit.Rendering;
1010
/// </summary>
1111
public sealed class BunitRenderer : Renderer
1212
{
13+
private readonly TestServiceProvider services;
14+
1315
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_isBatchInProgress")]
1416
private static extern ref bool GetIsBatchInProgressField(Renderer renderer);
1517

@@ -20,7 +22,6 @@ public sealed class BunitRenderer : Renderer
2022
private readonly Dictionary<int, RenderedFragment> renderedComponents = new();
2123
private readonly List<RootComponent> rootComponents = new();
2224
private readonly ILogger<BunitRenderer> logger;
23-
private readonly IRenderedComponentActivator activator;
2425
private bool disposed;
2526
private TaskCompletionSource<Exception> unhandledExceptionTsc = new(TaskCreationOptions.RunContinuationsAsynchronously);
2627
private Exception? capturedUnhandledException;
@@ -56,22 +57,22 @@ private bool IsBatchInProgress
5657
/// <summary>
5758
/// Initializes a new instance of the <see cref="BunitRenderer"/> class.
5859
/// </summary>
59-
public BunitRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory)
60+
public BunitRenderer(TestServiceProvider services, ILoggerFactory loggerFactory)
6061
: base(services, loggerFactory, new BunitComponentActivator(services.GetRequiredService<ComponentFactoryCollection>(), null))
6162
{
63+
this.services = services;
6264
logger = loggerFactory.CreateLogger<BunitRenderer>();
63-
activator = renderedComponentActivator;
6465
ElementReferenceContext = new WebElementReferenceContext(services.GetRequiredService<IJSRuntime>());
6566
}
6667

6768
/// <summary>
6869
/// Initializes a new instance of the <see cref="BunitRenderer"/> class.
6970
/// </summary>
70-
public BunitRenderer(IRenderedComponentActivator renderedComponentActivator, TestServiceProvider services, ILoggerFactory loggerFactory, IComponentActivator componentActivator)
71+
public BunitRenderer(TestServiceProvider services, ILoggerFactory loggerFactory, IComponentActivator componentActivator)
7172
: base(services, loggerFactory, new BunitComponentActivator(services.GetRequiredService<ComponentFactoryCollection>(), componentActivator))
7273
{
74+
this.services = services;
7375
logger = loggerFactory.CreateLogger<BunitRenderer>();
74-
activator = renderedComponentActivator;
7576
ElementReferenceContext = new WebElementReferenceContext(services.GetRequiredService<IJSRuntime>());
7677
}
7778

@@ -81,7 +82,7 @@ public BunitRenderer(IRenderedComponentActivator renderedComponentActivator, Tes
8182
/// <param name="renderFragment">The <see cref="Microsoft.AspNetCore.Components.RenderFragment"/> to render.</param>
8283
/// <returns>A <see cref="RenderedFragment"/> that provides access to the rendered <paramref name="renderFragment"/>.</returns>
8384
public RenderedFragment RenderFragment(RenderFragment renderFragment)
84-
=> Render(renderFragment, id => activator.CreateRenderedFragment(id));
85+
=> Render(renderFragment);
8586

8687
/// <summary>
8788
/// Renders a <typeparamref name="TComponent"/> with the <paramref name="parameters"/> passed to it.
@@ -95,7 +96,7 @@ public RenderedComponent<TComponent> Render<TComponent>(ComponentParameterCollec
9596
ArgumentNullException.ThrowIfNull(parameters);
9697

9798
var renderFragment = parameters.ToRenderFragment<TComponent>();
98-
return Render(renderFragment, id => activator.CreateRenderedComponent<TComponent>(id));
99+
return Render(renderFragment).FindComponent<TComponent>();
99100
}
100101

101102
/// <summary>
@@ -226,7 +227,6 @@ protected override IComponent ResolveComponentForRenderMode(Type componentType,
226227
return componentActivator.CreateInstance(componentType);
227228
}
228229

229-
/// <inheritdoc/>
230230
internal Task SetDirectParametersAsync(RenderedFragment renderedComponent, ParameterView parameters)
231231
{
232232
ObjectDisposedException.ThrowIf(disposed, this);
@@ -434,8 +434,7 @@ protected override void Dispose(bool disposing)
434434
}
435435
}
436436

437-
private TResult Render<TResult>(RenderFragment renderFragment, Func<int, TResult> activator)
438-
where TResult : RenderedFragment
437+
private RenderedFragment Render(RenderFragment renderFragment)
439438
{
440439
ObjectDisposedException.ThrowIf(disposed, this);
441440

@@ -445,14 +444,14 @@ private TResult Render<TResult>(RenderFragment renderFragment, Func<int, TResult
445444

446445
var root = new RootComponent(renderFragment);
447446
var rootComponentId = AssignRootComponentId(root);
448-
var result = activator(rootComponentId);
447+
var result = new RenderedFragment(rootComponentId, services);
449448
renderedComponents.Add(rootComponentId, result);
450449
rootComponents.Add(root);
451450
root.Render();
452451
return result;
453452
});
454453

455-
TResult result;
454+
RenderedFragment result;
456455

457456
if (!renderTask.IsCompleted)
458457
{
@@ -527,7 +526,7 @@ private RenderedComponent<TComponent> GetOrCreateRenderedComponent<TComponent>(R
527526
}
528527

529528
LoadRenderTreeFrames(componentId, framesCollection);
530-
var result = activator.CreateRenderedComponent(componentId, component, framesCollection);
529+
var result = new RenderedComponent<TComponent>(componentId, component, framesCollection, services);
531530
renderedComponents.Add(result.ComponentId, result);
532531

533532
return result;
@@ -558,13 +557,13 @@ private void LoadRenderTreeFrames(int componentId, RenderTreeFrameDictionary fra
558557
/// </summary>
559558
private ArrayRange<RenderTreeFrame> GetOrLoadRenderTreeFrame(RenderTreeFrameDictionary framesCollection, int componentId)
560559
{
561-
if (!framesCollection.Contains(componentId))
560+
if (!framesCollection.TryGetValue(componentId, out var frames))
562561
{
563-
var frames = GetCurrentRenderTreeFrames(componentId);
562+
frames = GetCurrentRenderTreeFrames(componentId);
564563
framesCollection.Add(componentId, frames);
565564
}
566565

567-
return framesCollection[componentId];
566+
return frames;
568567
}
569568

570569
/// <inheritdoc/>

src/bunit/Rendering/IRenderedComponentActivator.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/bunit/Rendering/RenderedComponentActivator.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/bunit/TestContext.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,11 @@ protected virtual void BuildRenderTree(RenderTreeBuilder builder) { }
177177

178178
private BunitRenderer CreateRenderer()
179179
{
180-
var renderedComponentActivator = Services.GetRequiredService<IRenderedComponentActivator>();
181180
var logger = Services.GetRequiredService<ILoggerFactory>();
182181

183182
var componentActivator = Services.GetService<IComponentActivator>();
184183
return componentActivator is null
185-
? new BunitRenderer(renderedComponentActivator, Services, logger)
186-
: new BunitRenderer(renderedComponentActivator, Services, logger, componentActivator);
184+
? new BunitRenderer(Services, logger)
185+
: new BunitRenderer(Services, logger, componentActivator);
187186
}
188187
}

tests/bunit.tests/Asserting/MarkupMatchesTests.razor

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
@using Bunit.TestAssets.SampleComponents
21
@inherits TestContext
32

43
@code {

0 commit comments

Comments
 (0)