-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathBunitContextTest.cs
More file actions
310 lines (252 loc) · 8.85 KB
/
BunitContextTest.cs
File metadata and controls
310 lines (252 loc) · 8.85 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
using Bunit.TestAssets.SampleComponents.DisposeComponents;
using Bunit.TestDoubles;
namespace Bunit;
public class BunitContextTest : BunitContext
{
[Fact(DisplayName = "DisposeComponents disposes rendered components in parent to child order")]
public async Task Test101()
{
var callStack = new List<string>();
Render<ParentDispose>(ps => ps.Add(p => p.CallStack, callStack));
await DisposeComponentsAsync();
callStack.Count.ShouldBe(2);
callStack[0].ShouldBe("ParentDispose");
callStack[1].ShouldBe("ChildDispose");
}
[Fact(DisplayName = "DisposeComponents disposes multiple rendered components")]
public async Task Test102()
{
var callStack = new List<string>();
Render<ChildDispose>(ps => ps.Add(p => p.CallStack, callStack));
Render<ChildDispose>(ps => ps.Add(p => p.CallStack, callStack));
await DisposeComponentsAsync();
callStack.Count.ShouldBe(2);
}
[Fact(DisplayName = "DisposeComponents captures exceptions from Dispose in Renderer.UnhandledException")]
public async Task Test103()
{
Render<ThrowExceptionComponent>();
await DisposeComponentsAsync();
var actual = await Renderer.UnhandledException;
actual.ShouldBeAssignableTo<NotSupportedException>();
}
[Fact(DisplayName = "DisposeComponents disposes components nested in render fragments")]
public async Task Test104()
{
var callStack = new List<string>();
Render(DisposeFragments.ChildDisposeAsFragment(callStack));
await DisposeComponentsAsync();
callStack.Count.ShouldBe(1);
}
[Fact(DisplayName = "The test service provider should register a placeholder HttpClient which throws exceptions")]
public void Test024()
{
Should.Throw<MissingMockHttpClientException>(() => Render<SimpleWithHttpClient>());
}
[Fact(DisplayName = "The test service provider should register a placeholder IStringLocalizer which throws exceptions")]
public void Test026()
{
Should.Throw<MissingMockStringLocalizationException>(() => Render<SimpleUsingLocalizer>());
}
[Fact(DisplayName = "Render() renders fragment inside RenderTree")]
public void Test030()
{
RenderTree.Add<CascadingValue<string>>(ps => ps.Add(p => p.Value, "FOO"));
var cut = Render(b =>
{
b.OpenComponent<ReceivesCascadingValue>(0);
b.CloseComponent();
});
cut.FindComponent<ReceivesCascadingValue>()
.Instance
.Value
.ShouldBe("FOO");
}
[Fact(DisplayName = "Render<TComponent>() renders fragment inside RenderTreee")]
public void Test031()
{
RenderTree.Add<CascadingValue<string>>(ps => ps.Add(p => p.Value, "FOO"));
var cut = Render<ReceivesCascadingValue>(b =>
{
b.OpenComponent<ReceivesCascadingValue>(0);
b.CloseComponent();
});
cut.Instance
.Value
.ShouldBe("FOO");
}
[Fact(DisplayName = "Render<TComponent>(builder) renders TComponent inside RenderTreee")]
public void Test032()
{
RenderTree.Add<CascadingValue<string>>(ps => ps.Add(p => p.Value, "FOO"));
var cut = Render<ReceivesCascadingValue>(ps => ps.Add(p => p.Dummy, null));
cut.Instance
.Value
.ShouldBe("FOO");
}
[Fact(DisplayName = "Render<TComponent>(factories) renders TComponent inside RenderTreee")]
public void Test033()
{
RenderTree.Add<CascadingValue<string>>(ps => ps.Add(p => p.Value, "FOO"));
var cut = Render<ReceivesCascadingValue>(ps => ps.Add(p => p.Dummy, null!));
cut.Instance
.Value
.ShouldBe("FOO");
}
[Fact(DisplayName = "Can raise events from markup rendered with BunitContext")]
public void Test040()
{
Should.NotThrow(() => Render<ClickCounter>().Find("button").Click());
}
[Fact(DisplayName = "BunitContext should provide a default IErrorBoundaryLogger")]
public void Test001()
{
IErrorBoundaryLogger logger = Services.GetService<IErrorBoundaryLogger>();
logger.ShouldNotBe(null);
}
[Fact(DisplayName = "ComponentFactories CanCreate() method are checked during component instantiation")]
public void Test0001()
{
var mock = CreateMockComponentFactory(canCreate: _ => false, create: _ => null);
ComponentFactories.Add(mock);
Render<Simple1>();
mock.Received(1).CanCreate(typeof(Simple1));
mock.DidNotReceive().Create(Arg.Any<Type>());
}
[Fact(DisplayName = "ComponentFactories Create() method is called when their CanCreate() method returns true")]
public void Test0002()
{
var mock = CreateMockComponentFactory(canCreate: _ => true, create: _ => new Simple1());
ComponentFactories.Add(mock);
Render<Simple1>();
mock.Received(1).CanCreate(typeof(Simple1));
mock.Received(1).Create(typeof(Simple1));
}
[Fact(DisplayName = "ComponentFactories is used in last added order")]
public void Test0003()
{
var firstMock = CreateMockComponentFactory(canCreate: _ => true, create: _ => new Simple1());
var secondMock = CreateMockComponentFactory(canCreate: _ => true, create: _ => new Simple1());
ComponentFactories.Add(firstMock);
ComponentFactories.Add(secondMock);
Render<Simple1>();
firstMock.DidNotReceive().CanCreate(Arg.Any<Type>());
firstMock.DidNotReceive().Create(Arg.Any<Type>());
secondMock.Received(1).CanCreate(typeof(Simple1));
secondMock.Received(1).Create(typeof(Simple1));
}
[Fact(DisplayName = "DisposeComponents captures exceptions from DisposeAsync in Renderer.UnhandledException")]
public async Task Test201()
{
Render<AsyncThrowAfterDelayComponent>();
await DisposeComponentsAsync();
var actual = await Renderer.UnhandledException;
actual.ShouldBeAssignableTo<NotSupportedException>();
}
[Fact(DisplayName = "DisposeComponents calls DisposeAsync on rendered components")]
public async Task Test202()
{
var cut = Render<AsyncDisposableComponent>();
var wasDisposedTask = cut.Instance.DisposedTask;
await DisposeComponentsAsync();
wasDisposedTask.Status.ShouldBe(TaskStatus.RanToCompletion);
}
[Fact(DisplayName = "DisposeComponents should dispose components added via ComponentFactory")]
public async Task Test203()
{
ComponentFactories.Add<ChildDispose, MyChildDisposeStub>();
var cut = Render<ParentDispose>(ps => ps.Add(p => p.CallStack, new List<string>()));
var instance = cut.FindComponent<MyChildDisposeStub>().Instance;
await DisposeComponentsAsync();
instance.WasDisposed.ShouldBeTrue();
}
[Fact(DisplayName = "Can correctly resolve and dispose of scoped disposable service")]
public async Task Net5Test001()
{
AsyncDisposableService asyncDisposable;
await using (var sut = new BunitContext())
{
sut.Services.AddScoped<AsyncDisposableService>();
asyncDisposable = sut.Services.GetService<AsyncDisposableService>();
}
asyncDisposable.IsDisposed.ShouldBeTrue();
}
[Fact(DisplayName = "Can correctly resolve and dispose of transient disposable service")]
public async Task Net5Test002()
{
AsyncDisposableService asyncDisposable;
await using (var sut = new BunitContext())
{
sut.Services.AddTransient<AsyncDisposableService>();
asyncDisposable = sut.Services.GetService<AsyncDisposableService>();
}
asyncDisposable.IsDisposed.ShouldBeTrue();
}
[Fact(DisplayName = "Can correctly resolve and dispose of singleton disposable service")]
public async Task Net5Test003()
{
AsyncDisposableService asyncDisposable;
await using (var sut = new BunitContext())
{
sut.Services.AddSingleton<AsyncDisposableService>();
asyncDisposable = sut.Services.GetService<AsyncDisposableService>();
}
asyncDisposable.IsDisposed.ShouldBeTrue();
}
private sealed class ReceivesCascadingValue : ComponentBase
{
[CascadingParameter] public string? Value { get; set; }
[Parameter] public object? Dummy { get; set; }
}
private sealed class ThrowExceptionComponent : ComponentBase, IDisposable
{
public void Dispose()
{
#pragma warning disable S3877
throw new NotSupportedException();
#pragma warning restore S3877
}
}
private sealed class AsyncDisposableService : IAsyncDisposable
{
public bool IsDisposed { get; private set; }
public ValueTask DisposeAsync()
{
IsDisposed = true;
return ValueTask.CompletedTask;
}
}
private sealed class MyChildDisposeStub : ComponentBase, IDisposable
{
public bool WasDisposed { get; private set; }
public void Dispose()
{
WasDisposed = true;
}
}
private sealed class AsyncThrowAfterDelayComponent : ComponentBase, IAsyncDisposable
{
public async ValueTask DisposeAsync()
{
await Task.Delay(1).ConfigureAwait(ConfigureAwaitOptions.ForceYielding);
throw new NotSupportedException();
}
}
private sealed class AsyncDisposableComponent : ComponentBase, IAsyncDisposable
{
private readonly TaskCompletionSource tsc = new();
public Task DisposedTask => tsc.Task;
public async ValueTask DisposeAsync()
{
await Task.Delay(10);
tsc.SetResult();
}
}
private static IComponentFactory CreateMockComponentFactory(Func<Type, bool> canCreate, Func<Type, IComponent> create)
{
var result = Substitute.For<IComponentFactory>();
result.CanCreate(Arg.Any<Type>()).Returns(call => canCreate((Type)call[0]));
result.Create(Arg.Any<Type>()).Returns(call => create((Type)call[0]));
return result;
}
}