From cb257b184ff9dff990b6a657e8a32ff5198fb816 Mon Sep 17 00:00:00 2001 From: Argo Date: Mon, 25 Apr 2022 11:15:29 +0000 Subject: [PATCH] =?UTF-8?q?!2708=20test(#I54NDP):=20improve=20swal=20unit?= =?UTF-8?q?=20test=20*=20test:=20=E4=BF=AE=E5=A4=8D=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=A2=9E=E5=8A=A0=E4=BB=A3=E7=A0=81=E8=A6=86?= =?UTF-8?q?=E7=9B=96=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/SweetAlert/SweetAlert.razor.cs | 10 ++-- test/UnitTest/Components/SwalTest.cs | 54 +++++++++++++------ 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src/BootstrapBlazor/Components/SweetAlert/SweetAlert.razor.cs b/src/BootstrapBlazor/Components/SweetAlert/SweetAlert.razor.cs index 0bb3e13d0..f9280d84b 100644 --- a/src/BootstrapBlazor/Components/SweetAlert/SweetAlert.razor.cs +++ b/src/BootstrapBlazor/Components/SweetAlert/SweetAlert.razor.cs @@ -55,7 +55,7 @@ public partial class SweetAlert : IDisposable { await base.OnAfterRenderAsync(firstRender); - if (ModalContainer != null && IsShowDialog) + if (IsShowDialog) { IsShowDialog = false; await ModalContainer.Show(); @@ -128,8 +128,12 @@ public partial class SweetAlert : IDisposable { if (disposing) { - DelayToken?.Dispose(); - DelayToken = null; + if (DelayToken != null) + { + DelayToken.Cancel(); + DelayToken.Dispose(); + DelayToken = null; + } SwalService.UnRegister(this); } } diff --git a/test/UnitTest/Components/SwalTest.cs b/test/UnitTest/Components/SwalTest.cs index 22425270c..526b75f4b 100644 --- a/test/UnitTest/Components/SwalTest.cs +++ b/test/UnitTest/Components/SwalTest.cs @@ -7,7 +7,7 @@ namespace UnitTest.Components; public class SwalTest : SwalTestBase { [Fact] - public void Show_Ok() + public async Task Show_Ok() { var cut = Context.RenderComponent(pb => { @@ -16,7 +16,7 @@ public class SwalTest : SwalTestBase var swal = cut.FindComponent().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"), FooterTemplate = builder => builder.AddContent(0, "Test-FooterTemplate"), @@ -33,10 +33,10 @@ public class SwalTest : SwalTestBase // 测试关闭逻辑 var modal = cut.FindComponent(); - cut.InvokeAsync(() => modal.Instance.Close()); + await cut.InvokeAsync(() => modal.Instance.Close()); //测试Content - cut.InvokeAsync(() => swal.Show(new SwalOption() + await cut.InvokeAsync(() => swal.Show(new SwalOption() { Content = "I am Swal", })); @@ -44,10 +44,10 @@ public class SwalTest : SwalTestBase Assert.Contains("I am Swal", cut.Markup); modal = cut.FindComponent(); - cut.InvokeAsync(() => modal.Instance.Close()); + await cut.InvokeAsync(() => modal.Instance.Close()); //测试Title - cut.InvokeAsync(() => swal.Show(new SwalOption() + await cut.InvokeAsync(() => swal.Show(new SwalOption() { Content = "I am Title", })); @@ -55,32 +55,34 @@ public class SwalTest : SwalTestBase Assert.Contains("I am Title", cut.Markup); modal = cut.FindComponent(); - cut.InvokeAsync(() => modal.Instance.Close()); + await cut.InvokeAsync(() => modal.Instance.Close()); //测试Title - cut.InvokeAsync(() => swal.Show(new SwalOption() + await cut.InvokeAsync(() => swal.Show(new SwalOption() { ForceDelay = true, Delay = 1000 })); modal = cut.FindComponent(); - cut.InvokeAsync(() => modal.Instance.Close()); + await cut.InvokeAsync(() => modal.Instance.Close()); //测试Title - cut.InvokeAsync(() => swal.Show(new SwalOption() + await cut.InvokeAsync(() => swal.Show(new SwalOption() { ForceDelay = true, Delay = 1000, })); modal = cut.FindComponent(); - 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", + IsAutoHide = true, + Delay = 1000 })); var button = cut.Find(".btn-secondary"); @@ -88,7 +90,7 @@ public class SwalTest : SwalTestBase //测试Modal取消 var cancel = true; - cut.InvokeAsync(async () => + _ = cut.InvokeAsync(async () => { cancel = await swal.ShowModal(new SwalOption() { @@ -102,7 +104,7 @@ public class SwalTest : SwalTestBase //测试Modal确认 var confirm = false; - cut.InvokeAsync(async () => + _ = cut.InvokeAsync(async () => { 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-Content", 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); + + // 测试自动关闭 + 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 + })); }