KoogleApp/test/GoodWood.Tests/Components/HomePageTests.cs

141 lines
4.8 KiB
C#

using Bunit;
using Bunit.TestDoubles;
using Fluxor;
using GoodWood.Application.DTOs;
using GoodWood.Application.Interfaces;
using GoodWood.Web.Store.AuthState;
using GoodWood.Web.Store.ClubState;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using MudBlazor;
using MudBlazor.Services;
using System.Security.Claims;
namespace GoodWood.Tests.Components;
/// <summary>
/// Tests for the Home page component (now redirects to Dashboard).
/// </summary>
public class HomePageTests : IDisposable
{
private readonly TestContext _ctx;
private readonly Mock<IUserService> _userServiceMock;
private readonly Mock<ICurrentClubContext> _clubContextMock;
private readonly Mock<IDispatcher> _dispatcherMock;
private readonly Mock<IState<AuthState>> _authStateMock;
private readonly Mock<IState<ClubState>> _clubStateMock;
private readonly Mock<IAuthorizationService> _authorizationServiceMock;
public HomePageTests()
{
_ctx = new TestContext();
_userServiceMock = new Mock<IUserService>();
_clubContextMock = new Mock<ICurrentClubContext>();
_dispatcherMock = new Mock<IDispatcher>();
// Setup default club context
_clubContextMock.Setup(c => c.ClubId).Returns(Guid.NewGuid());
_clubContextMock.Setup(c => c.ClubName).Returns("Test Club");
// Setup Fluxor states
_authStateMock = new Mock<IState<AuthState>>();
_authStateMock.Setup(s => s.Value).Returns(AuthState.Initial);
_clubStateMock = new Mock<IState<ClubState>>();
_clubStateMock.Setup(s => s.Value).Returns(ClubState.Initial);
// Setup authorization service mock
_authorizationServiceMock = new Mock<IAuthorizationService>();
_authorizationServiceMock
.Setup(s => s.AuthorizeAsync(
It.IsAny<ClaimsPrincipal>(),
It.IsAny<object?>(),
It.IsAny<string>()))
.ReturnsAsync(AuthorizationResult.Success());
// Add test authorization first before any other services
_ctx.AddTestAuthorization().SetAuthorized("testuser");
// Register all services
_ctx.Services.AddMudServices(options =>
{
options.SnackbarConfiguration.PositionClass = Defaults.Classes.Position.BottomRight;
});
_ctx.Services.AddSingleton(_userServiceMock.Object);
_ctx.Services.AddSingleton(_clubContextMock.Object);
_ctx.Services.AddSingleton(_dispatcherMock.Object);
_ctx.Services.AddSingleton(_authStateMock.Object);
_ctx.Services.AddSingleton(_clubStateMock.Object);
_ctx.Services.AddSingleton(_authorizationServiceMock.Object);
// Additional Fluxor services
_ctx.Services.AddSingleton(new Mock<IActionSubscriber>().Object);
_ctx.Services.AddSingleton(new Mock<IStore>().Object);
// ASP.NET Core services
var httpContextAccessorMock = new Mock<IHttpContextAccessor>();
httpContextAccessorMock.Setup(x => x.HttpContext).Returns(new DefaultHttpContext());
_ctx.Services.AddSingleton(httpContextAccessorMock.Object);
// Stub complex components
_ctx.ComponentFactories.AddStub<GoodWood.Web.Components.Shared.LogoutButton>();
}
public void Dispose()
{
_ctx.Dispose();
}
[Fact]
public void HomePage_RedirectsToDashboard_WhenAuthorized()
{
// Arrange
_authStateMock.Setup(s => s.Value).Returns(CreateAuthState());
// Act
_ctx.RenderComponent<GoodWood.Web.Components.Pages.Home>();
// Assert - Home page should redirect to Dashboard
var navMan = _ctx.Services.GetRequiredService<FakeNavigationManager>();
navMan.Uri.Should().EndWith("/dashboard");
}
[Fact]
public void HomePage_RendersNoContent_BecauseItRedirects()
{
// Arrange
_authStateMock.Setup(s => s.Value).Returns(CreateAuthState());
// Act
var cut = _ctx.RenderComponent<GoodWood.Web.Components.Pages.Home>();
// Assert - Home page renders no visible content since it redirects
cut.Markup.Should().BeEmpty();
}
private static AuthState CreateAuthState(string displayName = "Test User")
{
return AuthState.Initial with
{
IsAuthenticated = true,
IsLoading = false,
CurrentUser = new UserDto
{
ProfileId = Guid.NewGuid(),
IdentityUserId = Guid.NewGuid(),
DisplayName = displayName,
Identity = new ApplicationUserDto
{
Id = Guid.NewGuid(),
Email = "test@example.com",
UserName = "test@example.com"
}
},
Roles = [],
Error = null
};
}
}