Skip to content

Commit 7e58b30

Browse files
authored
Add a test to verify HotReload events are not subscribed to as part of regular rendering (#35299)
Verification for #34414
1 parent 043e87d commit 7e58b30

File tree

4 files changed

+36
-18
lines changed

4 files changed

+36
-18
lines changed
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
54
using System.Reflection.Metadata;
65
using Microsoft.AspNetCore.Components.HotReload;
76

@@ -11,13 +10,16 @@ namespace Microsoft.AspNetCore.Components.HotReload
1110
{
1211
internal static class HotReloadManager
1312
{
14-
internal static event Action? OnDeltaApplied;
13+
public static event Action? OnDeltaApplied;
1514

16-
public static void DeltaApplied()
17-
{
18-
OnDeltaApplied?.Invoke();
19-
}
15+
/// <summary>
16+
/// Gets a value that determines if OnDeltaApplied is subscribed to.
17+
/// </summary>
18+
public static bool IsSubscribedTo => OnDeltaApplied is not null;
2019

20+
/// <summary>
21+
/// MetadataUpdateHandler event. This is invoked by the hot reload host via reflection.
22+
/// </summary>
2123
public static void UpdateApplication(Type[]? _) => OnDeltaApplied?.Invoke();
2224
}
2325
}

src/Components/test/E2ETest/ServerExecutionTests/PrerenderingTest.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
54
using System.Net;
6-
using System.Net.Http;
7-
using System.Threading.Tasks;
8-
using BasicTestApp;
5+
using System.Net.Http.Json;
96
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
107
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
118
using Microsoft.AspNetCore.E2ETesting;
12-
using Microsoft.AspNetCore.Testing;
139
using OpenQA.Selenium;
1410
using TestServer;
1511
using Xunit;
@@ -138,6 +134,28 @@ public void CanAccessAuthenticationStateDuringStaticPrerendering(string initialU
138134
Browser.Equal($"Hello, {interactiveUsername ?? "anonymous"}!", () => Browser.Exists(By.TagName("h1")).Text);
139135
}
140136

137+
[Fact]
138+
public async Task NoHotReloadListenersAreOrdinarilyRegistered()
139+
{
140+
Navigate("/prerendered/prerendered-transition");
141+
142+
// Prerendered output shows "not connected"
143+
Browser.Equal("not connected", () => Browser.Exists(By.Id("connected-state")).Text);
144+
145+
// Once connected, output changes
146+
BeginInteractivity();
147+
Browser.Equal("connected", () => Browser.Exists(By.Id("connected-state")).Text);
148+
149+
// Once connected, output changes
150+
BeginInteractivity();
151+
Browser.Equal("connected", () => Browser.Exists(By.Id("connected-state")).Text);
152+
153+
// Now query the hot reload manager and verify nothing is still wired up by default.
154+
var httpClient = new HttpClient { BaseAddress = _serverFixture.RootUri };
155+
var hasEventHandlers = await httpClient.GetFromJsonAsync<bool>("/prerendered/ishotreloadsubscribedto");
156+
Assert.False(hasEventHandlers);
157+
}
158+
141159
private void BeginInteractivity()
142160
{
143161
Browser.Exists(By.Id("load-boot-script")).Click();

src/Components/test/testassets/TestServer/Controllers/ReloadController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ReloadController : ControllerBase
1212
[HttpGet("/rerender")]
1313
public IActionResult Rerender()
1414
{
15-
HotReloadManager.DeltaApplied();
15+
HotReloadManager.UpdateApplication(default);
1616

1717
return Ok();
1818
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Globalization;
45
using Microsoft.AspNetCore.Authentication.Cookies;
5-
using Microsoft.AspNetCore.Builder;
6-
using Microsoft.AspNetCore.Hosting;
7-
using Microsoft.Extensions.Configuration;
8-
using Microsoft.Extensions.DependencyInjection;
9-
using Microsoft.Extensions.Hosting;
6+
using Microsoft.AspNetCore.Components.HotReload;
107
using Microsoft.AspNetCore.Components.WebAssembly.Services;
11-
using System.Globalization;
128

139
namespace TestServer
1410
{
@@ -51,6 +47,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
5147
app.UseRouting();
5248
app.UseEndpoints(endpoints =>
5349
{
50+
endpoints.MapGet("ishotreloadsubscribedto", () => HotReloadManager.IsSubscribedTo);
51+
5452
endpoints.MapRazorPages();
5553
endpoints.MapFallbackToPage("/PrerenderedHost");
5654
endpoints.MapBlazorHub();

0 commit comments

Comments
 (0)