Skip to content

Commit cc5109e

Browse files
Copilotjaviercn
andcommitted
Add custom serializer support to E2E tests for persistent component state
Co-authored-by: javiercn <[email protected]>
1 parent df6eb65 commit cc5109e

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,7 @@ public void CanPersistPrerenderedStateDeclaratively_Server()
10591059
Navigate($"{ServerPathBase}/persist-state?server=true&declarative=true");
10601060

10611061
Browser.Equal("restored", () => Browser.FindElement(By.Id("server")).Text);
1062+
Browser.Equal("42", () => Browser.FindElement(By.Id("custom-server")).Text);
10621063
Browser.Equal("Server", () => Browser.FindElement(By.Id("render-mode-server")).Text);
10631064
}
10641065

@@ -1077,6 +1078,7 @@ public void CanPersistPrerenderedStateDeclaratively_WebAssembly()
10771078
Navigate($"{ServerPathBase}/persist-state?wasm=true&declarative=true");
10781079

10791080
Browser.Equal("restored", () => Browser.FindElement(By.Id("wasm")).Text);
1081+
Browser.Equal("42", () => Browser.FindElement(By.Id("custom-wasm")).Text);
10801082
Browser.Equal("WebAssembly", () => Browser.FindElement(By.Id("render-mode-wasm")).Text);
10811083
}
10821084

@@ -1095,6 +1097,7 @@ public void CanPersistPrerenderedStateDeclaratively_Auto_PersistsOnWebAssembly()
10951097
Navigate($"{ServerPathBase}/persist-state?auto=true&declarative=true");
10961098

10971099
Browser.Equal("restored", () => Browser.FindElement(By.Id("auto")).Text);
1100+
Browser.Equal("42", () => Browser.FindElement(By.Id("custom-auto")).Text);
10981101
Browser.Equal("WebAssembly", () => Browser.FindElement(By.Id("render-mode-auto")).Text);
10991102
}
11001103

@@ -1156,6 +1159,7 @@ public void CanPersistPrerenderedStateDeclaratively_Auto_PersistsOnServer()
11561159
Navigate($"{ServerPathBase}/persist-state?auto=true&declarative=true");
11571160

11581161
Browser.Equal("restored", () => Browser.FindElement(By.Id("auto")).Text);
1162+
Browser.Equal("42", () => Browser.FindElement(By.Id("custom-auto")).Text);
11591163
Browser.Equal("Server", () => Browser.FindElement(By.Id("render-mode-auto")).Text);
11601164
}
11611165

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.Buffers;
5+
using System.Text;
6+
using Microsoft.AspNetCore.Components;
7+
8+
namespace TestServer;
9+
10+
/// <summary>
11+
/// A custom serializer for int values that uses a custom format to test serialization extensibility.
12+
/// This serializer prefixes integer values with "CUSTOM:" to clearly distinguish them from JSON serialization.
13+
/// </summary>
14+
public class CustomIntSerializer : IPersistentComponentStateSerializer<int>
15+
{
16+
public Task PersistAsync(int value, IBufferWriter<byte> writer)
17+
{
18+
var customFormat = $"CUSTOM:{value}";
19+
var bytes = Encoding.UTF8.GetBytes(customFormat);
20+
writer.Write(bytes);
21+
return Task.CompletedTask;
22+
}
23+
24+
public int Restore(ReadOnlySequence<byte> data)
25+
{
26+
var bytes = data.ToArray();
27+
var text = Encoding.UTF8.GetString(bytes);
28+
29+
if (text.StartsWith("CUSTOM:") && int.TryParse(text.Substring(7), out var value))
30+
{
31+
return value;
32+
}
33+
34+
// Fallback to direct parsing if format is unexpected
35+
return int.TryParse(text, out var fallbackValue) ? fallbackValue : 0;
36+
}
37+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
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;
1213
using Microsoft.AspNetCore.Components.Web;
1314
using Microsoft.AspNetCore.Components.WebAssembly.Server;
@@ -64,6 +65,9 @@ public void ConfigureServices(IServiceCollection services)
6465
services.AddScoped<InteractiveServerService>();
6566
services.AddScoped<InteractiveAutoService>();
6667

68+
// Register custom serializer for E2E testing of persistent component state serialization extensibility
69+
services.AddSingleton<IPersistentComponentStateSerializer<int>, CustomIntSerializer>();
70+
6771
services.AddHttpContextAccessor();
6872
services.AddSingleton<AsyncOperationService>();
6973
services.AddCascadingAuthenticationState();

src/Components/test/testassets/TestContentPackage/DeclarativePersistStateComponent.razor

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<p>Application state is <span id="@KeyName">@Value</span></p>
2+
<p>Custom value is <span id="custom-@KeyName">@CustomValue</span></p>
23
<p>Render mode: <span id="render-mode-@KeyName">@_renderMode</span></p>
34

45
@code {
@@ -11,11 +12,18 @@
1112
[PersistentState]
1213
public string Value { get; set; }
1314

15+
[PersistentState]
16+
public int CustomValue { get; set; }
17+
1418
private string _renderMode = "SSR";
1519

1620
protected override void OnInitialized()
1721
{
1822
Value ??= !RendererInfo.IsInteractive ? InitialValue : "not restored";
23+
if (CustomValue == 0)
24+
{
25+
CustomValue = !RendererInfo.IsInteractive ? 42 : 0;
26+
}
1927
_renderMode = OperatingSystem.IsBrowser() ? "WebAssembly" : "Server";
2028
}
2129
}

0 commit comments

Comments
 (0)