!2708 test(#I54NDP): improve swal unit test

* test: 修复单元测试增加代码覆盖率
This commit is contained in:
Argo 2022-04-25 11:15:29 +00:00
parent 259bba0d0f
commit cb257b184f
2 changed files with 45 additions and 19 deletions

View File

@ -55,7 +55,7 @@ public partial class SweetAlert : IDisposable
{ {
await base.OnAfterRenderAsync(firstRender); await base.OnAfterRenderAsync(firstRender);
if (ModalContainer != null && IsShowDialog) if (IsShowDialog)
{ {
IsShowDialog = false; IsShowDialog = false;
await ModalContainer.Show(); await ModalContainer.Show();
@ -128,8 +128,12 @@ public partial class SweetAlert : IDisposable
{ {
if (disposing) if (disposing)
{ {
DelayToken?.Dispose(); if (DelayToken != null)
DelayToken = null; {
DelayToken.Cancel();
DelayToken.Dispose();
DelayToken = null;
}
SwalService.UnRegister(this); SwalService.UnRegister(this);
} }
} }

View File

@ -7,7 +7,7 @@ namespace UnitTest.Components;
public class SwalTest : SwalTestBase public class SwalTest : SwalTestBase
{ {
[Fact] [Fact]
public void Show_Ok() public async Task Show_Ok()
{ {
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb => var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
{ {
@ -16,7 +16,7 @@ public class SwalTest : SwalTestBase
var swal = cut.FindComponent<MockSwalTest>().Instance.SwalService; var swal = cut.FindComponent<MockSwalTest>().Instance.SwalService;
cut.InvokeAsync(async () => await swal.Show(new SwalOption() await cut.InvokeAsync(async () => await swal.Show(new SwalOption()
{ {
BodyTemplate = builder => builder.AddContent(0, "Test-BodyTemplate"), BodyTemplate = builder => builder.AddContent(0, "Test-BodyTemplate"),
FooterTemplate = builder => builder.AddContent(0, "Test-FooterTemplate"), FooterTemplate = builder => builder.AddContent(0, "Test-FooterTemplate"),
@ -33,10 +33,10 @@ public class SwalTest : SwalTestBase
// 测试关闭逻辑 // 测试关闭逻辑
var modal = cut.FindComponent<Modal>(); var modal = cut.FindComponent<Modal>();
cut.InvokeAsync(() => modal.Instance.Close()); await cut.InvokeAsync(() => modal.Instance.Close());
//测试Content //测试Content
cut.InvokeAsync(() => swal.Show(new SwalOption() await cut.InvokeAsync(() => swal.Show(new SwalOption()
{ {
Content = "I am Swal", Content = "I am Swal",
})); }));
@ -44,10 +44,10 @@ public class SwalTest : SwalTestBase
Assert.Contains("I am Swal", cut.Markup); Assert.Contains("I am Swal", cut.Markup);
modal = cut.FindComponent<Modal>(); modal = cut.FindComponent<Modal>();
cut.InvokeAsync(() => modal.Instance.Close()); await cut.InvokeAsync(() => modal.Instance.Close());
//测试Title //测试Title
cut.InvokeAsync(() => swal.Show(new SwalOption() await cut.InvokeAsync(() => swal.Show(new SwalOption()
{ {
Content = "I am Title", Content = "I am Title",
})); }));
@ -55,32 +55,34 @@ public class SwalTest : SwalTestBase
Assert.Contains("I am Title", cut.Markup); Assert.Contains("I am Title", cut.Markup);
modal = cut.FindComponent<Modal>(); modal = cut.FindComponent<Modal>();
cut.InvokeAsync(() => modal.Instance.Close()); await cut.InvokeAsync(() => modal.Instance.Close());
//测试Title //测试Title
cut.InvokeAsync(() => swal.Show(new SwalOption() await cut.InvokeAsync(() => swal.Show(new SwalOption()
{ {
ForceDelay = true, ForceDelay = true,
Delay = 1000 Delay = 1000
})); }));
modal = cut.FindComponent<Modal>(); modal = cut.FindComponent<Modal>();
cut.InvokeAsync(() => modal.Instance.Close()); await cut.InvokeAsync(() => modal.Instance.Close());
//测试Title //测试Title
cut.InvokeAsync(() => swal.Show(new SwalOption() await cut.InvokeAsync(() => swal.Show(new SwalOption()
{ {
ForceDelay = true, ForceDelay = true,
Delay = 1000, Delay = 1000,
})); }));
modal = cut.FindComponent<Modal>(); modal = cut.FindComponent<Modal>();
cut.InvokeAsync(() => modal.Instance.Close()); await cut.InvokeAsync(() => modal.Instance.Close());
//测试关闭按钮 //测试关闭按钮
cut.InvokeAsync(() => swal.Show(new SwalOption() await cut.InvokeAsync(() => swal.Show(new SwalOption()
{ {
Content = "I am Swal", Content = "I am Swal",
IsAutoHide = true,
Delay = 1000
})); }));
var button = cut.Find(".btn-secondary"); var button = cut.Find(".btn-secondary");
@ -88,7 +90,7 @@ public class SwalTest : SwalTestBase
//测试Modal取消 //测试Modal取消
var cancel = true; var cancel = true;
cut.InvokeAsync(async () => _ = cut.InvokeAsync(async () =>
{ {
cancel = await swal.ShowModal(new SwalOption() cancel = await swal.ShowModal(new SwalOption()
{ {
@ -102,7 +104,7 @@ public class SwalTest : SwalTestBase
//测试Modal确认 //测试Modal确认
var confirm = false; var confirm = false;
cut.InvokeAsync(async () => _ = cut.InvokeAsync(async () =>
{ {
confirm = await swal.ShowModal(new SwalOption() confirm = await swal.ShowModal(new SwalOption()
{ {
@ -130,13 +132,33 @@ public class SwalTest : SwalTestBase
}); });
}); });
}); });
cut.InvokeAsync(() => cut.Find(".dropdown-item").Click()); await cut.InvokeAsync(() => cut.Find(".dropdown-item").Click());
Assert.Contains("Test-Swal-Title", cut.Markup); Assert.Contains("Test-Swal-Title", cut.Markup);
Assert.Contains("Test-Swal-Content", cut.Markup); Assert.Contains("Test-Swal-Content", cut.Markup);
Assert.Contains("Test-Swal-Footer", cut.Markup); Assert.Contains("Test-Swal-Footer", cut.Markup);
cut.InvokeAsync(() => cut.Find(".swal2-actions button").Click()); await cut.InvokeAsync(() => cut.Find(".swal2-actions button").Click());
Assert.DoesNotContain("Test-Swal-Content", cut.Markup); Assert.DoesNotContain("Test-Swal-Content", cut.Markup);
// 测试自动关闭
await cut.InvokeAsync(() => swal.Show(new SwalOption()
{
Content = "I am Swal",
IsAutoHide = true,
Delay = 100
}));
while (cut.Markup.Contains("I am Swal"))
{
await Task.Delay(100);
}
// 不关闭弹窗测试 Dispose
await cut.InvokeAsync(() => swal.Show(new SwalOption()
{
Content = "I am Swal",
IsAutoHide = true,
Delay = 1000
}));
} }