@@ -14,73 +14,17 @@ namespace Microsoft.JSInterop.Tests;
1414public class JSObjectReferenceExtensionsTest
1515{
1616 [ Fact ]
17- public void AsAsyncFunction_WithVoidValueTaskFunc_ReturnsFunc ( )
18- {
19- var jsRuntime = new TestJSRuntime ( ) ;
20- var jsObjectReference = new JSObjectReference ( jsRuntime , 1 ) ;
21-
22- // Act
23- var func = jsObjectReference . AsAsyncFunction < Func < int , ValueTask > > ( ) ;
24-
25- // Assert
26- Assert . NotNull ( func ) ;
27- Assert . IsType < Func < int , ValueTask > > ( func ) ;
28- }
29-
30- [ Fact ]
31- public void AsAsyncFunction_WithVoidTaskFunc_ReturnsFunc ( )
32- {
33- var jsRuntime = new TestJSRuntime ( ) ;
34- var jsObjectReference = new JSObjectReference ( jsRuntime , 1 ) ;
35-
36- // Act
37- var func = jsObjectReference . AsAsyncFunction < Func < int , Task > > ( ) ;
38-
39- // Assert
40- Assert . NotNull ( func ) ;
41- Assert . IsType < Func < int , Task > > ( func ) ;
42- }
43-
44- [ Fact ]
45- public void AsAsyncFunction_WithValueTaskFunc_ReturnsFunc ( )
46- {
47- var jsRuntime = new TestJSRuntime ( ) ;
48- var jsObjectReference = new JSObjectReference ( jsRuntime , 1 ) ;
49-
50- // Act
51- var func = jsObjectReference . AsAsyncFunction < Func < int , ValueTask < int > > > ( ) ;
52-
53- // Assert
54- Assert . NotNull ( func ) ;
55- Assert . IsType < Func < int , ValueTask < int > > > ( func ) ;
56- }
57-
58- [ Fact ]
59- public void AsAsyncFunction_WithTaskFunc_ReturnsFunc ( )
60- {
61- var jsRuntime = new TestJSRuntime ( ) ;
62- var jsObjectReference = new JSObjectReference ( jsRuntime , 1 ) ;
63-
64- // Act
65- var func = jsObjectReference . AsAsyncFunction < Func < int , Task < int > > > ( ) ;
66-
67- // Assert
68- Assert . NotNull ( func ) ;
69- Assert . IsType < Func < int , Task < int > > > ( func ) ;
70- }
71-
72- [ Fact ]
73- public void AsAsyncFunction_WithValueTaskFunc_ReturnsFunc_ThatInvokesInterop ( )
17+ public void AsFunction_ReturnsFunc_ThatInvokesInterop ( )
7418 {
7519 // Arrange
76- var jsRuntime = new TestJSRuntime ( ) ;
20+ var jsRuntime = new RecordingTestJSRuntime ( ) ;
7721 var jsObjectReference = new JSObjectReference ( jsRuntime , 1 ) ;
7822
7923 var bytes = Encoding . UTF8 . GetBytes ( JsonSerializer . Serialize ( 42 ) ) ;
8024 var reader = new Utf8JsonReader ( bytes ) ;
8125
8226 // Act
83- var func = jsObjectReference . AsAsyncFunction < Func < int , ValueTask < int > > > ( ) ;
27+ var func = jsObjectReference . AsFunction < int , int > ( ) ;
8428 ValueTask < int > task = func ( 1 ) ;
8529
8630 jsRuntime . EndInvokeJS (
@@ -95,98 +39,13 @@ public void AsAsyncFunction_WithValueTaskFunc_ReturnsFunc_ThatInvokesInterop()
9539#pragma warning restore xUnit1031 // Do not use blocking task operations in test method
9640 }
9741
98- [ Fact ]
99- public void AsAsyncFunction_WithTaskFunc_ReturnsFunc_ThatInvokesInterop ( )
100- {
101- // Arrange
102- var jsRuntime = new TestJSRuntime ( ) ;
103- var jsObjectReference = new JSObjectReference ( jsRuntime , 1 ) ;
104-
105- var bytes = Encoding . UTF8 . GetBytes ( JsonSerializer . Serialize ( 42 ) ) ;
106- var reader = new Utf8JsonReader ( bytes ) ;
107-
108- // Act
109- var func = jsObjectReference . AsAsyncFunction < Func < int , Task < int > > > ( ) ;
110- Task < int > task = func ( 1 ) ;
111-
112- jsRuntime . EndInvokeJS (
113- jsRuntime . InvokeCalls [ 0 ] . AsyncHandle ,
114- /* succeeded: */ true ,
115- ref reader ) ;
116-
117- // Assert
118- Assert . True ( task . IsCompleted ) ;
119- #pragma warning disable xUnit1031 // Do not use blocking task operations in test method
120- Assert . Equal ( 42 , task . Result ) ;
121- #pragma warning restore xUnit1031 // Do not use blocking task operations in test method
122- }
123-
124- [ Fact ]
125- public void AsAsyncFunction_WithEventHandlerDelegate_Throws ( )
126- {
127- var jsRuntime = new TestJSRuntime ( ) ;
128- var jsObjectReference = new JSObjectReference ( jsRuntime , 1 ) ;
129-
130- // Act/Assert
131- Assert . Throws < InvalidOperationException > ( jsObjectReference . AsAsyncFunction < EventHandler > ) ;
132- }
133-
134- [ Fact ]
135- public void AsAsyncFunction_WithActionDelegate_Throws ( )
136- {
137- var jsRuntime = new TestJSRuntime ( ) ;
138- var jsObjectReference = new JSObjectReference ( jsRuntime , 1 ) ;
139-
140- // Act/Assert
141- Assert . Throws < InvalidOperationException > ( jsObjectReference . AsAsyncFunction < Action < int > > ) ;
142- }
143-
144- [ Fact ]
145- public void AsAsyncFunction_WithFuncWithInvalidReturnType_Throws ( )
146- {
147- var jsRuntime = new TestJSRuntime ( ) ;
148- var jsObjectReference = new JSObjectReference ( jsRuntime , 1 ) ;
149-
150- // Act/Assert
151- Assert . Throws < InvalidOperationException > ( jsObjectReference . AsAsyncFunction < Func < int > > ) ;
152- }
153-
154- [ Fact ]
155- public void AsAsyncFunction_WithFuncWithTooManyParams_Throws ( )
156- {
157- var jsRuntime = new TestJSRuntime ( ) ;
158- var jsObjectReference = new JSObjectReference ( jsRuntime , 1 ) ;
159-
160- // Act/Assert
161- Assert . Throws < InvalidOperationException > ( jsObjectReference . AsAsyncFunction < Func < int , int , int , int , int , int , int , int , int , Task > > ) ;
162- }
163-
164- class TestJSRuntime : JSInProcessRuntime
42+ class RecordingTestJSRuntime : TestJSRuntime
16543 {
16644 public List < JSInvocationInfo > InvokeCalls { get ; set ; } = [ ] ;
16745
168- public string ? NextResultJson { get ; set ; }
169-
170- protected override string ? InvokeJS ( string identifier , string ? argsJson , JSCallResultType resultType , long targetInstanceId )
171- {
172- throw new NotImplementedException ( ) ;
173- }
174-
175- protected override string ? InvokeJS ( in JSInvocationInfo invocationInfo )
176- {
177- InvokeCalls . Add ( invocationInfo ) ;
178- return NextResultJson ;
179- }
180-
181- protected override void BeginInvokeJS ( long taskId , string identifier , [ StringSyntax ( "Json" ) ] string ? argsJson , JSCallResultType resultType , long targetInstanceId )
182- => throw new NotImplementedException ( "This test only covers sync calls" ) ;
183-
18446 protected override void BeginInvokeJS ( in JSInvocationInfo invocationInfo )
18547 {
18648 InvokeCalls . Add ( invocationInfo ) ;
18749 }
188-
189- protected internal override void EndInvokeDotNet ( DotNetInvocationInfo invocationInfo , in DotNetInvocationResult invocationResult )
190- => throw new NotImplementedException ( "This test only covers sync calls" ) ;
19150 }
19251}
0 commit comments