Skip to content

Commit b2c2681

Browse files
committed
Add e2e test for the new reconnect UI
1 parent dd0143f commit b2c2681

File tree

5 files changed

+269
-0
lines changed

5 files changed

+269
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using BasicTestApp.Reconnection;
5+
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
6+
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
7+
using Microsoft.AspNetCore.Components.E2ETest.ServerExecutionTests;
8+
using Microsoft.AspNetCore.E2ETesting;
9+
using OpenQA.Selenium;
10+
using TestServer;
11+
using Xunit.Abstractions;
12+
13+
namespace Microsoft.AspNetCore.Components.E2ETest.ServerExecutionTests;
14+
15+
public class ServerReconnectionCustomUITest : ServerReconnectionTest
16+
{
17+
public ServerReconnectionCustomUITest(
18+
BrowserFixture browserFixture,
19+
BasicTestAppServerSiteFixture<ServerStartup> serverFixture,
20+
ITestOutputHelper output)
21+
: base(browserFixture, serverFixture, output)
22+
{
23+
}
24+
25+
protected override void InitializeAsyncCore()
26+
{
27+
Navigate($"{ServerPathBase}?useCustomReconnectModal=true");
28+
Browser.MountTestComponent<ReconnectionComponent>();
29+
Browser.Exists(By.Id("count"));
30+
}
31+
32+
[Fact]
33+
public void CustomReconnectUI()
34+
{
35+
Browser.Exists(By.Id("increment")).Click();
36+
37+
Browser.Equal("dialog", () => Browser.Exists(By.Id("components-reconnect-modal")).TagName);
38+
39+
var javascript = (IJavaScriptExecutor)Browser;
40+
javascript.ExecuteScript("Blazor._internal.forceCloseConnection()");
41+
42+
// We should see the 'reconnecting' UI appear
43+
Browser.NotEqual(null, () => Browser.Exists(By.Id("components-reconnect-modal")).GetAttribute("open"));
44+
45+
// Then it should disappear
46+
Browser.Equal(null, () => Browser.Exists(By.Id("components-reconnect-modal")).GetAttribute("open"));
47+
48+
Browser.Exists(By.Id("increment")).Click();
49+
50+
// Can dispatch events after reconnect
51+
Browser.Equal("2", () => Browser.Exists(By.Id("count")).Text);
52+
}
53+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script type="module" src="@Assets["Shared/ReconnectModal.razor.js"]"></script>
2+
3+
<dialog id="components-reconnect-modal">
4+
<div class="components-reconnect-container">
5+
<div class="components-rejoining-animation">
6+
<div></div>
7+
<div></div>
8+
</div>
9+
<p class="components-reconnect-first-attempt-visible">
10+
Rejoining the server...
11+
</p>
12+
<p class="components-reconnect-repeated-attempt-visible">
13+
Rejoin failed... trying again in <span id="components-seconds-to-next-attempt"></span> seconds.
14+
</p>
15+
<p class="components-reconnect-failed-visible">
16+
Failed to rejoin.<br />Please retry or reload the page.
17+
</p>
18+
<button id="components-reconnect-button" class="components-reconnect-failed-visible">
19+
Retry
20+
</button>
21+
</div>
22+
</dialog>
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
.components-reconnect-first-attempt-visible,
2+
.components-reconnect-repeated-attempt-visible,
3+
.components-reconnect-failed-visible,
4+
.components-rejoining-animation {
5+
display: none;
6+
}
7+
8+
#components-reconnect-modal.components-reconnect-show .components-reconnect-first-attempt-visible,
9+
#components-reconnect-modal.components-reconnect-show .components-rejoining-animation,
10+
#components-reconnect-modal.components-reconnect-retrying,
11+
#components-reconnect-modal.components-reconnect-retrying .components-reconnect-repeated-attempt-visible,
12+
#components-reconnect-modal.components-reconnect-retrying .components-rejoining-animation,
13+
#components-reconnect-modal.components-reconnect-failed,
14+
#components-reconnect-modal.components-reconnect-failed .components-reconnect-failed-visible {
15+
display: block;
16+
}
17+
18+
19+
#components-reconnect-modal {
20+
background-color: white;
21+
width: 20rem;
22+
margin: 20vh auto;
23+
padding: 2rem;
24+
border: 0;
25+
border-radius: 0.5rem;
26+
box-shadow: 0 3px 6px 2px rgba(0, 0, 0, 0.3);
27+
opacity: 0;
28+
transition: display 0.5s allow-discrete, overlay 0.5s allow-discrete;
29+
animation: components-reconnect-modal-fadeOutOpacity 0.5s both;
30+
&[open] {
31+
animation: components-reconnect-modal-slideUp 1.5s cubic-bezier(.05, .89, .25, 1.02) 0.3s, components-reconnect-modal-fadeInOpacity 0.5s ease-in-out 0.3s;
32+
animation-fill-mode: both;
33+
}
34+
}
35+
36+
#components-reconnect-modal::backdrop {
37+
background-color: rgba(0, 0, 0, 0.4);
38+
animation: components-reconnect-modal-fadeInOpacity 0.5s ease-in-out;
39+
opacity: 1;
40+
}
41+
42+
@keyframes components-reconnect-modal-slideUp {
43+
0% {
44+
transform: translateY(30px) scale(0.95);
45+
}
46+
47+
100% {
48+
transform: translateY(0);
49+
}
50+
}
51+
52+
@keyframes components-reconnect-modal-fadeInOpacity {
53+
0% {
54+
opacity: 0;
55+
}
56+
57+
100% {
58+
opacity: 1;
59+
}
60+
}
61+
62+
@keyframes components-reconnect-modal-fadeOutOpacity {
63+
0% {
64+
opacity: 1;
65+
}
66+
67+
100% {
68+
opacity: 0;
69+
}
70+
}
71+
72+
.components-reconnect-container {
73+
display: flex;
74+
flex-direction: column;
75+
align-items: center;
76+
gap: 1rem;
77+
}
78+
79+
#components-reconnect-modal p {
80+
margin: 0;
81+
text-align: center;
82+
}
83+
84+
#components-reconnect-modal button {
85+
border: 0;
86+
background-color: #6b9ed2;
87+
color: white;
88+
padding: 4px 24px;
89+
border-radius: 4px;
90+
}
91+
92+
#components-reconnect-modal button:hover {
93+
background-color: #3b6ea2;
94+
}
95+
96+
#components-reconnect-modal button:active {
97+
background-color: #6b9ed2;
98+
}
99+
100+
.components-rejoining-animation {
101+
display: block;
102+
position: relative;
103+
width: 80px;
104+
height: 80px;
105+
}
106+
107+
.components-rejoining-animation div {
108+
position: absolute;
109+
border: 3px solid #0087ff;
110+
opacity: 1;
111+
border-radius: 50%;
112+
animation: components-rejoining-animation 1.5s cubic-bezier(0, 0.2, 0.8, 1) infinite;
113+
}
114+
115+
.components-rejoining-animation div:nth-child(2) {
116+
animation-delay: -0.5s;
117+
}
118+
119+
@keyframes components-rejoining-animation {
120+
0% {
121+
top: 40px;
122+
left: 40px;
123+
width: 0;
124+
height: 0;
125+
opacity: 0;
126+
}
127+
128+
4.9% {
129+
top: 40px;
130+
left: 40px;
131+
width: 0;
132+
height: 0;
133+
opacity: 0;
134+
}
135+
136+
5% {
137+
top: 40px;
138+
left: 40px;
139+
width: 0;
140+
height: 0;
141+
opacity: 1;
142+
}
143+
144+
100% {
145+
top: 0px;
146+
left: 0px;
147+
width: 80px;
148+
height: 80px;
149+
opacity: 0;
150+
}
151+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Set up event handlers
2+
const retryButton = document.getElementById("components-reconnect-button");
3+
4+
retryButton.addEventListener('click', retry);
5+
6+
async function retry() {
7+
document.removeEventListener('visibilitychange', retryWhenDocumentBecomesVisible);
8+
9+
try {
10+
// Reconnect will asynchronously return:
11+
// - true to mean success
12+
// - false to mean we reached the server, but it rejected the connection (e.g., unknown circuit ID)
13+
// - exception to mean we didn't reach the server (this can be sync or async)
14+
const successful = await Blazor.reconnect();
15+
if (!successful) {
16+
// We have been able to reach the server, but the circuit is no longer available.
17+
// We'll reload the page so the user can continue using the app as quickly as possible.
18+
location.reload();
19+
}
20+
} catch (err) {
21+
// We got an exception, server is currently unavailable
22+
document.addEventListener('visibilitychange', retryWhenDocumentBecomesVisible);
23+
}
24+
}
25+
26+
async function retryWhenDocumentBecomesVisible() {
27+
if (document.visibilityState === 'visible') {
28+
await retry();
29+
}
30+
}

src/Components/test/testassets/BasicTestApp/Reconnection/ReconnectionComponent.razor

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
@using System.Timers
2+
@using System.Web
3+
@inject NavigationManager Navigation
24
@implements IDisposable
35
<h3>Reconnection Component</h3>
46

@@ -20,16 +22,27 @@
2022

2123
<button id="cause-error" @onclick="CauseError">Cause error</button>
2224
</div>
25+
26+
@if (useCustomReconnectModal)
27+
{
28+
<ReconnectModal />
29+
}
30+
2331
@code {
2432
int count;
2533
void Increment() => count++;
2634
int tickCount = 0;
2735
Timer timer;
2836
bool causeError = false;
37+
bool useCustomReconnectModal = false;
2938

3039
protected override void OnInitialized()
3140
{
3241
timer = StartTimer();
42+
43+
var uri = Navigation.ToAbsoluteUri(Navigation.Uri);
44+
var query = HttpUtility.ParseQueryString(uri.Query);
45+
useCustomReconnectModal = query["useCustomReconnectModal"] == "true";
3346
}
3447

3548
private Timer StartTimer()

0 commit comments

Comments
 (0)