Skip to content

Commit 09b25f5

Browse files
author
Warren Buckley
authored
Merge pull request #61 from Matthew-Wise/feature/optional-link
Added tests for LinkTagHelper
2 parents c588003 + 9da891e commit 09b25f5

File tree

4 files changed

+157
-33
lines changed

4 files changed

+157
-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: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using System.IO;
2+
using System.Text.Encodings.Web;
3+
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.Razor.TagHelpers;
5+
using NUnit.Framework;
6+
using Our.Umbraco.TagHelpers.Tests.Helpers;
7+
using Umbraco.Cms.Core.Models;
8+
9+
namespace Our.Umbraco.TagHelpers.Tests
10+
{
11+
public class LinkTagHelperTests
12+
{
13+
private static readonly Link _onSiteLink = new() { Url = "/", Name = "example" };
14+
15+
private static readonly Link _externalLink = new() { Url = "/", Name = "example", Target = "_blank", Type = LinkType.External };
16+
17+
private TagHelperContext _tagHelperContext;
18+
19+
[SetUp]
20+
public void SetUp()
21+
{
22+
_tagHelperContext = TestContextHelpers.GetTagHelperContext("link");
23+
}
24+
25+
[TestCase("", "example")]
26+
[TestCase(null, "example")]
27+
[TestCase("content", "content")]
28+
public async Task Internal_Link_Renders_AnchorAroundContentOrLinkName(string childContent, string expectedContent)
29+
{
30+
var output = TestContextHelpers.GetTagHelperOutput("our-link", childContent: childContent);
31+
output.Content.SetContent(childContent);
32+
33+
LinkTagHelper tagHelper = new() { Link = _onSiteLink };
34+
35+
var markup = await GetMarkupAsync(tagHelper, output);
36+
Assert.AreEqual($"<a href=\"/\">{expectedContent}</a>", markup);
37+
}
38+
39+
[TestCase("", "example")]
40+
[TestCase(null, "example")]
41+
[TestCase("content", "content")]
42+
public async Task External_Link_Renders_AnchorAroundContentOrLinkName(string childContent, string expectedContent)
43+
{
44+
var output = TestContextHelpers.GetTagHelperOutput("our-link", childContent: childContent);
45+
output.Content.SetContent(childContent);
46+
47+
LinkTagHelper tagHelper = new() { Link = _externalLink };
48+
49+
var markup = await GetMarkupAsync(tagHelper, output);
50+
Assert.AreEqual($"<a href=\"/\" target=\"_blank\" rel=\"noopener\">{expectedContent}</a>", markup);
51+
}
52+
53+
[Test]
54+
public async Task NoUrl_WithoutFallback_RendersNothing()
55+
{
56+
var output = TestContextHelpers.GetTagHelperOutput("our-link", childContent: string.Empty);
57+
output.Content.SetContent(string.Empty);
58+
59+
LinkTagHelper tagHelper = new() { Link = new() };
60+
61+
var markup = await GetMarkupAsync(tagHelper, output);
62+
Assert.AreEqual(string.Empty, markup);
63+
}
64+
65+
[Test]
66+
public async Task Null_Link_WithoutFallback_RendersNothing()
67+
{
68+
var output = TestContextHelpers.GetTagHelperOutput("our-link", childContent: string.Empty);
69+
output.Content.SetContent(string.Empty);
70+
71+
LinkTagHelper tagHelper = new() { Link = null };
72+
73+
var markup = await GetMarkupAsync(tagHelper, output);
74+
Assert.AreEqual(string.Empty, markup);
75+
}
76+
77+
[TestCase("", "")]
78+
[TestCase(null, "")]
79+
[TestCase("content", "content")]
80+
public async Task Null_Link_WithFallback_NoElement_RendersContent(string childContent, string expectedContent)
81+
{
82+
var output = TestContextHelpers.GetTagHelperOutput("our-link", childContent: childContent);
83+
output.Content.SetContent(childContent);
84+
85+
LinkTagHelper tagHelper = new() { Link = null, Fallback = true };
86+
87+
var markup = await GetMarkupAsync(tagHelper, output);
88+
Assert.AreEqual(expectedContent, markup);
89+
}
90+
91+
[TestCase("", "")]
92+
[TestCase(null, "")]
93+
[TestCase("content", "<div>content</div>")]
94+
public async Task Null_Link_WithFallback_AndElement_RendersContent(string childContent, string expectedContent)
95+
{
96+
var output = TestContextHelpers.GetTagHelperOutput("our-link", childContent: childContent);
97+
output.Content.SetContent(childContent);
98+
99+
LinkTagHelper tagHelper = new() { Link = null, Fallback = true, FallbackElement = "div" };
100+
101+
var markup = await GetMarkupAsync(tagHelper, output);
102+
Assert.AreEqual(expectedContent, markup);
103+
}
104+
105+
private async Task<string> GetMarkupAsync(LinkTagHelper tagHelper, TagHelperOutput output)
106+
{
107+
await tagHelper.ProcessAsync(_tagHelperContext, output);
108+
109+
using var txtWriter = new StringWriter();
110+
output.WriteTo(txtWriter, HtmlEncoder.Default);
111+
return txtWriter.ToString();
112+
}
113+
}
114+
}

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)