100 lines
4.1 KiB
C#
100 lines
4.1 KiB
C#
using System.Text.Json;
|
|
using FluentAssertions;
|
|
using SlpModularCms.Modules.Offerings.Models;
|
|
using SlpModularCms.Modules.Offerings.Services;
|
|
|
|
namespace SlpModularCms.Modules.Offerings.Tests.Services;
|
|
|
|
/// <summary>
|
|
/// The plain HTML written between the CMS markers must be exactly what the website build expects: the golden files of
|
|
/// the website repository (Golden/SOURCE.md names the commit) are run through <see cref="CmsPageHtml"/>.
|
|
/// </summary>
|
|
public class GoldenHtmlTests
|
|
{
|
|
private static string Dir => Path.Combine(AppContext.BaseDirectory, "Golden", "host-injection");
|
|
|
|
public static IEnumerable<object[]> Offerings() => Names("offerings-");
|
|
public static IEnumerable<object[]> Banners() => Names("banner-");
|
|
|
|
private static IEnumerable<object[]> Names(string prefix) =>
|
|
Directory.GetFiles(Dir, prefix + "*.input.json").Select(f => new object[] { Path.GetFileName(f)[..^".input.json".Length] }).OrderBy(o => (string)o[0]);
|
|
|
|
// The expected files end with one newline that is not part of the HTML.
|
|
private static string Expected(string name)
|
|
{
|
|
var text = File.ReadAllText(Path.Combine(Dir, name + ".expected.html"));
|
|
return text.EndsWith('\n') ? text[..^1] : text;
|
|
}
|
|
|
|
private static JsonElement Input(string name) => JsonDocument.Parse(File.ReadAllText(Path.Combine(Dir, name + ".input.json"))).RootElement;
|
|
|
|
private static string? Text(JsonElement e, string property) =>
|
|
e.TryGetProperty(property, out var v) && v.ValueKind == JsonValueKind.String ? v.GetString() : null;
|
|
|
|
[Fact]
|
|
public void TheGoldenFiles_AreThere()
|
|
{
|
|
Offerings().Count().Should().BeGreaterThanOrEqualTo(5);
|
|
Banners().Count().Should().BeGreaterThanOrEqualTo(6);
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData(nameof(Offerings))]
|
|
public void OfferingsList_MatchesTheGoldenFile(string name)
|
|
{
|
|
var offerings = Input(name).EnumerateArray().Select((o, i) => new OfferingDto(
|
|
Id: i.ToString(),
|
|
Slug: Text(o, "slug")!,
|
|
Title: Text(o, "title")!,
|
|
Description: Text(o, "description") ?? string.Empty,
|
|
Price: Text(o, "price")!,
|
|
PriceNote: Text(o, "priceNote") ?? string.Empty,
|
|
Features: o.GetProperty("features").EnumerateArray().Select(f => f.GetString()!).ToList(),
|
|
CtaLabel: Text(o, "ctaLabel") ?? string.Empty,
|
|
Featured: o.TryGetProperty("featured", out var f) && f.ValueKind == JsonValueKind.True,
|
|
DisplayOrder: o.TryGetProperty("displayOrder", out var d) ? d.GetInt32() : 0)).ToList();
|
|
|
|
CmsPageHtml.OfferingsList(offerings).Should().Be(Expected(name));
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData(nameof(Banners))]
|
|
public void Banner_MatchesTheGoldenFile(string name)
|
|
{
|
|
var input = Input(name);
|
|
var position = input.GetProperty("position").GetString()!;
|
|
var answer = input.GetProperty("answer");
|
|
PublicOfferingBannerDto? banner = answer.ValueKind == JsonValueKind.Null
|
|
? null
|
|
: new PublicOfferingBannerDto(Text(answer, "title")!, Text(answer, "text")!, Text(answer, "linkLabel"), Text(answer, "linkUrl"), position);
|
|
|
|
CmsPageHtml.Banner(banner, position).Should().Be(Expected(name));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("€ 175", 175)]
|
|
[InlineData("€ 1.250", 1250)]
|
|
[InlineData("175,-", 175)]
|
|
[InlineData("€ 175,00", 175)]
|
|
[InlineData("€ 1.250,-", 1250)]
|
|
[InlineData("op aanvraag", null)]
|
|
[InlineData("€ 12,50", null)]
|
|
[InlineData("€ 0", null)]
|
|
[InlineData("175 of 185", null)]
|
|
[InlineData("", null)]
|
|
[InlineData(null, null)]
|
|
public void WholeEuros_ReadsOnlyWholeEuroPrices(string? text, int? expected) =>
|
|
CmsPageHtml.WholeEuros(text).Should().Be(expected);
|
|
|
|
[Theory]
|
|
[InlineData(185, "185")]
|
|
[InlineData(1250, "1.250")]
|
|
[InlineData(1000000, "1.000.000")]
|
|
public void FormatAmount_UsesADotAsThousandsSeparator(int euros, string expected) =>
|
|
CmsPageHtml.FormatAmount(euros).Should().Be(expected);
|
|
|
|
[Fact]
|
|
public void Encode_EscapesTheFiveCharacters_AndLeavesAccentsAlone()
|
|
=> CmsPageHtml.Encode("é & < > \" '").Should().Be("é & < > " '");
|
|
}
|