diff --git a/src/SlpModularCms.Modules.Offerings.Tests/Services/OfferingsHtmlTransformTests.cs b/src/SlpModularCms.Modules.Offerings.Tests/Services/OfferingsHtmlTransformTests.cs new file mode 100644 index 0000000..5c2ca35 --- /dev/null +++ b/src/SlpModularCms.Modules.Offerings.Tests/Services/OfferingsHtmlTransformTests.cs @@ -0,0 +1,225 @@ +using System.Text.Json; +using FluentAssertions; +using NSubstitute; +using NSubstitute.ExceptionExtensions; +using SlpModularCms.Core.Hosting.Website; +using SlpModularCms.Modules.Offerings.Models; +using SlpModularCms.Modules.Offerings.Services; + +namespace SlpModularCms.Modules.Offerings.Tests.Services; + +public class OfferingsHtmlTransformTests +{ + private readonly IOfferingsService _offerings = Substitute.For(); + private readonly IOfferingBannersService _banners = Substitute.For(); + private static readonly HtmlTransformContext Context = new("https://test.andreaslop.nl"); + + private OfferingsHtmlTransform Sut() => new(_offerings, _banners); + + private static OfferingDto Offering(string slug, string title, string price, int order = 0, bool featured = false) => new( + Guid.NewGuid().ToString(), slug, title, "Beschrijving", price, "eenmalig", ["1 uur fotografie", "Online galerij"], "Boek", featured, order); + + private void Live(params OfferingDto[] list) => _offerings.GetOfferingsAsync().Returns(list); + + private void Banner(string position, PublicOfferingBannerDto? banner) => _banners.GetActiveAsync(position).Returns(banner); + + // ---- what a page asks for decides what is loaded ---- + + [Theory] + [InlineData("

nothing

", false)] + [InlineData("", true)] + [InlineData("x", true)] + public void Wants_OnlyPagesWithCmsMarkers(string html, bool expected) => Sut().Wants(html).Should().Be(expected); + + [Fact] + public async Task APageWithOnlyAnOfferingsRegion_NeverLoadsBanners() + { + Live(Offering("newborn", "Newborn", "€ 185")); + + await Sut().ApplyAsync("x", Context, default); + + await _banners.DidNotReceiveWithAnyArgs().GetActiveAsync(default!); + } + + [Fact] + public async Task APageWithOnlyABannerRegion_NeverLoadsOfferings() + { + Banner("top", null); + + await Sut().ApplyAsync("x", Context, default); + + await _offerings.DidNotReceive().GetOfferingsAsync(); + } + + // ---- regions ---- + + [Fact] + public async Task TheRegions_AreFilledFromLiveData_AndAnEndedBannerEmptiesItsRegion() + { + Live(Offering("newborn", "Newborn", "€ 185")); + Banner("top", new PublicOfferingBannerDto("", "Nog twee plekken", null, null, "top")); + Banner("bottom", null); + var html = "baked top" + + "baked list" + + "baked bottom"; + + var result = await Sut().ApplyAsync(html, Context, default); + + result.Should().Contain(""); + result.Should().Contain("