Skip to content

Commit 3dcbf02

Browse files
committed
fix(FakeNavigationManager): Do Not set Uri if navigation is prevented on net7.0 or greater
1 parent 5a10c37 commit 3dcbf02

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ protected override void NavigateToCore(string uri, NavigationOptions options)
8080
BaseUri = GetBaseUri(absoluteUri);
8181
}
8282

83-
Uri = ToAbsoluteUri(uri).OriginalString;
84-
8583
if (options.ReplaceHistoryEntry && history.Count > 0)
8684
history.Pop();
8785

@@ -92,7 +90,6 @@ protected override void NavigateToCore(string uri, NavigationOptions options)
9290
testContextBase.Renderer.Dispatcher.InvokeAsync(() =>
9391
#endif
9492
{
95-
Uri = absoluteUri.OriginalString;
9693

9794
#if NET7_0_OR_GREATER
9895
var shouldContinueNavigation = false;
@@ -112,8 +109,13 @@ protected override void NavigateToCore(string uri, NavigationOptions options)
112109
{
113110
return;
114111
}
112+
else
113+
{
114+
Uri = ToAbsoluteUri(uri).OriginalString;
115+
}
115116
#else
116117
history.Push(new NavigationHistory(uri, options));
118+
Uri = absoluteUri.OriginalString;
117119
#endif
118120

119121

@@ -135,7 +137,7 @@ protected override void NavigateToCore(string uri, NavigationOptions options)
135137

136138
#if NET7_0_OR_GREATER
137139
/// <inheritdoc/>
138-
protected override void SetNavigationLockState(bool value) {}
140+
protected override void SetNavigationLockState(bool value) { }
139141

140142
/// <inheritdoc/>
141143
protected override void HandleLocationChangingHandlerException(Exception ex, LocationChangingContext context)

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

Lines changed: 37 additions & 8 deletions
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

@@ -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,39 @@ 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+
[Fact(DisplayName = "Preventing Navigation does not change Uri")]
301+
public void Test018()
302+
{
303+
var sut = CreateFakeNavigationManager();
304+
string expectedUri = new Uri(new Uri(sut.BaseUri, UriKind.Absolute), new Uri("/expected-path", UriKind.Relative)).AbsoluteUri;
305+
306+
sut.NavigateTo("/expected-path");
307+
using var handler = sut.RegisterLocationChangingHandler(LocationChangingHandler);
308+
sut.NavigateTo("/prevented-path");
309+
310+
sut.History.First().State.ShouldBe(NavigationState.Prevented);
311+
sut.Uri.ShouldBe(expectedUri);
312+
313+
ValueTask LocationChangingHandler(LocationChangingContext arg)
314+
{
315+
arg.PreventNavigation();
316+
return ValueTask.CompletedTask;
317+
}
318+
}
319+
291320
private sealed class InterceptNavigateToCounterComponent : ComponentBase
292321
{
293322
protected override void BuildRenderTree(RenderTreeBuilder builder)

0 commit comments

Comments
 (0)