-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: better handling of disposed event handlers with bubbling events
- Loading branch information
Showing
8 changed files
with
144 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/bunit.core/Rendering/UnknownEventHandlerIdException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Runtime.Serialization; | ||
using Microsoft.AspNetCore.Components.RenderTree; | ||
|
||
namespace Bunit.Rendering | ||
{ | ||
/// <summary> | ||
/// Represents an exception that is thrown when the Blazor <see cref="Renderer"/> | ||
/// does not have any event handler with the specified a given ID. | ||
/// </summary> | ||
[Serializable] | ||
public sealed class UnknownEventHandlerIdException : Exception | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="UnknownEventHandlerIdException"/> class. | ||
/// </summary> | ||
public UnknownEventHandlerIdException(ulong eventHandlerId, EventFieldInfo fieldInfo, Exception innerException) | ||
: base(CreateMessage(eventHandlerId, fieldInfo), innerException) | ||
{ | ||
} | ||
|
||
private UnknownEventHandlerIdException(SerializationInfo serializationInfo, StreamingContext streamingContext) | ||
: base(serializationInfo, streamingContext) { } | ||
|
||
private static string CreateMessage(ulong eventHandlerId, EventFieldInfo fieldInfo) | ||
=> $"There is no event handler with ID '{eventHandlerId}' associated with the '{fieldInfo.FieldValue}' event " + | ||
"in the current render tree. This can happen, for example, when using cut.FindAll(), and calling event trigger methods " + | ||
"on the found elements after a re-render of the render tree. The workaround is to use re-issue the cut.FindAll() after " + | ||
"each render of a component, this ensures you have the latest version of the render tree and DOM tree available in your test code."; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
tests/bunit.testassets/SampleComponents/BubbleEventsRemoveTriggers.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<div @onclick="() => TopDivClicked = true"> | ||
@if (!BtnClicked) | ||
{ | ||
<div @onclick="() => MiddleDivClicked = true"> | ||
<button @onclick="() => BtnClicked = true">Click me!</button> | ||
</div> | ||
} | ||
</div> | ||
@code { | ||
public bool BtnClicked { get; private set; } = false; | ||
public bool MiddleDivClicked { get; private set; } = false; | ||
public bool TopDivClicked { get; private set; } = false; | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/bunit.testassets/SampleComponents/BubbleEventsThrows.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<div @onclick="() => TopDivClicked = true"> | ||
<div @onclick=@MiddleDivHandler> | ||
<button @onclick="() => BtnClicked = true">Click me!</button> | ||
</div> | ||
</div> | ||
|
||
<p>@BtnClicked</p> | ||
<p>@MiddleDivClicked</p> | ||
<p>@TopDivClicked</p> | ||
|
||
@code { | ||
[Parameter] public string ExceptionMessage { get; set; } | ||
|
||
public bool BtnClicked { get; private set; } = false; | ||
public bool MiddleDivClicked { get; private set; } = false; | ||
public bool TopDivClicked { get; private set; } = false; | ||
|
||
private void MiddleDivHandler() => throw new Exception(ExceptionMessage); | ||
} |
9 changes: 9 additions & 0 deletions
9
tests/bunit.testassets/SampleComponents/ClickRemovesEventHandler.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<button id="first" @onclick="() => visible = false"></button> | ||
@if (visible) | ||
{ | ||
<button id="second" @onclick="() => SecondButtonClicked = true"></button> | ||
} | ||
@code { | ||
private bool visible = true; | ||
public bool SecondButtonClicked { get; private set; } | ||
} |
5 changes: 5 additions & 0 deletions
5
tests/bunit.testassets/SampleComponents/EventHandlerThrows.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<button @onclick="Handler"></button> | ||
@code { | ||
[Parameter] public string ExceptionMessage { get; set; } | ||
private void Handler() => throw new Exception(ExceptionMessage); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters