-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathBunitJSInteropTest.cs
More file actions
794 lines (623 loc) · 26.7 KB
/
BunitJSInteropTest.cs
File metadata and controls
794 lines (623 loc) · 26.7 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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
#pragma warning disable CS0618 // This method is obsolete.
namespace Bunit.JSInterop;
public partial class BunitJSInteropTest
{
private static BunitJSInterop CreateSut(JSRuntimeMode mode) => new BunitJSInterop { Mode = mode };
[Fact(DisplayName = "Mock returns default value in loose mode without invocation setup")]
public async Task Test001()
{
var sut = CreateSut(JSRuntimeMode.Loose);
var result = await sut.JSRuntime.InvokeAsync<object>("ident", Array.Empty<object>());
result.ShouldBe(default);
}
[Fact(DisplayName = "After invocation a invocation should be visible from the Invocations list")]
public void Test002()
{
var identifier = "fooFunc";
var args = new[] { "bar", "baz" };
using var cts = new CancellationTokenSource();
var sut = CreateSut(JSRuntimeMode.Loose);
sut.JSRuntime.InvokeAsync<object>(identifier, cts.Token, args);
var invocation = sut.Invocations[identifier].Single();
invocation.Identifier.ShouldBe(identifier);
invocation.Arguments.ShouldBe(args);
invocation.CancellationToken.ShouldBe(cts.Token);
}
[Fact(DisplayName = "Mock throws exception when in strict mode and invocation has not been setup")]
public void Test003()
{
var sut = CreateSut(JSRuntimeMode.Strict);
var identifier = "func";
var args = new[] { "bar", "baz" };
Should.Throw<JSRuntimeUnhandledInvocationException>(async () => await sut.JSRuntime.InvokeVoidAsync(identifier, args))
.Invocation.ShouldSatisfyAllConditions(
x => x.Identifier.ShouldBe(identifier),
x => x.Arguments.ShouldBe(args));
Should.Throw<JSRuntimeUnhandledInvocationException>(async () => await sut.JSRuntime.InvokeAsync<object>(identifier, args))
.Invocation.ShouldSatisfyAllConditions(
x => x.Identifier.ShouldBe(identifier),
x => x.Arguments.ShouldBe(args));
}
[Fact(DisplayName = "All invocations received AFTER a invocation handler " +
"has a result set, receives the same result")]
public async Task Test005x()
{
var identifier = "func";
var expectedResult = Guid.NewGuid();
var sut = CreateSut(JSRuntimeMode.Strict);
var jsRuntime = sut.JSRuntime;
var handler = sut.Setup<Guid>(identifier);
handler.SetResult(expectedResult);
var i1 = jsRuntime.InvokeAsync<Guid>(identifier);
var i2 = jsRuntime.InvokeAsync<Guid>(identifier);
(await i1).ShouldBe(expectedResult);
(await i2).ShouldBe(expectedResult);
}
[Fact(DisplayName = "All invocations received BEFORE a invocation handler " +
"has a result set, receives the same result")]
public async Task Test005()
{
var identifier = "func";
var expectedResult = Guid.NewGuid();
var sut = CreateSut(JSRuntimeMode.Strict);
var jsRuntime = sut.JSRuntime;
var handler = sut.Setup<Guid>(identifier);
var i1 = jsRuntime.InvokeAsync<Guid>(identifier);
var i2 = jsRuntime.InvokeAsync<Guid>(identifier);
handler.SetResult(expectedResult);
(await i1).ShouldBe(expectedResult);
(await i2).ShouldBe(expectedResult);
}
[Fact(DisplayName = "Invocations receive the latest result set in a invocation handler")]
public async Task Test006x()
{
var identifier = "func";
var sut = CreateSut(JSRuntimeMode.Strict);
var handler = sut.Setup<Guid>(identifier);
var jsRuntime = sut.JSRuntime;
var expectedResult1 = Guid.NewGuid();
handler.SetResult(expectedResult1);
var i1 = jsRuntime.InvokeAsync<Guid>(identifier);
var expectedResult2 = Guid.NewGuid();
handler.SetResult(expectedResult2);
var i2 = jsRuntime.InvokeAsync<Guid>(identifier);
(await i1).ShouldBe(expectedResult1);
(await i2).ShouldBe(expectedResult2);
}
[Fact(DisplayName = "A invocation handler can be canceled for any waiting received invocations")]
public void Test007()
{
var identifier = "func";
var sut = CreateSut(JSRuntimeMode.Strict);
var handler = sut.Setup<Guid>(identifier);
var invocation = sut.JSRuntime.InvokeAsync<Guid>(identifier);
handler.SetCanceled();
invocation.IsCanceled.ShouldBeTrue();
}
[Fact(DisplayName = "A invocation handler can be canceled after it has been set to a different result")]
public void Test107()
{
var identifier = "func";
var sut = CreateSut(JSRuntimeMode.Strict);
var handler = sut.Setup<Guid>(identifier);
var invocation1 = sut.JSRuntime.InvokeAsync<Guid>(identifier);
handler.SetResult(Guid.NewGuid());
invocation1.IsCompletedSuccessfully.ShouldBeTrue();
handler.SetCanceled();
var invocation2 = sut.JSRuntime.InvokeAsync<Guid>(identifier);
invocation2.IsCanceled.ShouldBeTrue();
}
[Fact(DisplayName = "A invocation handler can throw an exception for any waiting received invocations")]
public async Task Test008()
{
var identifier = "func";
var sut = CreateSut(JSRuntimeMode.Strict);
var handler = sut.Setup<Guid>(identifier);
var invocation = sut.JSRuntime.InvokeAsync<Guid>(identifier);
var expectedException = new InvalidOperationException("TADA");
handler.SetException(expectedException);
var actual = await Should.ThrowAsync<InvalidOperationException>(invocation.AsTask());
actual.ShouldBe(expectedException);
invocation.IsFaulted.ShouldBeTrue();
}
[Fact(DisplayName = "A invocation handler can throw an exception after it has been set to a different result")]
public void Test108()
{
var identifier = "func";
var sut = CreateSut(JSRuntimeMode.Strict);
var handler = sut.Setup<Guid>(identifier);
var expectedException = new InvalidOperationException("TADA");
var invocation1 = sut.JSRuntime.InvokeAsync<Guid>(identifier);
handler.SetResult(Guid.NewGuid());
invocation1.IsCompletedSuccessfully.ShouldBeTrue();
handler.SetException(expectedException);
var invocation2 = sut.JSRuntime.InvokeAsync<Guid>(identifier);
invocation2.IsFaulted.ShouldBeTrue();
}
[Fact(DisplayName = "Invocations returns all from a invocation handler")]
public void Test009()
{
var identifier = "func";
var sut = new BunitJSInterop();
var handler = sut.Setup<Guid>(identifier, _ => true);
sut.JSRuntime.InvokeAsync<Guid>(identifier, "first");
sut.JSRuntime.InvokeAsync<Guid>(identifier, "second");
var invocations = handler.Invocations;
invocations[identifier].Count.ShouldBe(2);
invocations[identifier][0].Arguments[0].ShouldBe("first");
invocations[identifier][1].Arguments[0].ShouldBe("second");
}
[Fact(DisplayName = "Arguments used in Setup are matched with invocations")]
public void Test010()
{
var sut = CreateSut(JSRuntimeMode.Strict);
var planned = sut.Setup<object>("foo", "bar", 42);
sut.JSRuntime.InvokeAsync<object>("foo", "bar", 42);
Should.Throw<JSRuntimeUnhandledInvocationException>(() => sut.JSRuntime.InvokeAsync<object>("foo", "bar", 41));
planned.Invocations.Count.ShouldBe(1);
var invocation = planned.Invocations["foo"][0];
invocation.Identifier.ShouldBe("foo");
invocation.Arguments[0].ShouldBe("bar");
invocation.Arguments[1].ShouldBe(42);
}
[Fact(DisplayName = "Argument matcher used in Setup are matched with invocations")]
public void Test011()
{
var sut = CreateSut(JSRuntimeMode.Strict);
var planned = sut.Setup<object>("foo", x => x.Arguments.Count == 1);
sut.JSRuntime.InvokeAsync<object>("foo", 42);
Should.Throw<JSRuntimeUnhandledInvocationException>(() => sut.JSRuntime.InvokeAsync<object>("foo", "bar", 42));
planned.Invocations.Count.ShouldBe(1);
var invocation = planned.Invocations["foo"][0];
invocation.Identifier.ShouldBe("foo");
invocation.Arguments.Count.ShouldBe(1);
invocation.Arguments[0].ShouldBe(42);
}
[Fact(DisplayName = "SetupVoid returns a invocation handler that does not take a result object")]
public async Task Test012()
{
var identifier = "func";
var sut = CreateSut(JSRuntimeMode.Strict);
var handler = sut.SetupVoid(identifier);
var invocation = sut.JSRuntime.InvokeVoidAsync(identifier);
handler.SetVoidResult();
await invocation;
invocation.IsCompletedSuccessfully.ShouldBeTrue();
}
[Fact(DisplayName = "Arguments used in SetupVoid are matched with invocations")]
public void Test013()
{
var sut = CreateSut(JSRuntimeMode.Strict);
var planned = sut.SetupVoid("foo", "bar", 42);
sut.JSRuntime.InvokeVoidAsync("foo", "bar", 42);
Should.Throw<JSRuntimeUnhandledInvocationException>(async () => await sut.JSRuntime.InvokeVoidAsync("foo", "bar", 41));
planned.Invocations.Count.ShouldBe(1);
var invocation = planned.Invocations["foo"][0];
invocation.Identifier.ShouldBe("foo");
invocation.Arguments[0].ShouldBe("bar");
invocation.Arguments[1].ShouldBe(42);
}
[Fact(DisplayName = "Argument matcher used in SetupVoid are matched with invocations")]
public void Test014()
{
var sut = CreateSut(JSRuntimeMode.Strict);
var planned = sut.SetupVoid("foo", x => x.Arguments.Count == 2);
sut.JSRuntime.InvokeVoidAsync("foo", "bar", 42);
Should.Throw<JSRuntimeUnhandledInvocationException>(async () => await sut.JSRuntime.InvokeVoidAsync("foo", 42));
Should.Throw<JSRuntimeUnhandledInvocationException>(async () => await sut.JSRuntime.InvokeVoidAsync("foo"));
planned.Invocations.Count.ShouldBe(1);
var invocation = planned.Invocations["foo"][0];
invocation.Identifier.ShouldBe("foo");
invocation.Arguments.Count.ShouldBe(2);
invocation.Arguments[0].ShouldBe("bar");
invocation.Arguments[1].ShouldBe(42);
}
[Fact(DisplayName = "Empty Setup returns the same result for all matching return type invocation")]
public async Task Test015()
{
var sut = CreateSut(JSRuntimeMode.Strict);
var handler = sut.Setup<Guid>();
var jsRuntime = sut.JSRuntime;
var expectedResult1 = Guid.NewGuid();
handler.SetResult(expectedResult1);
var i1 = jsRuntime.InvokeAsync<Guid>("someFunc");
var i2 = jsRuntime.InvokeAsync<Guid>("otherFunc");
(await i1).ShouldBe(expectedResult1);
(await i2).ShouldBe(expectedResult1);
}
[Fact(DisplayName = "Empty Setup only matches the configured return type")]
public void Test016()
{
var sut = CreateSut(JSRuntimeMode.Strict);
var planned = sut.Setup<Guid>();
Should.Throw<JSRuntimeUnhandledInvocationException>(() => sut.JSRuntime.InvokeAsync<string>("foo"));
planned.Invocations.Count.ShouldBe(0);
}
[Fact(DisplayName = "Empty Setup allows to return different results by return types")]
public async Task Test017()
{
var sut = CreateSut(JSRuntimeMode.Strict);
var handler1 = sut.Setup<Guid>();
var handler2 = sut.Setup<string>();
var jsRuntime = sut.JSRuntime;
var expectedResult1 = Guid.NewGuid();
handler1.SetResult(expectedResult1);
var i1 = jsRuntime.InvokeAsync<Guid>("someFunc");
var expectedResult2 = "somestring";
handler2.SetResult(expectedResult2);
var i2 = jsRuntime.InvokeAsync<string>("otherFunc");
(await i1).ShouldBe(expectedResult1);
(await i2).ShouldBe(expectedResult2);
}
[Fact(DisplayName = "Empty Setup is only used when there is no handler exist for the invocation identifier")]
public async Task Test018()
{
var sut = CreateSut(JSRuntimeMode.Strict);
var catchAllhandler = sut.Setup<Guid>();
var jsRuntime = sut.JSRuntime;
var catchAllexpectedResult = Guid.NewGuid();
catchAllhandler.SetResult(catchAllexpectedResult);
var expectedResult = Guid.NewGuid();
var handler = sut.Setup<Guid>("func");
handler.SetResult(expectedResult);
var i1 = jsRuntime.InvokeAsync<Guid>("someFunc");
var i2 = jsRuntime.InvokeAsync<Guid>("func");
(await i1).ShouldBe(catchAllexpectedResult);
(await i2).ShouldBe(expectedResult);
}
[Fact(DisplayName = "Empty Setup uses the last set result")]
public async Task Test019()
{
var sut = CreateSut(JSRuntimeMode.Strict);
var handler1 = sut.Setup<Guid>();
var handler2 = sut.Setup<Guid>();
var jsRuntime = sut.JSRuntime;
var expectedResult1 = Guid.NewGuid();
var expectedResult2 = Guid.NewGuid();
handler1.SetResult(expectedResult1);
handler2.SetResult(expectedResult2);
var i1 = jsRuntime.InvokeAsync<Guid>("someFunc");
(await i1).ShouldBe(expectedResult2);
}
[Fact(DisplayName = "SetupVoid matches all void invocations")]
public async Task Test020()
{
var identifier = "someFunc";
var sut = CreateSut(JSRuntimeMode.Strict);
var handler = sut.SetupVoid();
Should.Throw<JSRuntimeUnhandledInvocationException>(() => sut.JSRuntime.InvokeAsync<string>(identifier));
var invocation = sut.JSRuntime.InvokeVoidAsync(identifier);
handler.SetVoidResult();
await invocation;
invocation.IsCompletedSuccessfully.ShouldBeTrue();
handler.Invocations.Count.ShouldBe(1);
}
[Fact(DisplayName = "Empty Setup is not used for invocation with void return types")]
public void Test021()
{
var sut = CreateSut(JSRuntimeMode.Strict);
sut.Setup<Guid>();
Should.Throw<JSRuntimeUnhandledInvocationException>(async () => await sut.JSRuntime.InvokeVoidAsync("someFunc"));
}
[Fact(DisplayName = "CatchAll handlers SetupVoid is only used when there is no void handler")]
public async Task Test022()
{
var identifier = "someFunc";
var sut = CreateSut(JSRuntimeMode.Strict);
var handler = sut.SetupVoid(identifier);
var plannedCatchall = sut.SetupVoid(); // should not be used, even if registered after more specific handler
var invocation = sut.JSRuntime.InvokeVoidAsync(identifier);
handler.SetVoidResult();
handler.Invocations.Count.ShouldBe(1);
plannedCatchall.Invocations.Count.ShouldBe(0);
await invocation;
invocation.IsCompletedSuccessfully.ShouldBeTrue();
}
[Fact(DisplayName = "The last handler matching an invocation receives the invocation")]
public void Test030()
{
var identifier = "someFunc";
var sut = CreateSut(JSRuntimeMode.Strict);
var h1 = sut.Setup<string>(identifier);
var h2 = sut.Setup<string>(identifier);
sut.JSRuntime.InvokeAsync<string>(identifier);
h1.Invocations.ShouldBeEmpty();
h2.Invocations.Count.ShouldBe(1);
}
[Fact(DisplayName = "TryGetInvokeHandler returns null when no handlers matches the arguments")]
public void Test040()
{
var sut = CreateSut(JSRuntimeMode.Loose);
var actual = sut.TryGetInvokeHandler<string>("foo");
actual.ShouldBeNull();
}
[Fact(DisplayName = "TryGetInvokeHandler returns the last handler matching the input parameters")]
public void Test041()
{
var sut = CreateSut(JSRuntimeMode.Loose);
var h1 = sut.Setup<string>("foo");
var expected = sut.Setup<string>("foo");
var actual = sut.TryGetInvokeHandler<string>("foo");
actual.ShouldBe(expected);
actual.ShouldNotBe(h1);
}
[Fact(DisplayName = "TryGetInvokeVoidHandler can find void-return handlers")]
public void Test042()
{
var sut = CreateSut(JSRuntimeMode.Loose);
sut.Setup<object>("foo");
var expected = sut.SetupVoid("foo");
var actual = sut.TryGetInvokeVoidHandler("foo");
actual.ShouldBe(expected);
}
[Fact(DisplayName = "Mock returns default value from IJSInProcessRuntime's invoke method in loose mode without invocation setup")]
public void Test043()
{
var sut = CreateSut(JSRuntimeMode.Loose);
var result = ((IJSInProcessRuntime)sut.JSRuntime).Invoke<object>("ident", Array.Empty<object>());
result.ShouldBe(default);
}
[Fact(DisplayName = "After IJSInProcessRuntime invocation a invocation should be visible from the Invocations list")]
public void Test044()
{
var identifier = "fooFunc";
var args = new[] { "bar", "baz" };
var sut = CreateSut(JSRuntimeMode.Loose);
((IJSInProcessRuntime)sut.JSRuntime).Invoke<object>(identifier, args);
var invocation = sut.Invocations[identifier].Single();
invocation.Identifier.ShouldBe(identifier);
invocation.Arguments.ShouldBe(args);
}
[Fact(DisplayName = "IJSInProcessRuntime invocations receive the result set in a planned invocation")]
public void Test045()
{
var identifier = "func";
var args = new[] { "bar", "baz" };
var sut = CreateSut(JSRuntimeMode.Strict);
var expectedResult = Guid.NewGuid();
var planned = sut.Setup<Guid>(identifier, args);
planned.SetResult(expectedResult);
var i = ((IJSInProcessRuntime)sut.JSRuntime).Invoke<Guid>(identifier, args);
i.ShouldBe(expectedResult);
}
[Fact(DisplayName = "Mock throws exception when in strict mode and IJSInProcessRuntime invocation has not been setup")]
public void Test046()
{
var sut = CreateSut(JSRuntimeMode.Strict);
var identifier = "func";
var args = new[] { "bar", "baz" };
var exception = Should.Throw<JSRuntimeUnhandledInvocationException>(() => ((IJSInProcessRuntime)sut.JSRuntime).Invoke<object>(identifier, args));
exception.Invocation.Identifier.ShouldBe(identifier);
exception.Invocation.Arguments.ShouldBe(args);
}
#if !NET9_0_OR_GREATER
[Fact(DisplayName = "Mock throws exception when in strict mode and IJSUnmarshalledRuntime invocation has not been setup with one argument")]
public void Test047()
{
var sut = CreateSut(JSRuntimeMode.Strict);
var identifier = "func";
var args = new[] { "bar" };
var exception = Should.Throw<JSRuntimeUnhandledInvocationException>(() => ((IJSUnmarshalledRuntime)sut.JSRuntime).InvokeUnmarshalled<string, object>(identifier, "bar"));
exception.Invocation.Identifier.ShouldBe(identifier);
exception.Invocation.Arguments.ShouldBe(args);
}
[Fact(DisplayName = "Mock throws exception when in strict mode and IJSUnmarshalledRuntime invocation has not been setup with two arguments")]
public void Test048()
{
var sut = CreateSut(JSRuntimeMode.Strict);
var identifier = "func";
var args = new[] { "bar", "baz" };
var exception = Should.Throw<JSRuntimeUnhandledInvocationException>(() => ((IJSUnmarshalledRuntime)sut.JSRuntime).InvokeUnmarshalled<string, string, object>(identifier, "bar", "baz"));
exception.Invocation.Identifier.ShouldBe(identifier);
exception.Invocation.Arguments.ShouldBe(args);
}
[Fact(DisplayName = "Mock throws exception when in strict mode and IJSUnmarshalledRuntime invocation has not been setup with three arguments")]
public void Test049()
{
var sut = CreateSut(JSRuntimeMode.Strict);
var identifier = "func";
var args = new[] { "bar", "baz", "bau" };
var exception = Should.Throw<JSRuntimeUnhandledInvocationException>(() => ((IJSUnmarshalledRuntime)sut.JSRuntime).InvokeUnmarshalled<string, string, string, object>(identifier, "bar", "baz", "bau"));
exception.Invocation.Identifier.ShouldBe(identifier);
exception.Invocation.Arguments.ShouldBe(args);
}
[Fact(DisplayName = "Mock throws exception when in strict mode and IJSUnmarshalledRuntime invocation has not been setup with zero arguments")]
public void Test050()
{
var sut = CreateSut(JSRuntimeMode.Strict);
var identifier = "func";
var exception = Should.Throw<JSRuntimeUnhandledInvocationException>(() => ((IJSUnmarshalledRuntime)sut.JSRuntime).InvokeUnmarshalled<object>(identifier));
exception.Invocation.Identifier.ShouldBe(identifier);
exception.Invocation.Arguments.ShouldBeEmpty();
}
[Fact(DisplayName = "An IJSUnmarshalledRuntime invocation should return the correct result when passed one arguments.")]
public void Test055()
{
var identifier = "fooFunc";
var args = new[] { "bar" };
var sut = CreateSut(JSRuntimeMode.Strict);
var expectedResult = Guid.NewGuid();
var planned = sut.Setup<Guid>("fooFunc", args);
planned.SetResult(expectedResult);
var actual = ((IJSUnmarshalledRuntime)sut.JSRuntime).InvokeUnmarshalled<string, Guid>(identifier, "bar");
actual.ShouldBe(expectedResult);
}
[Fact(DisplayName = "An IJSUnmarshalledRuntime invocation should return the correct result when passed two arguments.")]
public void Test056()
{
var identifier = "fooFunc";
var args = new[] { "bar", "baz" };
var sut = CreateSut(JSRuntimeMode.Strict);
var expectedResult = Guid.NewGuid();
var planned = sut.Setup<Guid>("fooFunc", args);
planned.SetResult(expectedResult);
var actual = ((IJSUnmarshalledRuntime)sut.JSRuntime).InvokeUnmarshalled<string, string, Guid>(identifier, "bar", "baz");
actual.ShouldBe(expectedResult);
}
[Fact(DisplayName = "An IJSUnmarshalledRuntime invocation should return the correct result when passed three arguments.")]
public void Test057()
{
var identifier = "fooFunc";
var args = new[] { "bar", "baz", "bao" };
var sut = CreateSut(JSRuntimeMode.Strict);
var expectedResult = Guid.NewGuid();
var planned = sut.Setup<Guid>("fooFunc", args);
planned.SetResult(expectedResult);
var actual = ((IJSUnmarshalledRuntime)sut.JSRuntime).InvokeUnmarshalled<string, string, string, Guid>(identifier, "bar", "baz", "bao");
actual.ShouldBe(expectedResult);
}
[Fact(DisplayName = "An IJSUnmarshalledRuntime invocation should return the correct result when passed zero arguments.")]
public void Test058()
{
var identifier = "fooFunc";
var sut = CreateSut(JSRuntimeMode.Strict);
var expectedResult = Guid.NewGuid();
var planned = sut.Setup<Guid>("fooFunc");
planned.SetResult(expectedResult);
var actual = ((IJSUnmarshalledRuntime)sut.JSRuntime).InvokeUnmarshalled<Guid>(identifier);
actual.ShouldBe(expectedResult);
}
[Theory(DisplayName = "When calling InvokeUnmarshalled(identifier), then the invocation should be visible from the Invocations list"), AutoData]
public void Test310(string identifier)
{
var sut = CreateSut(JSRuntimeMode.Loose);
var jsUnmarshalledRuntime = (IJSUnmarshalledRuntime)sut.JSRuntime;
jsUnmarshalledRuntime.InvokeUnmarshalled<string>(identifier);
sut.Invocations[identifier]
.ShouldHaveSingleItem()
.ShouldBe(new JSRuntimeInvocation(
identifier,
cancellationToken: null,
Array.Empty<object>(),
typeof(string),
invocationMethodName: "InvokeUnmarshalled"));
}
[Theory(DisplayName = "When calling InvokeUnmarshalled(identifier, arg0), then the invocation should be visible from the Invocations list"), AutoData]
public void Test306(string identifier, string arg0)
{
var sut = CreateSut(JSRuntimeMode.Loose);
var jsUnmarshalledRuntime = (IJSUnmarshalledRuntime)sut.JSRuntime;
jsUnmarshalledRuntime.InvokeUnmarshalled<string, string>(identifier, arg0);
sut.Invocations[identifier]
.ShouldHaveSingleItem()
.ShouldBe(new JSRuntimeInvocation(
identifier,
cancellationToken: null,
args: new[] { arg0 },
resultType: typeof(string),
invocationMethodName: "InvokeUnmarshalled"));
}
[Theory(DisplayName = "When calling InvokeUnmarshalled(identifier, arg0, arg1), then the invocation should be visible from the Invocations list"), AutoData]
public void Test307(string identifier, string arg0, string arg1)
{
var sut = CreateSut(JSRuntimeMode.Loose);
var jsUnmarshalledRuntime = (IJSUnmarshalledRuntime)sut.JSRuntime;
jsUnmarshalledRuntime.InvokeUnmarshalled<string, string, string>(identifier, arg0, arg1);
sut.Invocations[identifier]
.ShouldHaveSingleItem()
.ShouldBe(new JSRuntimeInvocation(
identifier,
cancellationToken: null,
args: new[] { arg0, arg1 },
resultType: typeof(string),
invocationMethodName: "InvokeUnmarshalled"));
}
[Theory(DisplayName = "When calling InvokeUnmarshalled(identifier, arg0, arg1, arg2), then the invocation should be visible from the Invocations list"), AutoData]
public void Test308(string identifier, string arg0, string arg1, string arg2)
{
var sut = CreateSut(JSRuntimeMode.Loose);
var jsUnmarshalledRuntime = (IJSUnmarshalledRuntime)sut.JSRuntime;
jsUnmarshalledRuntime.InvokeUnmarshalled<string, string, string, string>(
identifier, arg0, arg1, arg2);
sut.Invocations[identifier]
.ShouldHaveSingleItem()
.ShouldBe(new JSRuntimeInvocation(
identifier,
cancellationToken: null,
args: new[] { arg0, arg1, arg2 },
resultType: typeof(string),
invocationMethodName: "InvokeUnmarshalled"));
}
#endif
[Fact(DisplayName = "JSRuntime invocation times out when handler is not configured")]
public async Task Test309()
{
// Arrange
const string identifier = "testFunction";
BunitContext.DefaultWaitTimeout = TimeSpan.FromMilliseconds(100);
var sut = CreateSut(JSRuntimeMode.Strict);
sut.Setup<int>(identifier);
var invocationTask = sut.JSRuntime.InvokeAsync<int>(identifier);
var exception = await Should.ThrowAsync<JSRuntimeInvocationNotSetException>(invocationTask.AsTask());
exception.Invocation.Identifier.ShouldBe(identifier);
}
#if NET10_0_OR_GREATER
[Fact(DisplayName = "InvokeConstructorAsync returns IJSObjectReference in loose mode without setup")]
public async Task Test400()
{
var sut = CreateSut(JSRuntimeMode.Loose);
var result = await sut.JSRuntime.InvokeConstructorAsync("SomeClass");
result.ShouldNotBeNull();
result.ShouldBeAssignableTo<IJSObjectReference>();
}
[Fact(DisplayName = "InvokeConstructorAsync throws in strict mode when no handler is set up")]
public void Test401()
{
var sut = CreateSut(JSRuntimeMode.Strict);
Should.Throw<JSRuntimeUnhandledInvocationException>(
async () => await sut.JSRuntime.InvokeConstructorAsync("SomeClass"));
}
[Theory(DisplayName = "InvokeConstructorAsync records invocation with correct method name and arguments"), AutoData]
public void Test402(string identifier)
{
var args = new object[] { "arg1", 42 };
var sut = CreateSut(JSRuntimeMode.Loose);
sut.JSRuntime.InvokeConstructorAsync(identifier, args);
var invocation = sut.Invocations[identifier].ShouldHaveSingleItem();
invocation.Identifier.ShouldBe(identifier);
invocation.Arguments.ShouldBe(args);
invocation.InvocationMethodName.ShouldBe("InvokeConstructorAsync");
invocation.ResultType.ShouldBe(typeof(IJSObjectReference));
}
[Theory(DisplayName = "InvokeConstructorAsync with CancellationToken records invocation correctly"), AutoData]
public void Test403(string identifier)
{
var args = new object[] { "arg1" };
using var cts = new CancellationTokenSource();
var sut = CreateSut(JSRuntimeMode.Loose);
sut.JSRuntime.InvokeConstructorAsync(identifier, cts.Token, args);
var invocation = sut.Invocations[identifier].ShouldHaveSingleItem();
invocation.Identifier.ShouldBe(identifier);
invocation.Arguments.ShouldBe(args);
invocation.CancellationToken.ShouldBe(cts.Token);
invocation.InvocationMethodName.ShouldBe("InvokeConstructorAsync");
}
[Fact(DisplayName = "InvokeConstructorAsync with SetupModule handler returns configured object reference")]
public async Task Test404()
{
var sut = CreateSut(JSRuntimeMode.Strict);
sut.SetupModule(inv => inv.Identifier == "SomeClass" && inv.InvocationMethodName == "InvokeConstructorAsync");
var result = await sut.JSRuntime.InvokeConstructorAsync("SomeClass");
result.ShouldNotBeNull();
result.ShouldBeAssignableTo<IJSObjectReference>();
}
[Fact(DisplayName = "InvokeConstructorAsync on IJSObjectReference from module import works in loose mode")]
public async Task Test405()
{
var sut = CreateSut(JSRuntimeMode.Loose);
var module = await sut.JSRuntime.InvokeAsync<IJSObjectReference>("import", "./myModule.js");
var result = await module.InvokeConstructorAsync("JsClass", "arg1", "arg2");
result.ShouldNotBeNull();
result.ShouldBeAssignableTo<IJSObjectReference>();
}
[Fact(DisplayName = "InvokeConstructorAsync on IJSObjectReference records invocation")]
public async Task Test406()
{
var sut = CreateSut(JSRuntimeMode.Loose);
var module = await sut.JSRuntime.InvokeAsync<IJSObjectReference>("import", "./myModule.js");
await module.InvokeConstructorAsync("JsClass", "arg1");
sut.Invocations["JsClass"].ShouldHaveSingleItem()
.InvocationMethodName.ShouldBe("InvokeConstructorAsync");
}
#endif
}