#region ========================================================================= USING =====================================================================================
using Nemera.Presentation.Web.Common.DTO.Themes;
using Nemera.Presentation.Web.Core.Themes;
using Nemera.Presentation.Web.Fixtures.Common.TestHelpers;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Localization;
using NSubstitute;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Nemera.Presentation.Web.UnitTests.Core.Themes;
///
/// Contains unit tests for the class.
///
[ExcludeFromCodeCoverage]
public class ThemeFileSystemBrowserBuilderTests
{
private readonly IStringLocalizerFactory _mockStringLocalizerFactory;
private readonly IStringLocalizer _mockStringLocalizer;
private readonly IHttpContextAccessor _mockHttpContextAccessor;
private readonly ThemeFileSystemBrowserBuilder _sut;
///
/// Initializes a new instance of the class.
///
public ThemeFileSystemBrowserBuilderTests()
{
_mockStringLocalizer = CreateLocalizer();
_mockStringLocalizerFactory = Substitute.For();
_mockStringLocalizerFactory.Create(Arg.Any(), Arg.Any()).Returns(_mockStringLocalizer);
_mockHttpContextAccessor = Substitute.For();
_sut = new ThemeFileSystemBrowserBuilder(_mockStringLocalizerFactory, _mockHttpContextAccessor);
}
[Fact]
public void Build_WhenCalled_ShouldResolveThemeAssetUrlsFromAssetBase()
{
// Arrange
// Act
ThemeFileSystemBrowserDto model = _sut.Build("/theme-assets/editorial/assets", "{{name}}
", "{{name}}
", "{{path}}");
// Assert
Assert.Equal("/theme-assets/editorial/assets", model.AssetBase);
Assert.Equal("/theme-assets/editorial/assets/images/icons", model.IconBaseUrl);
Assert.Equal("/theme-assets/editorial/assets/file-icons.json", model.FileIconsUrl);
}
[Fact]
public void Build_WhenCalled_ShouldKeepTheSubTemplatesOfTheDynamicContent()
{
// Arrange
// Act
ThemeFileSystemBrowserDto model = _sut.Build("/theme-assets/editorial/assets", "{{name}}
", "{{name}}
", "{{path}}");
// Assert
Assert.Equal("{{name}}
", model.TreeNodeTemplate);
Assert.Equal("{{name}}
", model.ExplorerItemTemplate);
Assert.Equal("{{path}}", model.PathSegmentTemplate);
}
[Fact]
public void Build_WhenCalled_ShouldLocalizeAllFileSystemBrowserStrings()
{
// Arrange
// Act
ThemeFileSystemBrowserDto model = _sut.Build("/theme-assets/editorial/assets", string.Empty, string.Empty, string.Empty);
// Assert
string[] expectedKeys =
[
"listView", "detailsView", "smallIconsView", "mediumIconsView", "largeIconsView", "extraLargeIconsView",
"back", "forward", "upOneLevel", "toggleTreeView", "toggleThumbnails", "toggleHiddenItems",
"toggleSelectionMode", "editPath", "navigate", "newDirectory", "favoriteDirectory",
"name", "directory", "cancel", "open"
];
foreach (string key in expectedKeys)
{
Assert.True(model.Strings.TryGetValue(key, out object? value), $"The '{key}' string is missing.");
Assert.StartsWith("Localized-", Assert.IsType(value));
}
}
[Fact]
public void Build_WhenRequestIsAvailable_ShouldResolveAppBaseFromCurrentRequest()
{
// Arrange
DefaultHttpContext httpContext = TestHttpContextFactory.Create();
httpContext.Request.Scheme = "https";
httpContext.Request.Host = new HostString("localhost", 5001);
_mockHttpContextAccessor.HttpContext.Returns(httpContext);
// Act
ThemeFileSystemBrowserDto model = _sut.Build("/theme-assets/editorial/assets", string.Empty, string.Empty, string.Empty);
// Assert
Assert.Equal("https://localhost:5001/", model.AppBase);
}
[Fact]
public void Build_WhenRequestIsUnavailable_ShouldReturnEmptyAppBase()
{
// Arrange
_mockHttpContextAccessor.HttpContext.Returns((HttpContext?)null);
// Act
ThemeFileSystemBrowserDto model = _sut.Build("/theme-assets/editorial/assets", string.Empty, string.Empty, string.Empty);
// Assert
Assert.Equal(string.Empty, model.AppBase);
}
///
/// Creates a substitute string localizer that returns a string prefixed with "Localized-".
///
/// The created substitute string localizer.
private static IStringLocalizer CreateLocalizer()
{
IStringLocalizer localizer = Substitute.For();
localizer[Arg.Any()].Returns(callInfo => new LocalizedString(callInfo.Arg(), $"Localized-{callInfo.Arg()}"));
return localizer;
}
}