Files
slp-modular-cms/src/SlpModularCms.Modules.Hero.Tests/HeroTextOnlyTests.cs
T

91 lines
4.2 KiB
C#

using FluentAssertions;
using NSubstitute;
using SlpModularCms.Core.Hosting.Website;
using SlpModularCms.Modules.Hero.Models;
using SlpModularCms.Modules.Hero.Services;
namespace SlpModularCms.Modules.Hero.Tests;
/// <summary>
/// A hero without slides (SLP Software): when the site asks for it (<c>Hero:TextOnlyRegion</c>) the host writes the
/// title, the lead and the buttons into the first HTML; otherwise (ASF) the baked page stays.
/// </summary>
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(
"<h1 class=\"cms-hero-title\"><span class=\"cms-hero-title-lines\">Websites,<br>apps<br>&amp; meer.</span></h1>" +
"<p class=\"cms-hero-lead\">Een lead.</p>" +
"<p class=\"cms-hero-buttons\"><a class=\"cms-hero-button\" href=\"#pakketten\">Bekijk pakketten</a><a class=\"cms-hero-button\" href=\"#contact\">Plan een gesprek</a></p>");
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("<h1 class=\"cms-hero-title\"><span class=\"cms-hero-tagline\">Label</span> <span class=\"cms-hero-title-lines\">Titel</span></h1>");
}
[Fact]
public void TheLead_KeepsItsLineBreaks_AndEverythingIsEncoded()
{
var injection = HeroPictureRenderer.Render(TextOnly("<b>x</b>", null, "a & b\nc \"d\"", new HeroButtonDto("<i>", "/p?a=1&b=2")), textOnlyRegion: true);
injection.Region.Should().Contain("&lt;b&gt;x&lt;/b&gt;").And.Contain("a &amp; b<br>c &quot;d&quot;").And.Contain("href=\"/p?a=1&amp;b=2\">&lt;i&gt;</a>");
injection.Region.Should().NotContain("<b>").And.NotContain("<i>");
}
[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();
}
private const string Page = "<head><!--CMS:HERO-PRELOAD--><!--/CMS:HERO-PRELOAD--></head><body><!--CMS:HERO-HTML-->baked<!--/CMS:HERO-HTML--></body>";
private static HeroHtmlTransform Transform(bool textOnly)
{
var model = Substitute.For<IHeroPageModel>();
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("<!--CMS:HERO-HTML--><h1 class=\"cms-hero-title\">").And.NotContain("baked");
html.Should().Contain("<!--CMS:HERO-PRELOAD--><!--/CMS:HERO-PRELOAD-->");
}
[Fact]
public async Task TheTransform_LeavesTheWholePageAlone_WhenTheSiteDoesNotAskForText()
{
(await Transform(textOnly: false).ApplyAsync(Page, new HtmlTransformContext("https://x"), default)).Should().Be(Page);
}
}