From f863ddee80dc0b054a767f5377cd9a81798e51fb Mon Sep 17 00:00:00 2001 From: Matthew Wise <6782865+Matthew-Wise@users.noreply.github.com> Date: Fri, 5 May 2023 13:01:59 +0100 Subject: [PATCH 1/2] Added tests for LinkTagHelper --- .../Helpers/TestContextHelpers.cs | 39 ++++++++++++++ .../IncludeIfTagHelperTests.cs | 35 ++---------- .../LinkTagHelperTests.cs | 53 +++++++++++++++++++ .../Our.Umbraco.TagHelpers.Tests.csproj | 2 +- 4 files changed, 96 insertions(+), 33 deletions(-) create mode 100644 Our.Umbraco.TagHelpers.Tests/Helpers/TestContextHelpers.cs create mode 100644 Our.Umbraco.TagHelpers.Tests/LinkTagHelperTests.cs diff --git a/Our.Umbraco.TagHelpers.Tests/Helpers/TestContextHelpers.cs b/Our.Umbraco.TagHelpers.Tests/Helpers/TestContextHelpers.cs new file mode 100644 index 0000000..206daeb --- /dev/null +++ b/Our.Umbraco.TagHelpers.Tests/Helpers/TestContextHelpers.cs @@ -0,0 +1,39 @@ +using Microsoft.AspNetCore.Razor.TagHelpers; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Our.Umbraco.TagHelpers.Tests.Helpers +{ + internal static class TestContextHelpers + { + public static TagHelperContext GetTagHelperContext(string id = "testid") + { + return new TagHelperContext( + tagName: "p", + allAttributes: new TagHelperAttributeList(), + items: new Dictionary(), + uniqueId: id); + } + + public static TagHelperOutput GetTagHelperOutput( + string tagName = "p", + TagHelperAttributeList attributes = null, + string childContent = "some child content") + { + attributes ??= new TagHelperAttributeList(); + + return new TagHelperOutput( + tagName, + attributes, + getChildContentAsync: (useCachedResult, encoder) => + { + var tagHelperContent = new DefaultTagHelperContent(); + var content = tagHelperContent.SetHtmlContent(childContent); + return Task.FromResult(content); + }); + } + } +} diff --git a/Our.Umbraco.TagHelpers.Tests/IncludeIfTagHelperTests.cs b/Our.Umbraco.TagHelpers.Tests/IncludeIfTagHelperTests.cs index 977e694..727bdbb 100644 --- a/Our.Umbraco.TagHelpers.Tests/IncludeIfTagHelperTests.cs +++ b/Our.Umbraco.TagHelpers.Tests/IncludeIfTagHelperTests.cs @@ -1,9 +1,7 @@ using Microsoft.AspNetCore.Razor.TagHelpers; using NUnit.Framework; -using System; -using System.Collections.Generic; -using Our.Umbraco.TagHelpers; using System.Threading.Tasks; +using Our.Umbraco.TagHelpers.Tests.Helpers; namespace Our.Umbraco.TagHelpers.Tests { @@ -15,8 +13,8 @@ public async Task Given_Predicate_Return_Contents_Or_Empty(bool predicate, strin { // Arrange var id = "unique-id"; - var tagHelperContext = GetTagHelperContext(id); - var tagHelperOutput = GetTagHelperOutput( + var tagHelperContext = TestContextHelpers.GetTagHelperContext(id); + var tagHelperOutput = TestContextHelpers.GetTagHelperOutput( attributes: new TagHelperAttributeList(), childContent: childContent); tagHelperOutput.Content.SetContent(childContent); @@ -31,32 +29,5 @@ public async Task Given_Predicate_Return_Contents_Or_Empty(bool predicate, strin // Assert Assert.AreEqual(expected, content); } - - private static TagHelperContext GetTagHelperContext(string id = "testid") - { - return new TagHelperContext( - tagName: "p", - allAttributes: new TagHelperAttributeList(), - items: new Dictionary(), - uniqueId: id); - } - - private static TagHelperOutput GetTagHelperOutput( - string tagName = "p", - TagHelperAttributeList attributes = null, - string childContent = "some child content") - { - attributes = attributes ?? new TagHelperAttributeList { { "attr", "value" } }; - - return new TagHelperOutput( - tagName, - attributes, - getChildContentAsync: (useCachedResult, encoder) => - { - var tagHelperContent = new DefaultTagHelperContent(); - var content = tagHelperContent.SetHtmlContent(childContent); - return Task.FromResult(content); - }); - } } } \ No newline at end of file diff --git a/Our.Umbraco.TagHelpers.Tests/LinkTagHelperTests.cs b/Our.Umbraco.TagHelpers.Tests/LinkTagHelperTests.cs new file mode 100644 index 0000000..5f43f8b --- /dev/null +++ b/Our.Umbraco.TagHelpers.Tests/LinkTagHelperTests.cs @@ -0,0 +1,53 @@ +using System.Collections.Generic; +using System.IO; +using System.Text.Encodings.Web; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Razor.TagHelpers; +using NUnit.Framework; +using Our.Umbraco.TagHelpers.Tests.Helpers; +using Umbraco.Cms.Core.Models; + +namespace Our.Umbraco.TagHelpers.Tests +{ + public class LinkTagHelperTests + { + + [TestCaseSource(nameof(TestCases))] + public async Task LinkHelperTests(LinkTagHelper tagHelper, string childContent, string expectedOutput) + { + var tagHelperContext = TestContextHelpers.GetTagHelperContext("link"); + var output = TestContextHelpers.GetTagHelperOutput("our-link", + attributes: new TagHelperAttributeList(), + childContent: childContent); + output.Content.SetContent(childContent); + + await tagHelper.ProcessAsync(tagHelperContext, output); + + using var txtWriter = new StringWriter(); + output.WriteTo(txtWriter, HtmlEncoder.Default); + Assert.AreEqual(expectedOutput, txtWriter.ToString()); + } + + private static IEnumerable TestCases() + { + var onSiteLink = new Link { Url = "/", Name = "example" }; + var externalLink = new Link { Url = "/", Name = "example", Target = "_blank", Type = LinkType.External }; + + return new[] { + new TestCaseData(new LinkTagHelper(), "content", string.Empty).SetName("No content rendered if Link is null"), + + new TestCaseData(new LinkTagHelper { Link = onSiteLink},null,"example").SetName("Internal link is rendered with no content"), + new TestCaseData(new LinkTagHelper { Link = onSiteLink},"content","content").SetName("Internal link is rendered with content"), + + new TestCaseData(new LinkTagHelper { Link = externalLink},null,"example").SetName("External link with target is rendered with no content"), + new TestCaseData(new LinkTagHelper { Link = externalLink},"content","content").SetName("External link with target is rendered with content"), + + + new TestCaseData(new LinkTagHelper() { Fallback = true }, "", "").SetName("Fallback with no content"), + new TestCaseData(new LinkTagHelper() { Fallback = true }, "content", "content").SetName("Fallback with only content"), + new TestCaseData(new LinkTagHelper() { Fallback = true, FallbackElement = "div" }, "content", "
content
") + .SetName("Fallback with fallback element and content") + }; + } + } +} diff --git a/Our.Umbraco.TagHelpers.Tests/Our.Umbraco.TagHelpers.Tests.csproj b/Our.Umbraco.TagHelpers.Tests/Our.Umbraco.TagHelpers.Tests.csproj index 6de1b9e..341eb07 100644 --- a/Our.Umbraco.TagHelpers.Tests/Our.Umbraco.TagHelpers.Tests.csproj +++ b/Our.Umbraco.TagHelpers.Tests/Our.Umbraco.TagHelpers.Tests.csproj @@ -1,4 +1,4 @@ - + net5.0 From 9da891e44d3021386681642de1eaf69335ce6165 Mon Sep 17 00:00:00 2001 From: Matthew Wise <6782865+Matthew-Wise@users.noreply.github.com> Date: Fri, 5 May 2023 14:24:40 +0100 Subject: [PATCH 2/2] refactored link tests so they are more split --- .../LinkTagHelperTests.cs | 113 ++++++++++++++---- 1 file changed, 87 insertions(+), 26 deletions(-) diff --git a/Our.Umbraco.TagHelpers.Tests/LinkTagHelperTests.cs b/Our.Umbraco.TagHelpers.Tests/LinkTagHelperTests.cs index 5f43f8b..55ae682 100644 --- a/Our.Umbraco.TagHelpers.Tests/LinkTagHelperTests.cs +++ b/Our.Umbraco.TagHelpers.Tests/LinkTagHelperTests.cs @@ -1,5 +1,4 @@ -using System.Collections.Generic; -using System.IO; +using System.IO; using System.Text.Encodings.Web; using System.Threading.Tasks; using Microsoft.AspNetCore.Razor.TagHelpers; @@ -11,43 +10,105 @@ namespace Our.Umbraco.TagHelpers.Tests { public class LinkTagHelperTests { + private static readonly Link _onSiteLink = new() { Url = "/", Name = "example" }; - [TestCaseSource(nameof(TestCases))] - public async Task LinkHelperTests(LinkTagHelper tagHelper, string childContent, string expectedOutput) + private static readonly Link _externalLink = new() { Url = "/", Name = "example", Target = "_blank", Type = LinkType.External }; + + private TagHelperContext _tagHelperContext; + + [SetUp] + public void SetUp() + { + _tagHelperContext = TestContextHelpers.GetTagHelperContext("link"); + } + + [TestCase("", "example")] + [TestCase(null, "example")] + [TestCase("content", "content")] + public async Task Internal_Link_Renders_AnchorAroundContentOrLinkName(string childContent, string expectedContent) { - var tagHelperContext = TestContextHelpers.GetTagHelperContext("link"); - var output = TestContextHelpers.GetTagHelperOutput("our-link", - attributes: new TagHelperAttributeList(), - childContent: childContent); + var output = TestContextHelpers.GetTagHelperOutput("our-link", childContent: childContent); output.Content.SetContent(childContent); - await tagHelper.ProcessAsync(tagHelperContext, output); + LinkTagHelper tagHelper = new() { Link = _onSiteLink }; - using var txtWriter = new StringWriter(); - output.WriteTo(txtWriter, HtmlEncoder.Default); - Assert.AreEqual(expectedOutput, txtWriter.ToString()); + var markup = await GetMarkupAsync(tagHelper, output); + Assert.AreEqual($"{expectedContent}", markup); } - private static IEnumerable TestCases() + [TestCase("", "example")] + [TestCase(null, "example")] + [TestCase("content", "content")] + public async Task External_Link_Renders_AnchorAroundContentOrLinkName(string childContent, string expectedContent) { - var onSiteLink = new Link { Url = "/", Name = "example" }; - var externalLink = new Link { Url = "/", Name = "example", Target = "_blank", Type = LinkType.External }; + var output = TestContextHelpers.GetTagHelperOutput("our-link", childContent: childContent); + output.Content.SetContent(childContent); - return new[] { - new TestCaseData(new LinkTagHelper(), "content", string.Empty).SetName("No content rendered if Link is null"), + LinkTagHelper tagHelper = new() { Link = _externalLink }; - new TestCaseData(new LinkTagHelper { Link = onSiteLink},null,"example").SetName("Internal link is rendered with no content"), - new TestCaseData(new LinkTagHelper { Link = onSiteLink},"content","content").SetName("Internal link is rendered with content"), + var markup = await GetMarkupAsync(tagHelper, output); + Assert.AreEqual($"{expectedContent}", markup); + } - new TestCaseData(new LinkTagHelper { Link = externalLink},null,"example").SetName("External link with target is rendered with no content"), - new TestCaseData(new LinkTagHelper { Link = externalLink},"content","content").SetName("External link with target is rendered with content"), + [Test] + public async Task NoUrl_WithoutFallback_RendersNothing() + { + var output = TestContextHelpers.GetTagHelperOutput("our-link", childContent: string.Empty); + output.Content.SetContent(string.Empty); + + LinkTagHelper tagHelper = new() { Link = new() }; + + var markup = await GetMarkupAsync(tagHelper, output); + Assert.AreEqual(string.Empty, markup); + } + [Test] + public async Task Null_Link_WithoutFallback_RendersNothing() + { + var output = TestContextHelpers.GetTagHelperOutput("our-link", childContent: string.Empty); + output.Content.SetContent(string.Empty); + + LinkTagHelper tagHelper = new() { Link = null }; + + var markup = await GetMarkupAsync(tagHelper, output); + Assert.AreEqual(string.Empty, markup); + } - new TestCaseData(new LinkTagHelper() { Fallback = true }, "", "").SetName("Fallback with no content"), - new TestCaseData(new LinkTagHelper() { Fallback = true }, "content", "content").SetName("Fallback with only content"), - new TestCaseData(new LinkTagHelper() { Fallback = true, FallbackElement = "div" }, "content", "
content
") - .SetName("Fallback with fallback element and content") - }; + [TestCase("", "")] + [TestCase(null, "")] + [TestCase("content", "content")] + public async Task Null_Link_WithFallback_NoElement_RendersContent(string childContent, string expectedContent) + { + var output = TestContextHelpers.GetTagHelperOutput("our-link", childContent: childContent); + output.Content.SetContent(childContent); + + LinkTagHelper tagHelper = new() { Link = null, Fallback = true }; + + var markup = await GetMarkupAsync(tagHelper, output); + Assert.AreEqual(expectedContent, markup); + } + + [TestCase("", "")] + [TestCase(null, "")] + [TestCase("content", "
content
")] + public async Task Null_Link_WithFallback_AndElement_RendersContent(string childContent, string expectedContent) + { + var output = TestContextHelpers.GetTagHelperOutput("our-link", childContent: childContent); + output.Content.SetContent(childContent); + + LinkTagHelper tagHelper = new() { Link = null, Fallback = true, FallbackElement = "div" }; + + var markup = await GetMarkupAsync(tagHelper, output); + Assert.AreEqual(expectedContent, markup); + } + + private async Task GetMarkupAsync(LinkTagHelper tagHelper, TagHelperOutput output) + { + await tagHelper.ProcessAsync(_tagHelperContext, output); + + using var txtWriter = new StringWriter(); + output.WriteTo(txtWriter, HtmlEncoder.Default); + return txtWriter.ToString(); } } }