using FluentAssertions;
using NSubstitute;
using SlpModularCms.Core.Hosting.Website;
using SlpModularCms.Modules.Hero.Models;
using SlpModularCms.Modules.Hero.Services;
namespace SlpModularCms.Modules.Hero.Tests;
///
/// A hero without slides (SLP Software): when the site asks for it (Hero:TextOnlyRegion) the host writes the
/// title, the lead and the buttons into the first HTML; otherwise (ASF) the baked page stays.
///
public class HeroTextOnlyTests
{
private static PublicHeroDto TextOnly(string title = "Websites,\napps\n& meer.", string? tagline = null, string? lead = "Een lead.", params HeroButtonDto[] buttons)
=> new("single", title, tagline, lead, buttons, []);
[Fact]
public void ADisplayedHeroWithoutSlides_IsATextOnlyRegion_WithNothingToPreload()
{
var hero = TextOnly(buttons: [new HeroButtonDto("Bekijk pakketten", "#pakketten"), new HeroButtonDto("Plan een gesprek", "#contact")]);
var injection = HeroPictureRenderer.Render(hero, textOnlyRegion: true);
injection.Region.Should().Be(
"
Websites,
apps
& meer.
" +
"Een lead.
" +
"Bekijk pakkettenPlan een gesprek
");
injection.Preload.Should().BeEmpty();
injection.IsEmpty.Should().BeFalse();
}
[Fact]
public void WithoutTheSiteAskingForIt_AHeroWithoutSlides_LeavesTheBakedHero()
{
var hero = TextOnly(buttons: [new HeroButtonDto("Knop", "#x")]);
HeroPictureRenderer.Render(hero).IsEmpty.Should().BeTrue();
HeroPictureRenderer.Render(hero, textOnlyRegion: false).IsEmpty.Should().BeTrue();
}
[Fact]
public void WithoutALead_OrButtons_ThosePartsAreLeftOut_AndATaglineComesFirst()
{
var injection = HeroPictureRenderer.Render(TextOnly("Titel", "Label", null), textOnlyRegion: true);
injection.Region.Should().Be("Label Titel
");
}
[Fact]
public void TheLead_KeepsItsLineBreaks_AndEverythingIsEncoded()
{
var injection = HeroPictureRenderer.Render(TextOnly("x", null, "a & b\nc \"d\"", new HeroButtonDto("", "/p?a=1&b=2")), textOnlyRegion: true);
injection.Region.Should().Contain("<b>x</b>").And.Contain("a & b
c "d"").And.Contain("href=\"/p?a=1&b=2\"><i>");
injection.Region.Should().NotContain("").And.NotContain("");
}
[Fact]
public void AHeroWithSlidesThatAreAllUnusable_StaysEmpty_EvenWhenTheSiteAsksForText()
{
var hero = new PublicHeroDto("single", "Titel", null, null, [], [new PublicSlideDto("x", "", false, new PublicImageDto(1, 1, new FocusDto(0.5, 0.5, 1), "/m.webp", []))]);
HeroPictureRenderer.Render(hero, textOnlyRegion: true).IsEmpty.Should().BeTrue();
}
[Fact]
public void ThePrefix_IsTheFirstHiddenPartOfTheH1_AndIsEncoded()
{
var injection = HeroPictureRenderer.Render(TextOnly("Titel", "Label", null), textOnlyRegion: true, titlePrefix: "SLP & Co: ");
injection.Region.Should().Be("SLP & Co: Label Titel
");
}
[Theory]
[InlineData("")]
[InlineData(" ")]
public void AnEmptyPrefix_ChangesNothing(string prefix)
{
var hero = TextOnly();
HeroPictureRenderer.Render(hero, textOnlyRegion: true, titlePrefix: prefix).Region
.Should().Be(HeroPictureRenderer.Render(hero, textOnlyRegion: true).Region).And.NotContain("title-prefix");
}
[Fact]
public void APictureHero_NeverGetsThePrefix()
{
var variant = new HeroVariantDto("wide", 640, 427, "image/webp", "/media/hero/0123456789abcdef/wide-640.webp");
var image = new PublicImageDto(1600, 1067, new FocusDto(0.5, 0.5, 1), "/media/hero/0123456789abcdef/wide-640.webp", [variant]);
var hero = new PublicHeroDto("single", "Titel", null, null, [], [new PublicSlideDto("alt", "", false, image)]);
HeroPictureRenderer.Render(hero, textOnlyRegion: true, titlePrefix: "SLP: ").Region.Should().NotContain("title-prefix");
}
[Fact]
public async Task TheTransform_PassesTheConfiguredPrefix()
{
var model = Substitute.For();
model.GetAsync(default).ReturnsForAnyArgs(TextOnly());
var transform = new HeroHtmlTransform(model, Options.Create(new HeroOptions { TextOnlyRegion = true, TitlePrefix = "SLP Software: " }));
var html = await transform.ApplyAsync(Page, new HtmlTransformContext("https://x"), default);
html.Should().Contain("SLP Software: ");
}
private const string Page ="baked";
private static HeroHtmlTransform Transform(bool textOnly)
{
var model = Substitute.For();
model.GetAsync(default).ReturnsForAnyArgs(TextOnly());
return new HeroHtmlTransform(model, Options.Create(new HeroOptions { TextOnlyRegion = textOnly }));
}
[Fact]
public async Task TheTransform_FillsTheTextRegion_WhenTheSiteAsksForIt_AndLeavesThePreloadRegionAlone()
{
var html = await Transform(textOnly: true).ApplyAsync(Page, new HtmlTransformContext("https://x"), default);
html.Should().Contain("").And.NotContain("baked");
html.Should().Contain("");
}
[Fact]
public async Task TheTransform_LeavesTheWholePageAlone_WhenTheSiteDoesNotAskForText()
{
(await Transform(textOnly: false).ApplyAsync(Page, new HtmlTransformContext("https://x"), default)).Should().Be(Page);
}
}