Added web module tests
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user