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,63 @@
using Bunit;
using HopFrame.Core.Config;
using HopFrame.Core.Services;
using HopFrame.Tests.Web.Models;
using HopFrame.Web;
using HopFrame.Web.Components.Dialogs;
using HopFrame.Web.Models;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.FluentUI.AspNetCore.Components;
using Moq;
namespace HopFrame.Tests.Web.Components.Dialogs;
public class HopFrameEditorTests : TestContext {
[Fact]
public void Renders_Properties_Correctly() {
// Arrange
var contextExplorerMock = new Mock<IContextExplorer>();
var authHandlerMock = new Mock<IHopFrameAuthHandler>();
var dialogServiceMock = new Mock<IDialogService>();
var toastServiceMock = new Mock<IToastService>();
var serviceProviderMock = new Mock<IServiceProvider>();
var contextConfig = new DbContextConfig(typeof(MyDbContext));
var tableConfig = new TableConfig(contextConfig, typeof(MyTable), "Table1", 0) {
DisplayName = "Table1",
ViewPolicy = "Policy1"
};
contextConfig.Tables.Add(tableConfig);
var config = new HopFrameConfig() {
Contexts = { contextConfig }
};
contextExplorerMock.Setup(e => e.GetTable("Table1")).Returns(tableConfig);
contextExplorerMock.Setup(e => e.GetTableManager("Table1")).Returns(Mock.Of<ITableManager>());
authHandlerMock.Setup(h => h.IsAuthenticatedAsync(It.IsAny<string>())).ReturnsAsync(true);
Services.AddHopFrame(config);
Services.AddSingleton(contextExplorerMock.Object);
Services.AddSingleton(authHandlerMock.Object);
Services.AddSingleton(dialogServiceMock.Object);
Services.AddSingleton(toastServiceMock.Object);
Services.AddSingleton(serviceProviderMock.Object);
JSInterop.Mode = JSRuntimeMode.Loose;
var dialogData = new EditorDialogData(tableConfig, new MyTable());
var dialog = new FluentDialog() {
Instance = new DialogInstance(typeof(HopFrameEditor), new DialogParameters(), dialogData)
};
// Act
var cut = RenderComponent<HopFrameEditor>(parameters => parameters
.Add(p => p.Content, dialogData)
.Add(p => p.Dialog, dialog));
// Assert
var dialogBody = cut.FindComponent<FluentDialogBody>();
Assert.NotNull(dialogBody);
var textFields = cut.FindComponents<FluentNumberField<double>>();
Assert.Single(textFields);
}
}

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"));
}
}

View File

@@ -0,0 +1,62 @@
using Bunit;
using HopFrame.Core.Config;
using HopFrame.Core.Services;
using HopFrame.Tests.Web.Models;
using HopFrame.Web;
using HopFrame.Web.Components.Pages;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.FluentUI.AspNetCore.Components;
using Moq;
namespace HopFrame.Tests.Web.Components.Pages;
public class HopFrameHomeTests : TestContext {
[Fact]
public void Renders_Table_Cards_Correctly() {
// Arrange
var contextExplorerMock = new Mock<IContextExplorer>();
var authHandlerMock = new Mock<IHopFrameAuthHandler>();
var contextConfig = new DbContextConfig(typeof(MyDbContext));
var tableConfigs = new List<TableConfig> {
new TableConfig(contextConfig, typeof(MyTable), "Table1", 0) {
DisplayName = "Table1",
ViewPolicy = "Policy1",
Description = "Description1"
},
new TableConfig(contextConfig, typeof(MyTable2), "Table2", 1) {
DisplayName = "Table2",
ViewPolicy = "Policy2",
Description = "Description2"
}
};
contextConfig.Tables.AddRange(tableConfigs);
var config = new HopFrameConfig() {
Contexts = { contextConfig }
};
contextExplorerMock.Setup(e => e.GetTables()).Returns(tableConfigs);
authHandlerMock.Setup(h => h.IsAuthenticatedAsync(It.IsAny<string>()))
.ReturnsAsync(true);
Services.AddHopFrame(config);
Services.AddSingleton(contextExplorerMock.Object);
Services.AddSingleton(authHandlerMock.Object);
// Act
var cut = RenderComponent<HopFrameHome>();
// Assert
var cards = cut.FindComponents<FluentCard>();
Assert.Equal(2, cards.Count);
Assert.Contains(cards, card => card.Markup.Contains("Table1"));
Assert.Contains(cards, card => card.Markup.Contains("Description1"));
Assert.Contains(cards, card => card.Markup.Contains("Policy1"));
Assert.Contains(cards, card => card.Markup.Contains("Open"));
Assert.Contains(cards, card => card.Markup.Contains("Table2"));
Assert.Contains(cards, card => card.Markup.Contains("Description2"));
Assert.Contains(cards, card => card.Markup.Contains("Policy2"));
Assert.Contains(cards, card => card.Markup.Contains("Open"));
}
}

View File

