|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.JSInterop; |
| 8 | + |
| 9 | +namespace Bunit |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Represents an bUnit's implementation of Blazor's JSInterop. |
| 13 | + /// </summary> |
| 14 | + public class BunitJSInterop |
| 15 | + { |
| 16 | + private readonly Dictionary<string, List<JSRuntimeInvocation>> _invocations = new(); |
| 17 | + private readonly Dictionary<string, List<object>> _handlers = new(); |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Gets a dictionary of all <see cref="List{JSRuntimeInvocation}"/> this mock has observed. |
| 21 | + /// </summary> |
| 22 | + public IReadOnlyDictionary<string, List<JSRuntimeInvocation>> Invocations => _invocations; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Gets or sets whether the mock is running in <see cref="JSRuntimeMode.Loose"/> or |
| 26 | + /// <see cref="JSRuntimeMode.Strict"/>. |
| 27 | + /// </summary> |
| 28 | + public JSRuntimeMode Mode { get; set; } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Gets the mocked <see cref="IJSRuntime"/> instance. |
| 32 | + /// </summary> |
| 33 | + /// <returns></returns> |
| 34 | + public IJSRuntime JSRuntime { get; } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Creates a <see cref="BunitJSInterop"/>. |
| 38 | + /// </summary> |
| 39 | + public BunitJSInterop() |
| 40 | + { |
| 41 | + Mode = JSRuntimeMode.Strict; |
| 42 | + JSRuntime = new BUnitJSRuntime(this); |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Configure a catch all JSInterop invocation handler for a specific return type. |
| 47 | + /// This will match only on the <typeparamref name="TResult"/>, and any arguments passed to |
| 48 | + /// <see cref="IJSRuntime.InvokeAsync{TValue}(string, object[])"/>. |
| 49 | + /// </summary> |
| 50 | + /// <typeparam name="TResult">The result type of the invocation.</typeparam> |
| 51 | + /// <returns>A <see cref="JSRuntimeInvocationHandler{TResult}"/>.</returns> |
| 52 | + public JSRuntimeInvocationHandler<TResult> Setup<TResult>() |
| 53 | + { |
| 54 | + var result = new JSRuntimeInvocationHandler<TResult>(JSRuntimeInvocationHandler<object>.CatchAllIdentifier, _ => true); |
| 55 | + |
| 56 | + AddHandler(result); |
| 57 | + |
| 58 | + return result; |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Configure a JSInterop invocation handler with the <paramref name="identifier"/> and arguments |
| 63 | + /// passing the <paramref name="argumentsMatcher"/> test. |
| 64 | + /// </summary> |
| 65 | + /// <typeparam name="TResult">The result type of the invocation.</typeparam> |
| 66 | + /// <param name="identifier">The identifier to setup a response for.</param> |
| 67 | + /// <param name="argumentsMatcher">A matcher that is passed arguments received in invocations to <paramref name="identifier"/>. If it returns true the invocation is matched.</param> |
| 68 | + /// <returns>A <see cref="JSRuntimeInvocationHandler{TResult}"/>.</returns> |
| 69 | + public JSRuntimeInvocationHandler<TResult> Setup<TResult>(string identifier, Func<IReadOnlyList<object?>, bool> argumentsMatcher) |
| 70 | + { |
| 71 | + var result = new JSRuntimeInvocationHandler<TResult>(identifier, argumentsMatcher); |
| 72 | + |
| 73 | + AddHandler(result); |
| 74 | + |
| 75 | + return result; |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Configure a JSInterop invocation handler with the <paramref name="identifier"/> and <paramref name="arguments"/>. |
| 80 | + /// </summary> |
| 81 | + /// <typeparam name="TResult"></typeparam> |
| 82 | + /// <param name="identifier">The identifier to setup a response for.</param> |
| 83 | + /// <param name="arguments">The arguments that an invocation to <paramref name="identifier"/> should match.</param> |
| 84 | + /// <returns>A <see cref="JSRuntimeInvocationHandler{TResult}"/>.</returns> |
| 85 | + public JSRuntimeInvocationHandler<TResult> Setup<TResult>(string identifier, params object[] arguments) |
| 86 | + { |
| 87 | + return Setup<TResult>(identifier, args => args.SequenceEqual(arguments)); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Configure a JSInterop invocation handler with the <paramref name="identifier"/> and arguments |
| 92 | + /// passing the <paramref name="argumentsMatcher"/> test, that should not receive any result. |
| 93 | + /// </summary> |
| 94 | + /// <param name="identifier">The identifier to setup a response for.</param> |
| 95 | + /// <param name="argumentsMatcher">A matcher that is passed arguments received in invocations to <paramref name="identifier"/>. If it returns true the invocation is matched.</param> |
| 96 | + /// <returns>A <see cref="JSRuntimeInvocationHandler"/>.</returns> |
| 97 | + public JSRuntimeInvocationHandler SetupVoid(string identifier, Func<IReadOnlyList<object?>, bool> argumentsMatcher) |
| 98 | + { |
| 99 | + var result = new JSRuntimeInvocationHandler(identifier, argumentsMatcher); |
| 100 | + |
| 101 | + AddHandler(result); |
| 102 | + |
| 103 | + return result; |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Configure a JSInterop invocation handler with the <paramref name="identifier"/> |
| 108 | + /// and <paramref name="arguments"/>, that should not receive any result. |
| 109 | + /// </summary> |
| 110 | + /// <param name="identifier">The identifier to setup a response for.</param> |
| 111 | + /// <param name="arguments">The arguments that an invocation to <paramref name="identifier"/> should match.</param> |
| 112 | + /// <returns>A <see cref="JSRuntimeInvocationHandler"/>.</returns> |
| 113 | + public JSRuntimeInvocationHandler SetupVoid(string identifier, params object[] arguments) |
| 114 | + { |
| 115 | + return SetupVoid(identifier, args => args.SequenceEqual(arguments)); |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Configure a catch all JSInterop invocation handler, that should not receive any result. |
| 120 | + /// </summary> |
| 121 | + /// <returns>A <see cref="JSRuntimeInvocationHandler"/>.</returns> |
| 122 | + public JSRuntimeInvocationHandler SetupVoid() |
| 123 | + { |
| 124 | + var result = new JSRuntimeInvocationHandler(JSRuntimeInvocationHandler<object>.CatchAllIdentifier, _ => true); |
| 125 | + |
| 126 | + AddHandler(result); |
| 127 | + |
| 128 | + return result; |
| 129 | + } |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Looks through the registered handlers and returns the latest registered that can handle |
| 133 | + /// the provided <paramref name="identifier"/> and <paramref name="args"/>, and that |
| 134 | + /// will return <typeparamref name="TResult"/>. |
| 135 | + /// </summary> |
| 136 | + /// <returns>Returns the <see cref="JSRuntimeInvocationHandler{TResult}"/> or null if no one is found.</returns> |
| 137 | + public JSRuntimeInvocationHandler<TResult>? TryGetInvokeHandler<TResult>(string identifier, object?[]? args = null) |
| 138 | + => TryGetHandlerFor<TResult>(new JSRuntimeInvocation(identifier, default, args)); |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// Looks through the registered handlers and returns the latest registered that can handle |
| 142 | + /// the provided <paramref name="identifier"/> and <paramref name="args"/>, and that returns a "void" result. |
| 143 | + /// </summary> |
| 144 | + /// <returns>Returns the <see cref="JSRuntimeInvocationHandler"/> or null if no one is found.</returns> |
| 145 | + public JSRuntimeInvocationHandler? TryGetInvokeVoidHandler(string identifier, object?[]? args = null) |
| 146 | + => TryGetHandlerFor<object>(new JSRuntimeInvocation(identifier, default, args), x => x.IsVoidResultHandler) as JSRuntimeInvocationHandler; |
| 147 | + |
| 148 | + private void AddHandler<TResult>(JSRuntimeInvocationHandler<TResult> handler) |
| 149 | + { |
| 150 | + if (!_handlers.ContainsKey(handler.Identifier)) |
| 151 | + { |
| 152 | + _handlers.Add(handler.Identifier, new List<object>()); |
| 153 | + } |
| 154 | + _handlers[handler.Identifier].Add(handler); |
| 155 | + } |
| 156 | + |
| 157 | + private void RegisterInvocation(JSRuntimeInvocation invocation) |
| 158 | + { |
| 159 | + if (!_invocations.ContainsKey(invocation.Identifier)) |
| 160 | + { |
| 161 | + _invocations.Add(invocation.Identifier, new List<JSRuntimeInvocation>()); |
| 162 | + } |
| 163 | + _invocations[invocation.Identifier].Add(invocation); |
| 164 | + } |
| 165 | + |
| 166 | + private JSRuntimeInvocationHandler<TResult>? TryGetHandlerFor<TResult>(JSRuntimeInvocation invocation, Predicate<JSRuntimeInvocationHandler<TResult>>? handlerPredicate = null) |
| 167 | + { |
| 168 | + handlerPredicate ??= _ => true; |
| 169 | + JSRuntimeInvocationHandler<TResult>? result = default; |
| 170 | + if (_handlers.TryGetValue(invocation.Identifier, out var plannedInvocations)) |
| 171 | + { |
| 172 | + result = plannedInvocations.OfType<JSRuntimeInvocationHandler<TResult>>() |
| 173 | + .Where(x => handlerPredicate(x) && x.Matches(invocation)).LastOrDefault(); |
| 174 | + } |
| 175 | + |
| 176 | + if (result is null && _handlers.TryGetValue(JSRuntimeInvocationHandler<TResult>.CatchAllIdentifier, out var catchAllHandlers)) |
| 177 | + { |
| 178 | + result = catchAllHandlers.OfType<JSRuntimeInvocationHandler<TResult>>().Where(x => handlerPredicate(x)).LastOrDefault(); |
| 179 | + } |
| 180 | + return result; |
| 181 | + } |
| 182 | + |
| 183 | + private class BUnitJSRuntime : IJSRuntime |
| 184 | + { |
| 185 | + private readonly BunitJSInterop _jsInterop; |
| 186 | + |
| 187 | + public BUnitJSRuntime(BunitJSInterop bunitJsInterop) |
| 188 | + { |
| 189 | + _jsInterop = bunitJsInterop; |
| 190 | + } |
| 191 | + |
| 192 | + public ValueTask<TValue> InvokeAsync<TValue>(string identifier, object?[]? args) |
| 193 | + => InvokeAsync<TValue>(identifier, default, args); |
| 194 | + |
| 195 | + public ValueTask<TValue> InvokeAsync<TValue>(string identifier, CancellationToken cancellationToken, object?[]? args) |
| 196 | + { |
| 197 | + var invocation = new JSRuntimeInvocation(identifier, cancellationToken, args); |
| 198 | + _jsInterop.RegisterInvocation(invocation); |
| 199 | + |
| 200 | + return TryHandlePlannedInvocation<TValue>(invocation) ?? new ValueTask<TValue>(default(TValue)!); |
| 201 | + } |
| 202 | + |
| 203 | + private ValueTask<TValue>? TryHandlePlannedInvocation<TValue>(JSRuntimeInvocation invocation) |
| 204 | + { |
| 205 | + ValueTask<TValue>? result = default; |
| 206 | + |
| 207 | + if (_jsInterop.TryGetHandlerFor<TValue>(invocation) is JSRuntimeInvocationHandler<TValue> handler) |
| 208 | + { |
| 209 | + var task = handler.RegisterInvocation(invocation); |
| 210 | + result = new ValueTask<TValue>(task); |
| 211 | + } |
| 212 | + else if (_jsInterop.Mode == JSRuntimeMode.Strict) |
| 213 | + { |
| 214 | + throw new JSRuntimeUnhandledInvocationException(invocation); |
| 215 | + } |
| 216 | + |
| 217 | + return result; |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments