Skip to content

Commit 12150dc

Browse files
committed
Add E2E test for interop that uses an object reference to a JS function
1 parent 571106f commit 12150dc

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/Components/test/E2ETest/Tests/InteropTest.cs

+4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ public void CanInvokeInteropMethods()
107107
["invokeNewWithClassConstructorAsync.dataProperty"] = "abraka",
108108
["invokeNewWithClassConstructorAsync.function"] = "6",
109109
["invokeNewWithNonConstructorAsync"] = "Success",
110+
// Function reference tests
111+
["changeFunctionViaObjectReferenceAsync"] = "42"
110112
};
111113

112114
var expectedSyncValues = new Dictionary<string, string>
@@ -171,6 +173,8 @@ public void CanInvokeInteropMethods()
171173
["invokeNewWithClassConstructor.dataProperty"] = "abraka",
172174
["invokeNewWithClassConstructor.function"] = "6",
173175
["invokeNewWithNonConstructor"] = "Success",
176+
// Function reference tests
177+
["changeFunctionViaObjectReference"] = "42"
174178
};
175179

176180
// Include the sync assertions only when running under WebAssembly

src/Components/test/testassets/BasicTestApp/InteropComponent.razor

+25
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,13 @@
301301
InvokeNewTests();
302302
}
303303

304+
await FunctionReferenceAsyncTests();
305+
306+
if (shouldSupportSyncInterop)
307+
{
308+
FunctionReferenceTests();
309+
}
310+
304311
Invocations = invocations;
305312
DoneWithInterop = true;
306313
}
@@ -601,6 +608,24 @@
601608
}
602609
}
603610

611+
private async Task FunctionReferenceAsyncTests()
612+
{
613+
var funcRef = await JSRuntime.GetValueAsync<IJSObjectReference>("jsInteropTests.nonConstructorFunction");
614+
var testClassRef = await JSRuntime.InvokeNewAsync("jsInteropTests.TestClass", "abraka");
615+
await testClassRef.SetValueAsync("getTextLength", funcRef);
616+
ReturnValues["changeFunctionViaObjectReferenceAsync"] = (await testClassRef.InvokeAsync<int>("getTextLength")).ToString();
617+
}
618+
619+
private void FunctionReferenceTests()
620+
{
621+
var inProcRuntime = ((IJSInProcessRuntime)JSRuntime);
622+
623+
var funcRef = inProcRuntime.GetValue<IJSObjectReference>("jsInteropTests.nonConstructorFunction");
624+
var testClassRef = inProcRuntime.InvokeNew("jsInteropTests.TestClass", "abraka");
625+
testClassRef.SetValue("getTextLength", funcRef);
626+
ReturnValues["changeFunctionViaObjectReference"] = testClassRef.Invoke<int>("getTextLength").ToString();
627+
}
628+
604629
public class PassDotNetObjectByRefArgs
605630
{
606631
public string StringValue { get; set; }

0 commit comments

Comments
 (0)