#region ========================================================================= USING ===================================================================================== using Nemera.Presentation.Web.Common.Api; using Nemera.Presentation.Web.Common.DTO.Themes; using Nemera.Presentation.Web.Common.Primitives; using Nemera.Presentation.Web.Common.Routes; using Nemera.Presentation.Web.Core.Themes; using Nemera.Presentation.Web.Fixtures.Common.DTO.Themes; using NSubstitute; using System.Diagnostics.CodeAnalysis; using System.Threading; using System.Threading.Tasks; #endregion namespace Nemera.Presentation.Web.UnitTests.Core.Themes; /// /// Contains unit tests for the class. /// [ExcludeFromCodeCoverage] public class ThemePageRendererTests { private readonly IApiHttpClient _mockApiHttpClient; private readonly ThemePageRenderer _sut; private readonly ThemeResponseDtoFixture _themeResponseDtoFixture = new(); private readonly ThemePageDtoFixture _themePageDtoFixture = new(); private readonly ThemeTemplateResponseDtoFixture _themeTemplateResponseDtoFixture = new(); /// /// Initializes a new instance of the class. /// public ThemePageRendererTests() { _mockApiHttpClient = Substitute.For(); ThemeService themeService = new(_mockApiHttpClient); _sut = new ThemePageRenderer(themeService, new ThemeTemplateEngine()); } [Fact] public async Task RenderAsync_WhenThemeIdProvided_ShouldRenderPageAndPopulateThemeMetadata() { // Arrange ThemeResponseDto theme = _themeResponseDtoFixture.Create(themeId: "editorial-paper"); string pageKey = "home/index"; string template = "

{{title}}

{{#scripts}}{{/scripts}}"; string expectedEndpoint = ApiRoutes.Themes.GET_THEME_TEMPLATE .Replace("{themeId}", "editorial-paper") .Replace("{*pageKey}", pageKey); _mockApiHttpClient.GetAsync(expectedEndpoint, Arg.Any()) .Returns(_themeTemplateResponseDtoFixture.Create(theme: theme, template: template)); ThemePageDto model = _themePageDtoFixture.Create(pageKey: pageKey, title: "Home"); // Act Result result = await _sut.RenderAsync(model, "editorial-paper", CancellationToken.None); // Assert Assert.True(result.IsSuccess); Assert.Equal("

Home

", result.Value.Content); Assert.Equal("", result.Value.Script); Assert.Equal("editorial-paper", model.ThemeId); Assert.Equal("/theme-assets/editorial-paper/assets", model.AssetBase); Assert.StartsWith("script_", model.ScriptId); await _mockApiHttpClient.DidNotReceive().GetAsync(ApiRoutes.Themes.GET_CURRENT_THEME, Arg.Any()); } [Fact] public async Task RenderAsync_WhenThemeIdMissing_ShouldResolveCurrentThemeBeforeRendering() { // Arrange ThemeResponseDto currentTheme = _themeResponseDtoFixture.Create(themeId: "nemera-default"); string pageKey = "home/index"; string expectedEndpoint = ApiRoutes.Themes.GET_THEME_TEMPLATE .Replace("{themeId}", "nemera-default") .Replace("{*pageKey}", pageKey); _mockApiHttpClient.GetAsync(ApiRoutes.Themes.GET_CURRENT_THEME, Arg.Any()) .Returns(currentTheme); _mockApiHttpClient.GetAsync(expectedEndpoint, Arg.Any()) .Returns(_themeTemplateResponseDtoFixture.Create(theme: currentTheme, template: "{{title}}")); ThemePageDto model = _themePageDtoFixture.Create(pageKey: pageKey, title: "Home"); // Act Result result = await _sut.RenderAsync(model, null, CancellationToken.None); // Assert Assert.True(result.IsSuccess); Assert.Equal("nemera-default", model.ThemeId); Assert.Equal("Home", result.Value.Content); await _mockApiHttpClient.Received(1).GetAsync(ApiRoutes.Themes.GET_CURRENT_THEME, Arg.Any()); } [Fact] public async Task RenderAsync_WhenTemplateRenderingFails_ShouldReturnFailure() { // Arrange ThemeResponseDto theme = _themeResponseDtoFixture.Create(themeId: "editorial-paper"); string pageKey = "home/index"; string expectedEndpoint = ApiRoutes.Themes.GET_THEME_TEMPLATE .Replace("{themeId}", "editorial-paper") .Replace("{*pageKey}", pageKey); _mockApiHttpClient.GetAsync(expectedEndpoint, Arg.Any()) .Returns(_themeTemplateResponseDtoFixture.Create(theme: theme, template: "{{#unclosed}}")); ThemePageDto model = _themePageDtoFixture.Create(pageKey: pageKey, title: "Home"); // Act Result result = await _sut.RenderAsync(model, "editorial-paper", CancellationToken.None); // Assert Assert.True(result.IsFailure); Assert.Equal("Theme.Template.Invalid", result.FirstError.Code); } }