Skip to content

Commit e148b13

Browse files
committed
Persitent services E2E tests
1 parent cf8b9e9 commit e148b13

File tree

8 files changed

+156
-0
lines changed

8 files changed

+156
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,41 @@ public void CanPersistPrerenderedState_Auto_PersistsOnServer()
11111111
Browser.Equal("Server", () => Browser.FindElement(By.Id("render-mode-auto")).Text);
11121112
}
11131113

1114+
[Theory]
1115+
[InlineData("server", "Server state", "Auto state", "not restored")]
1116+
[InlineData("auto", "Server state", "Auto state", "not restored")]
1117+
public void CanPersistPrerenderedState_ServicesState_PersistsOnServer(string mode, string expectedServerState, string expectedAutoState, string expectedWebAssemblyState)
1118+
{
1119+
Navigate(ServerPathBase);
1120+
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
1121+
if (mode == "auto")
1122+
{
1123+
BlockWebAssemblyResourceLoad();
1124+
}
1125+
1126+
Navigate($"{ServerPathBase}/persist-services-state?mode={mode}");
1127+
Browser.Equal("Server", () => Browser.FindElement(By.Id("render-mode")).Text);
1128+
Browser.Equal(expectedServerState, () => Browser.FindElement(By.Id("server-state")).Text);
1129+
Browser.Equal(expectedAutoState, () => Browser.FindElement(By.Id("auto-state")).Text);
1130+
Browser.Equal(expectedWebAssemblyState, () => Browser.FindElement(By.Id("wasm-state")).Text);
1131+
}
1132+
1133+
[Theory]
1134+
[InlineData("auto", "not restored", "Auto state", "WebAssembly state")]
1135+
[InlineData("wasm", "not restored", "Auto state", "WebAssembly state")]
1136+
public void CanPersistPrerenderedState_ServicesState_PersistsOnWasm(string mode, string expectedServerState, string expectedAutoState, string expectedWebAssemblyState)
1137+
{
1138+
Navigate(ServerPathBase);
1139+
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
1140+
1141+
Navigate($"{ServerPathBase}/persist-services-state?mode={mode}");
1142+
1143+
Browser.Equal("WebAssembly", () => Browser.FindElement(By.Id("render-mode")).Text);
1144+
Browser.Equal(expectedServerState, () => Browser.FindElement(By.Id("server-state")).Text);
1145+
Browser.Equal(expectedAutoState, () => Browser.FindElement(By.Id("auto-state")).Text);
1146+
Browser.Equal(expectedWebAssemblyState, () => Browser.FindElement(By.Id("wasm-state")).Text);
1147+
}
1148+
11141149
[Fact]
11151150
public void CanPersistPrerenderedStateDeclaratively_Auto_PersistsOnServer()
11161151
{

src/Components/test/testassets/Components.TestServer/RazorComponentEndpointsStartup.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
using Components.TestServer.RazorComponents;
99
using Components.TestServer.RazorComponents.Pages.Forms;
1010
using Components.TestServer.Services;
11+
using Microsoft.AspNetCore.Components;
1112
using Microsoft.AspNetCore.Components.Server.Circuits;
13+
using Microsoft.AspNetCore.Components.Web;
1214
using Microsoft.AspNetCore.Components.WebAssembly.Server;
1315
using Microsoft.AspNetCore.Mvc;
16+
using TestContentPackage.Services;
1417

1518
namespace TestServer;
1619

@@ -40,6 +43,14 @@ public void ConfigureServices(IServiceCollection services)
4043
options.SerializeAllClaims = serializeAllClaims;
4144
});
4245

