@@ -438,6 +438,74 @@ public void CanNavigateProgrammaticallyWithForceLoad()
438
438
} ) ;
439
439
}
440
440
441
+ [ Fact ]
442
+ public void CanNavigateProgrammaticallyWithStateValidateNoReplaceHistoryEntry ( )
443
+ {
444
+ // This test checks if default navigation does not replace Browser history entries
445
+ SetUrlViaPushState ( "/" ) ;
446
+
447
+ var app = Browser . MountTestComponent < TestRouter > ( ) ;
448
+ var testSelector = Browser . WaitUntilTestSelectorReady ( ) ;
449
+
450
+ app . FindElement ( By . LinkText ( "Programmatic navigation cases" ) ) . Click ( ) ;
451
+ Browser . True ( ( ) => Browser . Url . EndsWith ( "/ProgrammaticNavigationCases" , StringComparison . Ordinal ) ) ;
452
+ Browser . Contains ( "programmatic navigation" , ( ) => app . FindElement ( By . Id ( "test-info" ) ) . Text ) ;
453
+
454
+ // We navigate to the /Other page
455
+ app . FindElement ( By . Id ( "do-other-navigation-state" ) ) . Click ( ) ;
456
+ Browser . True ( ( ) => Browser . Url . EndsWith ( "/Other" , StringComparison . Ordinal ) ) ;
457
+ Browser . Contains ( "state" , ( ) => app . FindElement ( By . Id ( "test-state" ) ) . Text ) ;
458
+ AssertHighlightedLinks ( "Other" , "Other with base-relative URL (matches all)" ) ;
459
+
460
+ // After we press back, we should end up at the "/ProgrammaticNavigationCases" page so we know browser history has not been replaced
461
+ // If history had been replaced we would have ended up at the "/" page
462
+ Browser . Navigate ( ) . Back ( ) ;
463
+ Browser . True ( ( ) => Browser . Url . EndsWith ( "/ProgrammaticNavigationCases" , StringComparison . Ordinal ) ) ;
464
+ AssertHighlightedLinks ( "Programmatic navigation cases" ) ;
465
+
466
+ // When the navigation is forced, the state is ignored (we could choose to throw here).
467
+ app . FindElement ( By . Id ( "do-other-navigation-forced-state" ) ) . Click ( ) ;
468
+ Browser . True ( ( ) => Browser . Url . EndsWith ( "/Other" , StringComparison . Ordinal ) ) ;
469
+ Browser . DoesNotExist ( By . Id ( "test-state" ) ) ;
470
+
471
+ // We check if we had a force load
472
+ Assert . Throws < StaleElementReferenceException > ( ( ) =>
473
+ testSelector . SelectedOption . GetAttribute ( "value" ) ) ;
474
+
475
+ // But still we should be able to navigate back, and end up at the "/ProgrammaticNavigationCases" page
476
+ Browser . Navigate ( ) . Back ( ) ;
477
+ Browser . True ( ( ) => Browser . Url . EndsWith ( "/ProgrammaticNavigationCases" , StringComparison . Ordinal ) ) ;
478
+ Browser . WaitUntilTestSelectorReady ( ) ;
479
+ }
480
+
481
+ [ Fact ]
482
+ public void CanNavigateProgrammaticallyWithStateReplaceHistoryEntry ( )
483
+ {
484
+ SetUrlViaPushState ( "/" ) ;
485
+
486
+ var app = Browser . MountTestComponent < TestRouter > ( ) ;
487
+ var testSelector = Browser . WaitUntilTestSelectorReady ( ) ;
488
+
489
+ app . FindElement ( By . LinkText ( "Programmatic navigation cases" ) ) . Click ( ) ;
490
+ Browser . True ( ( ) => Browser . Url . EndsWith ( "/ProgrammaticNavigationCases" , StringComparison . Ordinal ) ) ;
491
+ Browser . Contains ( "programmatic navigation" , ( ) => app . FindElement ( By . Id ( "test-info" ) ) . Text ) ;
492
+
493
+ // We navigate to the /Other page, with "replace" enabled
494
+ app . FindElement ( By . Id ( "do-other-navigation-state-replacehistoryentry" ) ) . Click ( ) ;
495
+ Browser . True ( ( ) => Browser . Url . EndsWith ( "/Other" , StringComparison . Ordinal ) ) ;
496
+ Browser . Contains ( "state" , ( ) => app . FindElement ( By . Id ( "test-state" ) ) . Text ) ;
497
+ AssertHighlightedLinks ( "Other" , "Other with base-relative URL (matches all)" ) ;
498
+
499
+ // After we press back, we should end up at the "/" page so we know browser history has been replaced
500
+ // If history would not have been replaced we would have ended up at the "/ProgrammaticNavigationCases" page
501
+ Browser . Navigate ( ) . Back ( ) ;
502
+ Browser . True ( ( ) => Browser . Url . EndsWith ( "/" , StringComparison . Ordinal ) ) ;
503
+ AssertHighlightedLinks ( "Default (matches all)" , "Default with base-relative URL (matches all)" ) ;
504
+
505
+ // Because this was all with client-side navigation, we didn't lose the state in the test selector
506
+ Assert . Equal ( typeof ( TestRouter ) . FullName , testSelector . SelectedOption . GetAttribute ( "value" ) ) ;
507
+ }
508
+
441
509
[ Fact ]
442
510
public void CanNavigateProgrammaticallyValidateNoReplaceHistoryEntry ( )
443
511
{
@@ -452,7 +520,7 @@ public void CanNavigateProgrammaticallyValidateNoReplaceHistoryEntry()
452
520
Browser . Contains ( "programmatic navigation" , ( ) => app . FindElement ( By . Id ( "test-info" ) ) . Text ) ;
453
521
454
522
// We navigate to the /Other page
455
- // This will also test our new NavigatTo (string uri) overload (it should not replace the browser history)
523
+ // This will also test our new NavigateTo (string uri) overload (it should not replace the browser history)
456
524
app . FindElement ( By . Id ( "do-other-navigation" ) ) . Click ( ) ;
457
525
Browser . True ( ( ) => Browser . Url . EndsWith ( "/Other" , StringComparison . Ordinal ) ) ;
458
526
AssertHighlightedLinks ( "Other" , "Other with base-relative URL (matches all)" ) ;
0 commit comments