From 3dcbf021e17caccf8fbcf8a04070bc89c9c3a31f Mon Sep 17 00:00:00 2001 From: Aaron Somers Date: Tue, 14 Jan 2025 16:13:43 -0500 Subject: [PATCH 1/5] fix(FakeNavigationManager): Do Not set Uri if navigation is prevented on net7.0 or greater --- .../FakeNavigationManager.cs | 10 +++-- .../FakeNavigationManagerTest.cs | 45 +++++++++++++++---- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/bunit.web/TestDoubles/NavigationManager/FakeNavigationManager.cs b/src/bunit.web/TestDoubles/NavigationManager/FakeNavigationManager.cs index 574807cf3..c4b8f35bc 100644 --- a/src/bunit.web/TestDoubles/NavigationManager/FakeNavigationManager.cs +++ b/src/bunit.web/TestDoubles/NavigationManager/FakeNavigationManager.cs @@ -80,8 +80,6 @@ protected override void NavigateToCore(string uri, NavigationOptions options) BaseUri = GetBaseUri(absoluteUri); } - Uri = ToAbsoluteUri(uri).OriginalString; - if (options.ReplaceHistoryEntry && history.Count > 0) history.Pop(); @@ -92,7 +90,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; @@ -112,8 +109,13 @@ protected override void NavigateToCore(string uri, NavigationOptions options) { return; } + else + { + Uri = ToAbsoluteUri(uri).OriginalString; + } #else history.Push(new NavigationHistory(uri, options)); + Uri = absoluteUri.OriginalString; #endif @@ -135,7 +137,7 @@ protected override void NavigateToCore(string uri, NavigationOptions options) #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..e8407376c 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; @@ -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,31 @@ 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); } + [Fact(DisplayName = "Preventing Navigation does not change Uri")] + public void Test018() + { + var sut = CreateFakeNavigationManager(); + string expectedUri = new Uri(new Uri(sut.BaseUri, UriKind.Absolute), new Uri("/expected-path", UriKind.Relative)).AbsoluteUri; + + sut.NavigateTo("/expected-path"); + using var handler = sut.RegisterLocationChangingHandler(LocationChangingHandler); + sut.NavigateTo("/prevented-path"); + + sut.History.First().State.ShouldBe(NavigationState.Prevented); + sut.Uri.ShouldBe(expectedUri); + + ValueTask LocationChangingHandler(LocationChangingContext arg) + { + arg.PreventNavigation(); + return ValueTask.CompletedTask; + } + } + private sealed class InterceptNavigateToCounterComponent : ComponentBase { protected override void BuildRenderTree(RenderTreeBuilder builder) From 09b7a24344b16ccf738f7c034f4dbd78e6a56683 Mon Sep 17 00:00:00 2001 From: Aaron Somers Date: Thu, 16 Jan 2025 07:28:39 -0500 Subject: [PATCH 2/5] Adding changelog, updating FakeNavigatoinManager to not change baseuri on prevention; updating test case to match. --- CHANGELOG.md | 4 +++ .../FakeNavigationManager.cs | 20 ++++------- .../FakeNavigationManagerTest.cs | 36 ++++++++++--------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51081c37a..2f16434ce 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` property on the `FakeNavigationManager` if navigation is prevented by a handler on `net7.0` or greater. + ## [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 c4b8f35bc..85a27b1b4 100644 --- a/src/bunit.web/TestDoubles/NavigationManager/FakeNavigationManager.cs +++ b/src/bunit.web/TestDoubles/NavigationManager/FakeNavigationManager.cs @@ -75,11 +75,6 @@ protected override void NavigateToCore(string uri, NavigationOptions options) var absoluteUri = GetNewAbsoluteUri(uri); var changedBaseUri = HasDifferentBaseUri(absoluteUri); - if (changedBaseUri) - { - BaseUri = GetBaseUri(absoluteUri); - } - if (options.ReplaceHistoryEntry && history.Count > 0) history.Pop(); @@ -109,15 +104,16 @@ protected override void NavigateToCore(string uri, NavigationOptions options) { return; } - else - { - Uri = ToAbsoluteUri(uri).OriginalString; - } #else history.Push(new NavigationHistory(uri, options)); - Uri = absoluteUri.OriginalString; #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 @@ -127,10 +123,6 @@ protected override void NavigateToCore(string uri, NavigationOptions options) { NotifyLocationChanged(isInterceptedLink: false); } - else - { - BaseUri = GetBaseUri(absoluteUri); - } }); } #endif diff --git a/tests/bunit.web.tests/TestDoubles/NavigationManager/FakeNavigationManagerTest.cs b/tests/bunit.web.tests/TestDoubles/NavigationManager/FakeNavigationManagerTest.cs index e8407376c..c71b2e838 100644 --- a/tests/bunit.web.tests/TestDoubles/NavigationManager/FakeNavigationManagerTest.cs +++ b/tests/bunit.web.tests/TestDoubles/NavigationManager/FakeNavigationManagerTest.cs @@ -107,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")] @@ -297,18 +297,22 @@ public void Test017() sut.HistoryEntryState.ShouldBe(null); } - [Fact(DisplayName = "Preventing Navigation does not change Uri")] - public void Test018() + [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(); - string expectedUri = new Uri(new Uri(sut.BaseUri, UriKind.Absolute), new Uri("/expected-path", UriKind.Relative)).AbsoluteUri; + Uri expectedUri = new Uri(new Uri(sut.BaseUri, UriKind.Absolute), new Uri("/expected-path", UriKind.Relative)); + string expectedBaseUri = sut.BaseUri; - sut.NavigateTo("/expected-path"); + sut.NavigateTo(expectedUri.AbsoluteUri); using var handler = sut.RegisterLocationChangingHandler(LocationChangingHandler); - sut.NavigateTo("/prevented-path"); + sut.NavigateTo(navToUri); sut.History.First().State.ShouldBe(NavigationState.Prevented); - sut.Uri.ShouldBe(expectedUri); + sut.BaseUri.ShouldBe(expectedBaseUri); + sut.Uri.ShouldBe(expectedUri.AbsoluteUri); ValueTask LocationChangingHandler(LocationChangingContext arg) { From 9649c0fb5046d5ce2ecbc68e41922220d15436cd Mon Sep 17 00:00:00 2001 From: Aaron Somers Date: Thu, 16 Jan 2025 07:41:15 -0500 Subject: [PATCH 3/5] Also mentioning BaseUri in the changelog. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f16434ce..b77d421a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ All notable changes to **bUnit** will be documented in this file. The project ad ### Fixed -- Do not set the `Uri` property on the `FakeNavigationManager` if navigation is prevented by a handler on `net7.0` or greater. +- Do not set the `Uri` or `BaseUri` property on the `FakeNavigationManager` if navigation is prevented by a handler on `net7.0` or greater. ## [1.38.5] - 2025-01-12 From 3d5ca0ecfe3103d6c92c164608043e9b09a43a17 Mon Sep 17 00:00:00 2001 From: Aaron Somers Date: Thu, 16 Jan 2025 09:38:45 -0500 Subject: [PATCH 4/5] restructure tests for navigation prevention. --- .../NavigationManager/FakeNavigationManagerTest.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/bunit.web.tests/TestDoubles/NavigationManager/FakeNavigationManagerTest.cs b/tests/bunit.web.tests/TestDoubles/NavigationManager/FakeNavigationManagerTest.cs index c71b2e838..1578dfe28 100644 --- a/tests/bunit.web.tests/TestDoubles/NavigationManager/FakeNavigationManagerTest.cs +++ b/tests/bunit.web.tests/TestDoubles/NavigationManager/FakeNavigationManagerTest.cs @@ -303,16 +303,13 @@ public void Test017() public void Test018(string navToUri) { var sut = CreateFakeNavigationManager(); - Uri expectedUri = new Uri(new Uri(sut.BaseUri, UriKind.Absolute), new Uri("/expected-path", UriKind.Relative)); - string expectedBaseUri = sut.BaseUri; - - sut.NavigateTo(expectedUri.AbsoluteUri); using var handler = sut.RegisterLocationChangingHandler(LocationChangingHandler); + sut.NavigateTo(navToUri); sut.History.First().State.ShouldBe(NavigationState.Prevented); - sut.BaseUri.ShouldBe(expectedBaseUri); - sut.Uri.ShouldBe(expectedUri.AbsoluteUri); + sut.BaseUri.ShouldBe("http://localhost/"); + sut.Uri.ShouldBe("http://localhost/"); ValueTask LocationChangingHandler(LocationChangingContext arg) { From 43f39cc33188b4013c15abaf6bc95bd87c39752f Mon Sep 17 00:00:00 2001 From: Aaron Date: Thu, 16 Jan 2025 09:42:40 -0500 Subject: [PATCH 5/5] Update CHANGELOG.md Co-authored-by: Steven Giesel --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b77d421a1..c1991bd23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ All notable changes to **bUnit** will be documented in this file. The project ad ### Fixed -- Do not set the `Uri` or `BaseUri` property on the `FakeNavigationManager` if navigation is prevented by a handler on `net7.0` or greater. +- 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