-
-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathErrorHandlerTest.cs
More file actions
86 lines (76 loc) · 2.99 KB
/
ErrorHandlerTest.cs
File metadata and controls
86 lines (76 loc) · 2.99 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
// 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
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.AspNetCore.Components.Web;
namespace UnitTest.Components;
public class ErrorHandlerTest : BootstrapBlazorTestBase
{
[Fact]
public async Task HandlerException_Ok()
{
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
{
pb.AddChildContent<MockDialogTest>();
});
var dialog = cut.FindComponent<MockDialogTest>().Instance.DialogService;
await cut.InvokeAsync(() => dialog.Show(new DialogOption()
{
BodyTemplate = BootstrapDynamicComponent.CreateComponent<ErrorComponent>().Render()
}));
var errorButton = cut.Find(".btn-error");
await cut.InvokeAsync(() => errorButton.Click());
Assert.Contains("<div class=\"modal-body\"><div class=\"error-stack\">", cut.Markup);
// 关闭弹窗
var btn = cut.Find(".modal-header .btn-close");
await cut.InvokeAsync(() => btn.Click());
}
[Fact]
public async Task ShowToast_Ok()
{
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
{
pb.AddChildContent<ErrorComponent>();
});
var errorButton = cut.Find(".btn-error");
await cut.InvokeAsync(() => errorButton.Click());
cut.Contains("<div class=\"toast-body\">test error logger</div>");
// 关闭 Toast
var toast = cut.FindComponent<Toast>().Instance;
await cut.InvokeAsync(() => toast.Close());
cut.DoesNotContain("<div class=\"toast-body\">test error logger</div>");
cut.SetParametersAndRender(pb =>
{
pb.Add(a => a.ShowToast, false);
});
errorButton = cut.Find(".btn-error");
await cut.InvokeAsync(() => errorButton.Click());
cut.DoesNotContain("<div class=\"toast-body\">test error logger</div>");
}
private class MockDialogTest : ComponentBase
{
[Inject]
[NotNull]
public DialogService? DialogService { get; set; }
}
private class ErrorComponent : ComponentBase
{
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenElement(0, "div");
builder.AddContent(1, pb =>
{
pb.OpenComponent<Button>(1);
pb.AddAttribute(2, nameof(Button.Text), "Error");
pb.AddAttribute(3, "class", "btn-error");
pb.AddAttribute(4, nameof(Button.OnClick), EventCallback.Factory.Create<MouseEventArgs>(this, e =>
{
throw new Exception("test error logger");
}));
pb.CloseComponent();
});
builder.CloseElement();
}
}
}