Skip to content

Commit 815aca3

Browse files
committed
test(Maui): wire navigation recorder into container mock
Assign PageNavigationContainerMock.SharedNavigationRecorder from PageNavigationServiceMock so DI-resolved pages record lifecycle events. Update NavigateAsync_From_NavigationPage_When_NotClearNavigationStack_And_SamePage to assert the Prism re-navigation sequence on the existing top page; the prior recorder.IsEmpty check passed only because resolved pages had no recorder.
1 parent 1685cbc commit 815aca3

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

tests/Maui/Prism.Maui.Tests/Fixtures/PageNavigationServiceFixture.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ public async Task NavigateAsync_From_NavigationPage_With_ChildPage_And_DoesNotRe
744744
[Fact]
745745
public async Task NavigateAsync_From_NavigationPage_When_NotClearNavigationStack_And_SamePage()
746746
{
747-
var recorder = new PageNavigationEventRecorder(); ;
747+
var recorder = new PageNavigationEventRecorder();
748748
var navigationService = new PageNavigationServiceMock(_container, _app, recorder);
749749
var contentPageMock = new ContentPageMock(recorder);
750750
var navigationPage = new NavigationPageMock(recorder, contentPageMock);
@@ -755,6 +755,7 @@ public async Task NavigateAsync_From_NavigationPage_When_NotClearNavigationStack
755755
Assert.True(r1.Success);
756756

757757
var secondContentPage = navigationPage.Navigation.NavigationStack.Last();
758+
var secondVm = Assert.IsType<SecondContentPageMockViewModel>(secondContentPage.BindingContext);
758759

759760
recorder.Clear();
760761
((IPageAware)navigationService).Page = navigationPage;
@@ -765,7 +766,18 @@ public async Task NavigateAsync_From_NavigationPage_When_NotClearNavigationStack
765766
Assert.Empty(navigationPage.Navigation.ModalStack);
766767
Assert.Equal(2, navigationPage.Navigation.NavigationStack.Count);
767768
Assert.Same(secondContentPage, navigationPage.Navigation.NavigationStack.Last());
768-
Assert.True(recorder.IsEmpty);
769+
770+
// DoNavigateAction still runs Initialize + NavigatedFrom/To on the existing top page (see PageNavigationService.ProcessNavigationForNavigationPage).
771+
// Previously this asserted recorder.IsEmpty because DI-created pages had no recorder wired; that hid real lifecycle callbacks.
772+
AssertPageNavigationRecords(recorder.Records,
773+
(secondContentPage, PageNavigationEvent.OnInitialized),
774+
(secondVm, PageNavigationEvent.OnInitialized),
775+
(secondContentPage, PageNavigationEvent.OnInitializedAsync),
776+
(secondVm, PageNavigationEvent.OnInitializedAsync),
777+
(secondContentPage, PageNavigationEvent.OnNavigatedFrom),
778+
(secondVm, PageNavigationEvent.OnNavigatedFrom),
779+
(secondContentPage, PageNavigationEvent.OnNavigatedTo),
780+
(secondVm, PageNavigationEvent.OnNavigatedTo));
769781
}
770782

771783

tests/Maui/Prism.Maui.Tests/Mocks/PageNavigationServiceMock.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ public sealed class PageNavigationServiceMock : PageNavigationService, IPageAwar
1111
public PageNavigationServiceMock(PageNavigationContainerMock container, ApplicationMock app, PageNavigationEventRecorder? recorder = null)
1212
: base(container, new TestWindowManager(app.Window), new EventAggregator(), new MutablePageAccessor())
1313
{
14-
_ = recorder;
14+
// Wire recorder into DI-resolved pages (ContentPageMock, etc.) via PageNavigationContainerMock.AfterResolve.
15+
// Always assign so a shared fixture container does not keep a stale recorder when the next test omits it.
16+
container.SharedNavigationRecorder = recorder;
1517
}
1618

1719
Page IPageAware.Page

0 commit comments

Comments
 (0)