44using System ;
55using System . Collections . Generic ;
66using System . Linq ;
7- using System . Linq . Expressions ;
87using System . Threading . Tasks ;
9- using Microsoft . AspNetCore . Components . Rendering ;
10- using Microsoft . AspNetCore . Components . RenderTree ;
118using Microsoft . AspNetCore . Components . Test . Helpers ;
129using Xunit ;
1310
@@ -35,7 +32,7 @@ public async Task ThrowsIfEditContextChanges()
3532 // Arrange
3633 var model = new TestModel ( ) ;
3734 var rootComponent = new TestInputHostComponent < string , TestInputComponent < string > > { EditContext = new EditContext ( model ) , ValueExpression = ( ) => model . StringProperty } ;
38- await RenderAndGetTestInputComponentAsync ( rootComponent ) ;
35+ await InputRenderer . RenderAndGetComponent ( rootComponent ) ;
3936
4037 // Act/Assert
4138 rootComponent . EditContext = new EditContext ( model ) ;
@@ -51,7 +48,7 @@ public async Task ThrowsIfNoValueExpressionIsSupplied()
5148 var rootComponent = new TestInputHostComponent < string , TestInputComponent < string > > { EditContext = new EditContext ( model ) } ;
5249
5350 // Act/Assert
54- var ex = await Assert . ThrowsAsync < InvalidOperationException > ( ( ) => RenderAndGetTestInputComponentAsync ( rootComponent ) ) ;
51+ var ex = await Assert . ThrowsAsync < InvalidOperationException > ( ( ) => InputRenderer . RenderAndGetComponent ( rootComponent ) ) ;
5552 Assert . Contains ( $ "{ typeof ( TestInputComponent < string > ) } requires a value for the 'ValueExpression' parameter. Normally this is provided automatically when using 'bind-Value'.", ex . Message ) ;
5653 }
5754
@@ -68,7 +65,7 @@ public async Task GetsCurrentValueFromValueParameter()
6865 } ;
6966
7067 // Act
71- var inputComponent = await RenderAndGetTestInputComponentAsync ( rootComponent ) ;
68+ var inputComponent = await InputRenderer . RenderAndGetComponent ( rootComponent ) ;
7269
7370 // Assert
7471 Assert . Equal ( "some value" , inputComponent . CurrentValue ) ;
@@ -87,7 +84,7 @@ public async Task ExposesEditContextToSubclass()
8784 } ;
8885
8986 // Act
90- var inputComponent = await RenderAndGetTestInputComponentAsync ( rootComponent ) ;
87+ var inputComponent = await InputRenderer . RenderAndGetComponent ( rootComponent ) ;
9188
9289 // Assert
9390 Assert . Same ( rootComponent . EditContext , inputComponent . EditContext ) ;
@@ -106,7 +103,7 @@ public async Task ExposesFieldIdentifierToSubclass()
106103 } ;
107104
108105 // Act
109- var inputComponent = await RenderAndGetTestInputComponentAsync ( rootComponent ) ;
106+ var inputComponent = await InputRenderer . RenderAndGetComponent ( rootComponent ) ;
110107
111108 // Assert
112109 Assert . Equal ( FieldIdentifier . Create ( ( ) => model . StringProperty ) , inputComponent . FieldIdentifier ) ;
@@ -123,7 +120,7 @@ public async Task CanReadBackChangesToCurrentValue()
123120 Value = "initial value" ,
124121 ValueExpression = ( ) => model . StringProperty
125122 } ;
126- var inputComponent = await RenderAndGetTestInputComponentAsync ( rootComponent ) ;
123+ var inputComponent = await InputRenderer . RenderAndGetComponent ( rootComponent ) ;
127124 Assert . Equal ( "initial value" , inputComponent . CurrentValue ) ;
128125
129126 // Act
@@ -146,7 +143,7 @@ public async Task WritingToCurrentValueInvokesValueChangedIfDifferent()
146143 ValueChanged = val => valueChangedCallLog . Add ( val ) ,
147144 ValueExpression = ( ) => model . StringProperty
148145 } ;
149- var inputComponent = await RenderAndGetTestInputComponentAsync ( rootComponent ) ;
146+ var inputComponent = await InputRenderer . RenderAndGetComponent ( rootComponent ) ;
150147 Assert . Empty ( valueChangedCallLog ) ;
151148
152149 // Act
@@ -169,7 +166,7 @@ public async Task WritingToCurrentValueDoesNotInvokeValueChangedIfUnchanged()
169166 ValueChanged = val => valueChangedCallLog . Add ( val ) ,
170167 ValueExpression = ( ) => model . StringProperty
171168 } ;
172- var inputComponent = await RenderAndGetTestInputComponentAsync ( rootComponent ) ;
169+ var inputComponent = await InputRenderer . RenderAndGetComponent ( rootComponent ) ;
173170 Assert . Empty ( valueChangedCallLog ) ;
174171
175172 // Act
@@ -190,7 +187,7 @@ public async Task WritingToCurrentValueNotifiesEditContext()
190187 Value = "initial value" ,
191188 ValueExpression = ( ) => model . StringProperty
192189 } ;
193- var inputComponent = await RenderAndGetTestInputComponentAsync ( rootComponent ) ;
190+ var inputComponent = await InputRenderer . RenderAndGetComponent ( rootComponent ) ;
194191 Assert . False ( rootComponent . EditContext . IsModified ( ( ) => model . StringProperty ) ) ;
195192
196193 // Act
@@ -213,7 +210,7 @@ public async Task SuppliesFieldClassCorrespondingToFieldState()
213210 var fieldIdentifier = FieldIdentifier . Create ( ( ) => model . StringProperty ) ;
214211
215212 // Act/Assert: Initially, it's valid and unmodified
216- var inputComponent = await RenderAndGetTestInputComponentAsync ( rootComponent ) ;
213+ var inputComponent = await InputRenderer . RenderAndGetComponent ( rootComponent ) ;
217214 Assert . Equal ( "valid" , inputComponent . CssClass ) ; // no Class was specified
218215
219216 // Act/Assert: Modify the field
@@ -251,7 +248,7 @@ public async Task CssClassCombinesClassWithFieldClass()
251248 var fieldIdentifier = FieldIdentifier . Create ( ( ) => model . StringProperty ) ;
252249
253250 // Act/Assert
254- var inputComponent = await RenderAndGetTestInputComponentAsync ( rootComponent ) ;
251+ var inputComponent = await InputRenderer . RenderAndGetComponent ( rootComponent ) ;
255252 Assert . Equal ( "my-class other-class valid" , inputComponent . CssClass ) ;
256253
257254 // Act/Assert: Retains custom class when changing field class
@@ -270,7 +267,7 @@ public async Task SuppliesCurrentValueAsStringWithFormatting()
270267 Value = new DateTime ( 1915 , 3 , 2 ) ,
271268 ValueExpression = ( ) => model . DateProperty
272269 } ;
273- var inputComponent = await RenderAndGetTestInputComponentAsync ( rootComponent ) ;
270+ var inputComponent = await InputRenderer . RenderAndGetComponent ( rootComponent ) ;
274271
275272 // Act/Assert
276273 Assert . Equal ( "1915/03/02" , inputComponent . CurrentValueAsString ) ;
@@ -289,7 +286,7 @@ public async Task ParsesCurrentValueAsStringWhenChanged_Valid()
289286 ValueExpression = ( ) => model . DateProperty
290287 } ;
291288 var fieldIdentifier = FieldIdentifier . Create ( ( ) => model . DateProperty ) ;
292- var inputComponent = await RenderAndGetTestInputComponentAsync ( rootComponent ) ;
289+ var inputComponent = await InputRenderer . RenderAndGetComponent ( rootComponent ) ;
293290 var numValidationStateChanges = 0 ;
294291 rootComponent . EditContext . OnValidationStateChanged += ( sender , eventArgs ) => { numValidationStateChanges ++ ; } ;
295292
@@ -319,7 +316,7 @@ public async Task ParsesCurrentValueAsStringWhenChanged_Invalid()
319316 ValueExpression = ( ) => model . DateProperty
320317 } ;
321318 var fieldIdentifier = FieldIdentifier . Create ( ( ) => model . DateProperty ) ;
322- var inputComponent = await RenderAndGetTestInputComponentAsync ( rootComponent ) ;
319+ var inputComponent = await InputRenderer . RenderAndGetComponent ( rootComponent ) ;
323320 var numValidationStateChanges = 0 ;
324321 rootComponent . EditContext . OnValidationStateChanged += ( sender , eventArgs ) => { numValidationStateChanges ++ ; } ;
325322
@@ -470,21 +467,6 @@ public async Task UserSpecifiedAriaValueIsNotChangedIfInvalid()
470467 Assert . Equal ( "userSpecifiedValue" , component . AdditionalAttributes [ "aria-invalid" ] ) ;
471468 }
472469
473- private static TComponent FindComponent < TComponent > ( CapturedBatch batch )
474- => batch . ReferenceFrames
475- . Where ( f => f . FrameType == RenderTreeFrameType . Component )
476- . Select ( f => f . Component )
477- . OfType < TComponent > ( )
478- . Single ( ) ;
479-
480- private static async Task < TComponent > RenderAndGetTestInputComponentAsync < TValue , TComponent > ( TestInputHostComponent < TValue , TComponent > hostComponent ) where TComponent : TestInputComponent < TValue >
481- {
482- var testRenderer = new TestRenderer ( ) ;
483- var componentId = testRenderer . AssignRootComponentId ( hostComponent ) ;
484- await testRenderer . RenderRootComponentAsync ( componentId ) ;
485- return FindComponent < TComponent > ( testRenderer . Batches . Single ( ) ) ;
486- }
487-
488470 class TestModel
489471 {
490472 public string StringProperty { get ; set ; }
@@ -530,7 +512,7 @@ public async Task SetCurrentValueAsStringAsync(string value)
530512 }
531513 }
532514
533- class TestDateInputComponent : TestInputComponent < DateTime >
515+ private class TestDateInputComponent : TestInputComponent < DateTime >
534516 {
535517 protected override string FormatValueAsString ( DateTime value )
536518 => value . ToString ( "yyyy/MM/dd" ) ;
@@ -549,35 +531,5 @@ protected override bool TryParseValueFromString(string value, out DateTime resul
549531 }
550532 }
551533 }
552-
553- class TestInputHostComponent < TValue , TComponent > : AutoRenderComponent where TComponent : TestInputComponent < TValue >
554- {
555- public Dictionary < string , object > AdditionalAttributes { get ; set ; }
556-
557- public EditContext EditContext { get ; set ; }
558-
559- public TValue Value { get ; set ; }
560-
561- public Action < TValue > ValueChanged { get ; set ; }
562-
563- public Expression < Func < TValue > > ValueExpression { get ; set ; }
564-
565- protected override void BuildRenderTree ( RenderTreeBuilder builder )
566- {
567- builder . OpenComponent < CascadingValue < EditContext > > ( 0 ) ;
568- builder . AddAttribute ( 1 , "Value" , EditContext ) ;
569- builder . AddAttribute ( 2 , "ChildContent" , new RenderFragment ( childBuilder =>
570- {
571- childBuilder . OpenComponent < TComponent > ( 0 ) ;
572- childBuilder . AddAttribute ( 0 , "Value" , Value ) ;
573- childBuilder . AddAttribute ( 1 , "ValueChanged" ,
574- EventCallback . Factory . Create ( this , ValueChanged ) ) ;
575- childBuilder . AddAttribute ( 2 , "ValueExpression" , ValueExpression ) ;
576- childBuilder . AddMultipleAttributes ( 3 , AdditionalAttributes ) ;
577- childBuilder . CloseComponent ( ) ;
578- } ) ) ;
579- builder . CloseComponent ( ) ;
580- }
581- }
582534 }
583535}
0 commit comments