forked from bUnit-dev/bUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBunitJSInterop.cs
166 lines (139 loc) · 4.98 KB
/
BunitJSInterop.cs
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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Bunit.JSInterop;
using Bunit.JSInterop.InvocationHandlers;
#if NET5_0_OR_GREATER
using Bunit.JSInterop.InvocationHandlers.Implementation;
#endif
using Microsoft.JSInterop;
namespace Bunit
{
/// <summary>
/// Represents an bUnit's implementation of Blazor's JSInterop.
/// </summary>
public class BunitJSInterop
{
private readonly Dictionary<Type, List<object>> handlers = new();
private JSRuntimeInvocationHandler? genericHandler;
private JSRuntimeMode mode;
/// <summary>
/// Gets a dictionary of all <see cref="List{JSRuntimeInvocation}"/> this mock has observed.
/// </summary>
public JSRuntimeInvocationDictionary Invocations { get; } = new();
/// <summary>
/// Gets or sets whether the <see cref="BunitJSInterop"/> is running in <see cref="JSRuntimeMode.Loose"/> or
/// <see cref="JSRuntimeMode.Strict"/>.
/// </summary>
public virtual JSRuntimeMode Mode { get => mode; set => mode = value; }
/// <summary>
/// Gets the mocked <see cref="IJSRuntime"/> instance.
/// </summary>
public IJSRuntime JSRuntime { get; }
/// <summary>
/// Initializes a new instance of the <see cref="BunitJSInterop"/> class.
/// </summary>
public BunitJSInterop()
{
mode = JSRuntimeMode.Strict;
JSRuntime = new BunitJSRuntime(this);
#if NET5_0_OR_GREATER
AddCustomHandlers();
#endif
}
/// <summary>
/// Adds an invocation handler to bUnit's JSInterop. Can be used to register
/// custom invocation handlers.
/// </summary>
public void AddInvocationHandler<TResult>(JSRuntimeInvocationHandlerBase<TResult> handler)
{
if (handler is null)
throw new ArgumentNullException(nameof(handler));
var resultType = typeof(TResult);
if (!handlers.ContainsKey(resultType))
handlers.Add(resultType, new List<object>());
handlers[resultType].Add(handler);
}
/// <summary>
/// Adds a generic invocation handler to bUnit's JSInterop.
/// The purpose of this untyped invocation handler is handle all js invocations.
/// </summary>
public void SetGenericInvocationHandler(JSRuntimeInvocationHandler? handler)
{
genericHandler = handler;
}
internal ValueTask<TValue> HandleInvocation<TValue>(JSRuntimeInvocation invocation)
{
RegisterInvocation(invocation);
return TryGenericInvocation<TValue>(invocation)
?? TryHandlePlannedInvocation<TValue>(invocation)
?? new ValueTask<TValue>(default(TValue)!);
}
private ValueTask<TValue>? TryGenericInvocation<TValue>(JSRuntimeInvocation invocation)
{
ValueTask<TValue>? result = default;
if (genericHandler != null && genericHandler.HandleAsync(invocation) is Task<object> res)
{
result = new ValueTask<TValue>(res.ContinueWith(r => (TValue) r.Result, System.Threading.CancellationToken.None, TaskContinuationOptions.NotOnCanceled, TaskScheduler.Default));
}
return result;
}
private ValueTask<TValue>? TryHandlePlannedInvocation<TValue>(JSRuntimeInvocation invocation)
{
ValueTask<TValue>? result = default;
if (TryGetHandlerFor<TValue>(invocation) is JSRuntimeInvocationHandlerBase<TValue> handler)
{
var task = handler.HandleAsync(invocation);
result = new ValueTask<TValue>(task);
}
else if (Mode == JSRuntimeMode.Strict)
{
throw new JSRuntimeUnhandledInvocationException(invocation);
}
return result;
}
internal virtual void RegisterInvocation(JSRuntimeInvocation invocation)
{
Invocations.RegisterInvocation(invocation);
}
internal JSRuntimeInvocationHandlerBase<TResult>? TryGetHandlerFor<TResult>(JSRuntimeInvocation invocation, Predicate<JSRuntimeInvocationHandlerBase<TResult>>? handlerPredicate = null)
{
var resultType = typeof(TResult);
handlerPredicate ??= _ => true;
if (!handlers.TryGetValue(resultType, out var plannedInvocations))
{
return default;
}
// Search from the latest added handler for a result type
// and find the first handler that can handle an invocation
// and is not a catch all handler.
for (int i = plannedInvocations.Count - 1; i >= 0; i--)
{
var candidate = (JSRuntimeInvocationHandlerBase<TResult>)plannedInvocations[i];
if (!candidate.IsCatchAllHandler && handlerPredicate(candidate) && candidate.CanHandle(invocation))
{
return candidate;
}
}
// If none of the non catch all handlers can handle,
// search for the latest added handler catch all handler that can, if any.
for (int i = plannedInvocations.Count - 1; i >= 0; i--)
{
var candidate = (JSRuntimeInvocationHandlerBase<TResult>)plannedInvocations[i];
if (candidate.IsCatchAllHandler && handlerPredicate(candidate) && candidate.CanHandle(invocation))
{
return candidate;
}
}
return default;
}
#if NET5_0_OR_GREATER
private void AddCustomHandlers()
{
AddInvocationHandler(new FocusAsyncInvocationHandler());
AddInvocationHandler(new VirtualizeJSRuntimeInvocationHandler());
AddInvocationHandler(new LooseModeJSObjectReferenceInvocationHandler(this));
}
#endif
}
}