-
-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathSwalTest.cs
More file actions
326 lines (296 loc) · 10.5 KB
/
SwalTest.cs
File metadata and controls
326 lines (296 loc) · 10.5 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
// 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 SwalTest : BootstrapBlazorTestBase
{
[Fact]
public void Show_Ok()
{
var cut = Context.Render<BootstrapBlazorRoot>(pb =>
{
pb.AddChildContent<MockSwalTest>();
});
var swal = cut.FindComponent<MockSwalTest>().Instance.SwalService;
cut.InvokeAsync(() => swal.Show(new SwalOption()
{
BodyTemplate = builder => builder.AddContent(0, "Test-BodyTemplate"),
FooterTemplate = builder => builder.AddContent(0, "Test-FooterTemplate"),
ButtonTemplate = builder => builder.AddContent(0, "Test-ButtonTemplate"),
ShowFooter = true,
ShowClose = true,
CloseButtonIcon = "test-close-icon",
CloseButtonText = "test-button-text-Cancel",
Class = "dialog-swal-test"
}));
// 代码覆盖模板单元测试
Assert.Contains("Test-BodyTemplate", cut.Markup);
Assert.Contains("Test-FooterTemplate", cut.Markup);
Assert.Contains("Test-ButtonTemplate", cut.Markup);
Assert.Contains("test-close-icon", cut.Markup);
Assert.Contains("test-button-text-Cancel", cut.Markup);
Assert.Contains("dialog-swal-test", cut.Markup);
// 测试关闭逻辑
var modals = cut.FindComponents<Modal>();
var modal = modals[modals.Count - 1];
cut.InvokeAsync(() => modal.Instance.CloseCallback());
// 测试 Category
cut.InvokeAsync(() => swal.Show(new SwalOption()
{
Content = "I am Eror",
Category = SwalCategory.Error
}));
Assert.Contains("swal2-x-mark-line-left", cut.Markup);
cut.InvokeAsync(() => modal.Instance.CloseCallback());
// 测试 Category
cut.InvokeAsync(() => swal.Show(new SwalOption()
{
Content = "I am Eror",
Category = SwalCategory.Information,
ShowFooter = true,
Footer = "I am Footer"
}));
Assert.Contains("swal2-info", cut.Markup);
Assert.Contains("I am Footer", cut.Markup);
cut.InvokeAsync(() => modal.Instance.CloseCallback());
// 测试 Category
cut.InvokeAsync(() => swal.Show(new SwalOption()
{
Content = "I am Eror",
Category = SwalCategory.Warning
}));
Assert.Contains("swal2-warning", cut.Markup);
cut.InvokeAsync(() => modal.Instance.CloseCallback());
// 测试 Category
cut.InvokeAsync(() => swal.Show(new SwalOption()
{
Content = "I am Eror",
Category = SwalCategory.Question
}));
Assert.Contains("swal2-question", cut.Markup);
cut.InvokeAsync(() => modal.Instance.CloseCallback());
//测试 Content
cut.InvokeAsync(() => swal.Show(new SwalOption()
{
Content = "I am Swal",
}));
Assert.Contains("I am Swal", cut.Markup);
cut.InvokeAsync(() => modal.Instance.CloseCallback());
//测试 Title
cut.InvokeAsync(() => swal.Show(new SwalOption()
{
Title = "I am Title",
}));
Assert.Contains("I am Title", cut.Markup);
cut.InvokeAsync(() => modal.Instance.CloseCallback());
//测试 Title
cut.InvokeAsync(() => swal.Show(new SwalOption()
{
Title = "I am Title",
Content = "I am Swal",
}));
Assert.Contains("I am Title", cut.Markup);
cut.InvokeAsync(() => modal.Instance.CloseCallback());
//测试关闭按钮
cut.InvokeAsync(() => swal.Show(new SwalOption()
{
Content = "I am Swal",
IsAutoHide = true,
Delay = 1000
}));
cut.InvokeAsync(() =>
{
var button = cut.Find(".btn-secondary");
button.Click();
});
cut.InvokeAsync(() => modal.Instance.CloseCallback());
// auto close
cut.InvokeAsync(() => swal.Show(new SwalOption()
{
Content = "I am auto hide",
IsAutoHide = true,
Delay = 100
}));
Thread.Sleep(150);
// 弹窗显示
cut.Contains("I am auto hide");
Thread.Sleep(150);
// 模拟关闭
cut.InvokeAsync(() => modal.Instance.CloseCallback());
cut.DoesNotContain("I am auto hide");
// 模态框
bool result = false;
bool confirmed = false;
Task.Run(async () => await cut.InvokeAsync(async () =>
{
result = await swal.ShowModal(new SwalOption()
{
Content = "I am Modal Swal",
CancelButtonText = "test-cancel-text",
ConfirmButtonIcon = "test-confirm-icon",
ConfirmButtonText = "test-confirm-text",
OnConfirmAsync = () =>
{
confirmed = true;
return Task.CompletedTask;
}
});
}));
var tick = DateTime.Now;
while (!cut.Markup.Contains("test-cancel-text"))
{
Thread.Sleep(100);
if (DateTime.Now > tick.AddSeconds(1))
{
break;
}
}
cut.Contains("test-cancel-text");
cut.Contains("I am Modal Swal");
cut.Contains("test-confirm-icon");
cut.Contains("test-confirm-text");
// 触发确认按钮
cut.InvokeAsync(() =>
{
var button = cut.Find(".btn-danger");
button.Click();
Assert.True(result);
Assert.True(confirmed);
});
cut.InvokeAsync(() => modal.Instance.CloseCallback());
// OnCloseAsync 测试
Task.Run(async () => await cut.InvokeAsync(async () =>
{
result = await swal.ShowModal(new SwalOption()
{
Content = "I am Modal Swal",
CancelButtonText = "test-cancel-text",
ConfirmButtonIcon = "test-confirm-icon",
ConfirmButtonText = "test-confirm-text",
OnCloseAsync = () =>
{
return Task.CompletedTask;
}
});
}));
tick = DateTime.Now;
while (!cut.Markup.Contains("test-cancel-text"))
{
Thread.Sleep(100);
if (DateTime.Now > tick.AddSeconds(1))
{
break;
}
}
// 触发关闭按钮
cut.InvokeAsync(() =>
{
var button = cut.Find(".btn-secondary");
button.Click();
});
cut.InvokeAsync(() => modal.Instance.CloseCallback());
// 带确认框的 Select
cut.Render(pb =>
{
pb.AddChildContent<Select<string>>(pb =>
{
pb.Add(a => a.Items, new List<SelectedItem>()
{
new("1", "Test1"),
new("2", "Test2") { IsDisabled = true }
});
pb.Add(a => a.SwalCategory, SwalCategory.Question);
pb.Add(a => a.SwalTitle, "Swal-Title");
pb.Add(a => a.SwalContent, "Swal-Content");
pb.Add(a => a.OnBeforeSelectedItemChange, item => Task.FromResult(true));
pb.Add(a => a.OnSelectedItemChanged, item => Task.CompletedTask);
pb.Add(a => a.SwalFooter, "test-swal-footer");
});
});
Task.Run(() => cut.InvokeAsync(() => cut.FindComponent<Select<string>>().Instance.ConfirmSelectedItem(0)));
tick = DateTime.Now;
while (!cut.Markup.Contains("test-swal-footer"))
{
Thread.Sleep(100);
if (DateTime.Now > tick.AddSeconds(2))
{
break;
}
}
cut.InvokeAsync(() =>
{
var button = cut.Find(".btn-danger");
button.Click();
});
cut.InvokeAsync(() => modal.Instance.CloseCallback());
// test force
var forceOption = new SwalOption()
{
Content = "I am auto hide",
ForceDelay = true,
Delay = 1234
};
cut.InvokeAsync(() => swal.Show(forceOption));
cut.InvokeAsync(() => modal.Instance.CloseCallback());
Assert.NotEqual(4000, forceOption.Delay);
Assert.Equal(1234, forceOption.Delay);
forceOption.ForceDelay = false;
cut.InvokeAsync(() => swal.Show(forceOption));
cut.InvokeAsync(async () =>
{
await modal.Instance.CloseCallback();
Assert.Equal(4000, forceOption.Delay);
});
// 自动关闭
cut.InvokeAsync(() => swal.Show(new SwalOption()
{
Content = "I am auto hide",
IsAutoHide = true,
ForceDelay = true,
Delay = 500,
OnCloseAsync = () =>
{
return Task.CompletedTask;
}
}));
Thread.Sleep(150);
// 弹窗显示
cut.Contains("I am auto hide");
Thread.Sleep(1000);
cut.InvokeAsync(() => modal.Instance.CloseCallback());
// CloseAsync 方法测试
var op = new SwalOption()
{
Content = "I am close swal",
ShowClose = false
};
cut.InvokeAsync(() => swal.Show(op));
cut.DoesNotContain("<button ");
cut.InvokeAsync(() => op.CloseAsync());
cut.InvokeAsync(() => modal.Instance.CloseCallback());
// IsConfirm CloseAsync
cut.InvokeAsync(() => swal.ShowModal(op));
cut.InvokeAsync(() => op.CloseAsync());
cut.InvokeAsync(() => modal.Instance.CloseCallback());
// 自动隐藏时间未到时触发 Disposing
cut.InvokeAsync(() => swal.Show(new SwalOption()
{
Content = "I am auto hide",
IsAutoHide = true,
Delay = 4000
}));
Thread.Sleep(150);
// 弹窗显示
cut.Contains("I am auto hide");
var alert = cut.FindComponent<SweetAlert>();
cut.InvokeAsync(async () => await alert.Instance.DisposeAsync());
cut.InvokeAsync(() => modal.Instance.CloseCallback());
}
private class MockSwalTest : ComponentBase
{
[Inject]
[NotNull]
public SwalService? SwalService { get; set; }
}
}