1- using System . Collections . Generic ;
2- using System . IO ;
1+ using System . IO ;
32using System . Text . Encodings . Web ;
43using System . Threading . Tasks ;
54using Microsoft . AspNetCore . Razor . TagHelpers ;
@@ -11,43 +10,105 @@ namespace Our.Umbraco.TagHelpers.Tests
1110{
1211 public class LinkTagHelperTests
1312 {
13+ private static readonly Link _onSiteLink = new ( ) { Url = "/" , Name = "example" } ;
1414
15- [ TestCaseSource ( nameof ( TestCases ) ) ]
16- public async Task LinkHelperTests ( LinkTagHelper tagHelper , string childContent , string expectedOutput )
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 )
1729 {
18- var tagHelperContext = TestContextHelpers . GetTagHelperContext ( "link" ) ;
19- var output = TestContextHelpers . GetTagHelperOutput ( "our-link" ,
20- attributes : new TagHelperAttributeList ( ) ,
21- childContent : childContent ) ;
30+ var output = TestContextHelpers . GetTagHelperOutput ( "our-link" , childContent : childContent ) ;
2231 output . Content . SetContent ( childContent ) ;
2332
24- await tagHelper . ProcessAsync ( tagHelperContext , output ) ;
33+ LinkTagHelper tagHelper = new ( ) { Link = _onSiteLink } ;
2534
26- using var txtWriter = new StringWriter ( ) ;
27- output . WriteTo ( txtWriter , HtmlEncoder . Default ) ;
28- Assert . AreEqual ( expectedOutput , txtWriter . ToString ( ) ) ;
35+ var markup = await GetMarkupAsync ( tagHelper , output ) ;
36+ Assert . AreEqual ( $ "<a href=\" /\" >{ expectedContent } </a>", markup ) ;
2937 }
3038
31- private static IEnumerable < TestCaseData > TestCases ( )
39+ [ TestCase ( "" , "example" ) ]
40+ [ TestCase ( null , "example" ) ]
41+ [ TestCase ( "content" , "content" ) ]
42+ public async Task External_Link_Renders_AnchorAroundContentOrLinkName ( string childContent , string expectedContent )
3243 {
33- var onSiteLink = new Link { Url = "/ ", Name = "example" } ;
34- var externalLink = new Link { Url = "/" , Name = "example" , Target = "_blank" , Type = LinkType . External } ;
44+ var output = TestContextHelpers . GetTagHelperOutput ( "our-link ", childContent : childContent ) ;
45+ output . Content . SetContent ( childContent ) ;
3546
36- return new [ ] {
37- new TestCaseData ( new LinkTagHelper ( ) , "content" , string . Empty ) . SetName ( "No content rendered if Link is null" ) ,
47+ LinkTagHelper tagHelper = new ( ) { Link = _externalLink } ;
3848
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" ) ,
49+ var markup = await GetMarkupAsync ( tagHelper , output ) ;
50+ Assert . AreEqual ( $ "<a href=\" /\" target=\" _blank\" rel=\" noopener\" >{ expectedContent } </a>", markup ) ;
51+ }
4152
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" ) ,
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+ }
4464
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+ }
4576
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- } ;
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 ( ) ;
51112 }
52113 }
53114}
0 commit comments