Skip to content

Commit 0e7349e

Browse files
authored
Fix flaky CanRenderComponentWithPersistedState test variations (#55603)
1 parent c2b6fb4 commit 0e7349e

File tree

5 files changed

+49
-57
lines changed

5 files changed

+49
-57
lines changed

src/Components/test/E2ETest/Tests/StatePersistenceTest.cs

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ public override Task InitializeAsync()
4545
[InlineData(true, typeof(InteractiveWebAssemblyRenderMode), (string)null)]
4646
[InlineData(true, typeof(InteractiveWebAssemblyRenderMode), "WebAssemblyStreaming")]
4747
[InlineData(true, typeof(InteractiveAutoRenderMode), (string)null)]
48-
// [InlineData(true, typeof(InteractiveAutoRenderMode), "AutoStreaming")] https://github.com/dotnet/aspnetcore/issues/50810
48+
[InlineData(true, typeof(InteractiveAutoRenderMode), "AutoStreaming")]
4949
[InlineData(false, typeof(InteractiveServerRenderMode), (string)null)]
5050
[InlineData(false, typeof(InteractiveServerRenderMode), "ServerStreaming")]
5151
[InlineData(false, typeof(InteractiveWebAssemblyRenderMode), (string)null)]
5252
[InlineData(false, typeof(InteractiveWebAssemblyRenderMode), "WebAssemblyStreaming")]
53-
// [InlineData(false, typeof(InteractiveAutoRenderMode), (string)null)] https://github.com/dotnet/aspnetcore/issues/50810
54-
// [InlineData(false, typeof(InteractiveAutoRenderMode), "AutoStreaming")] https://github.com/dotnet/aspnetcore/issues/50810
53+
[InlineData(false, typeof(InteractiveAutoRenderMode), (string)null)]
54+
[InlineData(false, typeof(InteractiveAutoRenderMode), "AutoStreaming")]
5555
public void CanRenderComponentWithPersistedState(bool suppressEnhancedNavigation, Type renderMode, string streaming)
5656
{
5757
var mode = renderMode switch
@@ -83,7 +83,11 @@ public void CanRenderComponentWithPersistedState(bool suppressEnhancedNavigation
8383
}
8484
else
8585
{
86-
SuppressEnhancedNavigation(true);
86+
EnhancedNavigationTestUtil.SuppressEnhancedNavigation(this, true);
87+
if (mode == "auto")
88+
{
89+
BlockWebAssemblyResourceLoad();
90+
}
8791
}
8892

8993
if (mode != "auto")
@@ -92,21 +96,11 @@ public void CanRenderComponentWithPersistedState(bool suppressEnhancedNavigation
9296
}
9397
else
9498
{
95-
if (suppressEnhancedNavigation)
96-
{
97-
BlockWebAssemblyResourceLoad();
98-
}
9999
// For auto mode, validate that the state is persisted for both runtimes and is able
100100
// to be loaded on server and wasm.
101101
RenderComponentsWithPersistentStateAndValidate(suppressEnhancedNavigation, mode, renderMode, streaming, interactiveRuntime: "server");
102102

103103
UnblockWebAssemblyResourceLoad();
104-
105-
if (suppressEnhancedNavigation)
106-
{
107-
RenderWebAssemblyComponentAndWaitForWebAssemblyRuntime(returnUrl: Browser.Url);
108-
}
109-
110104
Browser.Navigate().Refresh();
111105

112106
RenderComponentsWithPersistentStateAndValidate(suppressEnhancedNavigation, mode, renderMode, streaming, interactiveRuntime: "wasm");
@@ -151,6 +145,7 @@ private void BlockWebAssemblyResourceLoad()
151145
private void UnblockWebAssemblyResourceLoad()
152146
{
153147
((IJavaScriptExecutor)Browser).ExecuteScript("window.unblockLoadBootResource()");
148+
Browser.Exists(By.Id("unblocked-wasm"));
154149
}
155150

156151
private void RenderComponentsWithPersistentStateAndValidate(
@@ -159,9 +154,8 @@ private void RenderComponentsWithPersistentStateAndValidate(
159154
Type renderMode,
160155
string streaming,
161156
string interactiveRuntime = null,
162-
string stateValue = null)
157+
string stateValue = "restored")
163158
{
164-
stateValue ??= "restored";
165159
// No need to navigate if we are using enhanced navigation, the tests will have already navigated to the page via a link.
166160
if (suppressEnhancedNavigation)
167161
{
@@ -198,11 +192,13 @@ private void RenderComponentsWithPersistentStateAndValidate(
198192
streamingCompleted: false,
199193
interactiveRuntime: interactiveRuntime);
200194

201-
if (streaming != null)
195+
if (streaming == null)
202196
{
203-
Browser.Click(By.Id("end-streaming"));
197+
return;
204198
}
205199

200+
Browser.Click(By.Id("end-streaming"));
201+
206202
AssertPageState(
207203
mode: mode,
208204
renderMode: renderMode.Name,
@@ -240,19 +236,4 @@ private void AssertPageState(
240236
Browser.Equal("Streaming: True", () => Browser.FindElement(By.Id("streaming")).Text);
241237
}
242238
}
243-
244-
private void RenderWebAssemblyComponentAndWaitForWebAssemblyRuntime(string returnUrl = null)
245-
{
246-
Navigate("subdir/persistent-state/page-with-webassembly-interactivity");
247-
248-
Browser.Equal("True", () => Browser.FindElement(By.Id("is-interactive-counter")).Text);
249-
250-
if (returnUrl is not null)
251-
{
252-
Navigate(returnUrl);
253-
}
254-
}
255-
256-
private void SuppressEnhancedNavigation(bool shouldSuppress)
257-
=> EnhancedNavigationTestUtil.SuppressEnhancedNavigation(this, shouldSuppress);
258239
}

src/Components/test/testassets/BasicTestApp/wwwroot/JSInitializers/Modern/BasicTestApp.lib.module.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export function beforeServerStart(options) {
3030
return new Promise((resolve, reject) => {
3131
options.circuitHandlers.push({
3232
onCircuitOpened: () => {
33-
debugger;
3433
appendElement('modern-circuit-opened', 'Modern "circuitOpened"');
3534
},
3635
onCircuitClosed: () => appendElement('modern-circuit-closed', 'Modern "circuitClosed"')

src/Components/test/testassets/Components.TestServer/RazorComponents/App.razor

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,41 @@
1818
<script src="_framework/blazor.web.js" autostart="false" suppress-error="BL9992"></script>
1919
<script src="_content/TestContentPackage/counterInterop.js"></script>
2020
<script>
21-
const enableClassicInitializers = sessionStorage.getItem('enable-classic-initializers') === 'true';
22-
const suppressEnhancedNavigation = sessionStorage.getItem('suppress-enhanced-navigation') === 'true';
23-
const blockLoadBootResource = sessionStorage.getItem('block-load-boot-resource') === 'true';
24-
sessionStorage.removeItem('suppress-enhanced-navigation');
25-
sessionStorage.removeItem('block-load-boot-resource');
26-
sessionStorage.removeItem('enable-classic-initializers');
27-
28-
let loadBootResourceUnblocked = null;
29-
if (blockLoadBootResource) {
30-
loadBootResourceUnblocked = new Promise(resolve => {
31-
window.unblockLoadBootResource = resolve;
32-
});
21+
function appendHiddenParagraph(id) {
22+
const paragraph = document.createElement('p');
23+
paragraph.id = id;
24+
paragraph.style = 'display: none;';
25+
document.body.appendChild(paragraph);
3326
}
3427
35-
let maxParallelResourceDownloadCount = 0;
36-
let currentParallelResourceDownloadCount = 0;
37-
3828
function callBlazorStart() {
29+
const enableClassicInitializers = sessionStorage.getItem('enable-classic-initializers') === 'true';
30+
const suppressEnhancedNavigation = sessionStorage.getItem('suppress-enhanced-navigation') === 'true';
31+
const blockLoadBootResource = sessionStorage.getItem('block-load-boot-resource') === 'true';
32+
sessionStorage.removeItem('suppress-enhanced-navigation');
33+
sessionStorage.removeItem('block-load-boot-resource');
34+
sessionStorage.removeItem('enable-classic-initializers');
35+
36+
let loadBootResourceUnblocked = null;
37+
if (blockLoadBootResource) {
38+
loadBootResourceUnblocked = new Promise(resolve => {
39+
window.unblockLoadBootResource = () => {
40+
var origSetItem = localStorage.setItem;
41+
localStorage.setItem = function (key) {
42+
origSetItem.apply(this, arguments);
43+
if (key.startsWith('blazor-resource-hash:')) {
44+
localStorage.setItem = origSetItem;
45+
appendHiddenParagraph('unblocked-wasm');
46+
}
47+
};
48+
resolve();
49+
}
50+
});
51+
}
52+
53+
let maxParallelResourceDownloadCount = 0;
54+
let currentParallelResourceDownloadCount = 0;
55+
3956
Blazor.start({
4057
ssr: {
4158
disableDomPreservation: suppressEnhancedNavigation,
@@ -71,12 +88,7 @@
7188
}
7289
},
7390
},
74-
}).then(() => {
75-
const startedParagraph = document.createElement('p');
76-
startedParagraph.id = 'blazor-started';
77-
startedParagraph.style = 'display: none;';
78-
document.body.appendChild(startedParagraph);
79-
});
91+
}).then(() => appendHiddenParagraph('blazor-started'));
8092
}
8193
8294
if (location.search.indexOf('suppress-autostart') < 0) {

src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/PersistentState/PageWithoutComponents.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<a id="page-with-components-link" href=@($"persistent-state/page-with-components?render-mode={RenderMode}&streaming-id={StreamingId}")>Go to page with components</a>
66

7-
<a id="page-with-components-link-and-state" href=@($"persistent-state/page-with-components?render-mode={RenderMode}&streaming-id={StreamingId}&server-state=other")>Go to page with components</a>
7+
<a id="page-with-components-link-and-state" href=@($"persistent-state/page-with-components?render-mode={RenderMode}&streaming-id={StreamingId}&server-state=other")>Go to page with components and state</a>
88

99

1010
@code {

src/Components/test/testassets/TestContentPackage/PersistentComponents/NonStreamingComponentWithPersistentState.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<p>Non streaming component with persistent state</p>
22

3-
<p>This component demonstrates state persistence in the absence of streaming rendering. When the component renders it will try to restore the state and if present display that it succeded in doing so and the restored value. If the state is not present, it will indicate it didn't find it and display a "fresh" value.</p>
3+
<p>This component demonstrates state persistence in the absence of streaming rendering. When the component renders it will try to restore the state and if present display that it succeeded in doing so and the restored value. If the state is not present, it will indicate it didn't find it and display a "fresh" value.</p>
44

55
<p id="interactive">Interactive: @(!RunningOnServer)</p>
66
<p id="interactive-runtime">Interactive runtime: @_interactiveRuntime</p>

0 commit comments

Comments
 (0)