Skip to content

Commit f863dde

Browse files
committed
Added tests for LinkTagHelper
1 parent f136cd4 commit f863dde

File tree

4 files changed

+96
-33
lines changed

4 files changed

+96
-33
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Microsoft.AspNetCore.Razor.TagHelpers;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Our.Umbraco.TagHelpers.Tests.Helpers
9+
{
10+
internal static class TestContextHelpers
11+
{
12+
public static TagHelperContext GetTagHelperContext(string id = "testid")
13+
{
14+
return new TagHelperContext(
15+
tagName: "p",
16+
allAttributes: new TagHelperAttributeList(),
17+
items: new Dictionary<object, object>(),
18+
uniqueId: id);
19+
}
20+
21+
public static TagHelperOutput GetTagHelperOutput(
22+
string tagName = "p",
23+
TagHelperAttributeList attributes = null,
24+
string childContent = "some child content")
25+
{
26+
attributes ??= new TagHelperAttributeList();
27+
28+
return new TagHelperOutput(
29+
tagName,
30+
attributes,
31+
getChildContentAsync: (useCachedResult, encoder) =>
32+
{
33+
var tagHelperContent = new DefaultTagHelperContent();
34+
var content = tagHelperContent.SetHtmlContent(childContent);
35+
return Task.FromResult(content);
36+
});
37+
}
38+
}
39+
}
Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using Microsoft.AspNetCore.Razor.TagHelpers;
22
using NUnit.Framework;
3-
using System;
4-
using System.Collections.Generic;
5-
using Our.Umbraco.TagHelpers;
63
using System.Threading.Tasks;
4+
using Our.Umbraco.TagHelpers.Tests.Helpers;
75

86
namespace Our.Umbraco.TagHelpers.Tests
97
{
@@ -15,8 +13,8 @@ public async Task Given_Predicate_Return_Contents_Or_Empty(bool predicate, strin
1513
{
1614
// Arrange
1715
var id = "unique-id";
18-
var tagHelperContext = GetTagHelperContext(id);
19-
var tagHelperOutput = GetTagHelperOutput(
16+
var tagHelperContext = TestContextHelpers.GetTagHelperContext(id);
17+
var tagHelperOutput = TestContextHelpers.GetTagHelperOutput(
2018
attributes: new TagHelperAttributeList(),
2119
childContent: childContent);
2220
tagHelperOutput.Content.SetContent(childContent);
@@ -31,32 +29,5 @@ public async Task Given_Predicate_Return_Contents_Or_Empty(bool predicate, strin
3129
// Assert
3230
Assert.AreEqual(expected, content);
3331
}
34-
35-
private static TagHelperContext GetTagHelperContext(string id = "testid")
36-
{
37-
return new TagHelperContext(
38-
tagName: "p",
39-
allAttributes: new TagHelperAttributeList(),
40-
items: new Dictionary<object, object>(),
41-
uniqueId: id);
42-
}
43-
44-
private static TagHelperOutput GetTagHelperOutput(
45-
string tagName = "p",
46-
TagHelperAttributeList attributes = null,
47-
string childContent = "some child content")
48-
{
49-
attributes = attributes ?? new TagHelperAttributeList { { "attr", "value" } };
50-
51-
return new TagHelperOutput(
52-
tagName,
53-
attributes,
54-
getChildContentAsync: (useCachedResult, encoder) =>
55-
{
56-
var tagHelperContent = new DefaultTagHelperContent();
57-
var content = tagHelperContent.SetHtmlContent(childContent);
58-
return Task.FromResult<TagHelperContent>(content);
59-
});
60-
}
6132
}
6233
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Text.Encodings.Web;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Razor.TagHelpers;
6+
using NUnit.Framework;
7+
using Our.Umbraco.TagHelpers.Tests.Helpers;
8+
using Umbraco.Cms.Core.Models;
9+
10+
namespace Our.Umbraco.TagHelpers.Tests
11+
{
12+
public class LinkTagHelperTests
13+
{
14+
15+
[TestCaseSource(nameof(TestCases))]
16+
public async Task LinkHelperTests(LinkTagHelper tagHelper, string childContent, string expectedOutput)
17+
{
18+
var tagHelperContext = TestContextHelpers.GetTagHelperContext("link");
19+
var output = TestContextHelpers.GetTagHelperOutput("our-link",
20+
attributes: new TagHelperAttributeList(),
21+
childContent: childContent);
22+
output.Content.SetContent(childContent);
23+
24+
await tagHelper.ProcessAsync(tagHelperContext, output);
25+
26+
using var txtWriter = new StringWriter();
27+
output.WriteTo(txtWriter, HtmlEncoder.Default);
28+
Assert.AreEqual(expectedOutput, txtWriter.ToString());
29+
}
30+
31+
private static IEnumerable<TestCaseData> TestCases()
32+
{
33+
var onSiteLink = new Link { Url = "/", Name = "example" };
34+
var externalLink = new Link { Url = "/", Name = "example", Target = "_blank", Type = LinkType.External };
35+
36+
return new[] {
37+
new TestCaseData(new LinkTagHelper(), "content", string.Empty).SetName("No content rendered if Link is null"),
38+
39+
new TestCaseData(new LinkTagHelper { Link = onSiteLink},null,"<a href=\"/\">example</a>").SetName("Internal link is rendered with no content"),
40+
new TestCaseData(new LinkTagHelper { Link = onSiteLink},"content","<a href=\"/\">content</a>").SetName("Internal link is rendered with content"),
41+
42+
new TestCaseData(new LinkTagHelper { Link = externalLink},null,"<a href=\"/\" target=\"_blank\" rel=\"noopener\">example</a>").SetName("External link with target is rendered with no content"),
43+
new TestCaseData(new LinkTagHelper { Link = externalLink},"content","<a href=\"/\" target=\"_blank\" rel=\"noopener\">content</a>").SetName("External link with target is rendered with content"),
44+
45+
46+
new TestCaseData(new LinkTagHelper() { Fallback = true }, "", "").SetName("Fallback with no content"),
47+
new TestCaseData(new LinkTagHelper() { Fallback = true }, "content", "content").SetName("Fallback with only content"),
48+
new TestCaseData(new LinkTagHelper() { Fallback = true, FallbackElement = "div" }, "content", "<div>content</div>")
49+
.SetName("Fallback with fallback element and content")
50+
};
51+
}
52+
}
53+
}

Our.Umbraco.TagHelpers.Tests/Our.Umbraco.TagHelpers.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>

0 commit comments

Comments
 (0)