!2590 test(#I501DL): add unit test of TimePicker

* test: 增加代码覆盖率
* test: 增加 TimeCell_Up 单元测试
* chore: 增加 Dialog 单元测试隔离
This commit is contained in:
Argo 2022-03-29 02:01:30 +00:00
parent 150c51bb40
commit d8cc31e71a
4 changed files with 119 additions and 4 deletions

View File

@ -109,15 +109,13 @@ public partial class TimePickerCell : IDisposable
{
TimePickerCellViewModel.Hour => TimeSpan.FromHours(1),
TimePickerCellViewModel.Minute => TimeSpan.FromMinutes(1),
TimePickerCellViewModel.Second => TimeSpan.FromSeconds(1),
_ => TimeSpan.Zero
_ => TimeSpan.FromSeconds(1),
};
Value = Value.Subtract(ts);
if (Value < TimeSpan.Zero)
{
Value = Value.Add(TimeSpan.FromHours(24));
}
if (ValueChanged.HasDelegate)
{
await ValueChanged.InvokeAsync(Value);

View File

@ -492,4 +492,55 @@ public class DateTimePickerTest : BootstrapBlazorTestBase
Assert.True(value);
}
#endregion
#region TimeCell
[Theory]
[InlineData(TimePickerCellViewModel.Hour)]
[InlineData(TimePickerCellViewModel.Minute)]
[InlineData(TimePickerCellViewModel.Second)]
public async Task TimeCell_Up(TimePickerCellViewModel viewMode)
{
var valueChanged = false;
var cut = Context.RenderComponent<TimePickerCell>(pb =>
{
pb.Add(a => a.ViewModel, viewMode);
pb.Add(a => a.Value, new TimeSpan(6, 6, 6));
pb.Add(a => a.ValueChanged, v =>
{
valueChanged = true;
});
});
var i = cut.Find(".time-spinner-arrow.fa-angle-up");
await cut.InvokeAsync(() => i.Click());
Assert.True(valueChanged);
i = cut.Find(".time-spinner-arrow.fa-angle-down");
await cut.InvokeAsync(() => i.Click());
Assert.True(valueChanged);
}
[Fact]
public async Task TimeCell_OverDay()
{
var ts = new TimeSpan(23, 59, 59);
var cut = Context.RenderComponent<TimePickerCell>(pb =>
{
pb.Add(a => a.ViewModel, TimePickerCellViewModel.Second);
pb.Add(a => a.Value, ts);
pb.Add(a => a.ValueChanged, v =>
{
ts = v;
});
});
var i = cut.Find(".time-spinner-arrow.fa-angle-down");
await cut.InvokeAsync(() => i.Click());
Assert.Equal(0, ts.Days);
Assert.Equal(TimeSpan.Zero, ts);
i = cut.Find(".time-spinner-arrow.fa-angle-up");
await cut.InvokeAsync(() => i.Click());
Assert.Equal(new TimeSpan(23, 59, 59), ts);
}
#endregion
}

View File

@ -6,7 +6,7 @@ using BootstrapBlazor.Shared;
namespace UnitTest.Components;
public class DialogTest : BootstrapBlazorTestBase
public class DialogTest : DialogTestBase
{
[Fact]
public void Show_Ok()

View File

@ -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("DialogTestContext")]
public class DialogTestBase
{
protected TestContext Context { get; }
public DialogTestBase()
{
Context = DialogTestHost.Instance;
}
}
[CollectionDefinition("DialogTestContext")]
public class DialogTestCollection : ICollectionFixture<DialogTestHost>
{
}
public class DialogTestHost : IDisposable
{
[NotNull]
internal static TestContext? Instance { get; private set; }
public DialogTestHost()
{
Instance = new TestContext();
// Mock 脚本
Instance.JSInterop.Mode = JSRuntimeMode.Loose;
ConfigureServices(Instance.Services);
ConfigureConfigration(Instance.Services);
// 渲染 SwalRoot 组件 激活 ICacheManager 接口
Instance.Services.GetRequiredService<ICacheManager>();
}
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);
}
}