Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(FakeNavigationManager): Do Not set Uri if navigation is prevented… #1649

Merged
merged 5 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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
/// <inheritdoc/>
protected override void SetNavigationLockState(bool value) {}
protected override void SetNavigationLockState(bool value) { }

/// <inheritdoc/>
protected override void HandleLocationChangingHandlerException(Exception ex, LocationChangingContext context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text.Json;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Shouldly.ShouldlyExtensionMethods;

namespace Bunit.TestDoubles;

Expand Down Expand Up @@ -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")]
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -264,7 +273,7 @@ public void Test015()
Should.Throw<JsonException>(
() => fakeNavigationManager.History.Last().StateFromJson<InteractiveRequestOptions>());
}

[Fact(DisplayName = "Navigate to url with state should set that state on the NavigationManager")]
public void Test016()
{
Expand All @@ -275,19 +284,40 @@ public void Test016()

sut.HistoryEntryState.ShouldBe(State);
}

[Fact(DisplayName = "Navigate to url with force reload resets state")]
public void Test017()
{
const string State = "State";
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)
Expand Down
Loading