Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ All notable changes to **bUnit** will be documented in this file. The project ad

## [Unreleased]

### Fixed

- An element returned by `Find()` could be `null` for a moment when the component re-rendered on another thread, so using it threw a `NullReferenceException`, for example from `GetBunitContext` in `ClickAsync`. Reported and fixed by [@psu-prog](https://github.com/psu-prog).

## [2.11.3] - 2026-09-13

### Fixed
Expand Down
6 changes: 2 additions & 4 deletions src/bunit.generators.internal/Web.AngleSharp/WrapperBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ public TElement WrappedElement
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (element is null)
element = elementFactory.GetElement<TElement>();

return element;
// Read the field once: a re-render on another thread can reset it to null between two reads.
return element ??= elementFactory.GetElement<TElement>();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ public TElement WrappedElement
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (element is null)
element = elementFactory.GetElement<TElement>();

return element;
// Read the field once: a re-render on another thread can reset it to null between two reads.
return element ??= elementFactory.GetElement<TElement>();
}
}

Expand Down
42 changes: 42 additions & 0 deletions tests/bunit.tests/Extensions/RefreshingWrappedElementTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,30 @@ public void Test031()
Should.Throw<ElementRemovedFromDomException>(() => btn.TextContent);
}

[Fact(DisplayName = "Found element is never null while the component re-renders on another thread")]
public async Task Test040()
{
var cut = Render<Counter>();
var elm = cut.Find("span");
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
var renders = Task.Run(async () =>
{
while (!cts.IsCancellationRequested)
await cut.InvokeAsync(cut.Instance.Increment);
});

try
{
while (!cts.IsCancellationRequested)
_ = elm.TextContent;
}
finally
{
await cts.CancelAsync();
await renders;
}
}

private sealed class Markup : ComponentBase
{
[Parameter] public string Base { get; set; } = string.Empty;
Expand All @@ -89,4 +113,22 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
builder.AddMarkupContent(1, Optional);
}
}

private sealed class Counter : ComponentBase
{
private int count;

public void Increment()
{
count++;
StateHasChanged();
}

protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenElement(0, "span");
builder.AddContent(1, count);
builder.CloseElement();
}
}
}