1
+ using Microsoft . AspNetCore . Razor . TagHelpers ;
2
+ using NUnit . Framework ;
3
+ using System ;
4
+ using System . Collections . Generic ;
5
+ using Our . Umbraco . TagHelpers ;
6
+ using System . Threading . Tasks ;
7
+
8
+ namespace Our . Umbraco . TagHelpers . Tests
9
+ {
10
+ public class IncludeIfTagHelperTests
11
+ {
12
+ [ TestCase ( true , "original-child-content" , "original-child-content" ) ]
13
+ [ TestCase ( false , "original-child-content" , "" ) ]
14
+ public async Task Given_Predicate_Return_Contents_Or_Empty ( bool predicate , string childContent , string expected )
15
+ {
16
+ // Arrange
17
+ var id = "unique-id" ;
18
+ var tagHelperContext = GetTagHelperContext ( id ) ;
19
+ var tagHelperOutput = GetTagHelperOutput (
20
+ attributes : new TagHelperAttributeList ( ) ,
21
+ childContent : childContent ) ;
22
+ tagHelperOutput . Content . SetContent ( childContent ) ;
23
+
24
+ var tagHelper = new IncludeIfTagHelper { Predicate = predicate } ;
25
+
26
+ // Act
27
+ await tagHelper . ProcessAsync ( tagHelperContext , tagHelperOutput ) ;
28
+
29
+ var content = tagHelperOutput . Content . GetContent ( ) ;
30
+
31
+ // Assert
32
+ Assert . AreEqual ( expected , content ) ;
33
+ }
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
+ }
61
+ }
62
+ }
0 commit comments