From 28309d3d6bf4b44b8c18039b92e2aa90c3380112 Mon Sep 17 00:00:00 2001 From: zhangpeihang <49146231+Vision-Zhang@users.noreply.github.com> Date: Fri, 26 Jan 2024 14:57:01 +0800 Subject: [PATCH] test(SelectObject): add unit test improve code coverage (#2829) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 首次渲染给 CurrentValue 赋值 * test: 增加 Color 单元测试 * test: 增加 Arrow 单元测试 * test: 增加 Width 单元测试 * test: 增加 Text 单元测试 * test: 增加 Height 单元测试 * test: 增加 Template 单元测试 * test: 增加 Validate 单元测试 * Revert "fix: 首次渲染给 CurrentValue 赋值" This reverts commit 8dc0888692af146d5d4e91b8bbc0671a4f0e0e8f. * chore: 更新 checkout 版本 * chore: 更新版本 * chore: 更新 codecov 版本 --------- Co-authored-by: Argo-AscioTech --- .../workflows/auto-pull-request-checks.yml | 4 +- test/UnitTest/Components/SelectObjectTest.cs | 167 +++++++++++++++++- 2 files changed, 168 insertions(+), 3 deletions(-) diff --git a/.github/workflows/auto-pull-request-checks.yml b/.github/workflows/auto-pull-request-checks.yml index d392d3a01..0ce0912ff 100644 --- a/.github/workflows/auto-pull-request-checks.yml +++ b/.github/workflows/auto-pull-request-checks.yml @@ -12,10 +12,10 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup .NET SDK - uses: actions/setup-dotnet@v3 + uses: actions/setup-dotnet@v4 with: dotnet-version: 8.0.x diff --git a/test/UnitTest/Components/SelectObjectTest.cs b/test/UnitTest/Components/SelectObjectTest.cs index 93b1cf06f..f78196a56 100644 --- a/test/UnitTest/Components/SelectObjectTest.cs +++ b/test/UnitTest/Components/SelectObjectTest.cs @@ -4,7 +4,6 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Localization; -using System.Runtime.CompilerServices; namespace UnitTest.Components; @@ -51,6 +50,172 @@ public class SelectObjectTest : BootstrapBlazorTestBase Assert.Equal(url, v.ImageUrl); } + [Fact] + public void Color_Ok() + { + var cut = Context.RenderComponent>(pb => + { + pb.Add(a => a.Color, Color.Danger); + pb.Add(a => a.GetTextCallback, p => p); + pb.Add(a => a.ChildContent, context => pb => + { + pb.AddContent(0, "test"); + }); + }); + cut.Contains("border-danger"); + } + + [Fact] + public void ShowAppendArrow_Ok() + { + var cut = Context.RenderComponent>(pb => + { + pb.Add(a => a.ShowAppendArrow, false); + pb.Add(a => a.GetTextCallback, p => p); + pb.Add(a => a.ChildContent, context => pb => + { + pb.AddContent(0, "test"); + }); + }); + cut.DoesNotContain("form-select-append"); + } + + [Fact] + public void DropdownMinWidth_Ok() + { + var cut = Context.RenderComponent>(pb => + { + pb.Add(a => a.DropdownMinWidth, 500); + pb.Add(a => a.GetTextCallback, p => p); + pb.Add(a => a.ChildContent, context => pb => + { + pb.AddContent(0, "test"); + }); + }); + Assert.Contains("data-bb-min-width=\"500\"", cut.Markup); + } + + [Fact] + public void GetTextCallback_Ok() + { + Assert.Throws(() => Context.RenderComponent>(pb => + { + pb.Add(a => a.ChildContent, context => pb => + { + pb.AddContent(0, "test"); + }); + })); + } + + [Fact] + public void Height_Ok() + { + var cut = Context.RenderComponent>(pb => + { + pb.Add(a => a.Height, 500); + pb.Add(a => a.GetTextCallback, p => p); + pb.Add(a => a.ChildContent, context => pb => + { + pb.AddContent(0, "test"); + }); + }); + Assert.Contains($"height: 500px;", cut.Markup); + } + + [Fact] + public void Template_Ok() + { + var items = Enumerable.Range(1, 8).Select(i => new Product() + { + ImageUrl = $"./images/Pic{i}.jpg", + Description = $"Pic{i}.jpg" + }).ToList(); + var v = items[0]; + var cut = Context.RenderComponent>(pb => + { + pb.Add(a => a.Value, v); + pb.Add(a => a.GetTextCallback, p => p?.ImageUrl); + pb.Add(a => a.Template, p => builder => + { + builder.AddContent(0, $"Template-{p.ImageUrl}"); + }); + pb.Add(a => a.ChildContent, context => pb => + { + pb.OpenComponent>(0); + pb.AddAttribute(1, "Items", items); + pb.AddAttribute(2, "BodyTemplate", new RenderFragment(p => b => b.AddContent(0, p.ImageUrl))); + pb.CloseComponent(); + }); + }); + + Assert.Contains($"Template-{v.ImageUrl}", cut.Markup); + + cut.SetParametersAndRender(pb => + { + pb.Add(a => a.Value, null); + pb.Add(a => a.PlaceHolder, "Template-PlaceHolder"); + }); + + Assert.DoesNotContain($"Template-{items[0].ImageUrl}", cut.Markup); + cut.Contains("Template-PlaceHolder"); + } + + [Fact] + public async Task Validate_Ok() + { + var localizer = Context.Services.GetRequiredService>(); + var valid = false; + var invalid = false; + var model = Foo.Generate(localizer); + var cut = Context.RenderComponent(builder => + { + builder.Add(a => a.OnValidSubmit, context => + { + valid = true; + return Task.CompletedTask; + }); + builder.Add(a => a.OnInvalidSubmit, context => + { + invalid = true; + return Task.CompletedTask; + }); + builder.Add(a => a.Model, model); + builder.AddChildContent>(pb => + { + pb.Add(a => a.Value, model.Name); + pb.Add(a => a.ValueExpression, Utility.GenerateValueExpression(model, "Name", typeof(string))); + pb.Add(a => a.OnValueChanged, v => + { + model.Name = v; + return Task.CompletedTask; + }); + pb.Add(a => a.GetTextCallback, item => item); + pb.Add(a => a.ChildContent, context => pb => + { + pb.OpenComponent>(0); + pb.CloseComponent(); + }); + }); + }); + + await cut.InvokeAsync(() => + { + var form = cut.Find("form"); + form.Submit(); + }); + Assert.True(valid); + + model.Name = null; + var table = cut.FindComponent>(); + table.SetParametersAndRender(); + await cut.InvokeAsync(() => + { + var form = cut.Find("form"); + form.Submit(); + }); + Assert.True(invalid); + } + class Product { public string ImageUrl { get; set; } = "";