diff --git a/EstateManagementUI.BlazorServer.Tests/Pages/BaseTest.cs b/EstateManagementUI.BlazorServer.Tests/Pages/BaseTest.cs index 930d4ddd..7f8b9676 100644 --- a/EstateManagementUI.BlazorServer.Tests/Pages/BaseTest.cs +++ b/EstateManagementUI.BlazorServer.Tests/Pages/BaseTest.cs @@ -33,9 +33,11 @@ protected BaseTest() { this.Services.AddSingleton(this._mockPermissionStore.Object); - // Add required permission components - this.ComponentFactories.AddStub(); - this.ComponentFactories.AddStub(); + // Add required permission components that render their children + this.ComponentFactories.AddStub( + parameters => parameters.Get(p => p.ChildContent)); + this.ComponentFactories.AddStub( + 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); diff --git a/EstateManagementUI.BlazorServer.Tests/Pages/Contracts/ContractsIndexPageTests.cs b/EstateManagementUI.BlazorServer.Tests/Pages/Contracts/ContractsIndexPageTests.cs index eb65e945..4714fc33 100644 --- a/EstateManagementUI.BlazorServer.Tests/Pages/Contracts/ContractsIndexPageTests.cs +++ b/EstateManagementUI.BlazorServer.Tests/Pages/Contracts/ContractsIndexPageTests.cs @@ -1,3 +1,4 @@ +using AngleSharp.Dom; using Bunit; using EstateManagementUI.BlazorServer.Components.Permissions; using EstateManagementUI.BlazorServer.Permissions; @@ -20,7 +21,7 @@ public class ContractsIndexPageTests : BaseTest public void ContractsIndex_RendersCorrectly() { // Arrange - var contracts = new List + List contracts = new List { new ContractModel { @@ -36,7 +37,7 @@ public void ContractsIndex_RendersCorrectly() .ReturnsAsync(Result.Success(contracts)); // Act - var cut = RenderComponent(); + IRenderedComponent cut = RenderComponent(); // Assert cut.Markup.ShouldContain("Contract Management"); @@ -46,23 +47,22 @@ public void ContractsIndex_RendersCorrectly() public void ContractsIndex_WithNoContracts_ShowsEmptyState() { // Arrange - var emptyList = new List(); + List emptyList = new List(); _mockMediator.Setup(x => x.Send(It.IsAny(), default)) .ReturnsAsync(Result.Success(emptyList)); // Act - var cut = RenderComponent(); - cut.WaitForState(() => !cut.Markup.Contains("animate-spin")); + IRenderedComponent cut = RenderComponent(); // 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 + List contracts = new List { new ContractModel { @@ -86,11 +86,10 @@ public void ContractsIndex_WithContracts_DisplaysContractList() .ReturnsAsync(Result.Success(contracts)); // Act - var cut = RenderComponent(); - cut.WaitForState(() => !cut.Markup.Contains("animate-spin"), TimeSpan.FromSeconds(5)); + IRenderedComponent cut = RenderComponent(); // 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"); @@ -100,7 +99,7 @@ public void ContractsIndex_WithContracts_DisplaysContractList() public void ContractsIndex_DisplaysProductCount() { // Arrange - var contracts = new List + List contracts = new List { new ContractModel { @@ -120,11 +119,10 @@ public void ContractsIndex_DisplaysProductCount() .ReturnsAsync(Result.Success(contracts)); // Act - var cut = RenderComponent(); - cut.WaitForState(() => !cut.Markup.Contains("animate-spin"), TimeSpan.FromSeconds(5)); + IRenderedComponent cut = RenderComponent(); // Assert - cut.Markup.ShouldContain("Product(s)"); + cut.WaitForAssertion(() => cut.Markup.ShouldContain("Product(s)"), timeout: TimeSpan.FromSeconds(5)); } [Fact] @@ -135,10 +133,94 @@ public void ContractsIndex_HasCorrectPageTitle() .ReturnsAsync(Result.Success(new List())); // Act - var cut = RenderComponent(); + IRenderedComponent cut = RenderComponent(); // Assert - var pageTitle = cut.FindComponent(); + IRenderedComponent pageTitle = cut.FindComponent(); pageTitle.Instance.ChildContent.ShouldNotBeNull(); } + + [Fact] + public void ContractsIndex_AddNewContractButton_NavigatesToNewContractPage() + { + // Arrange + _mockMediator.Setup(x => x.Send(It.IsAny(), default)) + .ReturnsAsync(Result.Success(new List())); + + IRenderedComponent cut = RenderComponent(); + 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 contracts = new List + { + new ContractModel + { + ContractId = contractId, + Description = "Test Contract", + OperatorName = "Test Operator", + OperatorId = Guid.NewGuid(), + Products = new List() + } + }; + + _mockMediator.Setup(x => x.Send(It.IsAny(), default)) + .ReturnsAsync(Result.Success(contracts)); + + IRenderedComponent cut = RenderComponent(); + cut.WaitForAssertion(() => cut.Markup.ShouldContain("Test Contract"), timeout: TimeSpan.FromSeconds(5)); + + // Act - Find and click the "View" button + IRefreshableElementCollection 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 contracts = new List + { + new ContractModel + { + ContractId = contractId, + Description = "Test Contract", + OperatorName = "Test Operator", + OperatorId = Guid.NewGuid(), + Products = new List() + } + }; + + _mockMediator.Setup(x => x.Send(It.IsAny(), default)) + .ReturnsAsync(Result.Success(contracts)); + + IRenderedComponent cut = RenderComponent(); + cut.WaitForAssertion(() => cut.Markup.ShouldContain("Test Contract"), timeout: TimeSpan.FromSeconds(5)); + + // Act - Find and click the "Edit" button + IRefreshableElementCollection buttons = cut.FindAll("button"); + IElement? editButton = buttons.FirstOrDefault(b => b.TextContent.Contains("Edit")); + editButton.ShouldNotBeNull(); + editButton.Click(); + + // Assert + _fakeNavigationManager.Uri.ShouldContain($"/contracts/{contractId}/edit"); + } }