-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaScriptModuleBaseTests.cs
341 lines (285 loc) · 11.1 KB
/
JavaScriptModuleBaseTests.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
using Microsoft.Extensions.Logging;
using Microsoft.JSInterop;
using TestInfrastructure;
namespace Infrastructure.Tests;
[TestClass]
public class JavaScriptModuleBaseTests
{
private const string _importIdentifier = "import";
[TestMethod("Вызов метода модуля JavaScript с именем, совпадающим с названием метода класса, должен возвращать ожидаемое значение")]
[TestProperty(TestProperties.MethodName, "InvokeAsync")]
public async Task InvokeAsync1()
{
// Arrange.
CancellationToken cancellationToken = default;
Fixture fixture = new();
string scriptPath = fixture.Create<string>();
const string identifier = "returnValueMethod";
int arg1 = fixture.Create<int>();
string arg2 = fixture.Create<string>();
int expected = fixture.Create<int>();
await using TestJavaScriptModule module = GetMockedModuleForReturnValueMethod(
scriptPath,
identifier,
arg1,
arg2,
expected,
cancellationToken);
// Act.
int actual = await module.ReturnValueMethod(arg1, arg2, cancellationToken);
// Assert.
_ = actual.Should().Be(expected);
}
[TestMethod("Вызов метода модуля JavaScript с явно указанным именем должен возвращать ожидаемое значение")]
[TestProperty(TestProperties.MethodName, "InvokeAsync")]
public async Task InvokeAsync2()
{
// Arrange.
CancellationToken cancellationToken = default;
Fixture fixture = new();
string scriptPath = fixture.Create<string>();
string identifier = fixture.Create<string>();
int arg1 = fixture.Create<int>();
string arg2 = fixture.Create<string>();
int expected = fixture.Create<int>();
await using TestJavaScriptModule module = GetMockedModuleForReturnValueMethod(
scriptPath,
identifier,
arg1,
arg2,
expected,
cancellationToken);
// Act.
int actual = await module.ReturnValueMethodWithExplicitIdentifier(
identifier,
arg1,
arg2,
cancellationToken);
// Assert.
_ = actual.Should().Be(expected);
}
[TestMethod("Вызов метода модуля JavaScript с именем, совпадающим с названием метода класса, должен вызывать метод без возврата значения")]
[TestProperty(TestProperties.MethodName, "InvokeVoidAsync")]
public async Task InvokeVoidAsync1()
{
// Arrange.
CancellationToken cancellationToken = default;
Fixture fixture = new();
string scriptPath = fixture.Create<string>();
const string identifier = "voidMethod";
int arg1 = fixture.Create<int>();
string arg2 = fixture.Create<string>();
Mock<IJSObjectReference> jsObjectReferenceMock = GetJSObjectReferenceMockForVoidMethod(
identifier,
arg1,
arg2,
cancellationToken);
await using TestJavaScriptModule module = GetMockedModule(
jsObjectReferenceMock.Object,
scriptPath,
cancellationToken);
// Act.
await module.VoidMethod(arg1, arg2, cancellationToken);
// Assert.
jsObjectReferenceMock.Verify();
}
[TestMethod("Вызов метода модуля JavaScript с явно указанным именем должен вызывать метод без возврата значения")]
[TestProperty(TestProperties.MethodName, "InvokeVoidAsync")]
public async Task InvokeVoidAsync2()
{
// Arrange.
CancellationToken cancellationToken = default;
Fixture fixture = new();
string scriptPath = fixture.Create<string>();
string identifier = fixture.Create<string>();
int arg1 = fixture.Create<int>();
string arg2 = fixture.Create<string>();
Mock<IJSObjectReference> jsObjectReferenceMock = GetJSObjectReferenceMockForVoidMethod(
identifier,
arg1,
arg2,
cancellationToken);
await using TestJavaScriptModule module = GetMockedModule(
jsObjectReferenceMock.Object,
scriptPath,
cancellationToken);
// Act.
await module.VoidMethodWithExplicitIdentifier(identifier, arg1, arg2, cancellationToken);
// Assert.
jsObjectReferenceMock.Verify();
}
[TestMethod("При освобождении ресурсов экземпляра модуля должны освобождаться ресурсы внутренних объектов")]
[TestProperty(TestProperties.MethodName, nameof(TestJavaScriptModule.DisposeAsync))]
public async Task DisposeAsync1()
{
// Arrange.
CancellationToken cancellationToken = default;
Fixture fixture = new();
string scriptPath = fixture.Create<string>();
int arg1 = fixture.Create<int>();
string arg2 = fixture.Create<string>();
Mock<IJSObjectReference> jsObjectReferenceMock = new();
jsObjectReferenceMock
.Setup(jsObjectReference => jsObjectReference.DisposeAsync())
.Verifiable();
TestJavaScriptModule module = GetMockedModule(
jsObjectReferenceMock.Object,
scriptPath,
cancellationToken);
// Act.
_ = await module.ReturnValueMethod(arg1, arg2, cancellationToken);
await module.DisposeAsync();
// Assert.
jsObjectReferenceMock.Verify();
}
[TestMethod("При освобождении ресурсов внутренних объектов исключения должны игнорироваться")]
[TestProperty(TestProperties.MethodName, nameof(TestJavaScriptModule.DisposeAsync))]
public async Task DisposeAsync2()
{
// Arrange.
CancellationToken cancellationToken = default;
Fixture fixture = new();
string scriptPath = fixture.Create<string>();
int arg1 = fixture.Create<int>();
string arg2 = fixture.Create<string>();
Mock<IJSObjectReference> jsObjectReferenceMock = new();
jsObjectReferenceMock
.Setup(jsObjectReference => jsObjectReference.DisposeAsync())
.Throws(() => new JSDisconnectedException("Тестовое сообщение"))
.Verifiable();
TestJavaScriptModule module = GetMockedModule(
jsObjectReferenceMock.Object,
scriptPath,
cancellationToken);
// Act.
_ = await module.ReturnValueMethod(arg1, arg2, cancellationToken);
await module.DisposeAsync();
// Assert.
jsObjectReferenceMock.Verify();
}
private static TestJavaScriptModule GetMockedModuleForReturnValueMethod<T>(
string scriptPath,
string identifier,
int arg1,
string arg2,
T returnValue,
CancellationToken cancellationToken)
{
Mock<IJSObjectReference> jsObjectReferenceMock = new();
_ = jsObjectReferenceMock
.Setup(jsObjectReference => jsObjectReference.InvokeAsync<T>(
identifier,
cancellationToken,
new object[] { arg1, arg2 }))
.ReturnsAsync(returnValue);
return GetMockedModule(
jsObjectReferenceMock.Object,
scriptPath,
cancellationToken);
}
private static Mock<IJSObjectReference> GetJSObjectReferenceMockForVoidMethod(
string identifier,
int arg1,
string arg2,
CancellationToken cancellationToken)
{
Mock<IJSObjectReference> jsObjectReferenceMock = new();
jsObjectReferenceMock
.Setup(jsObjectReference => jsObjectReference.InvokeAsync<Microsoft.JSInterop.Infrastructure.IJSVoidResult>(
identifier,
cancellationToken,
new object[] { arg1, arg2 }))
.Verifiable();
return jsObjectReferenceMock;
}
private static TestJavaScriptModule GetMockedModule(
IJSObjectReference jsObjectReference,
string scriptPath,
CancellationToken cancellationToken)
{
Mock<IJSRuntime> jsRuntimeMock = new();
_ = jsRuntimeMock
.Setup(jsRuntime => jsRuntime.InvokeAsync<IJSObjectReference>(
_importIdentifier,
cancellationToken,
new object[] { scriptPath }))
.ReturnsAsync(jsObjectReference);
return new(
Mock.Of<ILogger<TestJavaScriptModule>>(),
jsRuntimeMock.Object,
scriptPath);
}
/// <summary>
/// Тестовый класс, унаследованный от <see cref="JavaScriptModuleBase"/>.
/// </summary>
internal sealed class TestJavaScriptModule : JavaScriptModuleBase
{
/// <inheritdoc/>
public TestJavaScriptModule(
ILogger<TestJavaScriptModule> logger,
IJSRuntime jsRuntime,
string scriptPath)
: base(logger, jsRuntime, scriptPath)
{ }
/// <summary>
/// Метод-обёртка для тестирования <see cref="JavaScriptModuleBase.InvokeVoidAsync(object?[]?, CancellationToken, string)"/>.
/// </summary>
/// <param name="arg1">Первый аргумент метода.</param>
/// <param name="arg2">Второй аргумент метода.</param>
/// <param name="cancellationToken"><inheritdoc cref="JavaScriptModuleBase.InvokeVoidAsync" path="/param[@name='cancellationToken']"/></param>
public ValueTask VoidMethod(
int arg1,
string arg2,
CancellationToken cancellationToken = default)
=> InvokeVoidAsync(
new object[] { arg1, arg2 },
cancellationToken);
/// <summary>
/// Метод-обёртка для тестирования <see cref="JavaScriptModuleBase.InvokeVoidAsync(string, object?[]?, CancellationToken)"/>.
/// </summary>
/// <param name="identifier"><inheritdoc cref="JavaScriptModuleBase.InvokeVoidAsync" path="/param[@name='identifier']"/></param>
/// <param name="arg1">Первый аргумент метода.</param>
/// <param name="arg2">Второй аргумент метода.</param>
/// <param name="cancellationToken"><inheritdoc cref="JavaScriptModuleBase.InvokeVoidAsync" path="/param[@name='cancellationToken']"/></param>
public ValueTask VoidMethodWithExplicitIdentifier(
string identifier,
int arg1,
string arg2,
CancellationToken cancellationToken = default)
=> InvokeVoidAsync(
identifier,
new object[] { arg1, arg2 },
cancellationToken);
/// <summary>
/// Метод-обёртка для тестирования <see cref="JavaScriptModuleBase.InvokeAsync{TValue}(object?[]?, CancellationToken, string)"/>.
/// </summary>
/// <param name="arg1">Первый аргумент метода.</param>
/// <param name="arg2">Второй аргумент метода.</param>
/// <param name="cancellationToken"><inheritdoc cref="JavaScriptModuleBase.InvokeVoidAsync" path="/param[@name='cancellationToken']"/></param>
/// <returns><inheritdoc cref="JavaScriptModuleBase.InvokeAsync" path="/returns"/></returns>
public ValueTask<int> ReturnValueMethod(
int arg1,
string arg2,
CancellationToken cancellationToken)
=> InvokeAsync<int>(
new object[] { arg1, arg2 },
cancellationToken);
/// <summary>
/// Метод-обёртка для тестирования <see cref="JavaScriptModuleBase.InvokeAsync{TValue}(string, object?[]?, CancellationToken)"/>.
/// </summary>
/// <param name="identifier"><inheritdoc cref="JavaScriptModuleBase.InvokeAsync" path="/param[@name='identifier']"/></param>
/// <param name="arg1">Первый аргумент метода.</param>
/// <param name="arg2">Второй аргумент метода.</param>
/// <param name="cancellationToken"><inheritdoc cref="JavaScriptModuleBase.InvokeVoidAsync" path="/param[@name='cancellationToken']"/></param>
/// <returns><inheritdoc cref="JavaScriptModuleBase.InvokeAsync" path="/returns"/></returns>
public ValueTask<int> ReturnValueMethodWithExplicitIdentifier(
string identifier,
int arg1,
string arg2,
CancellationToken cancellationToken)
=> InvokeAsync<int>(
identifier,
new object[] { arg1, arg2 },
cancellationToken);
}
}