Skip to content

Commit d56bc30

Browse files
committed
Tests for the fix.
1 parent cfe8046 commit d56bc30

File tree

6 files changed

+135
-66
lines changed

6 files changed

+135
-66
lines changed

src/Components/test/E2ETest/ServerRenderingTests/EnhancedNavigationTest.cs

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Components.TestServer.RazorComponents;
1010
using OpenQA.Selenium;
1111
using OpenQA.Selenium.Support.Extensions;
12+
using System.Threading.Tasks;
1213

1314
namespace Microsoft.AspNetCore.Components.E2ETests.ServerRenderingTests;
1415

@@ -663,77 +664,84 @@ public void CanUpdateHrefOnLinkTagWithIntegrity()
663664
}
664665

665666
[Theory]
666-
//[InlineData(false, false)] // FAILS, do-navigation scroll is not at thr top
667+
[InlineData(false, false)] // PASSES
667668
[InlineData(false, true)] // PASSES
668-
//[InlineData(true, true)] // FAILS: go back: Y=0, not 3211, streaming mode is lost on going back and many more
669-
//[InlineData(true, false)] // FAILS, do-navigation scroll is not at the top
670-
public void EnhancedNavigationScrollBehavesSameAsFullNavigation(bool enableStreaming, bool suppressEnhancedNavigation)
669+
[InlineData(true, true)] // line 709 Expected: 1732 Actual: 0
670+
[InlineData(true, false)] // line 709 Expected: 1732 Actual: 0
671+
public async Task EnhancedNavigationScrollBehavesSameAsFullNavigation(bool enableStreaming, bool useEnhancedNavigation)
671672
{
672673
// This test checks if the navigation to other path moves the scroll to the top of the page,
673674
// or to the beginning of a fragment, regardless of the previous scroll position,
674675
// checks if going backwards and forwards preserves the scroll position
675-
Navigate($"{ServerPathBase}/nav/testing-scroll/{enableStreaming}");
676-
EnhancedNavigationTestUtil.SuppressEnhancedNavigation(this, suppressEnhancedNavigation, skipNavigation: true);
676+
string landingPageSuffix = enableStreaming ? "" : "-no-streaming";
677+
Navigate($"{ServerPathBase}/nav/testing-scroll{landingPageSuffix}");
678+
EnhancedNavigationTestUtil.SuppressEnhancedNavigation(this, shouldSuppress: !useEnhancedNavigation, skipNavigation: true);
679+
677680
AssertWeAreOnScrollTestPage();
678-
AssertStreamingMode();
679681

680682
// assert enhanced navigation is enabled/disabled, as requested
681-
var elementForStalenessCheck = Browser.Exists(By.TagName("html"));
683+
var elementForStalenessCheckOnScrollPage = Browser.Exists(By.TagName("html"));
682684

683685
var jsExecutor = (IJavaScriptExecutor)Browser;
684686
var maxScrollPosition = (long)jsExecutor.ExecuteScript("return document.documentElement.scrollHeight - window.innerHeight;");
685687

686688
// scroll maximally down and go to another page - we should land at the top of that page
687689
Browser.SetScrollY(maxScrollPosition);
688690
Browser.Exists(By.Id("do-navigation")).Click();
689-
AssertEnhancedNavigation();
691+
AssertEnhancedNavigationOnHashPage();
690692
AssertWeAreOnHashPage();
691-
AssertStreamingMode();
692693
Assert.Equal(0, Browser.GetScrollY());
694+
var elementForStalenessCheckOnHashPage = Browser.Exists(By.TagName("html"));
695+
696+
if (enableStreaming)
697+
{
698+
// wait for the fragment to be visible
699+
await Task.Delay(TimeSpan.FromSeconds(1));
700+
}
693701
var fragmentScrollPosition = (long)jsExecutor.ExecuteScript("return Math.round(document.getElementById('some-content').getBoundingClientRect().top + window.scrollY);");
694-
fragmentScrollPosition = 357; // why 357, can we have non-static value?
695702

696703
// go back and check if the scroll position is preserved
697704
Browser.Navigate().Back();
698-
AssertEnhancedNavigation();
699705
AssertWeAreOnScrollTestPage();
700-
AssertStreamingMode();
701-
Assert.Equal(maxScrollPosition - 1, Browser.GetScrollY());
706+
AssertEnhancedNavigationOnScrollPage();
707+
708+
// parts of page conditioned with showContent are showing with a delay - it affect the scroll position
709+
// from some reason, scroll position differs by 1 pixel between enhanced and browser's navigation
710+
var expectedMaxScrollPositionAfterBackwardsAction = useEnhancedNavigation ? maxScrollPosition: maxScrollPosition - 1;
711+
Assert.Equal(expectedMaxScrollPositionAfterBackwardsAction, Browser.GetScrollY());
702712

703713
// navigate to a fragment on another page - we should land at the beginning of the fragment
704714
Browser.Exists(By.Id("do-navigation-with-fragment")).Click();
705-
AssertEnhancedNavigation();
706715
AssertWeAreOnHashPage();
707-
//AssertStreamingMode();
708-
Assert.Equal(fragmentScrollPosition, Browser.GetScrollY());
716+
AssertEnhancedNavigationOnHashPage();
717+
var expectedFragmentScrollPosition = fragmentScrollPosition - 1;
718+
Assert.Equal(expectedFragmentScrollPosition, Browser.GetScrollY());
709719

710720
// go back to be able to go forward and check if the scroll position is preserved
711721
Browser.Navigate().Back();
712-
AssertEnhancedNavigation();
713722
AssertWeAreOnScrollTestPage();
714-
AssertStreamingMode();
723+
AssertEnhancedNavigationOnScrollPage();
715724

716725
Browser.Navigate().Forward();
717-
AssertEnhancedNavigation();
718726
AssertWeAreOnHashPage();
719-
AssertStreamingMode();
720-
Assert.Equal(fragmentScrollPosition, Browser.GetScrollY());
727+
AssertEnhancedNavigationOnHashPage();
728+
Assert.Equal(expectedFragmentScrollPosition, Browser.GetScrollY());
729+
730+
void AssertEnhancedNavigationOnHashPage() =>
731+
AssertEnhancedNavigation(elementForStalenessCheckOnScrollPage);
721732

722-
void AssertStreamingMode()
733+
void AssertEnhancedNavigationOnScrollPage()
734+
=> AssertEnhancedNavigation(elementForStalenessCheckOnHashPage);
735+
736+
void AssertEnhancedNavigation(IWebElement elementForStalenessCheck)
723737
{
738+
// enhanced navigation asserts are not deterministic with streaming
724739
if (enableStreaming)
725740
{
726-
Browser.Contains("We add it asynchronously via streaming rendering.", () => Browser.Exists(By.Id("streaming-info")).Text);
727-
}
728-
else
729-
{
730-
Browser.DoesNotExist(By.Id("streaming-info"));
741+
return;
731742
}
732-
}
733-
734-
void AssertEnhancedNavigation()
735-
{
736-
Assert.Equal(suppressEnhancedNavigation, IsElementStale(elementForStalenessCheck));
743+
bool enhancedNavigationDetected = !IsElementStale(elementForStalenessCheck);
744+
Assert.Equal(useEnhancedNavigation, enhancedNavigationDetected);
737745
}
738746

739747
void AssertWeAreOnScrollTestPage()

src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/EnhancedNav/PageForScrollPositionTests.razor

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
1-
@page "/nav/testing-scroll/{enableStreaming:bool}"
1+
@page "/nav/testing-scroll"
22
@inject NavigationManager NavigationManager
3-
4-
@if (EnableStreaming)
5-
{
6-
@attribute [StreamRendering]
7-
}
3+
@attribute [StreamRendering]
84

95
<PageTitle>Page for testing scroll position</PageTitle>
106

117
<h1 id="test-info">Go back to me</h1>
128

13-
<p>If you scroll down a long way, you'll find more content.</p>
14-
@if (EnableStreaming)
15-
{
16-
<p id="streaming-info">We add it asynchronously via streaming rendering.</p>
17-
}
9+
<p>If you scroll down a long way, you'll find more content. We add it asynchronously via streaming rendering.</p>
1810

1911
<div style="height: 2000px; border: 2px dashed red;">spacer top</div>
2012

2113
@if (showContent)
2214
{
23-
<NavLink id="do-navigation-with-fragment" href="@($"nav/scroll-to-hash#some-content/{EnableStreaming}")">
15+
<NavLink id="do-navigation-with-fragment" href="@($"nav/scroll-to-hash#some-content")">
2416
Navigation to another page with fragment (scroll-to-hash#some-content)
2517
</NavLink>
2618
}
@@ -32,15 +24,12 @@
3224
<h2 id="some-content">Some content</h2>
3325
<p>This is the content.</p>
3426

35-
<NavLink id="do-navigation" href="@($"nav/scroll-to-hash/{EnableStreaming}")">
27+
<NavLink id="do-navigation" href="@($"nav/scroll-to-hash")">
3628
Navigation to another page (scroll-to-hash)
3729
</NavLink>
3830
}
3931

4032
@code {
41-
[Parameter]
42-
public bool EnableStreaming { get; set; }
43-
4433
bool showContent;
4534
string uriOnPageLoad;
4635

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
@page "/nav/testing-scroll-no-streaming"
2+
@inject NavigationManager NavigationManager
3+
@* This is a copy of PageForScrollPositionTests, without StreamRendering *@
4+
5+
<PageTitle>Page for testing scroll position</PageTitle>
6+
7+
<h1 id="test-info">Go back to me</h1>
8+
9+
<p>If you scroll down a long way, you'll find more content.</p>
10+
11+
<div style="height: 2000px; border: 2px dashed red;">spacer top</div>
12+
13+
@if (showContent)
14+
{
15+
<NavLink id="do-navigation-with-fragment" href="@($"nav/scroll-to-hash-no-streaming#some-content")">
16+
Navigation to another page with fragment (scroll-to-hash#some-content)
17+
</NavLink>
18+
}
19+
20+
<div style="height: 2000px; border: 2px dashed red;">spacer bottom</div>
21+
22+
@if (showContent)
23+
{
24+
<h2 id="some-content">Some content</h2>
25+
<p>This is the content.</p>
26+
27+
<NavLink id="do-navigation" href="@($"nav/scroll-to-hash-no-streaming")">
28+
Navigation to another page (scroll-to-hash)
29+
</NavLink>
30+
}
31+
32+
@code {
33+
bool showContent;
34+
string uriOnPageLoad;
35+
36+
protected override async Task OnInitializedAsync()
37+
{
38+
uriOnPageLoad = NavigationManager.Uri;
39+
await Task.Delay(1000);
40+
showContent = true;
41+
}
42+
}

src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/EnhancedNav/PageForScrollingToHash.razor

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
1-
@page "/nav/scroll-to-hash/{enableStreaming:bool=true}"
2-
@page "/nav/scroll-to-hash"
1+
@page "/nav/scroll-to-hash"
32
@inject NavigationManager NavigationManager
4-
5-
@if (EnableStreaming)
6-
{
7-
@attribute [StreamRendering]
8-
}
3+
@attribute [StreamRendering]
94

105
<PageTitle>Page for scrolling to hash</PageTitle>
116

127
<h1 id="test-info">Scroll to hash</h1>
138

14-
<p>If you scroll down a long way, you'll find more content.</p>
15-
@if (EnableStreaming)
16-
{
17-
<p id="streaming-info">We add it asynchronously via streaming rendering.</p>
18-
}
9+
<p>If you scroll down a long way, you'll find more content. We add it asynchronously via streaming rendering.</p>
1910

2011
<p>
2112
<a id="scroll-anchor" href="nav/scroll-to-hash#some-content">Scroll via anchor</a>
@@ -27,16 +18,16 @@
2718
@if (showContent)
2819
{
2920
<h2 id="some-content">Some content</h2>
21+
<NavLink id="do-navigation" href="@($"nav/testing-scroll")">
22+
Navigation to another page (testing-scroll)
23+
</NavLink>
3024

3125
<p>This is the content.</p>
3226
}
3327

3428
<div style="height: 2000px; border: 2px dashed red;">bottom spacer</div>
3529

3630
@code {
37-
[Parameter]
38-
public bool EnableStreaming { get; set; }
39-
4031
bool showContent;
4132
string uriOnPageLoad;
4233

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
@page "/nav/scroll-to-hash-no-streaming"
2+
@inject NavigationManager NavigationManager
3+
@* This is a copy of PageForScrollingToHash, without StreamRendering *@
4+
5+
<PageTitle>Page for scrolling to hash</PageTitle>
6+
7+
<h1 id="test-info">Scroll to hash</h1>
8+
9+
<p>If you scroll down a long way, you'll find more content.</p>
10+
11+
<p>
12+
<a id="scroll-anchor" href="nav/scroll-to-hash-no-streaming#some-content">Scroll via anchor</a>
13+
<div id="uri-on-page-load" style="display: none" data-value="@uriOnPageLoad"></div>
14+
</p>
15+
16+
<div style="height: 2000px; border: 2px dashed red;">top spacer</div>
17+
18+
@if (showContent)
19+
{
20+
<h2 id="some-content">Some content</h2>
21+
<NavLink id="do-navigation" href="@($"nav/testing-scroll-no-streaming")">
22+
Navigation to another page (testing-scroll)
23+
</NavLink>
24+
25+
<p>This is the content.</p>
26+
}
27+
28+
<div style="height: 2000px; border: 2px dashed red;">bottom spacer</div>
29+
30+
@code {
31+
32+
bool showContent;
33+
string uriOnPageLoad;
34+
35+
protected override async Task OnInitializedAsync()
36+
{
37+
uriOnPageLoad = NavigationManager.Uri;
38+
await Task.Delay(1000);
39+
showContent = true;
40+
}
41+
}

src/Components/test/testassets/Components.TestServer/RazorComponents/Shared/EnhancedNavLayout.razor

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
<NavLink href="nav/non-html-response">Non-HTML page</NavLink> |
1010
<NavLink href="nav/non-blazor-html-response">Non-Blazor HTML page</NavLink> |
1111
<NavLink href="nav/scroll-to-hash#some-content">Scroll to hash</NavLink> |
12-
<NavLink href="nav/testing-scroll/false">Scroll testing-scroll without streaming</NavLink> |
13-
<NavLink href="nav/testing-scroll/true">Scroll testing-scroll with streaming</NavLink> |
1412
<NavLink href="nav/throw-while-streaming">Error while streaming</NavLink> |
1513
<NavLink href="nav/interactive-component-navigation/server">Interactive component navigation (server)</NavLink> |
1614
<NavLink href="nav/interactive-component-navigation/webassembly">Interactive component navigation (webassembly)</NavLink> |

0 commit comments

Comments
 (0)