Skip to content

Commit

Permalink
Merge pull request #61 from Matthew-Wise/feature/optional-link
Browse files Browse the repository at this point in the history
Added tests for LinkTagHelper
  • Loading branch information
Warren Buckley authored May 5, 2023
2 parents c588003 + 9da891e commit 09b25f5
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 33 deletions.
39 changes: 39 additions & 0 deletions Our.Umbraco.TagHelpers.Tests/Helpers/TestContextHelpers.cs
Original file line number Diff line number Diff line change
@@ -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<object, object>(),
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);
});
}
}
}
35 changes: 3 additions & 32 deletions Our.Umbraco.TagHelpers.Tests/IncludeIfTagHelperTests.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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);
Expand All @@ -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<object, object>(),
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<TagHelperContent>(content);
});
}
}
}
114 changes: 114 additions & 0 deletions Our.Umbraco.TagHelpers.Tests/LinkTagHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
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
{
private static readonly Link _onSiteLink = new() { Url = "/", Name = "example" };

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 output = TestContextHelpers.GetTagHelperOutput("our-link", childContent: childContent);
output.Content.SetContent(childContent);

LinkTagHelper tagHelper = new() { Link = _onSiteLink };

var markup = await GetMarkupAsync(tagHelper, output);
Assert.AreEqual($"<a href=\"/\">{expectedContent}</a>", markup);
}

[TestCase("", "example")]
[TestCase(null, "example")]
[TestCase("content", "content")]
public async Task External_Link_Renders_AnchorAroundContentOrLinkName(string childContent, string expectedContent)
{
var output = TestContextHelpers.GetTagHelperOutput("our-link", childContent: childContent);
output.Content.SetContent(childContent);

LinkTagHelper tagHelper = new() { Link = _externalLink };

var markup = await GetMarkupAsync(tagHelper, output);
Assert.AreEqual($"<a href=\"/\" target=\"_blank\" rel=\"noopener\">{expectedContent}</a>", markup);
}

[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);
}

[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", "<div>content</div>")]
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<string> GetMarkupAsync(LinkTagHelper tagHelper, TagHelperOutput output)
{
await tagHelper.ProcessAsync(_tagHelperContext, output);

using var txtWriter = new StringWriter();
output.WriteTo(txtWriter, HtmlEncoder.Default);
return txtWriter.ToString();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
Expand Down

0 comments on commit 09b25f5

Please sign in to comment.