forked from dotnetcore/BootstrapBlazor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBootstrapBlazorErrorBoundaryTest.cs
More file actions
60 lines (52 loc) · 2.23 KB
/
BootstrapBlazorErrorBoundaryTest.cs
File metadata and controls
60 lines (52 loc) · 2.23 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
namespace UnitTest.Components;
public class BootstrapBlazorErrorBoundaryTest : BootstrapBlazorTestBase
{
// 1. Test rendering of child content when there is no exception
[Fact]
public void ShouldRenderChildContent_WhenNoException()
{
var cut = Context.RenderComponent<BootstrapBlazorErrorBoundary>(parameters => parameters
.AddChildContent("<p>Normal Content</p>")
);
cut.MarkupMatches("<p>Normal Content</p>");
}
// 2. Test rendering custom error content when an exception is thrown and ErrorContent is set
[Fact]
public void ShouldRenderCustomErrorContent_WhenExceptionOccurs()
{
var errorContentRendered = false;
var cut = Context.RenderComponent<BootstrapBlazorErrorBoundary>(parameters => parameters
.Add(p => p.ErrorContent, ex => builder =>
{
errorContentRendered = true;
builder.OpenElement(0, "div");
builder.AddAttribute(1, "class", "custom-error");
builder.AddContent(2, $"Custom error caught: {ex.Message}");
builder.CloseElement();
})
.AddChildContent<ExceptionThrowingComponent>()
);
cut.MarkupMatches("""<div class="custom-error">Custom error caught: Boom!</div>""");
Assert.True(errorContentRendered);
}
// 3. Test rendering default error content when an exception is thrown and ErrorContent is null
[Fact]
public void ShouldRenderDefaultExceptionContent_WhenExceptionOccurs_AndErrorContentIsNull()
{
var cut = Context.RenderComponent<BootstrapBlazorErrorBoundary>(parameters => parameters
.AddChildContent<ExceptionThrowingComponent>()
);
Assert.Contains("Boom!", cut.Markup);
}
private class ExceptionThrowingComponent : ComponentBase
{
protected override void OnInitialized()
{
throw new InvalidOperationException("Boom!");
}
}
}