Skip to content

Commit 4c21714

Browse files
egilactions-user
andauthored
Fixed bug where multiple calls to FindComponents with the same parent… (#205)
* Fixed bug where multiple calls to FindComponents with the same parent and input would fail * Automated dotnet-format update Co-authored-by: Github Actions <[email protected]>
1 parent 9ed7f33 commit 4c21714

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

src/bunit.core/Rendering/TestRenderer.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,6 @@ private IReadOnlyList<IRenderedComponentBase<TComponent>> FindComponents<TCompon
186186
lock (_renderTreeAccessLock)
187187
{
188188
FindComponentsInternal(parentComponent.ComponentId);
189-
foreach (var rc in result)
190-
{
191-
_renderedComponents.Add(rc.ComponentId, rc);
192-
}
193189
}
194190

195191
return result;
@@ -205,10 +201,7 @@ void FindComponentsInternal(int componentId)
205201
{
206202
if (frame.Component is TComponent component)
207203
{
208-
var id = frame.ComponentId;
209-
LoadRenderTreeFrames(id, framesCollection);
210-
var rc = _activator.CreateRenderedComponent(id, component, framesCollection);
211-
result.Add(rc);
204+
GetOrCreateRenderedComponent(frame.ComponentId, component);
212205

213206
if (result.Count == resultLimit)
214207
return;
@@ -221,6 +214,24 @@ void FindComponentsInternal(int componentId)
221214
}
222215
}
223216
}
217+
218+
void GetOrCreateRenderedComponent(int componentId, TComponent component)
219+
{
220+
IRenderedComponentBase<TComponent> rc;
221+
222+
if (_renderedComponents.TryGetValue(componentId, out var rf))
223+
{
224+
rc = (IRenderedComponentBase<TComponent>)rf;
225+
}
226+
else
227+
{
228+
LoadRenderTreeFrames(componentId, framesCollection);
229+
rc = _activator.CreateRenderedComponent(componentId, component, framesCollection);
230+
_renderedComponents.Add(rc.ComponentId, rc);
231+
}
232+
233+
result.Add(rc);
234+
}
224235
}
225236

226237
/// <summary>

tests/bunit.core.tests/Rendering/TestRendererTest.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,21 @@ public void Test022()
234234
Should.Throw<ComponentNotFoundException>(() => sut.FindComponent<HasParams>(cut));
235235
}
236236

237+
[Fact(DisplayName = "FindComponent returns same rendered component when called multiple times")]
238+
public void Test023()
239+
{
240+
var sut = Services.GetRequiredService<ITestRenderer>();
241+
242+
var cut = sut.RenderComponent<HasParams>(
243+
ChildContent<HasParams>()
244+
);
245+
246+
var child1 = sut.FindComponent<HasParams>(cut);
247+
var child2 = sut.FindComponent<HasParams>(cut);
248+
249+
child1.ShouldBe(child2);
250+
}
251+
237252
[Fact(DisplayName = "FindComponents returns all components nested inside another rendered component")]
238253
public void Test030()
239254
{
@@ -275,6 +290,25 @@ public void Test031()
275290
Should.Throw<ArgumentNullException>(() => sut.FindComponents<HasParams>(null!));
276291
}
277292

293+
[Fact(DisplayName = "FindComponents returns same rendered components when called multiple times")]
294+
public void Test032()
295+
{
296+
// arrange
297+
var sut = Services.GetRequiredService<ITestRenderer>();
298+
var cut = sut.RenderComponent<HasParams>(
299+
ChildContent<HasParams>(
300+
ChildContent<HasParams>()
301+
)
302+
);
303+
304+
// act
305+
var childCuts1 = sut.FindComponents<HasParams>(cut);
306+
var childCuts2 = sut.FindComponents<HasParams>(cut);
307+
308+
// assert
309+
childCuts1.ShouldBe(childCuts2);
310+
}
311+
278312
[Fact(DisplayName = "Retrieved rendered child component with FindComponent gets updated on re-render")]
279313
public async Task Test040()
280314
{

0 commit comments

Comments
 (0)