Skip to content

Commit 2eb8645

Browse files
committed
fix: captured error message from event dispatcher in renderer (related to #399)
1 parent ceca248 commit 2eb8645

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,22 @@ List of fixes in this release.
3838

3939
- Fixed issue where a registered fall-back service provider was not made available to resolve service dependencies of components under test. Thanks to [@dady8889](https://github.com/dady8889) for the reporting the issue.
4040

41-
- Fixed handling of escaped uri's in FakeNavigationManager. By [@linkdotnet](https://github.com/linkdotnet) in [#460](https://github.com/bUnit-dev/bUnit/pull/460)
41+
- Fixed handling of escaped uri's in FakeNavigationManager. By [@linkdotnet](https://github.com/linkdotnet) in [#460](https://github.com/bUnit-dev/bUnit/pull/460).
42+
43+
- Captured error message from event dispatcher in renderer that would previously be hidden from the user. Related to issue [#399](https://github.com/bUnit-dev/bUnit/issues/399).
4244

4345
## [1.1.5] - 2021-04-30
4446

4547
### Added
4648

4749
- All bUnit assemblies is now strong named signed.
50+
4851
- Added .NET 6 (preview 3) as a target framework for bUnit, bUnit.core and bUnit.web.
4952

5053
### Changed
5154

5255
- Changed bunit.template such that created projects only reference the bUnit package. Bumped other referenced packages to latest version.
56+
5357
- Changed TestServiceProvider to validate scopes of registered services, such that it behaves like the service provider (default IoC container) in Blazor.
5458

5559
## [1.0.16]

src/bunit.core/Rendering/TestRenderer.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,27 @@ public IRenderedComponentBase<TComponent> RenderComponent<TComponent>(ComponentP
7373

7474
ResetUnhandledException();
7575

76-
var result = Dispatcher.InvokeAsync(() => base.DispatchEventAsync(eventHandlerId, fieldInfo, eventArgs));
76+
var result = Dispatcher.InvokeAsync(() =>
77+
{
78+
try
79+
{
80+
return base.DispatchEventAsync(eventHandlerId, fieldInfo, eventArgs);
81+
}
82+
catch (ArgumentException ex) when (string.Equals(ex.Message, $"There is no event handler associated with this event. EventId: '{eventHandlerId}'. (Parameter 'eventHandlerId')", StringComparison.Ordinal))
83+
{
84+
var betterExceptionMsg = new ArgumentException($"There is no event handler with ID '{eventHandlerId}' associated with the '{fieldInfo.FieldValue}' event " +
85+
"in the current render tree. This can happen, for example, when using cut.FindAll(), and calling event trigger methods " +
86+
"on the found elements after a re-render of the render tree. The workaround is to use re-issue the cut.FindAll() after " +
87+
"each render of a component, this ensures you have the latest version of the render tree and DOM tree available in your test code.", ex);
88+
89+
return Task.FromException(betterExceptionMsg);
90+
}
91+
});
92+
93+
if(result.IsFaulted && result.Exception is not null)
94+
{
95+
HandleException(result.Exception);
96+
}
7797

7898
AssertNoUnhandledExceptions();
7999

tests/bunit.web.tests/EventDispatchExtensions/GeneralEventDispatchExtensionsTest.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,18 @@ public async Task Test114(
174174
cut.Instance.ParentTriggerCount.ShouldBe(1);
175175
cut.Instance.GrandParentTriggerCount.ShouldBe(1);
176176
}
177+
178+
179+
[Fact(DisplayName = "TriggerEventAsync throws when invoked with an unknown event handler ID")]
180+
public void Test115()
181+
{
182+
var eventName = "onclick";
183+
var cut = RenderComponent<Wrapper>(ps => ps.AddChildContent("<div />"));
184+
var div = cut.Find("div");
185+
div.SetAttribute(Htmlizer.ToBlazorAttribute(eventName), "42");
186+
187+
Should.Throw<ArgumentException>(() => div.TriggerEventAsync(eventName, EventArgs.Empty));
188+
}
189+
177190
}
178191
}

0 commit comments

Comments
 (0)