Skip to content

Commit 2284ed6

Browse files
fix(FakeNavigationManager): Do Not set Uri if navigation is prevented… (#1649)
* fix(FakeNavigationManager): Do Not set Uri if navigation is prevented on net7.0 or greater * Adding changelog, updating FakeNavigatoinManager to not change baseuri on prevention; updating test case to match. * Also mentioning BaseUri in the changelog. * restructure tests for navigation prevention. * Update CHANGELOG.md Co-authored-by: Steven Giesel <[email protected]> --------- Co-authored-by: Steven Giesel <[email protected]>
1 parent a689c16 commit 2284ed6

File tree

3 files changed

+59
-31
lines changed

3 files changed

+59
-31
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ All notable changes to **bUnit** will be documented in this file. The project ad
66

77
## [Unreleased]
88

9+
### Fixed
10+
11+
- 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)
12+
913
## [1.38.5] - 2025-01-12
1014

1115
### Added

src/bunit.web/TestDoubles/NavigationManager/FakeNavigationManager.cs

+7-13
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,6 @@ protected override void NavigateToCore(string uri, NavigationOptions options)
7575
var absoluteUri = GetNewAbsoluteUri(uri);
7676
var changedBaseUri = HasDifferentBaseUri(absoluteUri);
7777

78-
if (changedBaseUri)
79-
{
80-
BaseUri = GetBaseUri(absoluteUri);
81-
}
82-
83-
Uri = ToAbsoluteUri(uri).OriginalString;
84-
8578
if (options.ReplaceHistoryEntry && history.Count > 0)
8679
history.Pop();
8780

@@ -92,7 +85,6 @@ protected override void NavigateToCore(string uri, NavigationOptions options)
9285
testContextBase.Renderer.Dispatcher.InvokeAsync(() =>
9386
#endif
9487
{
95-
Uri = absoluteUri.OriginalString;
9688

9789
#if NET7_0_OR_GREATER
9890
var shouldContinueNavigation = false;
@@ -116,6 +108,12 @@ protected override void NavigateToCore(string uri, NavigationOptions options)
116108
history.Push(new NavigationHistory(uri, options));
117109
#endif
118110

111+
if (changedBaseUri)
112+
{
113+
BaseUri = GetBaseUri(absoluteUri);
114+
}
115+
116+
Uri = absoluteUri.OriginalString;
119117

120118
// Only notify of changes if user navigates within the same
121119
// base url (domain). Otherwise, the user navigated away
@@ -125,17 +123,13 @@ protected override void NavigateToCore(string uri, NavigationOptions options)
125123
{
126124
NotifyLocationChanged(isInterceptedLink: false);
127125
}
128-
else
129-
{
130-
BaseUri = GetBaseUri(absoluteUri);
131-
}
132126
});
133127
}
134128
#endif
135129

136130
#if NET7_0_OR_GREATER
137131
/// <inheritdoc/>
138-
protected override void SetNavigationLockState(bool value) {}
132+
protected override void SetNavigationLockState(bool value) { }
139133

140134
/// <inheritdoc/>
141135
protected override void HandleLocationChangingHandlerException(Exception ex, LocationChangingContext context)

tests/bunit.web.tests/TestDoubles/NavigationManager/FakeNavigationManagerTest.cs

+48-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Text.Json;
22
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
3+
using Shouldly.ShouldlyExtensionMethods;
34

45
namespace Bunit.TestDoubles;
56

@@ -106,18 +107,18 @@ public void Test007()
106107
}
107108

