-
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathBunitJSInteropSetupExtensions.cs
More file actions
287 lines (254 loc) · 17.3 KB
/
Copy pathBunitJSInteropSetupExtensions.cs
File metadata and controls
287 lines (254 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using Bunit.JSInterop.InvocationHandlers.Implementation;
namespace Bunit;
/// <summary>
/// Helper methods for creating invocation handlers and adding the to a <see cref="BunitJSInterop"/>.
/// </summary>
public static partial class BunitJSInteropSetupExtensions
{
private const string DefaultImportIdentifier = "import";
/// <summary>
/// Configure a JSInterop invocation handler for an <c>InvokeAsync<TResult></c> call with arguments
/// passing the <paramref name="invocationMatcher"/> test.
/// </summary>
/// <typeparam name="TResult">The result type of the invocation.</typeparam>
/// <param name="jsInterop">The bUnit JSInterop to setup the invocation handling with.</param>
/// <param name="invocationMatcher">A matcher that is passed an <see cref="JSRuntimeInvocation"/>. If it returns true the invocation is matched.</param>
/// <param name="isCatchAllHandler">Set to true if the created handler is a catch all handler, that should only be used if there are no other non-catch all handlers available.</param>
/// <returns>A <see cref="JSRuntimeInvocationHandler{TResult}"/>.</returns>
public static JSRuntimeInvocationHandler<TResult> Setup<TResult>(this BunitJSInterop jsInterop, InvocationMatcher invocationMatcher, bool isCatchAllHandler = false)
{
ArgumentNullException.ThrowIfNull(jsInterop);
EnsureResultNotIJSObjectReference<TResult>();
var result = new JSRuntimeInvocationHandler<TResult>(invocationMatcher, isCatchAllHandler);
jsInterop.AddInvocationHandler(result);
return result;
}
/// <summary>
/// Configure a JSInterop invocation handler with the <paramref name="identifier"/> and arguments
/// passing the <paramref name="invocationMatcher"/> test.
/// </summary>
/// <typeparam name="TResult">The result type of the invocation.</typeparam>
/// <param name="jsInterop">The bUnit JSInterop to setup the invocation handling with.</param>
/// <param name="identifier">The identifier to setup a response for.</param>
/// <param name="invocationMatcher">A matcher that is passed an <see cref="JSRuntimeInvocation"/> associated with the<paramref name="identifier"/>. If it returns true the invocation is matched.</param>
/// <returns>A <see cref="JSRuntimeInvocationHandler{TResult}"/>.</returns>
public static JSRuntimeInvocationHandler<TResult> Setup<TResult>(this BunitJSInterop jsInterop, string identifier, InvocationMatcher invocationMatcher)
=> Setup<TResult>(jsInterop, inv => identifier.Equals(inv.Identifier, StringComparison.Ordinal) && invocationMatcher(inv));
/// <summary>
/// Configure a JSInterop invocation handler with the <paramref name="identifier"/> and <paramref name="arguments"/>.
/// </summary>
/// <typeparam name="TResult">The type of value to match with in a InvokeAsync<TResult> call.</typeparam>
/// <param name="jsInterop">The bUnit JSInterop to setup the invocation handling with.</param>
/// <param name="identifier">The identifier to setup a response for.</param>
/// <param name="arguments">The arguments that an invocation to <paramref name="identifier"/> should match.</param>
/// <returns>A <see cref="JSRuntimeInvocationHandler{TResult}"/>.</returns>
public static JSRuntimeInvocationHandler<TResult> Setup<TResult>(this BunitJSInterop jsInterop, string identifier, params object?[]? arguments)
=> Setup<TResult>(jsInterop, identifier, invocation => invocation.Arguments.SequenceEqual(arguments ?? Array.Empty<object?>()));
/// <summary>
/// Configure a catch all JSInterop invocation handler for a specific return type.
/// This will match only on the <typeparamref name="TResult"/>, and any arguments passed to
/// <see cref="IJSRuntime.InvokeAsync{TValue}(string, object[])"/>.
/// </summary>
/// <typeparam name="TResult">The result type of the invocation.</typeparam>
/// <param name="jsInterop">The bUnit JSInterop to setup the invocation handling with.</param>
/// <returns>A <see cref="JSRuntimeInvocationHandler{TResult}"/>.</returns>
public static JSRuntimeInvocationHandler<TResult> Setup<TResult>(this BunitJSInterop jsInterop)
=> Setup<TResult>(jsInterop, _ => true, isCatchAllHandler: true);
/// <summary>
/// Configure a JSInterop invocation handler for an <c>InvokeVoidAsync</c> call with arguments
/// passing the <paramref name="invocationMatcher"/> test, that should not receive any result.
/// </summary>
/// <param name="jsInterop">The bUnit JSInterop to setup the invocation handling with.</param>
/// <param name="invocationMatcher">A matcher that is passed an <see cref="JSRuntimeInvocation"/>. If it returns true the invocation is matched.</param>
/// <param name="isCatchAllHandler">Set to true if the created handler is a catch all handler, that should only be used if there are no other non-catch all handlers available.</param>
/// <returns>A <see cref="JSRuntimeInvocationHandler"/>.</returns>
public static JSRuntimeInvocationHandler SetupVoid(this BunitJSInterop jsInterop, InvocationMatcher invocationMatcher, bool isCatchAllHandler = false)
{
ArgumentNullException.ThrowIfNull(jsInterop);
var result = new JSRuntimeInvocationHandler(invocationMatcher, isCatchAllHandler);
jsInterop.AddInvocationHandler(result);
return result;
}
/// <summary>
/// Configure a JSInterop invocation handler with the <paramref name="identifier"/> and arguments
/// passing the <paramref name="invocationMatcher"/> test, that should not receive any result.
/// </summary>
/// <param name="jsInterop">The bUnit JSInterop to setup the invocation handling with.</param>
/// <param name="identifier">The identifier to setup a response for.</param>
/// <param name="invocationMatcher">A matcher that is passed an <see cref="JSRuntimeInvocation"/> associated with the<paramref name="identifier"/>. If it returns true the invocation is matched.</param>
/// <returns>A <see cref="JSRuntimeInvocationHandler"/>.</returns>
public static JSRuntimeInvocationHandler SetupVoid(this BunitJSInterop jsInterop, string identifier, InvocationMatcher invocationMatcher)
=> SetupVoid(jsInterop, inv => identifier.Equals(inv.Identifier, StringComparison.Ordinal) && invocationMatcher(inv));
/// <summary>
/// Configure a JSInterop invocation handler with the <paramref name="identifier"/>
/// and <paramref name="arguments"/>, that should not receive any result.
/// </summary>
/// <param name="jsInterop">The bUnit JSInterop to setup the invocation handling with.</param>
/// <param name="identifier">The identifier to setup a response for.</param>
/// <param name="arguments">The arguments that an invocation to <paramref name="identifier"/> should match.</param>
/// <returns>A <see cref="JSRuntimeInvocationHandler"/>.</returns>
public static JSRuntimeInvocationHandler SetupVoid(this BunitJSInterop jsInterop, string identifier, params object?[]? arguments)
=> SetupVoid(jsInterop, identifier, invocation => invocation.Arguments.SequenceEqual(arguments ?? Array.Empty<object?>()));
/// <summary>
/// Configure a catch all JSInterop invocation handler, that should not receive any result.
/// </summary>
/// <param name="jsInterop">The bUnit JSInterop to setup the invocation handling with.</param>
/// <returns>A <see cref="JSRuntimeInvocationHandler"/>.</returns>
public static JSRuntimeInvocationHandler SetupVoid(this BunitJSInterop jsInterop)
=> SetupVoid(jsInterop, _ => true, isCatchAllHandler: true);
/// <summary>
/// Looks through the registered handlers and returns the latest registered that can handle
/// the provided <paramref name="identifier"/> and <paramref name="arguments"/>, and that
/// will return <typeparamref name="TResult"/>.
/// </summary>
/// <param name="jsInterop">The bUnit JSInterop to setup the invocation handling with.</param>
/// <param name="identifier">The identifier the handler should match with.</param>
/// <param name="arguments">The arguments that an invocation to <paramref name="identifier"/> should match.</param>
/// <typeparam name="TResult">The result type of the invocation.</typeparam>
/// <returns>Returns the <see cref="JSRuntimeInvocationHandler{TResult}"/> or null if no one is found.</returns>
public static JSRuntimeInvocationHandler<TResult>? TryGetInvokeHandler<TResult>(this BunitJSInterop jsInterop, string identifier, params object?[]? arguments)
{
ArgumentNullException.ThrowIfNull(jsInterop);
return jsInterop.TryGetHandlerFor<TResult>(
new JSRuntimeInvocation(
identifier,
default,
arguments,
typeof(TResult),
string.Empty)) as JSRuntimeInvocationHandler<TResult>;
}
/// <summary>
/// Looks through the registered handlers and returns the latest registered that can handle
/// the provided <paramref name="identifier"/> and <paramref name="arguments"/>, and that returns a "void" result.
/// </summary>
/// <param name="jsInterop">The bUnit JSInterop to setup the invocation handling with.</param>
/// <param name="identifier">The identifier the handler should match with.</param>
/// <param name="arguments">The arguments that an invocation to <paramref name="identifier"/> should match.</param>
/// <returns>Returns the <see cref="JSRuntimeInvocationHandler"/> or null if no one is found.</returns>
public static JSRuntimeInvocationHandler? TryGetInvokeVoidHandler(this BunitJSInterop jsInterop, string identifier, params object?[]? arguments)
{
ArgumentNullException.ThrowIfNull(jsInterop);
var invocation = new JSRuntimeInvocation(identifier, default, arguments, typeof(object), string.Empty);
return jsInterop.TryGetHandlerFor<Microsoft.JSInterop.Infrastructure.IJSVoidResult>(invocation, x => x.IsVoidResultHandler) as JSRuntimeInvocationHandler;
}
/// <summary>
/// Setup a handler for a <c>IJSRuntime.InvokeAsync<IJSObjectReference>()</c> call whose input parameters is matched by the provided
/// <paramref name="invocationMatcher"/>.
/// </summary>
/// <remarks>
/// The returned <see cref="BunitJSInterop"/> can be used to setup handlers for
/// <c>InvokeAsync<TValue>(string, object?[]?)"</c> calls to the module, using either
/// <see cref="SetupModule(BunitJSInterop, string)"/> or Setup calls.
/// </remarks>
/// <param name="jsInterop">The JSInterop to setup the handler for.</param>
/// <param name="invocationMatcher">The matcher to use to match <see cref="JSRuntimeInvocation"/>'s with.</param>
/// <param name="isCatchAllHandler">Set to true if the created handler is a catch all handler, that should only be used if there are no other non-catch all handlers available.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="jsInterop"/> is null.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="invocationMatcher"/> is null.</exception>
/// <returns>A <see cref="BunitJSModuleInterop"/>.</returns>
public static BunitJSModuleInterop SetupModule(this BunitJSInterop jsInterop, InvocationMatcher invocationMatcher, bool isCatchAllHandler = false)
{
ArgumentNullException.ThrowIfNull(jsInterop);
ArgumentNullException.ThrowIfNull(invocationMatcher);
var result = new JSObjectReferenceInvocationHandler(jsInterop, invocationMatcher, isCatchAllHandler);
jsInterop.AddInvocationHandler(result);
return result.JSInterop;
}
/// <summary>
/// Setup a handler for a <c>IJSRuntime.InvokeAsync<IJSObjectReference>()</c> call whose input parameters is matched by the provided
/// <paramref name="invocationMatcher"/> and the <paramref name="identifier"/>.
/// </summary>
/// <remarks>
/// The returned <see cref="BunitJSInterop"/> can be used to setup handlers for
/// <c>InvokeAsync<TValue>(string, object?[]?)"</c> calls to the module, using either
/// <see cref="SetupModule(BunitJSInterop, string)"/> or Setup calls.
/// </remarks>
/// <param name="jsInterop">The JSInterop to setup the handler for.</param>
/// <param name="identifier">The identifier to setup a response for.</param>
/// <param name="invocationMatcher">The matcher to use to match <see cref="JSRuntimeInvocation"/>'s with.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="jsInterop"/> is null.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="invocationMatcher"/> is null.</exception>
/// <returns>A <see cref="BunitJSModuleInterop"/>.</returns>
public static BunitJSModuleInterop SetupModule(this BunitJSInterop jsInterop, string identifier, InvocationMatcher invocationMatcher)
=> SetupModule(jsInterop, inv => identifier.Equals(inv.Identifier, StringComparison.Ordinal) && invocationMatcher(inv));
/// <summary>
/// Setup a handler for a <c>IJSRuntime.InvokeAsync<IJSObjectReference>("import", <paramref name="moduleName"/>)</c>
/// call.
/// </summary>
/// <remarks>
/// The returned <see cref="BunitJSInterop"/> can be used to setup handlers for
/// <c>InvokeAsync<TValue>(string, object?[]?)"</c> calls to the module, using either
/// <see cref="SetupModule(BunitJSInterop, string)"/> or Setup calls.
/// </remarks>
/// <param name="jsInterop">The JSInterop to setup the handler for.</param>
/// <param name="moduleName">The name of the JavaScript module to handle invocations for.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="jsInterop"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="moduleName"/> is null or whitespace.</exception>
/// <returns>A <see cref="BunitJSModuleInterop"/>.</returns>
public static BunitJSModuleInterop SetupModule(this BunitJSInterop jsInterop, string moduleName)
{
if (string.IsNullOrWhiteSpace(moduleName))
throw new ArgumentException($"'{nameof(moduleName)}' cannot be null or whitespace.", nameof(moduleName));
return SetupModule(
jsInterop,
DefaultImportIdentifier,
invocation => invocation.Arguments?[0] is string requestedModuleName
&& requestedModuleName.Equals(moduleName, StringComparison.Ordinal));
}
/// <summary>
/// Setup a handler for a <c>IJSRuntime.InvokeAsync<IJSObjectReference>(<paramref name="identifier"/>, <paramref name="arguments"/>)</c>
/// call.
/// </summary>
/// <remarks>
/// The returned <see cref="BunitJSInterop"/> can be used to setup handlers for
/// <c>InvokeAsync<TValue>(string, object?[]?)"</c> calls to the module, using either
/// <see cref="SetupModule(BunitJSInterop, string)"/> or Setup calls.
/// </remarks>
/// <param name="jsInterop">The JSInterop to setup the handler for.</param>
/// <param name="identifier">The identifier to setup a response for.</param>
/// <param name="arguments">The arguments that an invocation to <paramref name="identifier"/> should match. Use <c>Array.Empty<object?>()</c> for none.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="jsInterop"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="identifier"/> is null or whitespace.</exception>
/// <returns>A <see cref="BunitJSModuleInterop"/>.</returns>
public static BunitJSModuleInterop SetupModule(this BunitJSInterop jsInterop, string identifier, object?[] arguments)
=> SetupModule(jsInterop, identifier, invocation => invocation.Arguments.SequenceEqual(arguments ?? Array.Empty<object?>()));
/// <summary>
/// Configure a catch all JSObjectReferenceInvocationHandler invocation handler for any module load and invocations
/// on those modules.
/// </summary>
/// <remarks>
/// The returned <see cref="BunitJSInterop"/> can be used to setup handlers for
/// <c>InvokeAsync<TValue>(string, object?[]?)"</c> calls to the module, using either
/// <see cref="SetupModule(BunitJSInterop, string)"/> or Setup calls.
/// </remarks>
/// <param name="jsInterop">The JSInterop to setup the handler for.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="jsInterop"/> is null.</exception>
/// <returns>A <see cref="BunitJSModuleInterop"/>.</returns>
public static BunitJSModuleInterop SetupModule(this BunitJSInterop jsInterop)
=> SetupModule(jsInterop, _ => true, isCatchAllHandler: true);
/// <summary>
/// Looks through the registered handlers and returns the latest registered that can handle
/// the provided <paramref name="identifier"/> and <paramref name="arguments"/>, and that
/// will return <see cref="IJSObjectReference"/>.
/// </summary>
/// <param name="jsInterop">The JSInterop to setup the handler for.</param>
/// <param name="identifier">The identifier the handler should match with.</param>
/// <param name="arguments">The arguments that an invocation to <paramref name="identifier"/> should match.</param>
/// <returns>A <see cref="BunitJSModuleInterop"/> or null if no one is found.</returns>
public static BunitJSModuleInterop? TryGetModuleJSInterop(this BunitJSInterop jsInterop, string identifier, params object?[]? arguments)
{
ArgumentNullException.ThrowIfNull(jsInterop);
var invocation = new JSRuntimeInvocation(identifier, default, arguments, typeof(IJSObjectReference), "InvokeAsync");
var handler = jsInterop.TryGetHandlerFor<IJSObjectReference>(invocation) as JSObjectReferenceInvocationHandler;
return handler?.JSInterop;
}
private static void EnsureResultNotIJSObjectReference<TResult>()
{
const string UseSetupModuleErrorMessage = "Use one of the SetupModule() methods instead to set up an invocation handler that returns an IJSObjectReference.";
var resultType = typeof(TResult);
if (resultType == typeof(IJSObjectReference))
throw new ArgumentException(UseSetupModuleErrorMessage);
if (resultType == typeof(Microsoft.JSInterop.Implementation.JSObjectReference))
throw new ArgumentException(UseSetupModuleErrorMessage);
}
}