Skip to content

Commit a7f6abc

Browse files
committed
fix: Add test for FindComponents with base and derived components
1 parent 2284ed6 commit a7f6abc

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ All notable changes to **bUnit** will be documented in this file. The project ad
99
### Fixed
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)
12+
- `FindComponents<T>` throws an exception, when a base and derived class was searched for. Reported by [@BlueDragon709](https://github.com/BlueDragon709) in [#1691].
1213

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

src/bunit.core/Rendering/TestRenderer.cs

Lines changed: 7 additions & 1 deletion
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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,35 @@ public void Test021()
6666

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

0 commit comments

Comments
 (0)