Skip to content

Commit a14d4d4

Browse files
Add Unit Tests
1 parent e317f3c commit a14d4d4

3 files changed

Lines changed: 96 additions & 4 deletions

File tree

src/CommunityToolkit.Maui.UnitTests/BaseViewTest.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,17 @@ static void InitializeServicesAndSetMockApplication(out IServiceProvider service
8484
var mauiApp = appBuilder.Build();
8585
serviceProvider = mauiApp.Services;
8686

87-
var page = new ContentPage();
87+
var shell = new Shell();
88+
shell.Items.Add(new ContentPage());
89+
8890
var application = (MockApplication)mauiApp.Services.GetRequiredService<IApplication>();
89-
application.AddWindow(new Window { Page = page });
91+
application.AddWindow(new Window { Page = shell });
9092

9193
IPlatformApplication.Current = application;
9294

9395
application.Handler = new ApplicationHandlerStub();
9496
application.Handler.SetMauiContext(new HandlersContextStub(serviceProvider));
9597

96-
CreateViewHandler<MockPageHandler>(page);
98+
CreateViewHandler<MockPageHandler>(shell);
9799
}
98100
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using CommunityToolkit.Maui.Extensions;
2+
using CommunityToolkit.Maui.UnitTests.Mocks;
3+
using CommunityToolkit.Maui.UnitTests.Services;
4+
using Xunit;
5+
6+
namespace CommunityToolkit.Maui.UnitTests.Extensions;
7+
8+
public class NavigatedToEventArgsExtensionsTests : BaseViewTest
9+
{
10+
[Fact]
11+
public async Task NavigatedToEventArgsExtensions_WasPreviousPageACommunityToolkitPopupPage_ShouldReturnTrue()
12+
{
13+
// Arrange
14+
TaskCompletionSource<bool?> wasPreviousPageACommunityToolkitPopupPageTCS = new();
15+
var application = (MockApplication)ServiceProvider.GetRequiredService<IApplication>();
16+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>();
17+
var popupService = ServiceProvider.GetRequiredService<IPopupService>();
18+
19+
var shell = (Shell)(application.Windows[0].Page ?? throw new InvalidOperationException("Unable to retrieve Shell"));
20+
var mainPage = shell.CurrentPage;
21+
var shellContentPage = new ShellContentPage();
22+
shellContentPage.NavigatedToEventArgsReceived += HandleNavigatedToEventArgsReceived;
23+
24+
var shellParameters = new Dictionary<string, object>
25+
{
26+
{ nameof(ContentPage.BackgroundColor), Colors.Orange }
27+
};
28+
29+
30+
// Act
31+
await mainPage.Navigation.PushAsync(shellContentPage);
32+
await popupService.ShowPopupAsync<ShortLivedMockPageViewModel>(shell, null, shellParameters, TestContext.Current.CancellationToken);
33+
var wasPreviousPageACommunityToolkitPopupPage = await wasPreviousPageACommunityToolkitPopupPageTCS.Task;
34+
35+
// Assert
36+
Assert.True(wasPreviousPageACommunityToolkitPopupPage);
37+
38+
void HandleNavigatedToEventArgsReceived(object? sender, NavigatedToEventArgs e)
39+
{
40+
if (e.PreviousPage != mainPage)
41+
{
42+
wasPreviousPageACommunityToolkitPopupPageTCS.SetResult(e.WasPreviousPageACommunityToolkitPopupPage());
43+
}
44+
}
45+
}
46+
47+
[Fact]
48+
public async Task NavigatedToEventArgsExtensions_WasPreviousPageACommunityToolkitPopupPage_ShouldReturnFalse()
49+
{
50+
// Arrange
51+
TaskCompletionSource<bool?> wasPreviousPageACommunityToolkitPopupPageTCS = new();
52+
var application = (MockApplication)ServiceProvider.GetRequiredService<IApplication>();
53+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>();
54+
var popupService = ServiceProvider.GetRequiredService<IPopupService>();
55+
56+
var shell = (Shell)(application.Windows[0].Page ?? throw new InvalidOperationException("Unable to retrieve Shell"));
57+
var mainPage = shell.CurrentPage;
58+
var shellContentPage = new ShellContentPage();
59+
shellContentPage.NavigatedToEventArgsReceived += HandleNavigatedToEventArgsReceived;
60+
61+
var shellParameters = new Dictionary<string, object>
62+
{
63+
{ nameof(ContentPage.BackgroundColor), Colors.Orange }
64+
};
65+
66+
67+
// Act
68+
await mainPage.Navigation.PushAsync(shellContentPage);
69+
var wasPreviousPageACommunityToolkitPopupPage = await wasPreviousPageACommunityToolkitPopupPageTCS.Task;
70+
71+
// Assert
72+
Assert.False(wasPreviousPageACommunityToolkitPopupPage);
73+
74+
void HandleNavigatedToEventArgsReceived(object? sender, NavigatedToEventArgs e)
75+
{
76+
wasPreviousPageACommunityToolkitPopupPageTCS.SetResult(e.WasPreviousPageACommunityToolkitPopupPage());
77+
}
78+
}
79+
80+
sealed class ShellContentPage : ContentPage
81+
{
82+
public event EventHandler<NavigatedToEventArgs>? NavigatedToEventArgsReceived;
83+
84+
protected override void OnNavigatedTo(NavigatedToEventArgs args)
85+
{
86+
base.OnNavigatedTo(args);
87+
NavigatedToEventArgsReceived?.Invoke(this, args);
88+
}
89+
}
90+
}

src/CommunityToolkit.Maui/Extensions/NavigatedToEventArgsExtensions.shared.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ public static class NavigatedToEventArgsExtensions
1212
/// </summary>
1313
/// <param name="args">The current <see cref="NavigatedToEventArgs"/>.</param>
1414
/// <returns>A boolean indicating whether the previous page was a Community Toolkit <see cref="Popup"/>.</returns>
15-
public static bool WasPreviousPageAToolkitPopup(this NavigatedToEventArgs args) => args.PreviousPage is PopupPage;
15+
public static bool WasPreviousPageACommunityToolkitPopupPage(this NavigatedToEventArgs args) => args.PreviousPage is PopupPage;
1616
}

0 commit comments

Comments
 (0)