This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Forward exceptions to root ViewModel for handling
- Loading branch information
Showing
4 changed files
with
95 additions
and
30 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
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
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,68 @@ | ||
using BitzArt.Blazor.MVVM.Tests.ViewModels; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace BitzArt.Blazor.MVVM.Tests; | ||
|
||
public class ExceptionHandlingTests | ||
{ | ||
[Fact] | ||
public async Task HandleAsync_WhenExceptionIsThrown_ShouldInvokeExceptionHandler() | ||
{ | ||
// Arrange | ||
var viewModel = new TestLayer1ViewModel(); | ||
var exceptionHandlerInvoked = false; | ||
|
||
viewModel.ExceptionHandler = (sender, ex) => | ||
{ | ||
exceptionHandlerInvoked = true; | ||
return Task.CompletedTask; | ||
}; | ||
|
||
// Act | ||
try | ||
{ | ||
await viewModel.HandleAsync(() => throw new Exception()); | ||
} | ||
catch (Exception) | ||
{ | ||
} | ||
|
||
// Assert | ||
Assert.True(exceptionHandlerInvoked); | ||
} | ||
|
||
[Fact] | ||
public async Task HandleAsync_HierarchicalStructure_ShouldForwardToRoot() | ||
{ | ||
// Arrange | ||
|
||
var serviceCollection = new ServiceCollection(); | ||
serviceCollection.AddBlazorMvvm() | ||
.AddViewModel<TestLayer1ViewModel>() | ||
.AddViewModel<TestLayer2ViewModel>(); | ||
|
||
var serviceProvider = serviceCollection.BuildServiceProvider(); | ||
|
||
var rootViewModel = serviceProvider.GetRequiredService<TestLayer1ViewModel>(); | ||
var childViewModel = rootViewModel.TestLayer2ViewModel; | ||
|
||
var rootExceptionHandlerInvoked = false; | ||
rootViewModel.ExceptionHandler += (sender, ex) => | ||
{ | ||
rootExceptionHandlerInvoked = true; | ||
return Task.CompletedTask; | ||
}; | ||
|
||
// Act | ||
try | ||
{ | ||
await childViewModel.HandleAsync(() => throw new Exception()); | ||
} | ||
catch (Exception) | ||
{ | ||
} | ||
|
||
// Assert | ||
Assert.True(rootExceptionHandlerInvoked); | ||
} | ||
} |
30 changes: 0 additions & 30 deletions
30
tests/BitzArt.Blazor.MVVM.Tests/ViewModels/ExceptionHandlingTests.cs
This file was deleted.
Oops, something went wrong.