108109
#if !NET6_0_OR_GREATER
109-
[Theory(DisplayName = "NavigateTo(uri, forceLoad) is saved in history")]
110-
[InlineData("/uri", false)]
111-
[InlineData("/anotherUri", true)]
112-
public void Test100(string uri, bool forceLoad)
113-
{
114-
var sut = CreateFakeNavigationManager();
110+
[Theory(DisplayName = "NavigateTo(uri, forceLoad) is saved in history")]
111+
[InlineData("/uri", false)]
112+
[InlineData("/anotherUri", true)]
113+
public void Test100(string uri, bool forceLoad)
114+
{
115+
var sut = CreateFakeNavigationManager();
115116

116-
sut.NavigateTo(uri, forceLoad);
117+
sut.NavigateTo(uri, forceLoad);
117118

118-
sut.History.ShouldHaveSingleItem()
119-
.ShouldBeEquivalentTo(new NavigationHistory(uri, new NavigationOptions(forceLoad)));
120-
}
119+
sut.History.ShouldHaveSingleItem()
120+
.ShouldBeEquivalentTo(new NavigationHistory(uri, new NavigationOptions(forceLoad)));
121+
}
121122
#endif
122123
#if NET6_0_OR_GREATER
123124
[Theory(DisplayName = "NavigateTo(uri, forceLoad, replaceHistoryEntry) is saved in history")]
@@ -135,8 +136,12 @@ public void Test200(string uri, bool forceLoad, bool replaceHistoryEntry)
135136
.ShouldBeEquivalentTo(new NavigationHistory(uri,
136137
new NavigationOptions { ForceLoad = forceLoad, ReplaceHistoryEntry = replaceHistoryEntry }));
137138
#elif NET7_0_OR_GREATER
138-
var navigationOptions = new NavigationOptions { ForceLoad = forceLoad, ReplaceHistoryEntry =
139-
replaceHistoryEntry };
139+
var navigationOptions = new NavigationOptions
140+
{
141+
ForceLoad = forceLoad,
142+
ReplaceHistoryEntry =
143+
replaceHistoryEntry
144+
};
140145
sut.History.ShouldHaveSingleItem()
141146
.ShouldBeEquivalentTo(new NavigationHistory(uri, navigationOptions, NavigationState.Succeeded));
142147
#endif
@@ -156,8 +161,11 @@ public void Test201()
156161
new NavigationOptions { ReplaceHistoryEntry = true }));
157162
#elif NET7_0_OR_GREATER
158163
sut.History.ShouldHaveSingleItem()
159-
.ShouldBeEquivalentTo(new NavigationHistory("/secondUrl", new NavigationOptions { ReplaceHistoryEntry =
160-
true }, NavigationState.Succeeded));
164+
.ShouldBeEquivalentTo(new NavigationHistory("/secondUrl", new NavigationOptions
165+
{
166+
ReplaceHistoryEntry =
167+
true
168+
}, NavigationState.Succeeded));
161169
#endif
162170
}
163171
#endif
@@ -230,7 +238,8 @@ public void Test013()
230238
var fakeNavigationManager = CreateFakeNavigationManager();
231239
var requestOptions = new InteractiveRequestOptions
232240
{
233-
ReturnUrl = "return", Interaction = InteractionType.SignIn,
241+
ReturnUrl = "return",
242+
Interaction = InteractionType.SignIn,
234243
};
235244
requestOptions.TryAddAdditionalParameter("library", "bunit");
236245

@@ -264,7 +273,7 @@ public void Test015()
264273
Should.Throw<JsonException>(
265274
() => fakeNavigationManager.History.Last().StateFromJson<InteractiveRequestOptions>());
266275
}
267-
276+
268277
[Fact(DisplayName = "Navigate to url with state should set that state on the NavigationManager")]
269278
public void Test016()
270279
{
@@ -275,19 +284,40 @@ public void Test016()
275284

276285
sut.HistoryEntryState.ShouldBe(State);
277286
}
278-
287+
279288
[Fact(DisplayName = "Navigate to url with force reload resets state")]
280289
public void Test017()
281290
{
282291
const string State = "State";
283292
var sut = CreateFakeNavigationManager();
284293

285294
sut.NavigateTo("/internal", new NavigationOptions { HistoryEntryState = State });
286-
sut.NavigateTo("/internal", new NavigationOptions { HistoryEntryState = State, ForceLoad = true});
295+
sut.NavigateTo("/internal", new NavigationOptions { HistoryEntryState = State, ForceLoad = true });
287296

288297
sut.HistoryEntryState.ShouldBe(null);
289298
}
290299

300+
[Theory(DisplayName = "Preventing Navigation does not change Uri or BaseUri")]
301+
[InlineData("/prevented-path")]
302+
[InlineData("https://bunit.dev/uri")]
303+
public void Test018(string navToUri)
304+
{
305+
var sut = CreateFakeNavigationManager();
306+
using var handler = sut.RegisterLocationChangingHandler(LocationChangingHandler);
307+
308+
sut.NavigateTo(navToUri);
309+
310+
sut.History.First().State.ShouldBe(NavigationState.Prevented);
311+
sut.BaseUri.ShouldBe("http://localhost/");
312+
sut.Uri.ShouldBe("http://localhost/");
313+
314+
ValueTask LocationChangingHandler(LocationChangingContext arg)
315+
{
316+
arg.PreventNavigation();
317+
return ValueTask.CompletedTask;
318+
}
319+
}
320+
291321
private sealed class InterceptNavigateToCounterComponent : ComponentBase
292322
{
293323
protected override void BuildRenderTree(RenderTreeBuilder builder)

0 commit comments

Comments
 (0)