Should IRenderFragment.Dispose also dispose the wrapped component #640
-
The current setup if I want to test a component which implements var cut = RenderComponent<MyComponent>();
cut.Instance.Dispose();
// Assert something
MyComponent.DisposeWasCalled.ShouldBeTrue(); // Let's assume we have a static field which gets set on Dispose Now what does not work if we var cut = RenderComponent<MyComponent>();
cut.Dispose();
MyComponent.DisposeWasCalled.ShouldBeTrue(); // This will fail as our Dispose/DisposeAsync doesn't get called at all For simpler scenarios like that, there is no issue. The problem arises when you have multiple components which implement Parent.razor: @implements IDisposable
<Child></Child>
@code {
public void Dispose()
{
Console.WriteLIne("Parent was disposed");
} Child.razor: @implements IDisposable
@code {
public void Dispose()
{
Console.WriteLIne("Child was disposed");
} If we would navigate away from the site, Blazor would dispose from parent to child, so the console would print something like:
We already have that behavior when an items (plus all its subitems) get removed from the render tree. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
If this is wished the simplest solution is to remove the wrapped element in Dispose and let Blazor do the work (which then invokes |
Beta Was this translation helpful? Give feedback.
-
Since Without further design/analysis my idea to support this is:
|
Beta Was this translation helpful? Give feedback.
-
Hey Egil, hope you're having a great day.
Parent.razor: <Child></Child> Child.razor: @implements IDisposable
@code {
public void Dispose()
{
// Do something here
}
}
Let me know about your thoughts. If you think this is worth a shot I'd like to do so. Furthermore we can "convert" this into an issue. Cheers EDIT: I didn't read your idea completely and this was exactly my thought as well. I'll just leave my (now obsolete comment) for transparency. |
Beta Was this translation helpful? Give feedback.
-
I guess tomorrow I have a small draft / proposal PR. Have to figure one or two things out where I can’t wrap my head around right now. |
Beta Was this translation helpful? Give feedback.
Since
IRenderedFragmentBase
already implementsIDisposable
, and could be made to implementIAsyncDisposable
, its not a bad idea to support triggering the CUT's dispose method, if any, that way.Without further design/analysis my idea to support this is:
RenderFragment
s are rendered inside theWrapperComponent
in bUnit.WrapperComponent
gets support to remove whateverRenderFragment
it has been passed from the render tree again, then the Blazor renderer will take care of any disposing that is needed.IRenderedFragmentBase
to get to the rootWrapperComponent
and calling a method on it during a call to theIRenderedFragment.Dispose
method.Dispose
…