From d46bf675223c9e8439bbc6e8e656117baeac62c6 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 28 Dec 2021 12:33:50 +0800 Subject: [PATCH 1/4] doc: format document --- .../Components/Toast/Toast.razor.cs | 185 +++++++++--------- 1 file changed, 92 insertions(+), 93 deletions(-) diff --git a/src/BootstrapBlazor/Components/Toast/Toast.razor.cs b/src/BootstrapBlazor/Components/Toast/Toast.razor.cs index 7c56ae6ca..b00205add 100644 --- a/src/BootstrapBlazor/Components/Toast/Toast.razor.cs +++ b/src/BootstrapBlazor/Components/Toast/Toast.razor.cs @@ -10,112 +10,111 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Threading.Tasks; -namespace BootstrapBlazor.Components +namespace BootstrapBlazor.Components; + +/// +/// Toast 弹出窗组件 +/// +public partial class Toast : IDisposable { + private string? ClassString => CssBuilder.Default("toast-container") + .AddClassFromAttributes(AdditionalAttributes) + .Build(); + /// - /// Toast 弹出窗组件 + /// 获得 Toast 组件样式设置 /// - public partial class Toast : IDisposable - { - private string? ClassString => CssBuilder.Default("toast-container") - .AddClassFromAttributes(AdditionalAttributes) - .Build(); - - /// - /// 获得 Toast 组件样式设置 - /// - private string? StyleString => CssBuilder.Default() - .AddClass("top: 1rem; left: 1rem;", Placement == Placement.TopStart) - .AddClass("top: 1rem; right: 1rem;", Placement == Placement.TopEnd) - .AddClass("bottom: 1rem; left: 1rem;", Placement == Placement.BottomStart) - .AddClass("bottom: 1rem; right: 1rem;", Placement == Placement.BottomEnd) - .Build(); - - private string? ToastBoxClassString => CssBuilder.Default() - .AddClass("left", Placement == Placement.TopStart || Placement == Placement.BottomStart) - .Build(); - - /// - /// 获得 弹出窗集合 - /// - private List Toasts { get; } = new List(); - - /// - /// 获得/设置 显示文字 - /// - [Parameter] - [NotNull] - public Placement Placement { get; set; } - - [Inject] - [NotNull] - private ToastService? ToastService { get; set; } - - [Inject] - [NotNull] - private IOptions? Options { get; set; } - - /// - /// OnInitialized 方法 - /// - protected override void OnInitialized() - { - base.OnInitialized(); + private string? StyleString => CssBuilder.Default() + .AddClass("top: 1rem; left: 1rem;", Placement == Placement.TopStart) + .AddClass("top: 1rem; right: 1rem;", Placement == Placement.TopEnd) + .AddClass("bottom: 1rem; left: 1rem;", Placement == Placement.BottomStart) + .AddClass("bottom: 1rem; right: 1rem;", Placement == Placement.BottomEnd) + .Build(); - Placement = Options.Value.ToastPlacement ?? Placement.BottomEnd; + private string? ToastBoxClassString => CssBuilder.Default() + .AddClass("left", Placement == Placement.TopStart || Placement == Placement.BottomStart) + .Build(); - // 注册 Toast 弹窗事件 - if (ToastService != null) - { - ToastService.Register(this, Show); - } - } + /// + /// 获得 弹出窗集合 + /// + private List Toasts { get; } = new List(); - private async Task Show(ToastOption option) - { - Toasts.Add(option); - await InvokeAsync(StateHasChanged); - } + /// + /// 获得/设置 显示文字 + /// + [Parameter] + [NotNull] + public Placement Placement { get; set; } - /// - /// 清除 ToastBox 方法 - /// - [JSInvokable] - public async Task Clear() - { - Toasts.Clear(); - await InvokeAsync(StateHasChanged); - } + [Inject] + [NotNull] + private ToastService? ToastService { get; set; } - /// - /// 设置 Toast 容器位置方法 - /// - /// - public void SetPlacement(Placement placement) - { - Placement = placement; - StateHasChanged(); - } + [Inject] + [NotNull] + private IOptions? Options { get; set; } + + /// + /// OnInitialized 方法 + /// + protected override void OnInitialized() + { + base.OnInitialized(); - /// - /// Dispose 方法 - /// - /// - protected virtual void Dispose(bool disposing) + Placement = Options.Value.ToastPlacement ?? Placement.BottomEnd; + + // 注册 Toast 弹窗事件 + if (ToastService != null) { - if (disposing) - { - ToastService.UnRegister(this); - } + ToastService.Register(this, Show); } + } - /// - /// Dispose 方法 - /// - public void Dispose() + private async Task Show(ToastOption option) + { + Toasts.Add(option); + await InvokeAsync(StateHasChanged); + } + + /// + /// 清除 ToastBox 方法 + /// + [JSInvokable] + public async Task Clear() + { + Toasts.Clear(); + await InvokeAsync(StateHasChanged); + } + + /// + /// 设置 Toast 容器位置方法 + /// + /// + public void SetPlacement(Placement placement) + { + Placement = placement; + StateHasChanged(); + } + + /// + /// Dispose 方法 + /// + /// + protected virtual void Dispose(bool disposing) + { + if (disposing) { - Dispose(disposing: true); - GC.SuppressFinalize(this); + ToastService.UnRegister(this); } } + + /// + /// Dispose 方法 + /// + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } } -- Gitee From ecca282f9b9019b48fd75605e403236a9339fe25 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 28 Dec 2021 12:34:07 +0800 Subject: [PATCH 2/4] test: refactor GetSortFunc test --- test/UnitTest/Utils/UtilityTest.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/test/UnitTest/Utils/UtilityTest.cs b/test/UnitTest/Utils/UtilityTest.cs index 00c8cc46a..4007b64e3 100644 --- a/test/UnitTest/Utils/UtilityTest.cs +++ b/test/UnitTest/Utils/UtilityTest.cs @@ -7,6 +7,7 @@ using BootstrapBlazor.Shared; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Localization; using System; +using System.Collections.Generic; using System.Linq; using UnitTest.Core; using Xunit; @@ -35,14 +36,16 @@ public class UtilityTest : BootstrapBlazorTestBase [Fact] public void GetSortFunc_Ok() { - var localizer = Context.Services.GetRequiredService>(); - var foos = Foo.GenerateFoo(localizer, 2); + var foos = new List + { + new Foo { Count = 10}, + new Foo { Count = 20 } + }; var invoker = Utility.GetSortFunc(); - var data = invoker.Invoke(foos, nameof(Foo.Count), SortOrder.Asc).ToList(); - Assert.True(data[0].Count < data[1].Count); - - data = invoker.Invoke(foos, nameof(Foo.Count), SortOrder.Desc).ToList(); - Assert.True(data[0].Count > data[1].Count); + var orderFoos = invoker.Invoke(foos, nameof(Foo.Count), SortOrder.Asc).ToList(); + Assert.True(orderFoos[0].Count < orderFoos[1].Count); + orderFoos = invoker.Invoke(foos, nameof(Foo.Count), SortOrder.Desc).ToList(); + Assert.True(orderFoos[0].Count > orderFoos[1].Count); } [Fact] -- Gitee From f4d13aeaab068cda894d5454e0d6907153620a9e Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 28 Dec 2021 12:34:16 +0800 Subject: [PATCH 3/4] test: add Toast unit test --- test/UnitTest/Components/ToastTest.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 test/UnitTest/Components/ToastTest.cs diff --git a/test/UnitTest/Components/ToastTest.cs b/test/UnitTest/Components/ToastTest.cs new file mode 100644 index 000000000..8f369cefe --- /dev/null +++ b/test/UnitTest/Components/ToastTest.cs @@ -0,0 +1,22 @@ +// 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; +using UnitTest.Core; +using Xunit; + +namespace UnitTest.Components; + +public class ToastTest : BootstrapBlazorTestHost +{ + [Fact] + public void Placement_Ok() + { + + } +} -- Gitee From 8a649ecb6a41194947893aed167175647baa85b3 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 28 Dec 2021 12:54:32 +0800 Subject: [PATCH 4/4] test: add unit test for Toast --- .../Components/Toast/Toast.razor.cs | 3 +- test/UnitTest/Components/ToastTest.cs | 43 ++++++++++++++++--- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/BootstrapBlazor/Components/Toast/Toast.razor.cs b/src/BootstrapBlazor/Components/Toast/Toast.razor.cs index b00205add..f4d7ffbf1 100644 --- a/src/BootstrapBlazor/Components/Toast/Toast.razor.cs +++ b/src/BootstrapBlazor/Components/Toast/Toast.razor.cs @@ -32,7 +32,8 @@ public partial class Toast : IDisposable .Build(); private string? ToastBoxClassString => CssBuilder.Default() - .AddClass("left", Placement == Placement.TopStart || Placement == Placement.BottomStart) + .AddClass("left", Placement == Placement.TopStart) + .AddClass("left", Placement == Placement.BottomStart) .Build(); /// diff --git a/test/UnitTest/Components/ToastTest.cs b/test/UnitTest/Components/ToastTest.cs index 8f369cefe..9a98d11fb 100644 --- a/test/UnitTest/Components/ToastTest.cs +++ b/test/UnitTest/Components/ToastTest.cs @@ -2,21 +2,50 @@ // 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; +using BootstrapBlazor.Components; +using Bunit; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using UnitTest.Core; using Xunit; namespace UnitTest.Components; -public class ToastTest : BootstrapBlazorTestHost +public class ToastTest : BootstrapBlazorTestBase { [Fact] public void Placement_Ok() { - + var options = Context.Services.GetRequiredService>(); + options.Value.ToastPlacement = Placement.TopStart; + + Context.RenderComponent(); + + var service = Context.Services.GetRequiredService(); + service.Success("Test", "test content"); + + // 恢复设置 + options.Value.ToastPlacement = Placement.Auto; + } + + [Fact] + public void Clear_Ok() + { + var cut = Context.RenderComponent(); + cut.InvokeAsync(async () => await cut.Instance.Clear()); + + var service = Context.Services.GetRequiredService(); + service.Success("Test", "test content"); + } + + [Fact] + public void SetPlacement_Ok() + { + var cut = Context.RenderComponent(); + cut.InvokeAsync(() => cut.Instance.SetPlacement(Placement.BottomStart)); + Assert.Contains("left", cut.Markup); + + var service = Context.Services.GetRequiredService(); + service.Success("Test", "test content"); } } -- Gitee