test(web): add bUnit tests for StatusChip and FilterBar with ManualMockKeyInterceptorService
This commit is contained in:
1 parent
9df02856ad
commit
b851a20dde
2 files changed
+132
No files matched your search
@@ -0,0 +1,88 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Heimdall.Web.Components.Shared;
|
||||
using Shouldly;
|
||||
|
||||
namespace Heimdall.Web.Tests.Components;
|
||||
|
||||
public class StatusChipTests : MudTestBase
|
||||
{
|
||||
[Fact]
|
||||
public void Should_render_mud_chip()
|
||||
{
|
||||
var cut = Render<StatusChip>();
|
||||
cut.Markup.ShouldContain("mud-chip-container");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_render_without_error()
|
||||
{
|
||||
Should.NotThrow(() => Render<StatusChip>());
|
||||
}
|
||||
}
|
||||
|
||||
public class FilterBarTests : MudTestBase
|
||||
{
|
||||
[Fact]
|
||||
public void Should_render_search_field()
|
||||
{
|
||||
var cut = Render<FilterBar>();
|
||||
cut.Markup.ShouldContain("Rechercher");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_render_with_categories()
|
||||
{
|
||||
var cut = Render<FilterBar>();
|
||||
cut.Markup.ShouldContain("Rechercher");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_render_clear_button()
|
||||
{
|
||||
var cut = Render<FilterBar>();
|
||||
cut.Markup.ShouldContain("Effacer");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user