Skip to content

Commit 97420c2

Browse files
committed
Draft of tests.
1 parent 8da794c commit 97420c2

File tree

5 files changed

+162
-5
lines changed

5 files changed

+162
-5
lines changed

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,91 @@ public void CanUpdateHrefOnLinkTagWithIntegrity()
662662
Browser.Equal("rgba(0, 0, 255, 1)", () => originalH1Elem.GetCssValue("color"));
663663
}
664664

665+
[Theory]
666+
//[InlineData(false, false)] // FAILS, do-navigation scroll is not at thr top
667+
[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)
671+
{
672+
// This test checks if the navigation to other path moves the scroll to the top of the page,
673+
// or to the beginning of a fragment, regardless of the previous scroll position,
674+
// checks if going backwards and forwards preserves the scroll position
675+
Navigate($"{ServerPathBase}/nav/testing-scroll/{enableStreaming}");
676+
EnhancedNavigationTestUtil.SuppressEnhancedNavigation(this, suppressEnhancedNavigation, skipNavigation: true);
677+
AssertWeAreOnScrollTestPage();
678+
AssertStreamingMode();
679+
680+
// assert enhanced navigation is enabled/disabled, as requested
681+
var elementForStalenessCheck = Browser.Exists(By.TagName("html"));
682+
683+
var jsExecutor = (IJavaScriptExecutor)Browser;
684+
var maxScrollPosition = (long)jsExecutor.ExecuteScript("return document.documentElement.scrollHeight - window.innerHeight;");
685+
686+
// scroll maximally down and go to another page - we should land at the top of that page
687+
Browser.SetScrollY(maxScrollPosition);
688+
Browser.Exists(By.Id("do-navigation")).Click();
689+
AssertEnhancedNavigation();
690+
AssertWeAreOnHashPage();
691+
AssertStreamingMode();
692+
Assert.Equal(0, Browser.GetScrollY());
693+
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?
695+
696+
// go back and check if the scroll position is preserved
697+
Browser.Navigate().Back();
698+
AssertEnhancedNavigation();
699+
AssertWeAreOnScrollTestPage();
700+
AssertStreamingMode();
701+
Assert.Equal(maxScrollPosition - 1, Browser.GetScrollY());
702+
703+
// navigate to a fragment on another page - we should land at the beginning of the fragment
704+
Browser.Exists(By.Id("do-navigation-with-fragment")).Click();
705+
AssertEnhancedNavigation();
706+
AssertWeAreOnHashPage();
707+
//AssertStreamingMode();
708+
Assert.Equal(fragmentScrollPosition, Browser.GetScrollY());
709+
710+
// go back to be able to go forward and check if the scroll position is preserved
711+
Browser.Navigate().Back();
712+
AssertEnhancedNavigation();
713+
AssertWeAreOnScrollTestPage();
714+
AssertStreamingMode();
715+
716+
Browser.Navigate().Forward();
717+
AssertEnhancedNavigation();
718+
AssertWeAreOnHashPage();
719+
AssertStreamingMode();
720+
Assert.Equal(fragmentScrollPosition, Browser.GetScrollY());
721+
722+
void AssertStreamingMode()
723+
{
724+
if (enableStreaming)
725+
{
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"));
731+
}
732+
}
733+
734+
void AssertEnhancedNavigation()
735+
{
736+
Assert.Equal(suppressEnhancedNavigation, IsElementStale(elementForStalenessCheck));
737+
}
738+
739+
void AssertWeAreOnScrollTestPage()
740+
{
741+
Browser.Equal("Go back to me", () => Browser.Exists(By.Id("test-info")).Text);
742+
}
743+
744+
void AssertWeAreOnHashPage()
745+
{
746+
Browser.Equal("Scroll to hash", () => Browser.Exists(By.Id("test-info")).Text);
747+
}
748+
}
749+
665750
private void AssertEnhancedUpdateCountEquals(long count)
666751
=> Browser.Equal(count, () => ((IJavaScriptExecutor)Browser).ExecuteScript("return window.enhancedPageUpdateCount;"));
667752

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,7 @@ public static void SuppressEnhancedNavigation<TServerFixture>(ServerTestBase<TSe
3232

3333
public static long GetScrollY(this IWebDriver browser)
3434
=> Convert.ToInt64(((IJavaScriptExecutor)browser).ExecuteScript("return window.scrollY"), CultureInfo.CurrentCulture);
35+
36+
public static long SetScrollY(this IWebDriver browser, long value)
37+
=> Convert.ToInt64(((IJavaScriptExecutor)browser).ExecuteScript($"window.scrollTo(0, {value})"), CultureInfo.CurrentCulture);
3538
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
@page "/nav/testing-scroll/{enableStreaming:bool}"
2+
@inject NavigationManager NavigationManager
3+
4+
@if (EnableStreaming)
5+
{
6+
@attribute [StreamRendering]
7+
}
8+
9+
<PageTitle>Page for testing scroll position</PageTitle>
10+
11+
<h1 id="test-info">Go back to me</h1>
12+
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+
}
18+
19+
<div style="height: 2000px; border: 2px dashed red;">spacer top</div>
20+
21+
@if (showContent)
22+
{
23+
<NavLink id="do-navigation-with-fragment" href="@($"nav/scroll-to-hash#some-content/{EnableStreaming}")">
24+
Navigation to another page with fragment (scroll-to-hash#some-content)
25+
</NavLink>
26+
}
27+
28+
<div style="height: 2000px; border: 2px dashed red;">spacer bottom</div>
29+
30+
@if (showContent)
31+
{
32+
<h2 id="some-content">Some content</h2>
33+
<p>This is the content.</p>
34+
35+
<NavLink id="do-navigation" href="@($"nav/scroll-to-hash/{EnableStreaming}")">
36+
Navigation to another page (scroll-to-hash)
37+
</NavLink>
38+
}
39+
40+
@code {
41+
[Parameter]
42+
public bool EnableStreaming { get; set; }
43+
44+
bool showContent;
45+
string uriOnPageLoad;
46+
47+
protected override async Task OnInitializedAsync()
48+
{
49+
uriOnPageLoad = NavigationManager.Uri;
50+
await Task.Delay(1000);
51+
showContent = true;
52+
}
53+
}

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
1-
@page "/nav/scroll-to-hash"
2-
@attribute [StreamRendering]
1+
@page "/nav/scroll-to-hash/{enableStreaming:bool=true}"
2+
@page "/nav/scroll-to-hash"
33
@inject NavigationManager NavigationManager
44

5+
@if (EnableStreaming)
6+
{
7+
@attribute [StreamRendering]
8+
}
9+
510
<PageTitle>Page for scrolling to hash</PageTitle>
611

7-
<h1>Scroll to hash</h1>
12+
<h1 id="test-info">Scroll to hash</h1>
813

9-
<p>If you scroll down a long way, you'll find more content. We add it asynchronously via streaming rendering.</p>
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+
}
1019

1120
<p>
1221
<a id="scroll-anchor" href="nav/scroll-to-hash#some-content">Scroll via anchor</a>
1322
<div id="uri-on-page-load" style="display: none" data-value="@uriOnPageLoad"></div>
1423
</p>
1524

16-
<div style="height: 2000px; border: 2px dashed red;">spacer</div>
25+
<div style="height: 2000px; border: 2px dashed red;">top spacer</div>
1726

1827
@if (showContent)
1928
{
@@ -22,7 +31,12 @@
2231
<p>This is the content.</p>
2332
}
2433

34+
<div style="height: 2000px; border: 2px dashed red;">bottom spacer</div>
35+
2536
@code {
37+
[Parameter]
38+
public bool EnableStreaming { get; set; }
39+
2640
bool showContent;
2741
string uriOnPageLoad;
2842

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
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> |
1214
<NavLink href="nav/throw-while-streaming">Error while streaming</NavLink> |
1315
<NavLink href="nav/interactive-component-navigation/server">Interactive component navigation (server)</NavLink> |
1416
<NavLink href="nav/interactive-component-navigation/webassembly">Interactive component navigation (webassembly)</NavLink> |

0 commit comments

Comments
 (0)