@@ -10,6 +10,8 @@ namespace Bunit.Rendering;
10
10
/// </summary>
11
11
public sealed class BunitRenderer : Renderer
12
12
{
13
+ private readonly TestServiceProvider services ;
14
+
13
15
[ UnsafeAccessor ( UnsafeAccessorKind . Field , Name = "_isBatchInProgress" ) ]
14
16
private static extern ref bool GetIsBatchInProgressField ( Renderer renderer ) ;
15
17
@@ -20,7 +22,6 @@ public sealed class BunitRenderer : Renderer
20
22
private readonly Dictionary < int , RenderedFragment > renderedComponents = new ( ) ;
21
23
private readonly List < RootComponent > rootComponents = new ( ) ;
22
24
private readonly ILogger < BunitRenderer > logger ;
23
- private readonly IRenderedComponentActivator activator ;
24
25
private bool disposed ;
25
26
private TaskCompletionSource < Exception > unhandledExceptionTsc = new ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
26
27
private Exception ? capturedUnhandledException ;
@@ -56,22 +57,22 @@ private bool IsBatchInProgress
56
57
/// <summary>
57
58
/// Initializes a new instance of the <see cref="BunitRenderer"/> class.
58
59
/// </summary>
59
- public BunitRenderer ( IRenderedComponentActivator renderedComponentActivator , TestServiceProvider services , ILoggerFactory loggerFactory )
60
+ public BunitRenderer ( TestServiceProvider services , ILoggerFactory loggerFactory )
60
61
: base ( services , loggerFactory , new BunitComponentActivator ( services . GetRequiredService < ComponentFactoryCollection > ( ) , null ) )
61
62
{
63
+ this . services = services ;
62
64
logger = loggerFactory . CreateLogger < BunitRenderer > ( ) ;
63
- activator = renderedComponentActivator ;
64
65
ElementReferenceContext = new WebElementReferenceContext ( services . GetRequiredService < IJSRuntime > ( ) ) ;
65
66
}
66
67
67
68
/// <summary>
68
69
/// Initializes a new instance of the <see cref="BunitRenderer"/> class.
69
70
/// </summary>
70
- public BunitRenderer ( IRenderedComponentActivator renderedComponentActivator , TestServiceProvider services , ILoggerFactory loggerFactory , IComponentActivator componentActivator )
71
+ public BunitRenderer ( TestServiceProvider services , ILoggerFactory loggerFactory , IComponentActivator componentActivator )
71
72
: base ( services , loggerFactory , new BunitComponentActivator ( services . GetRequiredService < ComponentFactoryCollection > ( ) , componentActivator ) )
72
73
{
74
+ this . services = services ;
73
75
logger = loggerFactory . CreateLogger < BunitRenderer > ( ) ;
74
- activator = renderedComponentActivator ;
75
76
ElementReferenceContext = new WebElementReferenceContext ( services . GetRequiredService < IJSRuntime > ( ) ) ;
76
77
}
77
78
@@ -81,7 +82,7 @@ public BunitRenderer(IRenderedComponentActivator renderedComponentActivator, Tes
81
82
/// <param name="renderFragment">The <see cref="Microsoft.AspNetCore.Components.RenderFragment"/> to render.</param>
82
83
/// <returns>A <see cref="RenderedFragment"/> that provides access to the rendered <paramref name="renderFragment"/>.</returns>
83
84
public RenderedFragment RenderFragment ( RenderFragment renderFragment )
84
- => Render ( renderFragment , id => activator . CreateRenderedFragment ( id ) ) ;
85
+ => Render ( renderFragment ) ;
85
86
86
87
/// <summary>
87
88
/// Renders a <typeparamref name="TComponent"/> with the <paramref name="parameters"/> passed to it.
@@ -95,7 +96,7 @@ public RenderedComponent<TComponent> Render<TComponent>(ComponentParameterCollec
95
96
ArgumentNullException . ThrowIfNull ( parameters ) ;
96
97
97
98
var renderFragment = parameters . ToRenderFragment < TComponent > ( ) ;
98
- return Render ( renderFragment , id => activator . CreateRenderedComponent < TComponent > ( id ) ) ;
99
+ return Render ( renderFragment ) . FindComponent < TComponent > ( ) ;
99
100
}
100
101
101
102
/// <summary>
@@ -226,7 +227,6 @@ protected override IComponent ResolveComponentForRenderMode(Type componentType,
226
227
return componentActivator . CreateInstance ( componentType ) ;
227
228
}
228
229
229
- /// <inheritdoc/>
230
230
internal Task SetDirectParametersAsync ( RenderedFragment renderedComponent , ParameterView parameters )
231
231
{
232
232
ObjectDisposedException . ThrowIf ( disposed , this ) ;
@@ -434,8 +434,7 @@ protected override void Dispose(bool disposing)
434
434
}
435
435
}
436
436
437
- private TResult Render < TResult > ( RenderFragment renderFragment , Func < int , TResult > activator )
438
- where TResult : RenderedFragment
437
+ private RenderedFragment Render ( RenderFragment renderFragment )
439
438
{
440
439
ObjectDisposedException . ThrowIf ( disposed , this ) ;
441
440
@@ -445,14 +444,14 @@ private TResult Render<TResult>(RenderFragment renderFragment, Func<int, TResult
445
444
446
445
var root = new RootComponent ( renderFragment ) ;
447
446
var rootComponentId = AssignRootComponentId ( root ) ;
448
- var result = activator ( rootComponentId ) ;
447
+ var result = new RenderedFragment ( rootComponentId , services ) ;
449
448
renderedComponents . Add ( rootComponentId , result ) ;
450
449
rootComponents . Add ( root ) ;
451
450
root . Render ( ) ;
452
451
return result ;
453
452
} ) ;
454
453
455
- TResult result ;
454
+ RenderedFragment result ;
456
455
457
456
if ( ! renderTask . IsCompleted )
458
457
{
@@ -527,7 +526,7 @@ private RenderedComponent<TComponent> GetOrCreateRenderedComponent<TComponent>(R
527
526
}
528
527
529
528
LoadRenderTreeFrames ( componentId , framesCollection ) ;
530
- var result = activator . CreateRenderedComponent ( componentId , component , framesCollection ) ;
529
+ var result = new RenderedComponent < TComponent > ( componentId , component , framesCollection , services ) ;
531
530
renderedComponents . Add ( result . ComponentId , result ) ;
532
531
533
532
return result ;
@@ -558,13 +557,13 @@ private void LoadRenderTreeFrames(int componentId, RenderTreeFrameDictionary fra
558
557
/// </summary>
559
558
private ArrayRange < RenderTreeFrame > GetOrLoadRenderTreeFrame ( RenderTreeFrameDictionary framesCollection , int componentId )
560
559
{
561
- if ( ! framesCollection . Contains ( componentId ) )
560
+ if ( ! framesCollection . TryGetValue ( componentId , out var frames ) )
562
561
{
563
- var frames = GetCurrentRenderTreeFrames ( componentId ) ;
562
+ frames = GetCurrentRenderTreeFrames ( componentId ) ;
564
563
framesCollection . Add ( componentId , frames ) ;
565
564
}
566
565
567
- return framesCollection [ componentId ] ;
566
+ return frames ;
568
567
}
569
568
570
569
/// <inheritdoc/>
0 commit comments