Blazor: JSObjectReference ctor should take a IJSRuntime as input #26981
-
Hey Blazor team, I have started to look at making the fake/mock IJSRuntime in bUnit work seamlessly the new If devs have a component with code like this in it (from blog post), var module = await jsRuntime.InvokeAsync<JSObjectReference>("import", "./_content/MyComponents/exampleJsInterop.js");
return await module.InvokeAsync<string>("showPrompt", message); ... I will be hard pressed to return a In addition to that, the public methods on A better solution would be to not allow users to specify I know we are already a RC2, but I hope there are still time to sneak in a little tweak. Best, Egil cc. @captainsafia / @SteveSandersonMS / @danroth27 Related: bUnit-dev/bUnit#233 |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
It's IJSObjectReference in RC2. Does that help? |
Beta Was this translation helpful? Give feedback.
-
BTW we made that change specifically for testability. |
Beta Was this translation helpful? Give feedback.
-
@danroth27 IJSObjectReference is indeed in there. But I think the challenge is that JSObjectReference is also public, and it is thus possible for users to request that type in a bUnits IJSRuntime mock cannot return a JSObjectReference to the user, as far as I can see, unless the ctor changes, and that is the concern I am trying to address. Either the JSObjectReference ctor should take IJSRuntime as input, making it possible for bUnit to new it up and still influence any calls it received to it's Invoke methods, or it should be internal, such that users have to request the IJSObjectReference type. Please correct me if I'm wrong. |
Beta Was this translation helpful? Give feedback.
-
Even though jsRuntime.InvokeAsync<IJSObjectReference>(...); // Works
jsRuntime.InvokeAsync<JSObjectReference>(...); // Will not work (doesn't create an object reference) The reason for this is in the implementation at https://github.com/dotnet/aspnetcore/blob/master/src/Shared/JSInterop/JSCallResultTypeHelper.cs. As you can see in the code there, we are looking for the interface type, not any other type (not even ones that implement the interface). Additionally, all the docs will only refer to So I believe things will work fine for bUnit. And again, we made these specific choices to ensure testability :) In case you're wondering why |
Beta Was this translation helpful? Give feedback.
Even though
JSObjectReference
is public, developers must useIJSObjectReference
in order to get an object reference. That is:The reason for this is in the implementation at https://github.com/dotnet/aspnetcore/blob/master/src/Shared/JSInterop/JSCallResultTypeHelper.cs. As you can see in the code there, we are looking for the interface type, not any other type (not even ones that implement the interface). Additionally, all the docs will only refer to
IJSObjectReference
, so developers don't have any reason to consider trying the concr…