@@ -0,0 +1,96 @@
using Bunit;
using HopFrame.Core.Config;
using HopFrame.Core.Services;
using HopFrame.Tests.Web.Helpers;
using HopFrame.Tests.Web.Models;
using HopFrame.Web;
using HopFrame.Web.Components.Pages;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.FluentUI.AspNetCore.Components;
using Microsoft.JSInterop;
using Moq;
namespace HopFrame.Tests.Web.Components.Pages;
public class HopFrameTablePageTests : TestContext {
[Fact]
public void Renders_Table_Correctly() {
// Arrange
var contextExplorerMock = new Mock<IContextExplorer>();
var authHandlerMock = new Mock<IHopFrameAuthHandler>();
var dialogServiceMock = new Mock<IDialogService>();
var contextConfig = new DbContextConfig(typeof(MyDbContext));
var managerMock = new Mock<ITableManager>();
var tableConfig = new TableConfig(contextConfig, typeof(MyTable), "Table1", 0) {
DisplayName = "Table1",
ViewPolicy = "Policy1"
};
contextConfig.Tables.Add(tableConfig);
var config = new HopFrameConfig() {
Contexts = { contextConfig }
};
contextExplorerMock.Setup(e => e.GetTable("Table1")).Returns(tableConfig);
contextExplorerMock.Setup(e => e.GetTableManager("Table1")).Returns(managerMock.Object);
authHandlerMock.Setup(h => h.IsAuthenticatedAsync(It.IsAny<string>())).ReturnsAsync(true);
managerMock.Setup(m => m.LoadPage(It.IsAny<int>(), It.IsAny<int>())).Returns(Enumerable.Empty<object>().AsAsyncQueryable());
Services.AddHopFrame(config);
Services.AddSingleton(contextExplorerMock.Object);
Services.AddSingleton(authHandlerMock.Object);
Services.AddSingleton(dialogServiceMock.Object);
JSInterop.Mode = JSRuntimeMode.Loose;
// Act
var cut = RenderComponent<HopFrameTablePage>(parameters => parameters
.Add(p => p.TableDisplayName, "Table1"));
// Assert
var toolbar = cut.Find("fluent-toolbar");
Assert.NotNull(toolbar);
var searchBox = cut.Find("fluent-search");
Assert.NotNull(searchBox);
var dataGrid = cut.FindComponent<FluentDataGrid<object>>();
Assert.NotNull(dataGrid);
}
[Fact]
public void Displays_Properties_Correctly() {
// Arrange
var contextExplorerMock = new Mock<IContextExplorer>();
var authHandlerMock = new Mock<IHopFrameAuthHandler>();
var dialogServiceMock = new Mock<IDialogService>();
var contextConfig = new DbContextConfig(typeof(MyDbContext));
var tableConfig = new TableConfig(contextConfig, typeof(MyTable), "Table1", 0) {
DisplayName = "Table1",
ViewPolicy = "Policy1"
};
var tableManagerMock = new Mock<ITableManager>();
var items = new List<object> { new MyTable(), new MyTable() };
tableManagerMock.Setup(m => m.LoadPage(It.IsAny<int>(), It.IsAny<int>())).Returns(items.AsAsyncQueryable());
contextExplorerMock.Setup(e => e.GetTable("Table1")).Returns(tableConfig);
contextExplorerMock.Setup(e => e.GetTableManager("Table1")).Returns(tableManagerMock.Object);
authHandlerMock.Setup(h => h.IsAuthenticatedAsync(It.IsAny<string>())).ReturnsAsync(true);
Services.AddHopFrame(new HopFrameConfig());
Services.AddSingleton(contextExplorerMock.Object);
Services.AddSingleton(authHandlerMock.Object);
Services.AddSingleton(dialogServiceMock.Object);
JSInterop.Mode = JSRuntimeMode.Loose;
// Act
var cut = RenderComponent<HopFrameTablePage>(parameters => parameters
.Add(p => p.TableDisplayName, "Table1"));
// Assert
var dataGridItems = cut.FindComponents<PropertyColumn<object, string>>();
Assert.Single(dataGridItems);
Assert.Equal(nameof(MyTable.Id), dataGridItems[0].Instance.Title);
}
}

View File

@@ -0,0 +1,39 @@
using System.Collections;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Query.Internal;
namespace HopFrame.Tests.Web.Helpers;
public static class EnumerableExtensions {
public static AsyncQueryable AsAsyncQueryable(this IEnumerable<object> enumerable) {
return new AsyncQueryable(enumerable);
}
public class AsyncQueryable(IEnumerable<object> enumerable) : IQueryable<object>, IAsyncEnumerable<object> {
public IEnumerator<object> GetEnumerator() {
return enumerable.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
public Type ElementType { get; } = typeof(object);
public Expression Expression { get; }
public IQueryProvider Provider { get; }
public IAsyncEnumerator<object> GetAsyncEnumerator(CancellationToken cancellationToken = new CancellationToken()) {
return Enumerate().GetAsyncEnumerator(cancellationToken);
}
private async IAsyncEnumerable<object> Enumerate() {
foreach (var o in enumerable) {
yield return o;
}
}
}
}

View File

@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="bunit" Version="1.38.5" />
<PackageReference Include="coverlet.collector" Version="6.0.2"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.9.2"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2"/>
</ItemGroup>
<ItemGroup>
<Using Include="Xunit"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\HopFrame.Core\HopFrame.Core.csproj" />
<ProjectReference Include="..\..\src\HopFrame.Web\HopFrame.Web.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,12 @@
using Microsoft.EntityFrameworkCore;
namespace HopFrame.Tests.Web.Models;
public class MyDbContext : DbContext {
public DbSet<MyTable> Table1 { get; set; }
public DbSet<MyTable2> Table2 { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
optionsBuilder.UseInMemoryDatabase(nameof(MyDbContext));
}
}

View File

@@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace HopFrame.Tests.Web.Models;
public class MyTable {
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
}
public class MyTable2 {
[Key]
public string Id { get; set; }
}