|
1 | | -using System.Collections.Generic; |
2 | | -using System.Security.Claims; |
3 | | -using System.Threading.Tasks; |
| 1 | +using System.Security.Claims; |
4 | 2 | using Bunit; |
5 | 3 | using Microsoft.Extensions.Configuration; |
6 | 4 | using Microsoft.Extensions.DependencyInjection; |
7 | | -using MudBlazor.Services; |
8 | | -using MudBlazor; |
9 | 5 | using Microsoft.AspNetCore.Components; |
10 | 6 | using Microsoft.Extensions.Localization; |
11 | 7 | using Microsoft.AspNetCore.Authorization; |
|
14 | 10 | namespace Hedin.UI.Tests.Base; |
15 | 11 | /// <summary> |
16 | 12 | /// Base class for Blazor component tests. |
17 | | -/// Provides a TestContext with Hedin.UI services configured |
| 13 | +/// Provides a BunitContext with Hedin.UI services configured |
18 | 14 | /// and a minimal IConfiguration. |
19 | 15 | /// </summary> |
20 | | -public abstract class UiTestBase : TestContext |
| 16 | +public abstract class UiTestBase : BunitContext, IAsyncLifetime |
21 | 17 | { |
22 | 18 | protected IConfiguration Configuration { get; } |
23 | 19 |
|
| 20 | + // MudBlazor registers services that are IAsyncDisposable-only (e.g. KeyInterceptorService, |
| 21 | + // PointerEventsNoneService). xUnit 2.9 calls IDisposable.Dispose on test classes, which |
| 22 | + // triggers a synchronous ServiceProvider.Dispose and throws. Route disposal through |
| 23 | + // IAsyncLifetime.DisposeAsync and no-op the sync path. |
| 24 | + protected override void Dispose(bool disposing) { } |
| 25 | + |
| 26 | + Task IAsyncLifetime.InitializeAsync() => Task.CompletedTask; |
| 27 | + |
| 28 | + async Task IAsyncLifetime.DisposeAsync() => await ((IAsyncDisposable)this).DisposeAsync(); |
| 29 | + |
24 | 30 | protected UiTestBase(IDictionary<string, string?>? seedConfig = null) |
25 | 31 | { |
26 | 32 | // Build a minimal in-memory configuration; seed values if needed. |
@@ -55,26 +61,19 @@ protected IRenderedComponent<TComponent> RenderComponentWithMudProviders<TCompon |
55 | 61 | Action<ComponentParameterCollectionBuilder<TComponent>> parameterBuilder) |
56 | 62 | where TComponent : class, IComponent |
57 | 63 | { |
58 | | - // Build params -> fragment that renders <TComponent ... /> |
59 | | - var fragment = new ComponentParameterCollectionBuilder<TComponent>(parameterBuilder) |
60 | | - .Build() |
61 | | - .ToRenderFragment<TComponent>(); |
62 | | - |
63 | | - var shell = RenderComponent<MudTestHost>(host => host.Add(h => h.ChildContent, fragment)); |
| 64 | + var shell = Render<MudTestHost>(host => host.Add<TComponent>(h => h.ChildContent, parameterBuilder)); |
64 | 65 | return shell.FindComponent<TComponent>(); |
65 | 66 | } |
66 | 67 |
|
67 | 68 | protected IRenderedComponent<TComponent> RenderComponentWithMudProviders<TComponent>( |
68 | 69 | params (string Name, object? Value)[] parameters) |
69 | 70 | where TComponent : class, IComponent |
70 | 71 | { |
71 | | - var builder = new ComponentParameterCollectionBuilder<TComponent>(); |
72 | | - foreach (var (name, value) in parameters) |
73 | | - builder.TryAdd(name, value); // name/value fallback |
74 | | - |
75 | | - var fragment = builder.Build().ToRenderFragment<TComponent>(); |
76 | | - |
77 | | - var shell = RenderComponent<MudTestHost>(host => host.Add(h => h.ChildContent, fragment)); |
| 72 | + var shell = Render<MudTestHost>(host => host.Add<TComponent>(h => h.ChildContent, child => |
| 73 | + { |
| 74 | + foreach (var (name, value) in parameters) |
| 75 | + child.TryAdd(name, value); |
| 76 | + })); |
78 | 77 | return shell.FindComponent<TComponent>(); |
79 | 78 | } |
80 | 79 |
|
@@ -137,7 +136,7 @@ public override Task<AuthenticationState> GetAuthenticationStateAsync() |
137 | 136 |
|
138 | 137 | public event AuthenticationStateChangedHandler? AuthenticationStateChanged; |
139 | 138 |
|
140 | | - public void NotifyAuthenticationStateChanged(Task<AuthenticationState> task) |
| 139 | + public new void NotifyAuthenticationStateChanged(Task<AuthenticationState> task) |
141 | 140 | { |
142 | 141 | AuthenticationStateChanged?.Invoke(task); |
143 | 142 | } |
|
0 commit comments