11using System . Text . Json ;
22using Microsoft . AspNetCore . Components . WebAssembly . Authentication ;
3+ using Shouldly . ShouldlyExtensionMethods ;
34
45namespace 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