Skip to content

Commit d978148

Browse files
committed
Remove exception swallowing and update tests to exhibit expected behaviour
1 parent 2217d08 commit d978148

File tree

7 files changed

+217
-166
lines changed

7 files changed

+217
-166
lines changed

src/CommunityToolkit.Maui.UnitTests/BaseHandlerTest.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,10 @@ static void InitializeServicesAndSetMockApplication(out IServiceProvider service
7676
#endregion
7777

7878
#region Register Services for PopupServiceTests
79-
80-
var mockPageViewModel = new MockPageViewModel();
81-
var mockPopup = new MockSelfClosingPopup(mockPageViewModel, new());
82-
83-
PopupService.AddPopup(mockPopup, mockPageViewModel, appBuilder.Services, ServiceLifetime.Transient);
79+
80+
appBuilder.Services.AddTransientPopup<LongLivedSelfClosingPopup, LongLivedMockPageViewModel>();
81+
appBuilder.Services.AddTransientPopup<ShortLivedSelfClosingPopup, ShortLivedMockPageViewModel>();
82+
8483
appBuilder.Services.AddTransientPopup<MockPopup>();
8584
#endregion
8685

src/CommunityToolkit.Maui.UnitTests/Extensions/PopupExtensionsTests.cs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public async Task ClosePopupT_NullNavigation_ShouldThrowArgumentNullException()
124124
public async Task ShowPopupAsync_WithPopupType_ShowsPopupAndClosesPopup()
125125
{
126126
// Arrange
127-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
127+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
128128

129129
// Act
130130
navigation.ShowPopup(selfClosingPopup);
@@ -228,7 +228,7 @@ public void ShowPopupAsync_Shell_WithViewType_ShowsPopup()
228228
public async Task ShowPopupAsync_AwaitingShowPopupAsync_EnsurePreviousPopupClosed()
229229
{
230230
// Arrange
231-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
231+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
232232

233233
// Act
234234
await navigation.ShowPopupAsync<object?>(selfClosingPopup, PopupOptions.Empty, TestContext.Current.CancellationToken);
@@ -249,7 +249,7 @@ public async Task ShowPopupAsync_Shell_AwaitingShowPopupAsync_EnsurePreviousPopu
249249
Application.Current.Windows[0].Page = shell;
250250

251251
var shellNavigation = Shell.Current.Navigation;
252-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
252+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
253253

254254
// Act
255255
await shell.ShowPopupAsync<object?>(selfClosingPopup, PopupOptions.Empty, shellParameters, TestContext.Current.CancellationToken);
@@ -264,7 +264,7 @@ public async Task ShowPopupAsync_Shell_AwaitingShowPopupAsync_EnsurePreviousPopu
264264
public void ShowPopup_NavigationModalStackCountIncreases()
265265
{
266266
// Arrange
267-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
267+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
268268
Assert.Empty(navigation.ModalStack);
269269

270270
// Act
@@ -345,7 +345,7 @@ public void ShowPopupWithView_Shell_NavigationModalStackCountIncreases()
345345
public void ShowPopup_MultiplePopupsDisplayed()
346346
{
347347
// Arrange
348-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
348+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
349349

350350
// Act
351351
navigation.ShowPopup(selfClosingPopup, PopupOptions.Empty);
@@ -359,7 +359,7 @@ public void ShowPopup_MultiplePopupsDisplayed()
359359
public void ShowPopup_Shell_MultiplePopupsDisplayed()
360360
{
361361
// Arrange
362-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
362+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
363363
var shell = new Shell();
364364
shell.Items.Add(new MockPage(new MockPageViewModel()));
365365

@@ -421,7 +421,7 @@ public void ShowPopupAsync_WithCustomOptions_AppliesOptions()
421421
// Arrange
422422
var onTappingOutsideOfPopup = () => { };
423423

424-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
424+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
425425
var options = new PopupOptions
426426
{
427427
PageOverlayColor = Colors.Red,
@@ -493,7 +493,7 @@ public void ShowPopupAsync_Shell_WithCustomOptions_AppliesOptions()
493493
var shellNavigation = Shell.Current.Navigation;
494494
var onTappingOutsideOfPopup = () => { };
495495

496-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
496+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
497497
var options = new PopupOptions
498498
{
499499
PageOverlayColor = Colors.Red,
@@ -713,7 +713,7 @@ public void ShowPopupAsyncWithView_Shell_WithCustomOptions_AppliesOptions()
713713
public async Task ShowPopupAsync_CancellationTokenExpired()
714714
{
715715
// Arrange
716-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
716+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
717717
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(1));
718718

719719
// Act
@@ -734,7 +734,7 @@ public async Task ShowPopupAsync_Shell_CancellationTokenExpired()
734734
Application.Current.Windows[0].Page = shell;
735735

736736
var shellNavigation = Shell.Current.Navigation;
737-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
737+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
738738

739739
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(1));
740740

@@ -788,7 +788,7 @@ public async Task ShowPopupAsyncWithView_Shell_CancellationTokenExpired()
788788
public async Task ShowPopupAsync_CancellationTokenCanceled()
789789
{
790790
// Arrange
791-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
791+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
792792
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(1));
793793

794794
// Act
@@ -809,7 +809,7 @@ public async Task ShowPopupAsync_Shell_CancellationTokenCanceled()
809809
Application.Current.Windows[0].Page = shell;
810810

811811
var shellNavigation = Shell.Current.Navigation;
812-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
812+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
813813

814814
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(1));
815815

@@ -863,15 +863,15 @@ public async Task ShowPopupAsyncWithView_Shell_CancellationTokenCanceled()
863863
public async Task ShowPopupAsync_ShouldValidateProperBindingContext()
864864
{
865865
// Arrange
866-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
867-
var popupInstance = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
868-
var popupViewModel = ServiceProvider.GetRequiredService<MockPageViewModel>();
866+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
867+
var popupInstance = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>();
869868

870869
// Act
871870
await navigation.ShowPopupAsync<object?>(selfClosingPopup, PopupOptions.Empty, TestContext.Current.CancellationToken);
872871

873872
// Assert
874-
Assert.Same(popupInstance.BindingContext, popupViewModel);
873+
Assert.NotNull(popupInstance.BindingContext);
874+
Assert.IsType<ShortLivedMockPageViewModel>(popupInstance.BindingContext);
875875
}
876876

877877
[Fact(Timeout = (int)TestDuration.Medium)]
@@ -885,15 +885,15 @@ public async Task ShowPopupAsync_Shell_ShouldValidateProperBindingContext()
885885
Application.Current.Windows[0].Page = shell;
886886

887887
var shellNavigation = Shell.Current.Navigation;
888-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
889-
var popupInstance = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
890-
var popupViewModel = ServiceProvider.GetRequiredService<MockPageViewModel>();
888+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
889+
var popupInstance = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>();
891890

892891
// Act
893892
await shell.ShowPopupAsync<object?>(selfClosingPopup, PopupOptions.Empty, shellParameters, TestContext.Current.CancellationToken);
894893

895894
// Assert
896-
Assert.Same(popupInstance.BindingContext, popupViewModel);
895+
Assert.NotNull(popupInstance.BindingContext);
896+
Assert.IsType<ShortLivedMockPageViewModel>(popupInstance.BindingContext);
897897
Assert.Equal(shellParameterBackgroundColorValue, selfClosingPopup.BackgroundColor);
898898
}
899899

@@ -902,8 +902,7 @@ public async Task ShowPopupAsyncWithView_ShouldValidateProperBindingContext()
902902
{
903903
// Arrange
904904
var view = new Grid();
905-
var popupInstance = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
906-
var popupViewModel = ServiceProvider.GetRequiredService<MockPageViewModel>();
905+
var popupInstance = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>();
907906

908907
// Act
909908
var showPopupTask = navigation.ShowPopupAsync<object?>(view, PopupOptions.Empty, TestContext.Current.CancellationToken);
@@ -914,7 +913,8 @@ public async Task ShowPopupAsyncWithView_ShouldValidateProperBindingContext()
914913
await showPopupTask;
915914

916915
// Assert
917-
Assert.Same(popupInstance.BindingContext, popupViewModel);
916+
Assert.NotNull(popupInstance.BindingContext);
917+
Assert.IsType<ShortLivedMockPageViewModel>(popupInstance.BindingContext);
918918
}
919919

920920
[Fact(Timeout = (int)TestDuration.Medium)]
@@ -929,8 +929,7 @@ public async Task ShowPopupAsyncWithView_Shell_ShouldValidateProperBindingContex
929929

930930
var shellNavigation = Shell.Current.Navigation;
931931
var view = new ViewWithIQueryAttributable(new ViewModelWithIQueryAttributable());
932-
var popupInstance = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
933-
var popupViewModel = ServiceProvider.GetRequiredService<MockPageViewModel>();
932+
var popupInstance = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>();
934933

935934
// Act
936935
var showPopupTask = shell.ShowPopupAsync<object?>(view, PopupOptions.Empty, shellParameters, TestContext.Current.CancellationToken);
@@ -941,7 +940,8 @@ public async Task ShowPopupAsyncWithView_Shell_ShouldValidateProperBindingContex
941940
await showPopupTask;
942941

943942
// Assert
944-
Assert.Same(popupInstance.BindingContext, popupViewModel);
943+
Assert.NotNull(popupInstance.BindingContext);
944+
Assert.IsType<ShortLivedMockPageViewModel>(popupInstance.BindingContext);
945945
Assert.Equal(shellParameterBackgroundColorValue, view.BackgroundColor);
946946
Assert.Equal(shellParameterViewModelTextValue, view.BindingContext.Text);
947947
}
@@ -950,8 +950,8 @@ public async Task ShowPopupAsyncWithView_Shell_ShouldValidateProperBindingContex
950950
public async Task ShowPopupAsync_ShouldReturnResultOnceClosed()
951951
{
952952
// Arrange
953-
var mockPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
954-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
953+
var mockPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>();
954+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
955955

956956
// Act
957957
var result = await navigation.ShowPopupAsync<object?>(selfClosingPopup, PopupOptions.Empty, TestContext.Current.CancellationToken);
@@ -972,8 +972,8 @@ public async Task ShowPopupAsync_Shell_ShouldReturnResultOnceClosed()
972972
Application.Current.Windows[0].Page = shell;
973973

974974
var shellNavigation = Shell.Current.Navigation;
975-
var mockPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
976-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
975+
var mockPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>();
976+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
977977

978978
// Act
979979
var result = await shell.ShowPopupAsync<object?>(selfClosingPopup, PopupOptions.Empty, shellParameters, TestContext.Current.CancellationToken);
@@ -1070,7 +1070,7 @@ public async Task ShowPopupAsync_Shell_ShouldThrowArgumentNullException_WhenView
10701070
public async Task ShowPopupAsync_ShouldThrowArgumentNullException_WhenNavigationIsNull()
10711071
{
10721072
// Arrange
1073-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
1073+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
10741074

10751075
// Act / Assert
10761076
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
@@ -1089,7 +1089,7 @@ public async Task ShowPopupAsync_Shell_ShouldThrowArgumentNullException_WhenNavi
10891089
Application.Current.Windows[0].Page = shell;
10901090

10911091
var shellNavigation = Shell.Current.Navigation;
1092-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
1092+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
10931093

10941094
// Act/Assert
10951095
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.

0 commit comments

Comments
 (0)