diff --git a/CHANGELOG.md b/CHANGELOG.md index 51081c37a..c1991bd23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ All notable changes to **bUnit** will be documented in this file. The project ad ## [Unreleased] +### Fixed + +- Do not set the `Uri` or `BaseUri` property on the `FakeNavigationManager` if navigation is prevented by a handler on `net7.0` or greater. Reported and fixed by [@ayyron-dev](https://github.com/ayyron-dev) in [#1647](https://github.com/bUnit-dev/bUnit/issues/1647) + ## [1.38.5] - 2025-01-12 ### Added diff --git a/src/bunit.web/TestDoubles/NavigationManager/FakeNavigationManager.cs b/src/bunit.web/TestDoubles/NavigationManager/FakeNavigationManager.cs index 574807cf3..85a27b1b4 100644 --- a/src/bunit.web/TestDoubles/NavigationManager/FakeNavigationManager.cs +++ b/src/bunit.web/TestDoubles/NavigationManager/FakeNavigationManager.cs @@ -75,13 +75,6 @@ protected override void NavigateToCore(string uri, NavigationOptions options) var absoluteUri = GetNewAbsoluteUri(uri); var changedBaseUri = HasDifferentBaseUri(absoluteUri); - if (changedBaseUri) - { - BaseUri = GetBaseUri(absoluteUri); - } - - Uri = ToAbsoluteUri(uri).OriginalString; - if (options.ReplaceHistoryEntry && history.Count > 0) history.Pop(); @@ -92,7 +85,6 @@ protected override void NavigateToCore(string uri, NavigationOptions options) testContextBase.Renderer.Dispatcher.InvokeAsync(() => #endif { - Uri = absoluteUri.OriginalString; #if NET7_0_OR_GREATER var shouldContinueNavigation = false; @@ -116,6 +108,12 @@ protected override void NavigateToCore(string uri, NavigationOptions options) history.Push(new NavigationHistory(uri, options)); #endif + if (changedBaseUri) + { + BaseUri = GetBaseUri(absoluteUri); + } + + Uri = absoluteUri.OriginalString; // Only notify of changes if user navigates within the same // base url (domain). Otherwise, the user navigated away @@ -125,17 +123,13 @@ protected override void NavigateToCore(string uri, NavigationOptions options) { NotifyLocationChanged(isInterceptedLink: false); } - else - { - BaseUri = GetBaseUri(absoluteUri); - } }); } #endif #if NET7_0_OR_GREATER /// - protected override void SetNavigationLockState(bool value) {} + protected override void SetNavigationLockState(bool value) { } /// protected override void HandleLocationChangingHandlerException(Exception ex, LocationChangingContext context) diff --git a/tests/bunit.web.tests/TestDoubles/NavigationManager/FakeNavigationManagerTest.cs b/tests/bunit.web.tests/TestDoubles/NavigationManager/FakeNavigationManagerTest.cs index ca09b9423..1578dfe28 100644 --- a/tests/bunit.web.tests/TestDoubles/NavigationManager/FakeNavigationManagerTest.cs +++ b/tests/bunit.web.tests/TestDoubles/NavigationManager/FakeNavigationManagerTest.cs @@ -1,5 +1,6 @@ using System.Text.Json; using Microsoft.AspNetCore.Components.WebAssembly.Authentication; +using Shouldly.ShouldlyExtensionMethods; namespace Bunit.TestDoubles; @@ -106,18 +107,18 @@ public void Test007() } #if !NET6_0_OR_GREATER - [Theory(DisplayName = "NavigateTo(uri, forceLoad) is saved in history")] - [InlineData("/uri", false)] - [InlineData("/anotherUri", true)] - public void Test100(string uri, bool forceLoad) - { - var sut = CreateFakeNavigationManager(); + [Theory(DisplayName = "NavigateTo(uri, forceLoad) is saved in history")] + [InlineData("/uri", false)] + [InlineData("/anotherUri", true)] + public void Test100(string uri, bool forceLoad) + { + var sut = CreateFakeNavigationManager(); - sut.NavigateTo(uri, forceLoad); + sut.NavigateTo(uri, forceLoad); - sut.History.ShouldHaveSingleItem() - .ShouldBeEquivalentTo(new NavigationHistory(uri, new NavigationOptions(forceLoad))); - } + sut.History.ShouldHaveSingleItem() + .ShouldBeEquivalentTo(new NavigationHistory(uri, new NavigationOptions(forceLoad))); + } #endif #if NET6_0_OR_GREATER [Theory(DisplayName = "NavigateTo(uri, forceLoad, replaceHistoryEntry) is saved in history")] @@ -135,8 +136,12 @@ public void Test200(string uri, bool forceLoad, bool replaceHistoryEntry) .ShouldBeEquivalentTo(new NavigationHistory(uri, new NavigationOptions { ForceLoad = forceLoad, ReplaceHistoryEntry = replaceHistoryEntry })); #elif NET7_0_OR_GREATER - var navigationOptions = new NavigationOptions { ForceLoad = forceLoad, ReplaceHistoryEntry = - replaceHistoryEntry }; + var navigationOptions = new NavigationOptions + { + ForceLoad = forceLoad, + ReplaceHistoryEntry = + replaceHistoryEntry + }; sut.History.ShouldHaveSingleItem() .ShouldBeEquivalentTo(new NavigationHistory(uri, navigationOptions, NavigationState.Succeeded)); #endif @@ -156,8 +161,11 @@ public void Test201() new NavigationOptions { ReplaceHistoryEntry = true })); #elif NET7_0_OR_GREATER sut.History.ShouldHaveSingleItem() - .ShouldBeEquivalentTo(new NavigationHistory("/secondUrl", new NavigationOptions { ReplaceHistoryEntry = - true }, NavigationState.Succeeded)); + .ShouldBeEquivalentTo(new NavigationHistory("/secondUrl", new NavigationOptions + { + ReplaceHistoryEntry = + true + }, NavigationState.Succeeded)); #endif } #endif @@ -230,7 +238,8 @@ public void Test013() var fakeNavigationManager = CreateFakeNavigationManager(); var requestOptions = new InteractiveRequestOptions { - ReturnUrl = "return", Interaction = InteractionType.SignIn, + ReturnUrl = "return", + Interaction = InteractionType.SignIn, }; requestOptions.TryAddAdditionalParameter("library", "bunit"); @@ -264,7 +273,7 @@ public void Test015() Should.Throw( () => fakeNavigationManager.History.Last().StateFromJson()); } - + [Fact(DisplayName = "Navigate to url with state should set that state on the NavigationManager")] public void Test016() { @@ -275,7 +284,7 @@ public void Test016() sut.HistoryEntryState.ShouldBe(State); } - + [Fact(DisplayName = "Navigate to url with force reload resets state")] public void Test017() { @@ -283,11 +292,32 @@ public void Test017() var sut = CreateFakeNavigationManager(); sut.NavigateTo("/internal", new NavigationOptions { HistoryEntryState = State }); - sut.NavigateTo("/internal", new NavigationOptions { HistoryEntryState = State, ForceLoad = true}); + sut.NavigateTo("/internal", new NavigationOptions { HistoryEntryState = State, ForceLoad = true }); sut.HistoryEntryState.ShouldBe(null); } + [Theory(DisplayName = "Preventing Navigation does not change Uri or BaseUri")] + [InlineData("/prevented-path")] + [InlineData("https://bunit.dev/uri")] + public void Test018(string navToUri) + { + var sut = CreateFakeNavigationManager(); + using var handler = sut.RegisterLocationChangingHandler(LocationChangingHandler); + + sut.NavigateTo(navToUri); + + sut.History.First().State.ShouldBe(NavigationState.Prevented); + sut.BaseUri.ShouldBe("http://localhost/"); + sut.Uri.ShouldBe("http://localhost/"); + + ValueTask LocationChangingHandler(LocationChangingContext arg) + { + arg.PreventNavigation(); + return ValueTask.CompletedTask; + } + } + private sealed class InterceptNavigateToCounterComponent : ComponentBase { protected override void BuildRenderTree(RenderTreeBuilder builder)