Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions EstateManagementUI.BlazorServer.Tests/Pages/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ protected BaseTest() {
this.Services.AddSingleton(this._mockPermissionStore.Object);


// Add required permission components
this.ComponentFactories.AddStub<RequirePermission>();
this.ComponentFactories.AddStub<RequireSectionAccess>();
// Add required permission components that render their children
this.ComponentFactories.AddStub<RequirePermission>(
parameters => parameters.Get(p => p.ChildContent));
this.ComponentFactories.AddStub<RequireSectionAccess>(
parameters => parameters.Get(p => p.ChildContent));

var claims = new[] { new Claim(ClaimTypes.Role, "Estate"), new Claim("estateId", Guid.NewGuid().ToString()), new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", "EstateUser") };
this.AddTestAuthorization().SetClaims(claims);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using AngleSharp.Dom;
using Bunit;
using EstateManagementUI.BlazorServer.Components.Permissions;
using EstateManagementUI.BlazorServer.Permissions;
Expand All @@ -20,7 +21,7 @@ public class ContractsIndexPageTests : BaseTest
public void ContractsIndex_RendersCorrectly()
{
// Arrange
var contracts = new List<ContractModel>
List<ContractModel> contracts = new List<ContractModel>
{
new ContractModel
{
Expand All @@ -36,7 +37,7 @@ public void ContractsIndex_RendersCorrectly()
.ReturnsAsync(Result.Success(contracts));

// Act
var cut = RenderComponent<ContractsIndex>();
IRenderedComponent<ContractsIndex> cut = RenderComponent<ContractsIndex>();

// Assert
cut.Markup.ShouldContain("Contract Management");
Expand All @@ -46,23 +47,22 @@ public void ContractsIndex_RendersCorrectly()
public void ContractsIndex_WithNoContracts_ShowsEmptyState()
{
// Arrange
var emptyList = new List<ContractModel>();
List<ContractModel> emptyList = new List<ContractModel>();
_mockMediator.Setup(x => x.Send(It.IsAny<ContractQueries.GetContractsQuery>(), default))
.ReturnsAsync(Result.Success(emptyList));

// Act
var cut = RenderComponent<ContractsIndex>();
cut.WaitForState(() => !cut.Markup.Contains("animate-spin"));
IRenderedComponent<ContractsIndex> cut = RenderComponent<ContractsIndex>();

// Assert
cut.Markup.ShouldContain("No contracts found");
cut.WaitForAssertion(() => cut.Markup.ShouldContain("No contracts found"), timeout: TimeSpan.FromSeconds(5));
}

[Fact]
public void ContractsIndex_WithContracts_DisplaysContractList()
{
// Arrange
var contracts = new List<ContractModel>
List<ContractModel> contracts = new List<ContractModel>
{
new ContractModel
{
Expand All @@ -86,11 +86,10 @@ public void ContractsIndex_WithContracts_DisplaysContractList()
.ReturnsAsync(Result.Success(contracts));

// Act
var cut = RenderComponent<ContractsIndex>();
cut.WaitForState(() => !cut.Markup.Contains("animate-spin"), TimeSpan.FromSeconds(5));
IRenderedComponent<ContractsIndex> cut = RenderComponent<ContractsIndex>();

// Assert
cut.Markup.ShouldContain("Contract 1");
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Contract 1"), timeout: TimeSpan.FromSeconds(5));
cut.Markup.ShouldContain("Contract 2");
cut.Markup.ShouldContain("Operator A");
cut.Markup.ShouldContain("Operator B");
Expand All @@ -100,7 +99,7 @@ public void ContractsIndex_WithContracts_DisplaysContractList()
public void ContractsIndex_DisplaysProductCount()
{
// Arrange
var contracts = new List<ContractModel>
List<ContractModel> contracts = new List<ContractModel>
{
new ContractModel
{
Expand All @@ -120,11 +119,10 @@ public void ContractsIndex_DisplaysProductCount()
.ReturnsAsync(Result.Success(contracts));

// Act
var cut = RenderComponent<ContractsIndex>();
cut.WaitForState(() => !cut.Markup.Contains("animate-spin"), TimeSpan.FromSeconds(5));
IRenderedComponent<ContractsIndex> cut = RenderComponent<ContractsIndex>();

// Assert
cut.Markup.ShouldContain("Product(s)");
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Product(s)"), timeout: TimeSpan.FromSeconds(5));
}

[Fact]
Expand All @@ -135,10 +133,94 @@ public void ContractsIndex_HasCorrectPageTitle()
.ReturnsAsync(Result.Success(new List<ContractModel>()));

// Act
var cut = RenderComponent<ContractsIndex>();
IRenderedComponent<ContractsIndex> cut = RenderComponent<ContractsIndex>();

// Assert
var pageTitle = cut.FindComponent<Microsoft.AspNetCore.Components.Web.PageTitle>();
IRenderedComponent<Microsoft.AspNetCore.Components.Web.PageTitle> pageTitle = cut.FindComponent<Microsoft.AspNetCore.Components.Web.PageTitle>();
pageTitle.Instance.ChildContent.ShouldNotBeNull();
}

[Fact]
public void ContractsIndex_AddNewContractButton_NavigatesToNewContractPage()
{
// Arrange
_mockMediator.Setup(x => x.Send(It.IsAny<ContractQueries.GetContractsQuery>(), default))
.ReturnsAsync(Result.Success(new List<ContractModel>()));

IRenderedComponent<ContractsIndex> cut = RenderComponent<ContractsIndex>();
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Contract Management"), timeout: TimeSpan.FromSeconds(5));

// Act - Find and click the "Add New Contract" button
IElement addButton = cut.Find("#newContractButton");
addButton.Click();

// Assert
_fakeNavigationManager.Uri.ShouldContain("/contracts/new");
}

[Fact]
public void ContractsIndex_ViewButton_NavigatesToContractDetails()
{
// Arrange
Guid contractId = Guid.NewGuid();
List<ContractModel> contracts = new List<ContractModel>
{
new ContractModel
{
ContractId = contractId,
Description = "Test Contract",
OperatorName = "Test Operator",
OperatorId = Guid.NewGuid(),
Products = new List<ContractProductModel>()
}
};

_mockMediator.Setup(x => x.Send(It.IsAny<ContractQueries.GetContractsQuery>(), default))
.ReturnsAsync(Result.Success(contracts));

IRenderedComponent<ContractsIndex> cut = RenderComponent<ContractsIndex>();
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Test Contract"), timeout: TimeSpan.FromSeconds(5));

// Act - Find and click the "View" button
IRefreshableElementCollection<IElement> buttons = cut.FindAll("button");
IElement? viewButton = buttons.FirstOrDefault(b => b.TextContent.Contains("View"));
viewButton.ShouldNotBeNull();
viewButton.Click();

// Assert
_fakeNavigationManager.Uri.ShouldContain($"/contracts/{contractId}");
}

[Fact]
public void ContractsIndex_EditButton_NavigatesToEditContractPage()
{
// Arrange
Guid contractId = Guid.NewGuid();
List<ContractModel> contracts = new List<ContractModel>
{
new ContractModel
{
ContractId = contractId,
Description = "Test Contract",
OperatorName = "Test Operator",
OperatorId = Guid.NewGuid(),
Products = new List<ContractProductModel>()
}
};

_mockMediator.Setup(x => x.Send(It.IsAny<ContractQueries.GetContractsQuery>(), default))
.ReturnsAsync(Result.Success(contracts));

IRenderedComponent<ContractsIndex> cut = RenderComponent<ContractsIndex>();
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Test Contract"), timeout: TimeSpan.FromSeconds(5));

// Act - Find and click the "Edit" button
IRefreshableElementCollection<IElement> buttons = cut.FindAll("button");
IElement? editButton = buttons.FirstOrDefault(b => b.TextContent.Contains("Edit"));
editButton.ShouldNotBeNull();
editButton.Click();

// Assert
_fakeNavigationManager.Uri.ShouldContain($"/contracts/{contractId}/edit");
}
}
Loading