Skip to content

Commit

Permalink
test: adding Tabbed Navigation Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Jan 6, 2024
1 parent 9b03e57 commit 7a23272
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ public static void OnNavigationError(this Task<INavigationResult> navigationTask
});
}

/// <summary>
/// Selects a tab programatically
/// </summary>
/// <param name="navigationService"></param>
/// <param name="tabName">The name of the tab to select</param>
/// <returns>The <see cref="INavigationResult"/>.</returns>
public static Task<INavigationResult> SelectTabAsync(this INavigationService navigationService, string tabName) =>
navigationService.SelectTabAsync(tabName, new NavigationParameters());

private static INavigationParameters GetNavigationParameters((string Key, object Value)[] parameters)
{
var navParams = new NavigationParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public async Task GoBack_Issue2232()
}

[Fact]
public async Task TabbedPageSelectTabSetsCurrentTab()
public async Task TabbedPage_SelectTabSets_CurrentTab()
{
var mauiApp = CreateBuilder(prism => prism.CreateWindow("TabbedPage?createTab=MockViewA&createTab=MockViewB&selectedTab=MockViewB"))
.Build();
Expand All @@ -262,7 +262,7 @@ public async Task TabbedPageSelectTabSetsCurrentTab()
}

[Fact]
public async Task TabbedPageSelectTabSetsCurrentTabWithNavigationPageTab()
public async Task TabbedPage_SelectTab_SetsCurrentTab_WithNavigationPageTab()
{
var mauiApp = CreateBuilder(prism => prism.CreateWindow("TabbedPage?createTab=NavigationPage%2FMockViewA&createTab=NavigationPage%2FMockViewB&selectedTab=NavigationPage|MockViewB"))
.Build();
Expand All @@ -276,6 +276,62 @@ public async Task TabbedPageSelectTabSetsCurrentTabWithNavigationPageTab()
Assert.IsType<MockViewB>(navPage.CurrentPage);
}

[Fact]
public async Task TabbedPage_SelectsNewTab()
{
var mauiApp = CreateBuilder(prism => prism
.OnAppStart(nav => nav.CreateBuilder()

Check failure on line 283 in tests/Maui/Prism.DryIoc.Maui.Tests/Fixtures/Navigation/NavigationTests.cs

View workflow job for this annotation

GitHub Actions / build-prism-maui / Build Prism.Maui

'PrismAppBuilder' does not contain a definition for 'OnAppStart' and no accessible extension method 'OnAppStart' accepting a first argument of type 'PrismAppBuilder' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 283 in tests/Maui/Prism.DryIoc.Maui.Tests/Fixtures/Navigation/NavigationTests.cs

View workflow job for this annotation

GitHub Actions / build-prism-maui / Build Prism.Maui

'PrismAppBuilder' does not contain a definition for 'OnAppStart' and no accessible extension method 'OnAppStart' accepting a first argument of type 'PrismAppBuilder' could be found (are you missing a using directive or an assembly reference?)
.AddTabbedSegment(s => s.CreateTab("MockViewA")
.CreateTab("MockViewB")
.CreateTab("MockViewC"))
.NavigateAsync()))
.Build();
var window = GetWindow(mauiApp);
Assert.IsAssignableFrom<TabbedPage>(window.Page);
var tabbed = window.Page as TabbedPage;

Assert.NotNull(tabbed);

Assert.IsType<MockViewA>(tabbed.CurrentPage);
var mockViewA = tabbed.CurrentPage;
var mockViewANav = Prism.Navigation.Xaml.Navigation.GetNavigationService(mockViewA);

await mockViewANav.SelectTabAsync("MockViewB");

Assert.IsNotType<MockViewA>(tabbed.CurrentPage);
Assert.IsType<MockViewB>(tabbed.CurrentPage);
}

[Fact]
public async Task TabbedPage_SelectsNewTab_WithNavigationParameters()
{
var mauiApp = CreateBuilder(prism => prism
.OnAppStart(nav => nav.CreateBuilder()

Check failure on line 309 in tests/Maui/Prism.DryIoc.Maui.Tests/Fixtures/Navigation/NavigationTests.cs

View workflow job for this annotation

GitHub Actions / build-prism-maui / Build Prism.Maui

'PrismAppBuilder' does not contain a definition for 'OnAppStart' and no accessible extension method 'OnAppStart' accepting a first argument of type 'PrismAppBuilder' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 309 in tests/Maui/Prism.DryIoc.Maui.Tests/Fixtures/Navigation/NavigationTests.cs

View workflow job for this annotation

GitHub Actions / build-prism-maui / Build Prism.Maui

'PrismAppBuilder' does not contain a definition for 'OnAppStart' and no accessible extension method 'OnAppStart' accepting a first argument of type 'PrismAppBuilder' could be found (are you missing a using directive or an assembly reference?)
.AddTabbedSegment(s => s.CreateTab("MockViewA")
.CreateTab("MockViewB")
.CreateTab("MockViewC"))
.NavigateAsync()))
.Build();
var window = GetWindow(mauiApp);
Assert.IsAssignableFrom<TabbedPage>(window.Page);
var tabbed = window.Page as TabbedPage;

Assert.NotNull(tabbed);

Assert.IsType<MockViewA>(tabbed.CurrentPage);
var mockViewA = tabbed.CurrentPage;
var mockViewANav = Prism.Navigation.Xaml.Navigation.GetNavigationService(mockViewA);

var expectedMessage = nameof(TabbedPage_SelectsNewTab_WithNavigationParameters);
await mockViewANav.SelectTabAsync("MockViewB", new NavigationParameters { { "Message", expectedMessage } });

Assert.IsNotType<MockViewA>(tabbed.CurrentPage);
Assert.IsType<MockViewB>(tabbed.CurrentPage);

var viewModel = tabbed.CurrentPage.BindingContext as MockViewBViewModel;
Assert.Equal(expectedMessage, viewModel?.Message);
}

[Fact]
public async Task NavigationPage_DoesNotHave_MauiPage_AsRootPage()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Prism.DryIoc.Maui.Tests.Mocks.ViewModels;

public abstract class MockViewModelBase : IActiveAware, IConfirmNavigation
public abstract class MockViewModelBase : IActiveAware, INavigationAware, IConfirmNavigation
{
private readonly IPageAccessor _pageAccessor;

Expand All @@ -14,6 +14,11 @@ protected MockViewModelBase(IPageAccessor pageAccessor, INavigationService navig
NavigationService = navigationService;
}

public string Message { get; private set; }

private List<string> _actions = [];
public IEnumerable<string> Actions => _actions;

public INavigationService NavigationService { get; }

public Task<INavigationResult> GoBack() => NavigationService.GoBackAsync();
Expand All @@ -26,6 +31,21 @@ protected MockViewModelBase(IPageAccessor pageAccessor, INavigationService navig

public bool IsActive { get; set; }

public bool CanNavigate(INavigationParameters parameters) =>
!StopNavigation;
public bool CanNavigate(INavigationParameters parameters)
{
_actions.Add(nameof(CanNavigate));
return !StopNavigation;
}

public void OnNavigatedFrom(INavigationParameters parameters)
{
_actions.Add(nameof(OnNavigatedFrom));
}

public void OnNavigatedTo(INavigationParameters parameters)
{
_actions.Add(nameof(OnNavigatedTo));
if (parameters.TryGetValue<string>("Message", out var message))
Message = message;
}
}

0 comments on commit 7a23272

Please sign in to comment.