|
| 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 | +} |
0 commit comments