Files
Heimdall/tests/Heimdall.Web.Tests/Components/MudTestBase.cs
T

89 lines
4.2 KiB
C#

using Bunit;
using Microsoft.Extensions.DependencyInjection;
using MudBlazor;
using MudBlazor.Services;
namespace Heimdall.Web.Tests.Components;
/// <summary>
/// Base TestContext for MudBlazor component tests.
/// Registers MudBlazor services with NSubstitute-mocked IKeyInterceptorService
/// (avoids IAsyncDisposable-only conflict with bUnit's sync Dispose).
/// </summary>
public abstract class MudTestBase : TestContext
{
protected MudTestBase()
{
var services = (IServiceCollection)Services;
// Register all MudBlazor services except KeyInterceptorService
services.AddMudServices(config =>
{
config.SnackbarConfiguration.PositionClass = "mud-alert-center-top";
config.SnackbarConfiguration.PreventDuplicates = true;
config.SnackbarConfiguration.ShowCloseIcon = true;
config.SnackbarConfiguration.VisibleStateDuration = 4000;
});
// Replace KeyInterceptorService with a manual mock that implements
// both IDisposable and IAsyncDisposable (NSubstitute Castle proxy
// only implements IAsyncDisposable, causing disposal failures)
services.RemoveService<IKeyInterceptorService>();
services.AddScoped<IKeyInterceptorService>(_ =>
{
var mock = new ManualMockKeyInterceptorService();
return mock;
});
// JSInterop setup for MudBlazor internals
JSInterop.SetupVoid("mudKeyInterceptor.connect", _ => true);
JSInterop.SetupVoid("mudKeyInterceptor.disconnect", _ => true);
JSInterop.SetupVoid("mudPopover.connect", _ => true);
JSInterop.SetupVoid("mudPopover.disconnect", _ => true);
JSInterop.SetupVoid("mudElementReference.captureElement", _ => true);
JSInterop.SetupVoid("mudElementRef.addOnBlurEvent", _ => true);
JSInterop.SetupVoid("mudElementRef.removeOnBlurEvent", _ => true);
JSInterop.SetupVoid("mudTextField.focus", _ => true);
}
}
/// <summary>
/// Manual mock for IKeyInterceptorService that implements both IDisposable
/// and IAsyncDisposable. Required because NSubstitute Castle proxies only
/// implement IAsyncDisposable, which breaks bUnit's sync service provider Dispose.
/// </summary>
internal sealed class ManualMockKeyInterceptorService : IKeyInterceptorService, IDisposable, IAsyncDisposable
{
public void Dispose() { }
public ValueTask DisposeAsync() { GC.SuppressFinalize(this); return ValueTask.CompletedTask; }
public Task SubscribeAsync(IKeyInterceptorObserver observer, KeyInterceptorOptions options)
=> Task.CompletedTask;
public Task SubscribeAsync(string elementId, KeyInterceptorOptions options,
Action<KeyMapBuilder> builder) => Task.CompletedTask;
public Task SubscribeAsync(string elementId, KeyInterceptorOptions options,
IKeyDownObserver? keyDown, IKeyUpObserver? keyUp) => Task.CompletedTask;
public Task SubscribeAsync(string elementId, KeyInterceptorOptions options,
Action<Microsoft.AspNetCore.Components.Web.KeyboardEventArgs>? keyDown,
Action<Microsoft.AspNetCore.Components.Web.KeyboardEventArgs>? keyUp) => Task.CompletedTask;
public Task SubscribeAsync(string elementId, KeyInterceptorOptions options,
Func<Microsoft.AspNetCore.Components.Web.KeyboardEventArgs, Task>? keyDown,
Func<Microsoft.AspNetCore.Components.Web.KeyboardEventArgs, Task>? keyUp) => Task.CompletedTask;
public Task UnsubscribeAsync(string elementId) => Task.CompletedTask;
public Task UnsubscribeAsync(IKeyInterceptorObserver observer) => Task.CompletedTask;
public Task DispatchAsync(string elementId, KeyEventKind kind,
Microsoft.AspNetCore.Components.Web.KeyboardEventArgs args) => Task.CompletedTask;
public Task UpdateKeyAsync(IKeyInterceptorObserver observer, KeyOptions options) => Task.CompletedTask;
public Task UpdateKeyAsync(string elementId, KeyOptions options) => Task.CompletedTask;
}
internal static class ServiceCollectionExtensions
{
public static void RemoveService<T>(this IServiceCollection services) where T : class
{
var descriptor = services.FirstOrDefault(d => d.ServiceType == typeof(T));
if (descriptor is not null)
services.Remove(descriptor);
}
}