Skip to content

Commit

Permalink
Fix acceptance tests (update JS init scripts; wait for Blazor runtime…
Browse files Browse the repository at this point in the history
… to start before interacting with Interactive acceptance test pages)
  • Loading branch information
msmithNI committed Oct 25, 2024
1 parent 8ac8a2e commit 0113f99
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
* 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;
}

if (!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
Expand Down Expand Up @@ -164,32 +164,38 @@ 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) {
console.warn('Attempting to initialize NimbleBlazor multiple times!'); // eslint-disable-line
}

window.NimbleBlazor = window.NimbleBlazor ?? {
isBlazorWebApp: false,
hasInitialized: false,
hasRegisteredEvents: false,
hasRuntimeStarted: false,
Dialog: {
show: async function (dialogReference) {
const reason = await dialogReference.show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
* 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;
}

if (!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', {
Expand All @@ -35,30 +35,36 @@ 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) {
console.warn('Attempting to initialize SprightBlazor multiple times!'); // eslint-disable-line
}

window.SprightBlazor = window.SprightBlazor ?? {
isBlazorWebApp: false,
hasInitialized: false,
hasRegisteredEvents: false,
hasRuntimeStarted: false,
};
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
@@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

0 comments on commit 0113f99

Please sign in to comment.