From 219f3367c94d195f9dec7a581b3857086c5c87df Mon Sep 17 00:00:00 2001 From: zhangpeihang <948869991@qq.com> Date: Thu, 10 Feb 2022 11:40:34 +0800 Subject: [PATCH 1/4] =?UTF-8?q?test:=20=E6=B7=BB=E5=8A=A0=20swal=20?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/SwalTest.cs | 134 +++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 test/UnitTest/Components/SwalTest.cs diff --git a/test/UnitTest/Components/SwalTest.cs b/test/UnitTest/Components/SwalTest.cs new file mode 100644 index 000000000..1f27cb19a --- /dev/null +++ b/test/UnitTest/Components/SwalTest.cs @@ -0,0 +1,134 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace UnitTest.Components; + +public class SwalTest : BootstrapBlazorTestBase +{ + [Fact] + public void Show_Ok() + { + var cut = Context.RenderComponent(pb => + { + pb.AddChildContent(); + }); + + var swal = cut.FindComponent().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, + })); + + // 代码覆盖模板单元测试 + Assert.Contains("Test-BodyTemplate", cut.Markup); + Assert.Contains("Test-FooterTemplate", cut.Markup); + Assert.Contains("Test-ButtonTemplate", cut.Markup); + + var aa = cut.Markup; + + // 测试关闭逻辑 + var modal = cut.FindComponent(); + cut.InvokeAsync(() => modal.Instance.Close()); + + //测试Content + cut.InvokeAsync(() => swal.Show(new SwalOption() + { + Content = "I am Swal", + })); + + Assert.Contains("I am Swal", cut.Markup); + + modal = cut.FindComponent(); + cut.InvokeAsync(() => modal.Instance.Close()); + + //测试Title + cut.InvokeAsync(() => swal.Show(new SwalOption() + { + Content = "I am Title", + })); + + Assert.Contains("I am Title", cut.Markup); + + modal = cut.FindComponent(); + cut.InvokeAsync(() => modal.Instance.Close()); + + //测试Title + cut.InvokeAsync(() => swal.Show(new SwalOption() + { + ForceDelay = true, + Delay = 1000 + })); + + modal = cut.FindComponent(); + cut.InvokeAsync(() => modal.Instance.Close()); + + //测试Title + cut.InvokeAsync(() => swal.Show(new SwalOption() + { + ForceDelay = true, + Delay = 1000, + })); + + modal = cut.FindComponent(); + cut.InvokeAsync(() => modal.Instance.Close()); + + //测试关闭按钮 + cut.InvokeAsync(() => swal.Show(new SwalOption() + { + Content = "I am Swal", + })); + + var button = cut.Find(".btn-secondary"); + button.Click(); + + //测试Modal取消 + var cancel = true; + cut.InvokeAsync(async () => + { + cancel = await swal.ShowModal(new SwalOption() + { + Content = "I am Swal", + IsConfirm = true + }); + }); + + var cancelbutton = cut.Find(".btn-secondary"); + cancelbutton.Click(); + Assert.False(cancel); + + //测试Modal确认 + var confirm = false; + cut.InvokeAsync(async () => + { + confirm = await swal.ShowModal(new SwalOption() + { + Content = "I am Swal", + IsConfirm = true, + }); + }); + + var confirmbutton = cut.Find(".btn-danger"); + confirmbutton.Click(); + Assert.True(confirm); + } + + private class MockSwalTest : ComponentBase + { + [Inject] + [NotNull] + public SwalService? SwalService { get; set; } + } +} + + -- Gitee From 27acbf5c11718772834d3f75ab525d39bb7c2177 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Thu, 10 Feb 2022 16:31:41 +0800 Subject: [PATCH 2/4] =?UTF-8?q?doc:=20=E6=A0=BC=E5=BC=8F=E5=8C=96=E6=96=87?= =?UTF-8?q?=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Dialog/Dialog.razor.cs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/BootstrapBlazor/Components/Dialog/Dialog.razor.cs b/src/BootstrapBlazor/Components/Dialog/Dialog.razor.cs index aafeec6e0..d95fc48a6 100644 --- a/src/BootstrapBlazor/Components/Dialog/Dialog.razor.cs +++ b/src/BootstrapBlazor/Components/Dialog/Dialog.razor.cs @@ -104,16 +104,16 @@ public partial class Dialog : IDisposable parameters.Add(nameof(ModalDialog.OnClose), new Func(async () => { - // 回调 OnClose 方法 - // 移除当前对话框 - if (option.OnCloseAsync != null) + // 回调 OnClose 方法 + // 移除当前对话框 + if (option.OnCloseAsync != null) { await option.OnCloseAsync(); } DialogParameters.Remove(parameters); - // 支持多级弹窗 - await ModalContainer.CloseOrPopDialog(); + // 支持多级弹窗 + await ModalContainer.CloseOrPopDialog(); StateHasChanged(); })); @@ -130,11 +130,6 @@ public partial class Dialog : IDisposable { builder.OpenComponent(0); builder.AddMultipleAttributes(1, parameter); - builder.AddComponentReferenceCapture(2, dialog => - { - var modal = (ModalDialog)dialog; - ModalContainer.ShowDialog(modal); - }); builder.CloseComponent(); }; -- Gitee From 78cb3d9f410cea6656b1e088448ee30d9de40ad7 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Thu, 10 Feb 2022 16:31:52 +0800 Subject: [PATCH 3/4] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/SweetAlert/SweetAlert.razor.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/BootstrapBlazor/Components/SweetAlert/SweetAlert.razor.cs b/src/BootstrapBlazor/Components/SweetAlert/SweetAlert.razor.cs index 4d714c1b2..0bb3e13d0 100644 --- a/src/BootstrapBlazor/Components/SweetAlert/SweetAlert.razor.cs +++ b/src/BootstrapBlazor/Components/SweetAlert/SweetAlert.razor.cs @@ -116,11 +116,6 @@ public partial class SweetAlert : IDisposable { builder.AddAttribute(index++, p.Key, p.Value); } - builder.AddComponentReferenceCapture(index++, dialog => - { - var modal = (ModalDialog)dialog; - ModalContainer.ShowDialog(modal); - }); builder.CloseComponent(); } }; -- Gitee From 06c1f810e6fe1563eea48af522ef456d7da39a28 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Thu, 10 Feb 2022 17:54:16 +0800 Subject: [PATCH 4/4] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95=20Order=20=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/SelectTest.cs | 25 ---------- test/UnitTest/Components/SwalTest.cs | 39 ++++++++++----- test/UnitTest/Core/SwalTestBase.cs | 66 ++++++++++++++++++++++++++ test/UnitTest/Misc/DisplayNameOrder.cs | 16 +++++++ 4 files changed, 109 insertions(+), 37 deletions(-) create mode 100644 test/UnitTest/Core/SwalTestBase.cs create mode 100644 test/UnitTest/Misc/DisplayNameOrder.cs diff --git a/test/UnitTest/Components/SelectTest.cs b/test/UnitTest/Components/SelectTest.cs index 48dff867f..ce279ff1c 100644 --- a/test/UnitTest/Components/SelectTest.cs +++ b/test/UnitTest/Components/SelectTest.cs @@ -191,31 +191,6 @@ public class SelectTest : BootstrapBlazorTestBase cut.Find(".dropdown-item").Click(); } - [Fact] - public void OnBeforeSelectedItemChange_Ok() - { - var cut = Context.RenderComponent(pb => - { - pb.AddChildContent>(pb => - { - pb.Add(a => a.OnBeforeSelectedItemChange, item => Task.FromResult(true)); - pb.Add(a => a.SwalFooter, "Test-Swal-Footer"); - pb.Add(a => a.SwalCategory, SwalCategory.Question); - pb.Add(a => a.SwalTitle, "Test-Swal-Title"); - pb.Add(a => a.SwalContent, "Test-Swal-Content"); - pb.Add(a => a.Items, new SelectedItem[] - { - new SelectedItem("1", "Test1"), - new SelectedItem("2", "Test2") - }); - }); - }); - 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); - } - [Fact] public void NullItems_Ok() { diff --git a/test/UnitTest/Components/SwalTest.cs b/test/UnitTest/Components/SwalTest.cs index 1f27cb19a..e04d0e69f 100644 --- a/test/UnitTest/Components/SwalTest.cs +++ b/test/UnitTest/Components/SwalTest.cs @@ -2,15 +2,9 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Website: https://www.blazor.zone or https://argozhang.github.io/ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - namespace UnitTest.Components; -public class SwalTest : BootstrapBlazorTestBase +public class SwalTest : SwalTestBase { [Fact] public void Show_Ok() @@ -22,7 +16,7 @@ public class SwalTest : BootstrapBlazorTestBase var swal = cut.FindComponent().Instance.SwalService; - cut.InvokeAsync(() => swal.Show(new SwalOption() + cut.InvokeAsync(async () => await swal.Show(new SwalOption() { BodyTemplate = builder => builder.AddContent(0, "Test-BodyTemplate"), FooterTemplate = builder => builder.AddContent(0, "Test-FooterTemplate"), @@ -35,8 +29,6 @@ public class SwalTest : BootstrapBlazorTestBase Assert.Contains("Test-FooterTemplate", cut.Markup); Assert.Contains("Test-ButtonTemplate", cut.Markup); - var aa = cut.Markup; - // 测试关闭逻辑 var modal = cut.FindComponent(); cut.InvokeAsync(() => modal.Instance.Close()); @@ -121,8 +113,33 @@ public class SwalTest : BootstrapBlazorTestBase var confirmbutton = cut.Find(".btn-danger"); confirmbutton.Click(); Assert.True(confirm); + + cut.SetParametersAndRender(pb => + { + pb.AddChildContent>(pb => + { + pb.Add(a => a.OnBeforeSelectedItemChange, item => Task.FromResult(true)); + pb.Add(a => a.SwalFooter, "Test-Swal-Footer"); + pb.Add(a => a.SwalCategory, SwalCategory.Question); + pb.Add(a => a.SwalTitle, "Test-Swal-Title"); + pb.Add(a => a.SwalContent, "Test-Swal-Content"); + pb.Add(a => a.Items, new SelectedItem[] + { + new SelectedItem("1", "Test1"), + new SelectedItem("2", "Test2") + }); + }); + }); + 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()); + Assert.DoesNotContain("Test-Swal-Content", cut.Markup); } + private class MockSwalTest : ComponentBase { [Inject] @@ -130,5 +147,3 @@ public class SwalTest : BootstrapBlazorTestBase public SwalService? SwalService { get; set; } } } - - diff --git a/test/UnitTest/Core/SwalTestBase.cs b/test/UnitTest/Core/SwalTestBase.cs new file mode 100644 index 000000000..77fe8c7ca --- /dev/null +++ b/test/UnitTest/Core/SwalTestBase.cs @@ -0,0 +1,66 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace UnitTest.Core; + +[Collection("SwalTestContext")] +public class SwalTestBase +{ + protected TestContext Context { get; } + + public SwalTestBase() + { + Context = SwalTestHost.Instance; + } +} + +[CollectionDefinition("SwalTestContext")] +public class SwalTestCollection : ICollectionFixture +{ + +} + +public class SwalTestHost : IDisposable +{ + [NotNull] + internal static TestContext? Instance { get; private set; } + + public SwalTestHost() + { + Instance = new TestContext(); + + // Mock 脚本 + Instance.JSInterop.Mode = JSRuntimeMode.Loose; + + ConfigureServices(Instance.Services); + + ConfigureConfigration(Instance.Services); + + // 渲染 SwalRoot 组件 激活 ICacheManager 接口 + Instance.Services.GetRequiredService(); + } + + protected virtual void ConfigureServices(IServiceCollection services) + { + services.AddBootstrapBlazor(localizationAction: options => + { + options.AdditionalJsonAssemblies = new[] { typeof(Alert).Assembly }; + }); + } + + protected virtual void ConfigureConfigration(IServiceCollection services) + { + // 增加单元测试 appsettings.json 配置文件 + services.AddConfiguration(); + } + + public void Dispose() + { + Instance.Dispose(); + GC.SuppressFinalize(this); + } +} diff --git a/test/UnitTest/Misc/DisplayNameOrder.cs b/test/UnitTest/Misc/DisplayNameOrder.cs new file mode 100644 index 000000000..786609fbb --- /dev/null +++ b/test/UnitTest/Misc/DisplayNameOrder.cs @@ -0,0 +1,16 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +[assembly: CollectionBehavior(DisableTestParallelization = true)] +[assembly: TestCollectionOrderer("UnitTest.Misc.DisplayNameOrderer", "UnitTest")] + +namespace UnitTest.Misc; + +public class DisplayNameOrderer : ITestCollectionOrderer +{ + public IEnumerable OrderTestCollections(IEnumerable testCollections) + { + return testCollections.OrderBy(collection => collection.DisplayName); + } +} -- Gitee