Skip to content

Commit

Permalink
test: 更新 Button 单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
ArgoZhang committed Aug 23, 2024
1 parent 5022839 commit b82fe2a
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/ButtonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace UnitTest.Components;

public class ButtonTest : BootstrapBlazorTestBase
public class ButtonTest : BootstrapBlazorButtonTestBase
{
[Fact]
public void ButtonStyle_Ok()
Expand Down
92 changes: 92 additions & 0 deletions test/UnitTest/Core/BootstrapBlazorButtonTestBase.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("BlazorButtonTestContext")]
public class BootstrapBlazorButtonTestBase
{
protected TestContext Context { get; }

protected ICacheManager Cache { get; }

public BootstrapBlazorButtonTestBase()
{
Context = BootstrapBlazorButtonTestHost.Instance;
Cache = BootstrapBlazorButtonTestHost.Cache;
}
}

[CollectionDefinition("BlazorButtonTestContext")]
public class BootstrapBlazorButtonTestCollection : ICollectionFixture<BootstrapBlazorButtonTestHost>
{

}

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

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

public BootstrapBlazorButtonTestHost()
{
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 b82fe2a

Please sign in to comment.