test(SelectObject): add unit test improve code coverage (#2829)

* 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 <argo@live.ca>
This commit is contained in:
zhangpeihang 2024-01-26 14:57:01 +08:00 committed by GitHub
parent 828cfcb48f
commit 28309d3d6b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 168 additions and 3 deletions

View File

@ -12,10 +12,10 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v4
- name: Setup .NET SDK - name: Setup .NET SDK
uses: actions/setup-dotnet@v3 uses: actions/setup-dotnet@v4
with: with:
dotnet-version: 8.0.x dotnet-version: 8.0.x

View File

@ -4,7 +4,6 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization; using Microsoft.Extensions.Localization;
using System.Runtime.CompilerServices;
namespace UnitTest.Components; namespace UnitTest.Components;
@ -51,6 +50,172 @@ public class SelectObjectTest : BootstrapBlazorTestBase
Assert.Equal(url, v.ImageUrl); Assert.Equal(url, v.ImageUrl);
} }
[Fact]
public void Color_Ok()
{
var cut = Context.RenderComponent<SelectObject<string>>(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<SelectObject<string>>(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<SelectObject<string>>(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<InvalidOperationException>(() => Context.RenderComponent<SelectObject<string>>(pb =>
{
pb.Add(a => a.ChildContent, context => pb =>
{
pb.AddContent(0, "test");
});
}));
}
[Fact]
public void Height_Ok()
{
var cut = Context.RenderComponent<SelectObject<string>>(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<SelectObject<Product>>(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<ListView<Product>>(0);
pb.AddAttribute(1, "Items", items);
pb.AddAttribute(2, "BodyTemplate", new RenderFragment<Product>(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<IStringLocalizer<Foo>>();
var valid = false;
var invalid = false;
var model = Foo.Generate(localizer);
var cut = Context.RenderComponent<ValidateForm>(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<SelectObject<string>>(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<BootstrapInput<string>>(0);
pb.CloseComponent();
});
});
});
await cut.InvokeAsync(() =>
{
var form = cut.Find("form");
form.Submit();
});
Assert.True(valid);
model.Name = null;
var table = cut.FindComponent<SelectObject<string>>();
table.SetParametersAndRender();
await cut.InvokeAsync(() =>
{
var form = cut.Find("form");
form.Submit();
});
Assert.True(invalid);
}
class Product class Product
{ {
public string ImageUrl { get; set; } = ""; public string ImageUrl { get; set; } = "";