Skip to content

Commit

Permalink
test: 更新 DateTimePicker 单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
ArgoZhang committed Aug 23, 2024
1 parent 331aa45 commit b6a9875
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion test/UnitTest/Components/DateTimePickerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace UnitTest.Components;

public class DateTimePickerTest : BootstrapBlazorTestBase
public class DateTimePickerTest : BootstrapBlazorDateTimePickerTestBase
{
#region DateTimePicker
[Fact]
Expand Down
92 changes: 92 additions & 0 deletions test/UnitTest/Core/BootstrapBlazorDateTimePickerTestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (c) Argo Zhang ([email protected]). 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("BlazorDateTimePickerTestContext")]
public class BootstrapBlazorDateTimePickerTestBase
{
protected TestContext Context { get; }

protected ICacheManager Cache { get; }

public BootstrapBlazorDateTimePickerTestBase()
{
Context = BootstrapBlazorDateTimePickerTestHost.Instance;
Cache = BootstrapBlazorDateTimePickerTestHost.Cache;
}
}

[CollectionDefinition("BlazorDateTimePickerTestContext")]
public class BootstrapBlazorDateTimePickerTestCollection : ICollectionFixture<BootstrapBlazorDateTimePickerTestHost>
{

}

public class BootstrapBlazorDateTimePickerTestHost : IDisposable
{
[NotNull]
internal static TestContext? Instance { get; private set; }

[NotNull]
internal static ICacheManager? Cache { get; private set; }

public BootstrapBlazorDateTimePickerTestHost()
{
Instance = new TestContext();

// Mock 脚本
Instance.JSInterop.Mode = JSRuntimeMode.Loose;

ConfigureServices(Instance.Services);

ConfigureConfiguration(Instance.Services);

// 渲染 BootstrapBlazorRoot 组件 激活 ICacheManager 接口
Cache = Instance.Services.GetRequiredService<ICacheManager>();
}

protected virtual void ConfigureServices(IServiceCollection services)
{
services.AddBootstrapBlazor();
services.ConfigureJsonLocalizationOptions(op =>
{
op.IgnoreLocalizerMissing = false;
});
services.AddSingleton<ILookupService, FooLookupService>();
}

protected virtual void ConfigureConfiguration(IServiceCollection services)
{
// 增加单元测试 appsettings.json 配置文件
services.AddConfiguration();
}

public void Dispose()
{
Instance.Dispose();
GC.SuppressFinalize(this);
}

class FooLookupService : LookupServiceBase
{
public override IEnumerable<SelectedItem>? GetItemsByKey(string? key, object? data)
{
IEnumerable<SelectedItem>? ret = null;

if (key == "FooLookup")
{
ret = new SelectedItem[]
{
new("v1", "LookupService-Test-1"),
new("v2", "LookupService-Test-2")
};
}
return ret;
}
}
}

0 comments on commit b6a9875

Please sign in to comment.