Skip to content

Commit f60013e

Browse files
linkdotnetegil
authored andcommitted
fix: Add test for FindComponents with base and derived components
1 parent 4f15022 commit f60013e

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ All notable changes to **bUnit** will be documented in this file. The project ad
1010

1111
- Do not set the `Uri` or `BaseUri` property on the `FakeNavigationManager` if navigation is prevented by a handler on `net7.0` or greater. Reported and fixed by [@ayyron-dev](https://github.com/ayyron-dev) in [#1647](https://github.com/bUnit-dev/bUnit/issues/1647)
1212
- Use default renderer properties for AngleSharp. Reported by [@jtleaming](https://github.com/jtleaming) in [#1692].
13+
- `FindComponents` throws an exception, when a base and derived class was searched for. Reported by [@BlueDragon709](https://github.com/BlueDragon709) in [#1691].
1314

1415
## [1.38.5] - 2025-01-12
1516

src/bunit.core/Rendering/TestRenderer.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,13 @@ private IRenderedComponentBase<TComponent> GetOrCreateRenderedComponent<TCompone
610610
{
611611
if (renderedComponents.TryGetValue(componentId, out var renderedComponent))
612612
{
613-
return (IRenderedComponentBase<TComponent>)renderedComponent;
613+
if (renderedComponent is IRenderedComponentBase<TComponent> typedComponent)
614+
{
615+
return typedComponent;
616+
}
617+
618+
renderedComponent.Dispose();
619+
renderedComponents.Remove(componentId);
614620
}
615621

616622
LoadRenderTreeFrames(componentId, framesCollection);

tests/bunit.web.tests/Rendering/RenderedComponentTest.cs

+30
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,36 @@ public void Test021()
6969

7070
cut.Instance.JSRuntime.ShouldNotBeNull();
7171
}
72+
73+
[Fact(DisplayName = "Searching first for derived component and then base component finds correct (#1691)")]
74+
public void Test023()
75+
{
76+
var cut = RenderComponent<Wrapper>(
77+
ps => ps.AddChildContent<BaseComponent>()
78+
.AddChildContent<DerivedComponent>());
79+
80+
Should.NotThrow(() =>
81+
{
82+
cut.FindComponents<BaseComponent>();
83+
cut.FindComponents<DerivedComponent>();
84+
});
85+
}
86+
87+
private class BaseComponent : ComponentBase
88+
{
89+
protected override void BuildRenderTree(RenderTreeBuilder builder)
90+
{
91+
builder.AddContent(0, "base");
92+
}
93+
}
94+
95+
private sealed class DerivedComponent : BaseComponent
96+
{
97+
protected override void BuildRenderTree(RenderTreeBuilder builder)
98+
{
99+
builder.AddContent(0, "derived");
100+
}
101+
}
72102
#endif
73103

74104
[Fact(DisplayName = "Using relative units in style attribute can be retrieved")]

0 commit comments

Comments
 (0)