Skip to content

Commit 91e9e22

Browse files
committed
feat: Set HistoryEntryState in NavigationManager
1 parent 08ea910 commit 91e9e22

3 files changed

Lines changed: 25 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ All notable changes to **bUnit** will be documented in this file. The project ad
1313
### Fixed
1414

1515
- When the `TestContext` was disposed, the Blazor Renderer itself didn't dispose components under test. By [@linkdotnet](https://github.com/linkdotnet).
16+
- When navigating, the `HistoryEntryState` on `NavigationManager` will be populated. By [@linkdotnet](https://github.com/linkdotnet).
1617

1718
### Added
1819

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ protected override void NavigateToCore(string uri, NavigationOptions options)
8686
history.Pop();
8787

8888
#if NET7_0_OR_GREATER
89+
HistoryEntryState = options.ForceLoad ? null : options.HistoryEntryState;
8990
renderer.Dispatcher.InvokeAsync(async () =>
9091
#else
9192
renderer.Dispatcher.InvokeAsync(() =>

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,29 @@ public void Test015()
264264
Should.Throw<JsonException>(
265265
() => fakeNavigationManager.History.Last().StateFromJson<InteractiveRequestOptions>());
266266
}
267+
268+
[Fact(DisplayName = "Navigate to url with state should set that state on the NavigationManager")]
269+
public void Test016()
270+
{
271+
const string State = "State";
272+
var sut = CreateFakeNavigationManager();
273+
274+
sut.NavigateTo("/internal", new NavigationOptions { HistoryEntryState = State });
275+
276+
sut.HistoryEntryState.ShouldBe(State);
277+
}
278+
279+
[Fact(DisplayName = "Navigate to url with force reload resets state")]
280+
public void Test017()
281+
{
282+
const string State = "State";
283+
var sut = CreateFakeNavigationManager();
284+
285+
sut.NavigateTo("/internal", new NavigationOptions { HistoryEntryState = State });
286+
sut.NavigateTo("/internal", new NavigationOptions { HistoryEntryState = State, ForceLoad = true});
287+
288+
sut.HistoryEntryState.ShouldBe(null);
289+
}
267290

268291
private sealed class InterceptNavigateToCounterComponent : ComponentBase
269292
{

0 commit comments

Comments
 (0)