From 04ef15ff2b88a197fa0d17fb725df6a1369040b0 Mon Sep 17 00:00:00 2001 From: Michael Croes Date: Mon, 13 Aug 2018 16:06:24 +0200 Subject: [PATCH] Add test for custom text provided through builder --- .../ModelMetadataTagExtensionsTester.cs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/HtmlTags.AspNetCore.Testing/ModelMetadataTagExtensionsTester.cs b/test/HtmlTags.AspNetCore.Testing/ModelMetadataTagExtensionsTester.cs index bb06754..b2c1a2e 100644 --- a/test/HtmlTags.AspNetCore.Testing/ModelMetadataTagExtensionsTester.cs +++ b/test/HtmlTags.AspNetCore.Testing/ModelMetadataTagExtensionsTester.cs @@ -9,6 +9,7 @@ using System.Text.Encodings.Web; using System.Threading.Tasks; using HtmlTags.Conventions; +using HtmlTags.Conventions.Elements; using Microsoft.AspNetCore.Antiforgery; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -200,6 +201,18 @@ public void ShouldAllowOverridingOfConventions() editor.Value().ShouldBe(GetDateValue(offset)); } + [Fact] + public void ShouldAllowOverridingOfConventionTextUsingCustomBuilder() + { + var offset = new DateTimeOffset(2018, 1, 1, 12, 00, 00, TimeSpan.FromHours(-6)); + var subject = new Subject {DateValue = offset}; + var helper = GetHtmlHelper(subject); + + var display = helper.Display(m => m.DateValue); + + display.Text().ShouldBe(DateTimeBuilder.Token); + } + public static HtmlHelper GetHtmlHelper(TModel model) { return GetHtmlHelper(model, CreateViewEngine()); @@ -339,6 +352,8 @@ private static HtmlHelper GetHtmlHelper( reg.Editors.IfPropertyIs().ModifyWith(m => m.CurrentTag.Attr("type", "datetime-local") .Value(GetDateValue(m.Value()))); + + reg.Displays.IfPropertyIs().BuildBy(); }); var serviceProvider = serviceCollection.BuildServiceProvider(); @@ -419,6 +434,16 @@ private static string FormatOutput(ModelExplorer modelExplorer) modelExplorer.GetSimpleDisplayText() ?? "(null)"); } + public class DateTimeBuilder : IElementBuilder + { + public static string Token = "date-time-builder"; + + public HtmlTag Build(ElementRequest request) + { + return new HtmlTag("span").Text(Token); + } + } + public class TestOptionsManager : IOptions where TOptions : class, new() {