46+
services.AddScoped<InteractiveWebAssemblyService>();
47+
services.AddScoped<InteractiveServerService>();
48+
services.AddScoped<InteractiveAutoService>();
49+
50+
services.AddPersistentService<InteractiveServerService>(RenderMode.InteractiveServer);
51+
services.AddPersistentService<InteractiveAutoService>(RenderMode.InteractiveAuto);
52+
services.AddPersistentService<InteractiveWebAssemblyService>(RenderMode.InteractiveWebAssembly);
53+
4354
services.AddHttpContextAccessor();
4455
services.AddSingleton<AsyncOperationService>();
4556
services.AddCascadingAuthenticationState();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@page "/persist-services-state"
2+
@using Microsoft.AspNetCore.Components.Web
3+
4+
<h1>Persist Services State Components</h1>
5+
6+
<TestContentPackage.PersistServicesState @rendermode="@_renderMode" />
7+
8+
@code {
9+
private IComponentRenderMode _renderMode;
10+
11+
[Parameter, SupplyParameterFromQuery(Name = "mode")]
12+
public string Mode { get; set; }
13+
14+
protected override void OnInitialized()
15+
{
16+
_renderMode = Mode switch
17+
{
18+
"server" => RenderMode.InteractiveServer,
19+
"wasm" => RenderMode.InteractiveWebAssembly,
20+
"auto" => RenderMode.InteractiveAuto,
21+
_ => throw new InvalidOperationException($"Invalid mode: {Mode}")
22+
};
23+
}
24+
}

src/Components/test/testassets/Components.WasmMinimal/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@
44
using System.Runtime.InteropServices.JavaScript;
55
using System.Security.Claims;
66
using Components.TestServer.Services;
7+
using Microsoft.AspNetCore.Components;
8+
using Microsoft.AspNetCore.Components.Web;
79
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
10+
using TestContentPackage.Services;
811

912
var builder = WebAssemblyHostBuilder.CreateDefault(args);
1013
builder.Services.AddSingleton<AsyncOperationService>();
14+
builder.Services.AddSingleton<InteractiveWebAssemblyService>();
15+
builder.Services.AddSingleton<InteractiveAutoService>();
16+
builder.Services.AddSingleton<InteractiveServerService>();
17+
builder.Services.AddPersistentService<InteractiveAutoService>(RenderMode.InteractiveWebAssembly);
18+
builder.Services.AddPersistentService<InteractiveServerService>(RenderMode.InteractiveWebAssembly);
19+
builder.Services.AddPersistentService<InteractiveWebAssemblyService>(RenderMode.InteractiveWebAssembly);
20+
1121
builder.Services.AddCascadingAuthenticationState();
1222

1323
builder.Services.AddAuthenticationStateDeserialization(options =>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
@using TestContentPackage.Services
2+
3+
@inject InteractiveServerService InteractiveServerState
4+
@inject InteractiveWebAssemblyService InteractiveWebAssemblyState
5+
@inject InteractiveAutoService InteractiveAutoState
6+
7+
<p>Interactive server state is <span id="server-state">@InteractiveServerState.State</span></p>
8+
<p>Interactive webassembly state is <span id="wasm-state">@InteractiveWebAssemblyState.State</span></p>
9+
<p>Interactive auto state is <span id="auto-state">@InteractiveAutoState.State</span></p>
10+
11+
<p>Render mode: <span id="render-mode">@_renderMode</span></p>
12+
13+
@code {
14+
15+
private string _renderMode = "";
16+
17+
protected override void OnInitialized()
18+
{
19+
_renderMode = RendererInfo.Name;
20+
if (!RendererInfo.IsInteractive)
21+
{
22+
InteractiveServerState.State = "Server state";
23+
InteractiveWebAssemblyState.State = "WebAssembly state";
24+
InteractiveAutoState.State = "Auto state";
25+
}else
26+
{
27+
InteractiveServerState.State ??= "not restored";
28+
InteractiveWebAssemblyState.State ??= "not restored";
29+
InteractiveAutoState.State ??= "not restored";
30+
}
31+
}
32+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
using Microsoft.AspNetCore.Components;
8+
9+
namespace TestContentPackage.Services;
10+
11+
public class InteractiveAutoService
12+
{
13+
[SupplyParameterFromPersistentComponentState]
14+
public string State { get; set; }
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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 System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
using Microsoft.AspNetCore.Components;
8+
9+
namespace TestContentPackage.Services;
10+
public class InteractiveServerService
11+
{
12+
[SupplyParameterFromPersistentComponentState]
13+
public string State { get; set; }
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
using Microsoft.AspNetCore.Components;
8+
9+
namespace TestContentPackage.Services;
10+
11+
public class InteractiveWebAssemblyService
12+
{
13+
[SupplyParameterFromPersistentComponentState]
14+
public string State { get; set; }
15+
}

0 commit comments

Comments
 (0)