-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from Matthew-Wise/feature/optional-link
Added tests for LinkTagHelper
- Loading branch information
Showing
4 changed files
with
157 additions
and
33 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
Our.Umbraco.TagHelpers.Tests/Helpers/TestContextHelpers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
Our.Umbraco.TagHelpers.Tests/Our.Umbraco.TagHelpers.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters