Added web module tests

This commit is contained in:
2025-01-19 16:00:33 +01:00
parent 8d63910aae
commit 80aa650a2c
26 changed files with 587 additions and 71 deletions

View File

@@ -0,0 +1,74 @@
using Bunit;
using Bunit.TestDoubles;
using HopFrame.Core.Config;
using HopFrame.Core.Services;
using HopFrame.Web;
using HopFrame.Web.Components.Layout;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.FluentUI.AspNetCore.Components;
using Moq;
namespace HopFrame.Tests.Web.Components.Layout;
public class HopFrameLayoutTests : TestContext {
[Fact]
public void Renders_HopFrameLayout_Components() {
// Arrange
var authHandlerMock = new Mock<IHopFrameAuthHandler>();
var config = new HopFrameConfig {
DisplayUserInfo = true,
BasePolicy = "SomePolicy",
LoginPageRewrite = "/login"
};
authHandlerMock.Setup(h => h.IsAuthenticatedAsync("SomePolicy"))
.ReturnsAsync(true);
Services.AddSingleton(authHandlerMock.Object);
Services.AddHopFrame(config);
JSInterop.Mode = JSRuntimeMode.Loose;
// Act
var cut = RenderComponent<HopFrameLayout>();
// Assert
var header = cut.FindComponent<FluentHeader>();
Assert.NotNull(header);
var navigation = cut.FindComponent<HopFrameNavigation>();
Assert.NotNull(navigation);
var sideMenu = cut.FindComponent<HopFrameSideMenu>();
Assert.NotNull(sideMenu);
var footer = cut.FindComponent<FluentFooter>();
Assert.NotNull(footer);
}
[Fact]
public void Redirects_To_Login_When_Not_Authorized() {
// Arrange
var authHandlerMock = new Mock<IHopFrameAuthHandler>();
var navMock = new FakeNavigationManager(this);
var config = new HopFrameConfig {
DisplayUserInfo = true,
BasePolicy = "SomePolicy",
LoginPageRewrite = "/login"
};
authHandlerMock.Setup(h => h.IsAuthenticatedAsync("SomePolicy"))
.ReturnsAsync(false);
Services.AddSingleton(navMock);
Services.AddHopFrame(config);
JSInterop.Mode = JSRuntimeMode.Loose;
// Act
var cut = RenderComponent<HopFrameLayout>();
// Assert
// TODO: check if uri matches
}
}

View File

@@ -0,0 +1,68 @@
using Bunit;
using HopFrame.Core.Config;
using HopFrame.Core.Services;
using HopFrame.Web;
using HopFrame.Web.Components.Layout;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.FluentUI.AspNetCore.Components;
using Moq;
namespace HopFrame.Tests.Web.Components.Layout;
public class HopFrameNavigationTests : TestContext {
[Fact]
public void Renders_HopFrameNavigation_Components() {
// Arrange
var authHandlerMock = new Mock<IHopFrameAuthHandler>();
var config = new HopFrameConfig {
DisplayUserInfo = true
};
authHandlerMock.Setup(h => h.GetCurrentUserDisplayNameAsync())
.ReturnsAsync("John Doe");
Services.AddSingleton(authHandlerMock.Object);
Services.AddHopFrame(config);
JSInterop.Mode = JSRuntimeMode.Loose;
// Act
var cut = RenderComponent<HopFrameNavigation>();
// Assert
var header = cut.FindComponent<FluentHeader>();
Assert.NotNull(header);
var persona = cut.FindComponent<FluentPersona>();
Assert.NotNull(persona);
Assert.Equal("John Doe", persona.Instance.Name);
Assert.Equal("JD", persona.Instance.Initials);
}
[Fact]
public void Renders_HopFrameNavigation_WithoutPersona() {
// Arrange
var authHandlerMock = new Mock<IHopFrameAuthHandler>();
var config = new HopFrameConfig {
DisplayUserInfo = false
};
authHandlerMock.Setup(h => h.GetCurrentUserDisplayNameAsync())
.ReturnsAsync("John Doe");
Services.AddSingleton(authHandlerMock.Object);
Services.AddHopFrame(config);
JSInterop.Mode = JSRuntimeMode.Loose;
// Act
var cut = RenderComponent<HopFrameNavigation>();
// Assert
var header = cut.FindComponent<FluentHeader>();
Assert.NotNull(header);
Assert.False(cut.HasComponent<FluentPersona>());
authHandlerMock.Verify(h => h.GetCurrentUserDisplayNameAsync(), Times.Never);
}
}

View File

@@ -0,0 +1,49 @@
using HopFrame.Core.Config;
using HopFrame.Core.Services;
using Moq;
using Bunit;
using HopFrame.Tests.Web.Models;
using HopFrame.Web;
using HopFrame.Web.Components.Layout;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.FluentUI.AspNetCore.Components;
namespace HopFrame.Tests.Web.Components.Layout;
public class HopFrameSideMenuTests : TestContext {
[Fact]
public void Renders_FluentAppBar_Components() {
// Arrange
var contextExplorerMock = new Mock<IContextExplorer>();
var authHandlerMock = new Mock<IHopFrameAuthHandler>();
var contextConfig = new DbContextConfig(typeof(MyDbContext));
var tableConfigs = new List<TableConfig> {
new (contextConfig, typeof(MyTable), "Table1", 0),
new (contextConfig, typeof(MyTable2), "Table2", 1)
};
var config = new HopFrameConfig {
Contexts = { contextConfig }
};
contextExplorerMock.Setup(e => e.GetTables()).Returns(tableConfigs);
authHandlerMock.Setup(h => h.IsAuthenticatedAsync(null))
.ReturnsAsync(true);
Services.AddSingleton(contextExplorerMock.Object);
Services.AddSingleton(authHandlerMock.Object);
Services.AddHopFrame(config);
JSInterop.Mode = JSRuntimeMode.Loose;
// Act
var cut = RenderComponent<HopFrameSideMenu>();
// Assert
var items = cut.FindComponents<FluentAppBarItem>();
Assert.Equal(tableConfigs.Count + 1, items.Count);
Assert.Contains(items, item => item.Instance.Text.Equals("Table1"));
Assert.Contains(items, item => item.Instance.Text.Equals("Table2"));
}
}