22// The .NET Foundation licenses this file to you under the MIT license.
33
44using System . Text . Encodings . Web ;
5+ using Microsoft . AspNetCore . Components ;
6+ using Microsoft . AspNetCore . Components . Infrastructure ;
57using Microsoft . AspNetCore . Components . Rendering ;
6- using Microsoft . AspNetCore . Html ;
8+ using Microsoft . AspNetCore . Components . Routing ;
9+ using Microsoft . AspNetCore . Components . Web ;
10+ using Microsoft . AspNetCore . DataProtection ;
711using Microsoft . AspNetCore . Http ;
812using Microsoft . AspNetCore . Mvc . Rendering ;
913using Microsoft . AspNetCore . Mvc . ViewFeatures ;
1014using Microsoft . AspNetCore . Razor . TagHelpers ;
1115using Microsoft . Extensions . DependencyInjection ;
12- using Microsoft . Extensions . Logging ;
13- using Microsoft . Extensions . Logging . Abstractions ;
1416using Moq ;
1517
1618namespace Microsoft . AspNetCore . Mvc . TagHelpers ;
@@ -21,10 +23,12 @@ public class ComponentTagHelperTest
2123 public async Task ProcessAsync_RendersComponent ( )
2224 {
2325 // Arrange
26+ var viewContext = GetViewContext ( ) ;
2427 var tagHelper = new ComponentTagHelper
2528 {
26- ViewContext = GetViewContext ( ) ,
29+ ViewContext = viewContext ,
2730 RenderMode = RenderMode . Static ,
31+ ComponentType = typeof ( TestComponent ) ,
2832 } ;
2933 var context = GetTagHelperContext ( ) ;
3034 var output = GetTagHelperOutput ( ) ;
@@ -33,8 +37,9 @@ public async Task ProcessAsync_RendersComponent()
3337 await tagHelper . ProcessAsync ( context , output ) ;
3438
3539 // Assert
36- var content = HtmlContentUtilities . HtmlContentToString ( output . Content ) ;
37- Assert . Equal ( "Hello world" , content ) ;
40+ var htmlRenderer = viewContext . HttpContext . RequestServices . GetRequiredService < HtmlRenderer > ( ) ;
41+ var content = await htmlRenderer . Dispatcher . InvokeAsync ( ( ) => HtmlContentUtilities . HtmlContentToString ( output . Content ) ) ;
42+ Assert . Equal ( "Hello from the component" , content ) ;
3843 Assert . Null ( output . TagName ) ;
3944 }
4045
@@ -73,23 +78,31 @@ private static TagHelperOutput GetTagHelperOutput()
7378
7479 private ViewContext GetViewContext ( )
7580 {
76- var htmlContent = new HtmlContentBuilder ( ) . AppendHtml ( "Hello world" ) ;
77- var renderer = Mock . Of < IComponentRenderer > ( c =>
78- c . RenderComponentAsync ( It . IsAny < ViewContext > ( ) , It . IsAny < Type > ( ) , It . IsAny < RenderMode > ( ) , It . IsAny < object > ( ) ) == new ValueTask < IHtmlContent > ( htmlContent ) ) ;
81+ var navManager = new Mock < NavigationManager > ( ) ;
82+ navManager . As < IHostEnvironmentNavigationManager > ( ) ;
7983
8084 var httpContext = new DefaultHttpContext
8185 {
8286 RequestServices = new ServiceCollection ( )
83- . AddSingleton < IComponentRenderer > ( renderer )
87+ . AddScoped < ComponentPrerenderer > ( )
88+ . AddScoped < ServerComponentSerializer > ( )
89+ . AddScoped ( _ => Mock . Of < IDataProtectionProvider > (
90+ x => x . CreateProtector ( It . IsAny < string > ( ) ) == Mock . Of < IDataProtector > ( ) ) )
8491 . AddSingleton < HtmlRenderer > ( )
85- . AddSingleton < ILoggerFactory > ( NullLoggerFactory . Instance )
86- . AddSingleton < HtmlEncoder > ( HtmlEncoder . Default )
92+ . AddLogging ( )
93+ . AddScoped < ComponentStatePersistenceManager > ( )
94+ . AddScoped ( _ => navManager . Object )
8795 . BuildServiceProvider ( ) ,
8896 } ;
8997
90- return new ViewContext
98+ return new ViewContext { HttpContext = httpContext } ;
99+ }
100+
101+ private class TestComponent : ComponentBase
102+ {
103+ protected override void BuildRenderTree ( RenderTreeBuilder builder )
91104 {
92- HttpContext = httpContext ,
93- } ;
105+ builder . AddContent ( 0 , "Hello from the component" ) ;
106+ }
94107 }
95108}
0 commit comments