-
-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathModalTest.cs
More file actions
191 lines (164 loc) · 5.13 KB
/
ModalTest.cs
File metadata and controls
191 lines (164 loc) · 5.13 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
// 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;
namespace UnitTest.Components;
public class ModalTest : BootstrapBlazorTestBase
{
[Fact]
public void IsBackdrop_Ok()
{
var cut = Context.RenderComponent<Modal>(pb =>
{
pb.Add(m => m.IsBackdrop, true);
pb.Add(m => m.IsFade, false);
});
Assert.DoesNotContain("static", cut.Markup);
Assert.DoesNotContain("modal fade", cut.Markup);
cut.SetParametersAndRender(pb =>
{
pb.Add(m => m.IsBackdrop, false);
});
Assert.Contains("static", cut.Markup);
}
[Fact]
public void Toggle_Ok()
{
var cut = Context.RenderComponent<Modal>(pb =>
{
pb.AddChildContent<ModalDialog>();
});
cut.InvokeAsync(async () => await cut.Instance.Toggle());
Assert.Contains("modal fade", cut.Markup);
Assert.Contains("modal-dialog", cut.Markup);
}
[Fact]
public void Close_Ok()
{
var cut = Context.RenderComponent<Modal>();
cut.InvokeAsync(async () => await cut.Instance.Close());
cut.SetParametersAndRender(pb =>
{
pb.AddChildContent<ModalDialog>();
});
cut.InvokeAsync(async () => await cut.Instance.Close());
// 多弹窗
cut.SetParametersAndRender(pb =>
{
pb.AddChildContent(builder =>
{
builder.OpenComponent<ModalDialog>(0);
builder.CloseComponent();
builder.OpenComponent<ModalDialog>(1);
builder.CloseComponent();
});
});
cut.InvokeAsync(async () => await cut.Instance.Close());
}
[Fact]
public async Task SetHeaderText_Ok()
{
var cut = Context.RenderComponent<Modal>(pb =>
{
pb.AddChildContent<ModalDialog>();
});
var header = cut.Find(".modal-title");
Assert.Equal("", header.TextContent);
await cut.InvokeAsync(() => cut.Instance.SetHeaderText("Test-Header"));
header = cut.Find(".modal-title");
Assert.Equal("Test-Header", header.TextContent);
}
[Fact]
public void SetHeaderText_Null()
{
var cut = Context.RenderComponent<MockModal>(pb =>
{
pb.AddChildContent<ModalDialog>();
});
cut.Instance.TestSetHeaderText();
}
[Fact]
public async Task ShownCallbackAsync_Ok()
{
var cut = Context.RenderComponent<MockComponent>();
var modal = cut.FindComponent<MockModal>();
await cut.InvokeAsync(() => modal.Instance.Show_Test());
Assert.True(cut.Instance.Value);
}
[Fact]
public void FirstAfterRenderAsync_Ok()
{
var render = false;
var cut = Context.RenderComponent<Modal>(pb =>
{
pb.Add(a => a.FirstAfterRenderCallbackAsync, modal =>
{
render = true;
return Task.CompletedTask;
});
pb.AddChildContent<ModalDialog>();
});
Assert.True(render);
}
[Fact]
public async Task RegisterShownCallback_Ok()
{
var cut = Context.RenderComponent<Modal>(pb =>
{
pb.AddChildContent<MockFocusComponent>();
});
var component = cut.FindComponent<MockFocusComponent>();
Assert.False(component.Instance.Pass);
await cut.InvokeAsync(cut.Instance.ShownCallback);
Assert.True(component.Instance.Pass);
}
private class MockComponent : ComponentBase
{
public bool Value { get; set; }
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenComponent<MockModal>(0);
builder.AddAttribute(1, nameof(Modal.OnShownAsync), () =>
{
Value = true;
return Task.CompletedTask;
});
builder.CloseComponent();
}
}
private class MockModal : Modal
{
public async Task Show_Test()
{
await base.ShownCallback();
}
public void TestSetHeaderText()
{
Dialogs.Clear();
base.SetHeaderText("");
}
}
private class MockFocusComponent : ComponentBase, IDisposable
{
[CascadingParameter, NotNull]
private Modal? Modal { get; set; }
protected override void OnInitialized()
{
base.OnInitialized();
Modal?.RegisterShownCallback(this, TestCallback);
Modal?.RegisterShownCallback(this, TestCallback);
}
public bool Pass { get; set; }
public Task TestCallback()
{
Pass = true;
return Task.CompletedTask;
}
public void Dispose()
{
Modal?.UnRegisterShownCallback(this);
GC.SuppressFinalize(this);
}
}
}