@@ -10,6 +10,8 @@ namespace Bunit.Rendering;
1010/// </summary>
1111public 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/>
0 commit comments