1+ using ICG . AspNetCore . Utilities . Bootstrap5TagHelpers . Contexts ;
2+ using ICG . AspNetCore . Utilities . Bootstrap5TagHelpers . Modal ;
3+ using ICG . AspNetCore . Utilities . Bootstrap5TagHelpers . OffCanvas ;
4+ using Microsoft . AspNetCore . Razor . TagHelpers ;
5+ using System ;
6+ using System . Collections . Generic ;
7+ using System . Linq ;
8+ using System . Text ;
9+ using System . Threading . Tasks ;
10+
11+ namespace ICG . AspNetCore . Utilities . Bootstrap5TagHelpers . Tests . OffCanvas ;
12+ public class OffCanvasHeaderTagHelperTests : AbstractTagHelperTest
13+ {
14+ [ Fact ]
15+ public async Task Should_ThrowException_WhenMissingContext ( )
16+ {
17+ //Arrange
18+ TagHelperContext context = MakeTagHelperContext ( ) ;
19+ TagHelperOutput output = MakeTagHelperOutput ( " " ) ;
20+
21+ //Act
22+ var helper = new OffCanvasHeaderTagHelper ( ) ;
23+ Exception exceptionResult = await Record . ExceptionAsync ( ( ) => helper . ProcessAsync ( context , output ) ) ;
24+
25+ Assert . NotNull ( exceptionResult ) ;
26+ Assert . IsType < KeyNotFoundException > ( exceptionResult ) ;
27+ }
28+
29+ [ Fact ]
30+ public async Task Should_ThrowException_WhenContextIsNull ( )
31+ {
32+ //Arrange
33+ TagHelperContext context = MakeTagHelperContext ( ) ;
34+ context . Items . Add ( typeof ( OffCanvasContext ) , null ) ;
35+ TagHelperOutput output = MakeTagHelperOutput ( " " ) ;
36+
37+ //Act
38+ var helper = new OffCanvasHeaderTagHelper ( ) ;
39+ Exception exceptionResult = await Record . ExceptionAsync ( ( ) => helper . ProcessAsync ( context , output ) ) ;
40+
41+ Assert . NotNull ( exceptionResult ) ;
42+ Assert . IsType < ArgumentException > ( exceptionResult ) ;
43+ }
44+
45+ [ Fact ]
46+ public async Task Should_Render_As_Div ( )
47+ {
48+ //Arrange
49+ TagHelperContext context = MakeTagHelperContext ( ) ;
50+ context . Items . Add ( typeof ( OffCanvasContext ) , new OffCanvasContext ( ) ) ;
51+ TagHelperOutput output = MakeTagHelperOutput ( " " ) ;
52+
53+ //Act
54+ var helper = new OffCanvasHeaderTagHelper ( ) ;
55+ await helper . ProcessAsync ( context , output ) ;
56+
57+ //Assert
58+ Assert . Equal ( "div" , output . TagName ) ;
59+ }
60+
61+ [ Fact ]
62+ public async Task Should_Render_With_ClassAdded ( )
63+ {
64+ //Arrange
65+ TagHelperContext context = MakeTagHelperContext ( ) ;
66+ context . Items . Add ( typeof ( OffCanvasContext ) , new OffCanvasContext ( ) ) ;
67+ TagHelperOutput output = MakeTagHelperOutput ( " " ) ;
68+
69+ //Act
70+ var helper = new OffCanvasHeaderTagHelper ( ) ;
71+ await helper . ProcessAsync ( context , output ) ;
72+
73+ //Assert
74+ Assert . Equal ( "offcanvas-header" , output . Attributes [ "class" ] . Value ) ;
75+ }
76+
77+ [ Fact ]
78+ public async Task Should_Render_With_ClassAdded_PreservingCustomClasses ( )
79+ {
80+ //Arrange
81+ var customClass = "testing-out" ;
82+ var expectedClass = $ "{ customClass } offcanvas-header";
83+ var existingAttributes = new TagHelperAttributeList ( new List < TagHelperAttribute > { new ( "class" , customClass ) } ) ;
84+ TagHelperContext context = MakeTagHelperContext ( ) ;
85+ context . Items . Add ( typeof ( OffCanvasContext ) , new OffCanvasContext ( ) ) ;
86+ TagHelperOutput output = MakeTagHelperOutput ( " " , existingAttributes ) ;
87+
88+ //Act
89+ var helper = new OffCanvasHeaderTagHelper ( ) ;
90+ await helper . ProcessAsync ( context , output ) ;
91+
92+ //Assert
93+ Assert . Equal ( expectedClass , output . Attributes [ "class" ] . Value . ToString ( ) ) ;
94+ }
95+
96+ [ Fact ]
97+ public async Task Should_NotRender_InnerContent_When_Title_Missing ( )
98+ {
99+ //Arrange
100+ TagHelperContext context = MakeTagHelperContext ( ) ;
101+ context . Items . Add ( typeof ( OffCanvasContext ) , new OffCanvasContext ( ) ) ;
102+ TagHelperOutput output = MakeTagHelperOutput ( " " ) ;
103+
104+ //Act
105+ var helper = new OffCanvasHeaderTagHelper ( ) ;
106+ await helper . ProcessAsync ( context , output ) ;
107+
108+ //Assert
109+ Assert . Equal ( "" , output . Content . GetContent ( ) ) ;
110+ }
111+
112+ [ Theory ]
113+ [ InlineData ( "My Title" , "" , "<h5 class=\" offcanvas-title\" >My Title</h5>" ) ]
114+ [ InlineData ( "My Title" , "myOffcanvas" , "<h5 class=\" offcanvas-title\" id=\" myOffcanvasLabel\" >My Title</h5>" ) ]
115+ public async Task Should_Render_InnerContent_Title_When_Title_Provided ( string title , string id , string expectedHtml )
116+ {
117+ //Arrange
118+ TagHelperContext context = MakeTagHelperContext ( ) ;
119+ context . Items . Add ( typeof ( OffCanvasContext ) , new OffCanvasContext { Id = id } ) ;
120+ TagHelperOutput output = MakeTagHelperOutput ( " " ) ;
121+
122+ //Act
123+ var helper = new OffCanvasHeaderTagHelper { Title = title } ;
124+ await helper . ProcessAsync ( context , output ) ;
125+
126+ //Assert
127+ Assert . Equal ( expectedHtml , output . Content . GetContent ( ) ) ;
128+ }
129+
130+ [ Theory ]
131+ [ InlineData ( "My Title" , "" , "" , "<h5 class=\" offcanvas-title\" >My Title</h5>" ) ]
132+ [ InlineData ( "My Title" , "myOffcanvas" , "h4" , "<h4 class=\" offcanvas-title\" id=\" myOffcanvasLabel\" >My Title</h4>" ) ]
133+ public async Task Should_Render_InnerContent_Title_WithCustomTag_When_Title_Provided ( string title , string id , string tag , string expectedHtml )
134+ {
135+ //Arrange
136+ TagHelperContext context = MakeTagHelperContext ( ) ;
137+ context . Items . Add ( typeof ( OffCanvasContext ) , new OffCanvasContext { Id = id } ) ;
138+ TagHelperOutput output = MakeTagHelperOutput ( " " ) ;
139+
140+ //Act
141+ var helper = new OffCanvasHeaderTagHelper { Title = title } ;
142+ if ( ! string . IsNullOrEmpty ( tag ) )
143+ helper . TitleTag = tag ;
144+ await helper . ProcessAsync ( context , output ) ;
145+
146+ //Assert
147+ Assert . Equal ( expectedHtml , output . Content . GetContent ( ) ) ;
148+ }
149+ }
0 commit comments