Skip to content

Commit e10f5e2

Browse files
committed
fix: Moves cascading authentication state to service container (closes #1774)
1 parent de452e6 commit e10f5e2

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ All notable changes to **bUnit** will be documented in this file. The project ad
66

77
## [Unreleased]
88

9+
### Changed
10+
- Registering `AuthenticationState` in the services container rather than as part of the RenderTree. Fixes [#1774](https://github.com/bUnit-dev/bUnit/issues/1774) reported by [@aayjaychan](https://github.com/aayjaychan).
11+
912
## [2.0.66] - 2025-11-11
1013

1114
This major release focuses on platform updates and API simplifications.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Bunit;
2+
3+
[AttributeUsage(AttributeTargets.All, Inherited = false)]
4+
internal sealed class RemovedInFutureVersionAttribute(string todo) : Attribute
5+
{
6+
public string Todo { get; } = todo;
7+
}

src/bunit/TestDoubles/Authorization/BunitAuthorizationExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ public partial class BunitContext
1111
/// an authenticated user, as well as adding the <see cref="CascadingAuthenticationState"/> component to the
1212
/// test contexts render tree.
1313
/// </summary>
14+
[RemovedInFutureVersion("SignOutSessionStateManager should be removed from the container.")]
1415
public BunitAuthorizationContext AddAuthorization()
1516
{
16-
RenderTree.TryAdd<CascadingAuthenticationState>();
17+
Services.AddCascadingAuthenticationState();
1718
Services.AddSingleton<BunitSignOutSessionStateManager>();
1819
#pragma warning disable CS0618
1920
Services.AddSingleton<SignOutSessionStateManager>(s => s.GetRequiredService<BunitSignOutSessionStateManager>());

src/bunit/TestDoubles/NavigationManager/BunitSignOutSessionStateManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Bunit.TestDoubles;
77
/// that will help later to assert if the user was logged out
88
/// </summary>
99
#pragma warning disable CS0618
10+
[RemovedInFutureVersion("SignOutSessionStateManager is obsolete")]
1011
public class BunitSignOutSessionStateManager : SignOutSessionStateManager
1112
#pragma warning restore CS0618
1213
{

tests/bunit.tests/TestDoubles/Components/StubTest.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Microsoft.AspNetCore.Components.Authorization;
2+
13
namespace Bunit.TestDoubles.Components;
24

35
public class StubTest : BunitContext
@@ -25,4 +27,21 @@ public void Test002(string header, string attrValue)
2527
ps => ps.ShouldContain(x => x.Key == nameof(Simple1.AttrValue) && attrValue.Equals(x.Value)),
2628
ps => ps.Count.ShouldBe(2));
2729
}
30+
31+
[Fact(DisplayName = "Stubbing everything except cut keeps cascading AuthenticationState")]
32+
public void Test003()
33+
{
34+
AddAuthorization();
35+
ComponentFactories.AddStub(c => c != typeof(AuthTestComponent));
36+
37+
var cut = Render<AuthTestComponent>();
38+
39+
cut.Instance.Auth.ShouldNotBeNull();
40+
}
41+
42+
private sealed class AuthTestComponent : ComponentBase
43+
{
44+
[CascadingParameter]
45+
public Task<AuthenticationState> Auth { get; set; } = null!;
46+
}
2847
}

0 commit comments

Comments
 (0)