Skip to content

Commit a8b9834

Browse files
committed
fix: Check if components got disposed
1 parent 85f2f2c commit a8b9834

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/bunit/Rendering/BunitRenderer.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,15 +447,23 @@ protected override void ProcessPendingRender()
447447
/// <inheritdoc/>
448448
protected override Task UpdateDisplayAsync(in RenderBatch renderBatch)
449449
{
450+
var disposedComponentIds = new HashSet<int>();
450451
for (var i = 0; i < renderBatch.DisposedComponentIDs.Count; i++)
451452
{
452453
var id = renderBatch.DisposedComponentIDs.Array[i];
454+
disposedComponentIds.Add(id);
453455
returnedRenderedComponentIds.Remove(id);
454456
}
455457

456458
for (var i = 0; i < renderBatch.UpdatedComponents.Count; i++)
457459
{
458460
var diff = renderBatch.UpdatedComponents.Array[i];
461+
462+
if (disposedComponentIds.Contains(diff.ComponentId))
463+
{
464+
continue;
465+
}
466+
459467
var componentState = GetComponentState(diff.ComponentId);
460468
var renderedComponent = (IRenderedComponent)componentState;
461469

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<EditForm Model="model" OnValidSubmit="SetInvoked">
2+
<DataAnnotationsValidator />
3+
4+
<InputText id="title" @bind-Value="model.Title" />
5+
<button>Submit</button>
6+
</EditForm>
7+
8+
@code {
9+
10+
[Parameter]
11+
public bool Invoked { get; set; }
12+
13+
private Model model = new();
14+
15+
private void SetInvoked()
16+
{
17+
Invoked = true;
18+
model = new Model();
19+
}
20+
21+
public sealed class Model
22+
{
23+
public string? Title { get; set; }
24+
}
25+
26+
}

tests/bunit.tests/Rendering/RenderedComponentTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,17 @@ public void Test024()
244244

245245
text.ShouldNotBeNull();
246246
}
247+
248+
[Fact(DisplayName = "Form submission with DataAnnotationsValidator and model recreation works correctly")]
249+
public void Test025()
250+
{
251+
var cut = Render<FormWithValidation>();
252+
cut.Find("#title").Change("title");
253+
254+
cut.Find("form").Submit();
255+
256+
cut.Instance.Invoked.ShouldBeTrue();
257+
}
247258

248259
private class BaseComponent : ComponentBase
249260
{

0 commit comments

Comments
 (0)