6
6
namespace Bunit . Rendering ;
7
7
8
8
/// <summary>
9
- /// Represents a bUnit <see cref="TestRenderer "/> used to render Blazor components and fragments during bUnit tests.
9
+ /// Represents a bUnit <see cref="BunitRenderer "/> used to render Blazor components and fragments during bUnit tests.
10
10
/// </summary>
11
- public sealed class TestRenderer : Renderer
11
+ public sealed class BunitRenderer : Renderer
12
12
{
13
13
[ UnsafeAccessor ( UnsafeAccessorKind . Field , Name = "_isBatchInProgress" ) ]
14
- extern static ref bool GetIsBatchInProgressField ( Renderer renderer ) ;
14
+ private static extern ref bool GetIsBatchInProgressField ( Renderer renderer ) ;
15
15
16
16
[ UnsafeAccessor ( UnsafeAccessorKind . Method , Name = "SetDirectParameters" ) ]
17
- extern static void CallSetDirectParameters ( ComponentState componentState , ParameterView parameters ) ;
17
+ private static extern void CallSetDirectParameters ( ComponentState componentState , ParameterView parameters ) ;
18
18
19
19
private readonly object renderTreeUpdateLock = new ( ) ;
20
20
private readonly Dictionary < int , RenderedFragment > renderedComponents = new ( ) ;
21
21
private readonly List < RootComponent > rootComponents = new ( ) ;
22
- private readonly ILogger < TestRenderer > logger ;
22
+ private readonly ILogger < BunitRenderer > logger ;
23
23
private readonly IRenderedComponentActivator activator ;
24
24
private bool disposed ;
25
25
private TaskCompletionSource < Exception > unhandledExceptionTsc = new ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
@@ -39,7 +39,10 @@ private bool IsBatchInProgress
39
39
}
40
40
}
41
41
42
- /// <inheritdoc/>
42
+ /// <summary>
43
+ /// Gets a <see cref="Task{Exception}"/>, which completes when an unhandled exception
44
+ /// is thrown during the rendering of a component, that is caught by the renderer.
45
+ /// </summary>
43
46
public Task < Exception > UnhandledException => unhandledExceptionTsc . Task ;
44
47
45
48
/// <inheritdoc/>
@@ -51,32 +54,41 @@ private bool IsBatchInProgress
51
54
internal int RenderCount { get ; private set ; }
52
55
53
56
/// <summary>
54
- /// Initializes a new instance of the <see cref="TestRenderer "/> class.
57
+ /// Initializes a new instance of the <see cref="BunitRenderer "/> class.
55
58
/// </summary>
56
- public TestRenderer ( IRenderedComponentActivator renderedComponentActivator , TestServiceProvider services , ILoggerFactory loggerFactory )
59
+ public BunitRenderer ( IRenderedComponentActivator renderedComponentActivator , TestServiceProvider services , ILoggerFactory loggerFactory )
57
60
: base ( services , loggerFactory , new BunitComponentActivator ( services . GetRequiredService < ComponentFactoryCollection > ( ) , null ) )
58
61
{
59
- logger = loggerFactory . CreateLogger < TestRenderer > ( ) ;
62
+ logger = loggerFactory . CreateLogger < BunitRenderer > ( ) ;
60
63
activator = renderedComponentActivator ;
61
64
ElementReferenceContext = new WebElementReferenceContext ( services . GetRequiredService < IJSRuntime > ( ) ) ;
62
65
}
63
66
64
67
/// <summary>
65
- /// Initializes a new instance of the <see cref="TestRenderer "/> class.
68
+ /// Initializes a new instance of the <see cref="BunitRenderer "/> class.
66
69
/// </summary>
67
- public TestRenderer ( IRenderedComponentActivator renderedComponentActivator , TestServiceProvider services , ILoggerFactory loggerFactory , IComponentActivator componentActivator )
70
+ public BunitRenderer ( IRenderedComponentActivator renderedComponentActivator , TestServiceProvider services , ILoggerFactory loggerFactory , IComponentActivator componentActivator )
68
71
: base ( services , loggerFactory , new BunitComponentActivator ( services . GetRequiredService < ComponentFactoryCollection > ( ) , componentActivator ) )
69
72
{
70
- logger = loggerFactory . CreateLogger < TestRenderer > ( ) ;
73
+ logger = loggerFactory . CreateLogger < BunitRenderer > ( ) ;
71
74
activator = renderedComponentActivator ;
72
75
ElementReferenceContext = new WebElementReferenceContext ( services . GetRequiredService < IJSRuntime > ( ) ) ;
73
76
}
74
77
75
- /// <inheritdoc/>
78
+ /// <summary>
79
+ /// Renders the <paramref name="renderFragment"/>.
80
+ /// </summary>
81
+ /// <param name="renderFragment">The <see cref="Microsoft.AspNetCore.Components.RenderFragment"/> to render.</param>
82
+ /// <returns>A <see cref="RenderedFragment"/> that provides access to the rendered <paramref name="renderFragment"/>.</returns>
76
83
public RenderedFragment RenderFragment ( RenderFragment renderFragment )
77
84
=> Render ( renderFragment , id => activator . CreateRenderedFragment ( id ) ) ;
78
85
79
- /// <inheritdoc/>
86
+ /// <summary>
87
+ /// Renders a <typeparamref name="TComponent"/> with the <paramref name="parameters"/> passed to it.
88
+ /// </summary>
89
+ /// <typeparam name="TComponent">The type of component to render.</typeparam>
90
+ /// <param name="parameters">The parameters to pass to the component.</param>
91
+ /// <returns>A <see cref="RenderedComponent{TComponent}"/> that provides access to the rendered component.</returns>
80
92
public RenderedComponent < TComponent > RenderComponent < TComponent > ( ComponentParameterCollection parameters )
81
93
where TComponent : IComponent
82
94
{
@@ -86,14 +98,26 @@ public RenderedComponent<TComponent> RenderComponent<TComponent>(ComponentParame
86
98
return Render ( renderFragment , id => activator . CreateRenderedComponent < TComponent > ( id ) ) ;
87
99
}
88
100
89
- /// <inheritdoc/>
101
+ /// <summary>
102
+ /// Notifies the renderer that an event has occurred.
103
+ /// </summary>
104
+ /// <param name="eventHandlerId">The <see cref="RenderTreeFrame.AttributeEventHandlerId"/> value from the original event attribute.</param>
105
+ /// <param name="fieldInfo">Information that the renderer can use to update the state of the existing render tree to match the UI.</param>
106
+ /// <param name="eventArgs">Arguments to be passed to the event handler.</param>
107
+ /// <returns>A <see cref="Task"/> which will complete once all asynchronous processing related to the event has completed.</returns>
90
108
public new Task DispatchEventAsync (
91
109
ulong eventHandlerId ,
92
110
EventFieldInfo fieldInfo ,
93
111
EventArgs eventArgs ) => DispatchEventAsync ( eventHandlerId , fieldInfo , eventArgs , ignoreUnknownEventHandlers : false ) ;
94
112
95
- /// <exception cref="ObjectDisposedException"></exception>
96
- /// <inheritdoc/>
113
+ /// <summary>
114
+ /// Notifies the renderer that an event has occurred.
115
+ /// </summary>
116
+ /// <param name="eventHandlerId">The <see cref="RenderTreeFrame.AttributeEventHandlerId"/> value from the original event attribute.</param>
117
+ /// <param name="fieldInfo">Information that the renderer can use to update the state of the existing render tree to match the UI.</param>
118
+ /// <param name="eventArgs">Arguments to be passed to the event handler.</param>
119
+ /// <param name="ignoreUnknownEventHandlers">Set to true to ignore the <see cref="UnknownEventHandlerIdException"/>.</param>
120
+ /// <returns>A <see cref="Task"/> which will complete once all asynchronous processing related to the event has completed.</returns>
97
121
public new Task DispatchEventAsync (
98
122
ulong eventHandlerId ,
99
123
EventFieldInfo fieldInfo ,
@@ -142,7 +166,11 @@ public RenderedComponent<TComponent> RenderComponent<TComponent>(ComponentParame
142
166
}
143
167
}
144
168
145
- /// <inheritdoc/>
169
+ /// <summary>
170
+ /// Performs a depth-first search for the first <typeparamref name="TComponent"/> child component of the <paramref name="parentComponent"/>.
171
+ /// </summary>
172
+ /// <typeparam name="TComponent">Type of component to find.</typeparam>
173
+ /// <param name="parentComponent">Parent component to search.</param>
146
174
public RenderedComponent < TComponent > FindComponent < TComponent > ( RenderedFragment parentComponent )
147
175
where TComponent : IComponent
148
176
{
@@ -152,12 +180,18 @@ public RenderedComponent<TComponent> FindComponent<TComponent>(RenderedFragment
152
180
: throw new ComponentNotFoundException ( typeof ( TComponent ) ) ;
153
181
}
154
182
155
- /// <inheritdoc/>
183
+ /// <summary>
184
+ /// Performs a depth-first search for all <typeparamref name="TComponent"/> child components of the <paramref name="parentComponent"/>.
185
+ /// </summary>
186
+ /// <typeparam name="TComponent">Type of components to find.</typeparam>
187
+ /// <param name="parentComponent">Parent component to search.</param>
156
188
public IReadOnlyList < RenderedComponent < TComponent > > FindComponents < TComponent > ( RenderedFragment parentComponent )
157
189
where TComponent : IComponent
158
190
=> FindComponents < TComponent > ( parentComponent , int . MaxValue ) ;
159
191
160
- /// <inheritdoc />
192
+ /// <summary>
193
+ /// Disposes all components rendered by the <see cref="BunitRenderer" />.
194
+ /// </summary>
161
195
public void DisposeComponents ( )
162
196
{
163
197
ObjectDisposedException . ThrowIf ( disposed , this ) ;
@@ -225,7 +259,7 @@ internal Task SetDirectParametersAsync(RenderedFragment renderedComponent, Param
225
259
226
260
return result ;
227
261
228
- static void SetDirectParametersViaComponentState ( TestRenderer renderer , int componentId , in ParameterView parameters )
262
+ static void SetDirectParametersViaComponentState ( BunitRenderer renderer , int componentId , in ParameterView parameters )
229
263
{
230
264
var componentState = renderer . GetComponentState ( componentId ) ;
231
265
CallSetDirectParameters ( componentState , parameters ) ;
@@ -534,8 +568,10 @@ private ArrayRange<RenderTreeFrame> GetOrLoadRenderTreeFrame(RenderTreeFrameDict
534
568
}
535
569
536
570
/// <inheritdoc/>
537
- protected override void HandleException ( [ NotNull ] Exception exception )
571
+ protected override void HandleException ( Exception exception )
538
572
{
573
+ ArgumentNullException . ThrowIfNull ( exception ) ;
574
+
539
575
if ( disposed )
540
576
return ;
541
577
0 commit comments