From b851a20ddef4ea647e7f4c185d7f03e021c5904f Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Thu, 23 Jul 2026 20:40:56 +0000 Subject: [PATCH] test(web): add bUnit tests for StatusChip and FilterBar with ManualMockKeyInterceptorService --- .../Components/MudTestBase.cs | 88 +++++++++++++++++++ .../Components/StatusChipTests.cs | 44 ++++++++++ 2 files changed, 132 insertions(+) create mode 100644 tests/Heimdall.Web.Tests/Components/MudTestBase.cs create mode 100644 tests/Heimdall.Web.Tests/Components/StatusChipTests.cs diff --git a/tests/Heimdall.Web.Tests/Components/MudTestBase.cs b/tests/Heimdall.Web.Tests/Components/MudTestBase.cs new file mode 100644 index 0000000..cbf0e41 --- /dev/null +++ b/tests/Heimdall.Web.Tests/Components/MudTestBase.cs @@ -0,0 +1,88 @@ +using Bunit; +using Microsoft.Extensions.DependencyInjection; +using MudBlazor; +using MudBlazor.Services; + +namespace Heimdall.Web.Tests.Components; + +/// +/// Base TestContext for MudBlazor component tests. +/// Registers MudBlazor services with NSubstitute-mocked IKeyInterceptorService +/// (avoids IAsyncDisposable-only conflict with bUnit's sync Dispose). +/// +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(); + services.AddScoped(_ => + { + 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); + } +} + +/// +/// 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. +/// +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 builder) => Task.CompletedTask; + public Task SubscribeAsync(string elementId, KeyInterceptorOptions options, + IKeyDownObserver? keyDown, IKeyUpObserver? keyUp) => Task.CompletedTask; + public Task SubscribeAsync(string elementId, KeyInterceptorOptions options, + Action? keyDown, + Action? keyUp) => Task.CompletedTask; + public Task SubscribeAsync(string elementId, KeyInterceptorOptions options, + Func? keyDown, + Func? 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(this IServiceCollection services) where T : class + { + var descriptor = services.FirstOrDefault(d => d.ServiceType == typeof(T)); + if (descriptor is not null) + services.Remove(descriptor); + } +} diff --git a/tests/Heimdall.Web.Tests/Components/StatusChipTests.cs b/tests/Heimdall.Web.Tests/Components/StatusChipTests.cs new file mode 100644 index 0000000..d524513 --- /dev/null +++ b/tests/Heimdall.Web.Tests/Components/StatusChipTests.cs @@ -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(); + cut.Markup.ShouldContain("mud-chip-container"); + } + + [Fact] + public void Should_render_without_error() + { + Should.NotThrow(() => Render()); + } +} + +public class FilterBarTests : MudTestBase +{ + [Fact] + public void Should_render_search_field() + { + var cut = Render(); + cut.Markup.ShouldContain("Rechercher"); + } + + [Fact] + public void Should_render_with_categories() + { + var cut = Render(); + cut.Markup.ShouldContain("Rechercher"); + } + + [Fact] + public void Should_render_clear_button() + { + var cut = Render(); + cut.Markup.ShouldContain("Effacer"); + } +}