From 0113f993fc7790626b648da9cb9614ae2d3243de Mon Sep 17 00:00:00 2001 From: Malcolm Smith <20709258+msmithNI@users.noreply.github.com> Date: Fri, 25 Oct 2024 17:51:08 -0500 Subject: [PATCH] Fix acceptance tests (update JS init scripts; wait for Blazor runtime to start before interacting with Interactive acceptance test pages) --- .../wwwroot/NimbleBlazor.lib.module.js | 36 +++++++++++-------- .../wwwroot/SprightBlazor.lib.module.js | 36 +++++++++++-------- .../NimbleAcceptanceTestsBase.cs | 2 +- .../NimbleInteractiveAcceptanceTestsBase.cs | 16 +++++++++ .../Tests.InteractiveServer/DialogTests.cs | 2 +- .../Tests.InteractiveServer/DrawerTests.cs | 2 +- .../TableColumnMappingTests.cs | 2 +- .../TableColumnMenuButtonTests.cs | 2 +- .../TableColumnNumberTextTests.cs | 2 +- .../Tests.InteractiveServer/TableTests.cs | 2 +- .../ThemeProviderTests.cs | 2 +- .../Tests.InteractiveServer/WaferMapTests.cs | 2 +- .../WaferMapTestsExperimental.cs | 2 +- .../DialogTests.cs | 2 +- .../Tests/SprightAcceptanceTestsBase.cs | 2 +- 15 files changed, 70 insertions(+), 42 deletions(-) create mode 100644 packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/NimbleInteractiveAcceptanceTestsBase.cs diff --git a/packages/blazor-workspace/NimbleBlazor/wwwroot/NimbleBlazor.lib.module.js b/packages/blazor-workspace/NimbleBlazor/wwwroot/NimbleBlazor.lib.module.js index 6e518c3656..ae32235cea 100644 --- a/packages/blazor-workspace/NimbleBlazor/wwwroot/NimbleBlazor.lib.module.js +++ b/packages/blazor-workspace/NimbleBlazor/wwwroot/NimbleBlazor.lib.module.js @@ -9,9 +9,9 @@ * https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/startup?view=aspnetcore-8.0 */ -function initializeNimbleBlazor(Blazor) { - if (window.NimbleBlazor.hasInitialized) { - console.warn('Attempted to initialize Nimble Blazor multiple times!'); // eslint-disable-line +function registerNimbleEvents(Blazor) { + if (window.NimbleBlazor.hasRegisteredEvents) { + console.warn('Attempted to register Nimble Blazor events multiple times!'); // eslint-disable-line return; } @@ -19,7 +19,7 @@ function initializeNimbleBlazor(Blazor) { throw new Error('Blazor not ready to initialize Nimble with!'); } - window.NimbleBlazor.hasInitialized = true; + window.NimbleBlazor.hasRegisteredEvents = true; // Used by NimbleCheckbox.razor, NimbleSwitch.razor, NimbleToggleButton.razor // Necessary because the control's value property is always just the value 'on', so we need to look @@ -164,23 +164,29 @@ function initializeNimbleBlazor(Blazor) { } // Blazor Web Apps -export function beforeWebStart(_Blazor) { - window.NimbleBlazor.isBlazorWebApp = true; +export function afterWebStarted(Blazor) { + registerNimbleEvents(Blazor); + // Note: For static SSR, this is the last event called, and hasRuntimeStarted + // will remain false. } -export function afterWebStarted(Blazor) { - initializeNimbleBlazor(Blazor); +export function afterServerStarted(_Blazor) { + window.NimbleBlazor.hasRuntimeStarted = true; +} + +export function afterWebAssemblyStarted(_Blazor) { + window.NimbleBlazor.hasRuntimeStarted = true; } // Blazor Server/WebAssembly/Hybrid apps export function afterStarted(Blazor) { // In some cases afterStarted is called on Blazor Web Apps too, if Nimble is used in a component explicitly - // marked as InteractiveWebAssembly render mode. As long as afterWebStarted was already called, we've already - // initialized. - if (window.NimbleBlazor.isBlazorWebApp && window.NimbleBlazor.hasInitialized) { - return; + // marked as InteractiveWebAssembly render mode. So check if we've already registered our events first. + if (!window.NimbleBlazor.hasRegisteredEvents) { + registerNimbleEvents(Blazor); } - initializeNimbleBlazor(Blazor); + + window.NimbleBlazor.hasRuntimeStarted = true; } if (window.NimbleBlazor) { @@ -188,8 +194,8 @@ if (window.NimbleBlazor) { } window.NimbleBlazor = window.NimbleBlazor ?? { - isBlazorWebApp: false, - hasInitialized: false, + hasRegisteredEvents: false, + hasRuntimeStarted: false, Dialog: { show: async function (dialogReference) { const reason = await dialogReference.show(); diff --git a/packages/blazor-workspace/SprightBlazor/wwwroot/SprightBlazor.lib.module.js b/packages/blazor-workspace/SprightBlazor/wwwroot/SprightBlazor.lib.module.js index a6ffc3ee12..4989ccf2b2 100644 --- a/packages/blazor-workspace/SprightBlazor/wwwroot/SprightBlazor.lib.module.js +++ b/packages/blazor-workspace/SprightBlazor/wwwroot/SprightBlazor.lib.module.js @@ -9,9 +9,9 @@ * https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/startup?view=aspnetcore-8.0 */ -export function initializeSprightBlazor(Blazor) { - if (window.SprightBlazor.hasInitialized) { - console.warn('Attempted to initialize Spright Blazor multiple times!'); // eslint-disable-line +export function registerSprightEvents(Blazor) { + if (window.SprightBlazor.hasRegisteredEvents) { + console.warn('Attempted to register Spright Blazor events multiple times!'); // eslint-disable-line return; } @@ -19,7 +19,7 @@ export function initializeSprightBlazor(Blazor) { throw new Error('Blazor not ready to initialize Spright with!'); } - window.SprightBlazor.hasInitialized = true; + window.SprightBlazor.hasRegisteredEvents = true; /* Register any custom events here Blazor.registerCustomEventType('sprighteventname', { @@ -35,23 +35,29 @@ export function initializeSprightBlazor(Blazor) { } // Blazor Web Apps -export function beforeWebStart(_Blazor) { - window.SprightBlazor.isBlazorWebApp = true; +export function afterWebStarted(Blazor) { + registerSprightEvents(Blazor); + // Note: For static SSR, this is the last event called, and hasRuntimeStarted + // will remain false. } -export function afterWebStarted(Blazor) { - initializeSprightBlazor(Blazor); +export function afterServerStarted(_Blazor) { + window.SprightBlazor.hasRuntimeStarted = true; +} + +export function afterWebAssemblyStarted(_Blazor) { + window.SprightBlazor.hasRuntimeStarted = true; } // Blazor Server/WebAssembly/Hybrid apps export function afterStarted(Blazor) { // In some cases afterStarted is called on Blazor Web Apps too, if Spright is used in a component explicitly - // marked as InteractiveWebAssembly render mode. As long as afterWebStarted was already called, we've already - // initialized. - if (window.SprightBlazor.isBlazorWebApp && window.SprightBlazor.hasInitialized) { - return; + // marked as InteractiveWebAssembly render mode. So check if we've already registered our events first. + if (!window.SprightBlazor.hasRegisteredEvents) { + registerSprightEvents(Blazor); } - initializeSprightBlazor(Blazor); + + window.NimbleBlazor.hasRuntimeStarted = true; } if (window.SprightBlazor) { @@ -59,6 +65,6 @@ if (window.SprightBlazor) { } window.SprightBlazor = window.SprightBlazor ?? { - isBlazorWebApp: false, - hasInitialized: false, + hasRegisteredEvents: false, + hasRuntimeStarted: false, }; diff --git a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/NimbleAcceptanceTestsBase.cs b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/NimbleAcceptanceTestsBase.cs index f6ce8217a8..986ebac507 100644 --- a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/NimbleAcceptanceTestsBase.cs +++ b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/NimbleAcceptanceTestsBase.cs @@ -14,5 +14,5 @@ protected NimbleAcceptanceTestsBase( } protected override Uri ServerAddress { get; } - protected override string ComponentLibraryInitializationTestJavaScript => "window.NimbleBlazor && window.NimbleBlazor.hasInitialized === true"; + protected override string ComponentLibraryInitializationTestJavaScript => "window.NimbleBlazor && window.NimbleBlazor.hasRegisteredEvents === true"; } diff --git a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/NimbleInteractiveAcceptanceTestsBase.cs b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/NimbleInteractiveAcceptanceTestsBase.cs new file mode 100644 index 0000000000..d1dc05f0eb --- /dev/null +++ b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/NimbleInteractiveAcceptanceTestsBase.cs @@ -0,0 +1,16 @@ +using BlazorWorkspace.Testing.Acceptance; +using Xunit; + +namespace NimbleBlazor.Tests.Acceptance; + +public abstract class NimbleInteractiveAcceptanceTestsBase : NimbleAcceptanceTestsBase +{ + protected NimbleInteractiveAcceptanceTestsBase( + PlaywrightFixture playwrightFixture, + NimbleBlazorWebHostServerFixture blazorServerClassFixture) + : base(playwrightFixture, blazorServerClassFixture) + { + } + + protected override string ComponentLibraryInitializationTestJavaScript => "window.NimbleBlazor && window.NimbleBlazor.hasRegisteredEvents === true && window.NimbleBlazor.hasRuntimeStarted === true"; +} diff --git a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/DialogTests.cs b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/DialogTests.cs index 59a320f226..94c30c12f8 100644 --- a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/DialogTests.cs +++ b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/DialogTests.cs @@ -4,7 +4,7 @@ namespace NimbleBlazor.Tests.Acceptance.InteractiveServer; -public class DialogTests : NimbleAcceptanceTestsBase +public class DialogTests : NimbleInteractiveAcceptanceTestsBase { public DialogTests(PlaywrightFixture playwrightFixture, NimbleBlazorWebHostServerFixture blazorServerClassFixture) : base(playwrightFixture, blazorServerClassFixture) diff --git a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/DrawerTests.cs b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/DrawerTests.cs index 68aa87fde8..af735742b6 100644 --- a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/DrawerTests.cs +++ b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/DrawerTests.cs @@ -4,7 +4,7 @@ namespace NimbleBlazor.Tests.Acceptance.InteractiveServer; -public class DrawerTests : NimbleAcceptanceTestsBase +public class DrawerTests : NimbleInteractiveAcceptanceTestsBase { public DrawerTests(PlaywrightFixture playwrightFixture, NimbleBlazorWebHostServerFixture blazorServerClassFixture) : base(playwrightFixture, blazorServerClassFixture) diff --git a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/TableColumnMappingTests.cs b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/TableColumnMappingTests.cs index 8ac964e3fe..1db2ab6030 100644 --- a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/TableColumnMappingTests.cs +++ b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/TableColumnMappingTests.cs @@ -4,7 +4,7 @@ namespace NimbleBlazor.Tests.Acceptance.InteractiveServer; -public class TableColumnMappingTests : NimbleAcceptanceTestsBase +public class TableColumnMappingTests : NimbleInteractiveAcceptanceTestsBase { public TableColumnMappingTests(PlaywrightFixture playwrightFixture, NimbleBlazorWebHostServerFixture blazorServerClassFixture) : base(playwrightFixture, blazorServerClassFixture) diff --git a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/TableColumnMenuButtonTests.cs b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/TableColumnMenuButtonTests.cs index 1dabfc47ec..39c1f75c82 100644 --- a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/TableColumnMenuButtonTests.cs +++ b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/TableColumnMenuButtonTests.cs @@ -4,7 +4,7 @@ namespace NimbleBlazor.Tests.Acceptance.InteractiveServer; -public class TableColumnMenuButtonTests : NimbleAcceptanceTestsBase +public class TableColumnMenuButtonTests : NimbleInteractiveAcceptanceTestsBase { public TableColumnMenuButtonTests(PlaywrightFixture playwrightFixture, NimbleBlazorWebHostServerFixture blazorServerClassFixture) : base(playwrightFixture, blazorServerClassFixture) diff --git a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/TableColumnNumberTextTests.cs b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/TableColumnNumberTextTests.cs index 2ddbe1c488..467f7624d8 100644 --- a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/TableColumnNumberTextTests.cs +++ b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/TableColumnNumberTextTests.cs @@ -4,7 +4,7 @@ namespace NimbleBlazor.Tests.Acceptance.InteractiveServer; -public class TableColumnNumberTextTests : NimbleAcceptanceTestsBase +public class TableColumnNumberTextTests : NimbleInteractiveAcceptanceTestsBase { public TableColumnNumberTextTests(PlaywrightFixture playwrightFixture, NimbleBlazorWebHostServerFixture blazorServerClassFixture) : base(playwrightFixture, blazorServerClassFixture) diff --git a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/TableTests.cs b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/TableTests.cs index 2ea2afdaef..1aba933c04 100644 --- a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/TableTests.cs +++ b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/TableTests.cs @@ -4,7 +4,7 @@ namespace NimbleBlazor.Tests.Acceptance.InteractiveServer; -public class TableTests : NimbleAcceptanceTestsBase +public class TableTests : NimbleInteractiveAcceptanceTestsBase { public TableTests(PlaywrightFixture playwrightFixture, NimbleBlazorWebHostServerFixture blazorServerClassFixture) : base(playwrightFixture, blazorServerClassFixture) diff --git a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/ThemeProviderTests.cs b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/ThemeProviderTests.cs index e0f877a54f..47b35a42bd 100644 --- a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/ThemeProviderTests.cs +++ b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/ThemeProviderTests.cs @@ -4,7 +4,7 @@ namespace NimbleBlazor.Tests.Acceptance.InteractiveServer; -public class ThemeProviderTests : NimbleAcceptanceTestsBase +public class ThemeProviderTests : NimbleInteractiveAcceptanceTestsBase { public ThemeProviderTests(PlaywrightFixture playwrightFixture, NimbleBlazorWebHostServerFixture blazorServerClassFixture) : base(playwrightFixture, blazorServerClassFixture) diff --git a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/WaferMapTests.cs b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/WaferMapTests.cs index de3ac06c45..27ad754b22 100644 --- a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/WaferMapTests.cs +++ b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/WaferMapTests.cs @@ -4,7 +4,7 @@ namespace NimbleBlazor.Tests.Acceptance.InteractiveServer; -public class WaferMapTests : NimbleAcceptanceTestsBase +public class WaferMapTests : NimbleInteractiveAcceptanceTestsBase { public WaferMapTests(PlaywrightFixture playwrightFixture, NimbleBlazorWebHostServerFixture blazorServerClassFixture) : base(playwrightFixture, blazorServerClassFixture) diff --git a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/WaferMapTestsExperimental.cs b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/WaferMapTestsExperimental.cs index 89fb34ea72..0c427af4b3 100644 --- a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/WaferMapTestsExperimental.cs +++ b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveServer/WaferMapTestsExperimental.cs @@ -4,7 +4,7 @@ namespace NimbleBlazor.Tests.Acceptance.InteractiveServer; -public class WaferMapTestsExperimental : NimbleAcceptanceTestsBase +public class WaferMapTestsExperimental : NimbleInteractiveAcceptanceTestsBase { public WaferMapTestsExperimental(PlaywrightFixture playwrightFixture, NimbleBlazorWebHostServerFixture blazorServerClassFixture) : base(playwrightFixture, blazorServerClassFixture) diff --git a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveWebAssembly/DialogTests.cs b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveWebAssembly/DialogTests.cs index 271c47b47b..ba869dd8ad 100644 --- a/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveWebAssembly/DialogTests.cs +++ b/packages/blazor-workspace/Tests/NimbleBlazor.Tests.Acceptance/Tests.InteractiveWebAssembly/DialogTests.cs @@ -4,7 +4,7 @@ namespace NimbleBlazor.Tests.Acceptance.InteractiveWebAssembly; -public class DialogTests : NimbleAcceptanceTestsBase +public class DialogTests : NimbleInteractiveAcceptanceTestsBase { public DialogTests(PlaywrightFixture playwrightFixture, NimbleBlazorWebHostServerFixture blazorServerClassFixture) : base(playwrightFixture, blazorServerClassFixture) diff --git a/packages/blazor-workspace/Tests/SprightBlazor.Tests.Acceptance/Tests/SprightAcceptanceTestsBase.cs b/packages/blazor-workspace/Tests/SprightBlazor.Tests.Acceptance/Tests/SprightAcceptanceTestsBase.cs index 551cfddb1a..8a9fce7277 100644 --- a/packages/blazor-workspace/Tests/SprightBlazor.Tests.Acceptance/Tests/SprightAcceptanceTestsBase.cs +++ b/packages/blazor-workspace/Tests/SprightBlazor.Tests.Acceptance/Tests/SprightAcceptanceTestsBase.cs @@ -14,5 +14,5 @@ protected SprightAcceptanceTestsBase( } protected override Uri ServerAddress { get; } - protected override string ComponentLibraryInitializationTestJavaScript => "window.SprightBlazor && window.SprightBlazor.hasInitialized === true"; + protected override string ComponentLibraryInitializationTestJavaScript => "window.SprightBlazor && window.SprightBlazor.hasRegisteredEvents === true"; }