diff --git a/EstateManagementUI.BlazorServer.Tests/Pages/ErrorPageTests.cs b/EstateManagementUI.BlazorServer.Tests/Pages/ErrorPageTests.cs index 1a37fc8e..b38d126e 100644 --- a/EstateManagementUI.BlazorServer.Tests/Pages/ErrorPageTests.cs +++ b/EstateManagementUI.BlazorServer.Tests/Pages/ErrorPageTests.cs @@ -1,11 +1,36 @@ using Bunit; using EstateManagementUI.BlazorServer.Components.Pages; +using EstateManagementUI.BlazorServer.Tests.Pages.FileProcessing; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Moq; using Shouldly; namespace EstateManagementUI.BlazorServer.Tests.Pages; -public class ErrorPageTests : TestContext +public class ErrorPageTests : BaseTest { + public ErrorPageTests() :base() { + Mock _mockWebHostEnvironment = new(); + _mockWebHostEnvironment.Setup(x => x.EnvironmentName).Returns(Environments.Development); + + Services.AddSingleton(_mockWebHostEnvironment.Object); + + // Use a real IConfiguration with an in-memory value so GetSection/GetValue work as expected + var inMemorySettings = new Dictionary + { + { "AppSettings:SupportEmail", "support@example.com" } + }; + var configuration = new ConfigurationBuilder() + .AddInMemoryCollection(inMemorySettings) + .Build(); + + Services.AddSingleton(configuration); + + } + [Fact] public void Error_RendersCorrectly() { @@ -13,8 +38,8 @@ public void Error_RendersCorrectly() var cut = RenderComponent(); // Assert - cut.Find("h1").TextContent.ShouldBe("Error."); - cut.Find("h2").TextContent.ShouldBe("An error occurred while processing your request."); + cut.Find("h1").TextContent.ShouldContain("Oops! Something went wrong"); + cut.Markup.ShouldContain("We encountered an unexpected error"); } [Fact] @@ -24,7 +49,7 @@ public void Error_WithoutRequestId_DoesNotShowRequestId() var cut = RenderComponent(); // Assert - cut.FindAll("strong").ShouldNotContain(e => e.TextContent.Contains("Request ID:")); + cut.Markup.ShouldNotContain("Reference ID for support:"); } [Fact] @@ -34,7 +59,7 @@ public void Error_ShowsDevelopmentModeInformation() var cut = RenderComponent(); // Assert - cut.Markup.ShouldContain("Development Mode"); + cut.Markup.ShouldContain("Development Mode Information"); cut.Markup.ShouldContain("ASPNETCORE_ENVIRONMENT"); } @@ -48,4 +73,36 @@ public void Error_HasCorrectPageTitle() var pageTitle = cut.FindComponent(); pageTitle.Instance.ChildContent.ShouldNotBeNull(); } + + [Fact] + public void Error_HasHomePageLink() + { + // Act + var cut = RenderComponent(); + + // Assert + var homeLink = cut.Find("a[href='/']"); + homeLink.TextContent.ShouldContain("Go to Home"); + } + + [Fact] + public void Error_HasBackButton() + { + // Act + var cut = RenderComponent(); + + // Assert + var backButton = cut.Find("button[onclick='window.history.back()']"); + backButton.TextContent.ShouldContain("Go Back"); + } + + [Fact] + public void Error_HasSupportEmail() + { + // Act + var cut = RenderComponent(); + + // Assert + cut.Markup.ShouldContain("support@example.com"); + } } diff --git a/EstateManagementUI.BlazorServer/Components/Pages/Error.razor b/EstateManagementUI.BlazorServer/Components/Pages/Error.razor index 576cc2d2..a327ed60 100644 --- a/EstateManagementUI.BlazorServer/Components/Pages/Error.razor +++ b/EstateManagementUI.BlazorServer/Components/Pages/Error.razor @@ -1,28 +1,103 @@ @page "/Error" @using System.Diagnostics +@inject IConfiguration Configuration +@inject IWebHostEnvironment Environment Error -

Error.

-

An error occurred while processing your request.

+
+
+
+
+ +
+
+ + + +
+
-@if (ShowRequestId) -{ -

- Request ID: @RequestId -

-} + +

Oops! Something went wrong

+ + +

+ We encountered an unexpected error while processing your request. Please try again or contact support if the problem persists. +

+ + @if (ShowRequestId) + { +
+

Reference ID for support:

+ @RequestId +
+ } + + +
+ + + + + Go to Home + + +
+ + +
+

+ Need help? Contact our support team at + @SupportEmail +

+
+
+
-

Development Mode

-

- Swapping to Development environment will display more detailed information about the error that occurred. -

-

- The Development environment shouldn't be enabled for deployed applications. - It can result in displaying sensitive information from exceptions to end users. - For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development - and restarting the app. -

+ + @if (ShowDevelopmentInfo) + { +
+
+
+

+ + + + Development Mode Information +

+
+
+
+
+

+ Note: This information is only visible in development mode. +

+

+ Swapping to Development environment will display more detailed information about errors. +

+

+ Warning: The Development environment shouldn't be enabled for deployed applications. + It can result in displaying sensitive information from exceptions to end users. +

+

+ For local debugging, enable the Development environment by setting the + ASPNETCORE_ENVIRONMENT + environment variable to Development + and restarting the app. +

+
+
+
+ } +
+
@code{ [CascadingParameter] @@ -30,7 +105,13 @@ private string? RequestId { get; set; } private bool ShowRequestId => !string.IsNullOrEmpty(RequestId); + private bool ShowDevelopmentInfo { get; set; } + private string SupportEmail { get; set; } = "support@example.com"; - protected override void OnInitialized() => + protected override void OnInitialized() + { RequestId = Activity.Current?.Id ?? HttpContext?.TraceIdentifier; + ShowDevelopmentInfo = Environment.IsDevelopment(); + SupportEmail = Configuration.GetValue("AppSettings:SupportEmail", "support@example.com") ?? "support@example.com"; + } }