!2173 test(#I4L4IK): add unit test for Print

* doc: update doc
* test: 增加打印组件单元测试
* refactor: 重构打印组件代码
pull/2174/MERGE
Argo 3 years ago
parent bc374c34d7
commit 089356fd68

@ -3,13 +3,12 @@
<div align="center">
<h2>A set of enterprise-class UI components based on Bootstrap and Blazor.</h2>
[![Github build](https://img.shields.io/github/workflow/status/ArgoZhang/BootstrapBlazor/Build%20Project/main?label=main&logo=github&logoColor=green)](https://github.com/ArgoZhang/BootstrapBlazor/actions?query=workflow%3A%22Build+Project%22+branch%3Amain)
[![License](https://img.shields.io/github/license/argozhang/bootstrapblazor.svg?logo=git&logoColor=red)](https://github.com/ArgoZhang/BootstrapBlazor/blob/main/LICENSE)
[![Nuget](https://img.shields.io/nuget/v/BootstrapBlazor.svg?color=red&logo=nuget&logoColor=green)](https://www.nuget.org/packages/BootstrapBlazor/)
[![Nuget](https://img.shields.io/nuget/dt/BootstrapBlazor.svg?logo=nuget&logoColor=green)](https://www.nuget.org/packages/BootstrapBlazor/)
[![Github](https://img.shields.io/github/license/argozhang/bootstrapblazor.svg?logo=git&logoColor=red)](https://github.com/ArgoZhang/BootstrapBlazor/blob/main/LICENSE)
[![Repo Size](https://img.shields.io/github/repo-size/ArgoZhang/BootstrapBlazor.svg?logo=github&logoColor=green&label=repo)](https://github.com/ArgoZhang/BootstrapBlazor)
[![Commit Date](https://img.shields.io/github/last-commit/ArgoZhang/BootstrapBlazor/main.svg?logo=github&logoColor=green&label=commit)](https://github.com/ArgoZhang/BootstrapBlazor)
[![Github build](https://img.shields.io/github/workflow/status/ArgoZhang/BootstrapBlazor/Build%20Project/main?label=main&logo=github&logoColor=green)](https://github.com/ArgoZhang/BootstrapBlazor/actions?query=workflow%3A%22Build+Project%22+branch%3Amain)
[![codecov](https://codecov.io/gh/dotnetcore/BootstrapBlazor/branch/main/graph/badge.svg?token=5SXIWHXZC3)](https://codecov.io/gh/dotnetcore/BootstrapBlazor)
</div>

@ -3,12 +3,12 @@
<div align="center">
<h2>一套基于 Bootstrap 和 Blazor 的企业级组件库</h2>
[![Github build](https://img.shields.io/github/workflow/status/ArgoZhang/BootstrapBlazor/Build%20Project/main?label=main&logo=github&logoColor=green)](https://github.com/ArgoZhang/BootstrapBlazor/actions?query=workflow%3A%22Build+Project%22+branch%3Amain)
[![License](https://img.shields.io/github/license/argozhang/bootstrapblazor.svg?logo=git&logoColor=red)](https://github.com/ArgoZhang/BootstrapBlazor/blob/main/LICENSE)
[![Nuget](https://img.shields.io/nuget/v/BootstrapBlazor.svg?color=red&logo=nuget&logoColor=green)](https://www.nuget.org/packages/BootstrapBlazor/)
[![Nuget](https://img.shields.io/nuget/dt/BootstrapBlazor.svg?logo=nuget&logoColor=green)](https://www.nuget.org/packages/BootstrapBlazor/)
[![Github](https://img.shields.io/github/license/argozhang/bootstrapblazor.svg?logo=git&logoColor=red)](https://github.com/ArgoZhang/BootstrapBlazor/blob/main/LICENSE)
[![Repo Size](https://img.shields.io/github/repo-size/ArgoZhang/BootstrapBlazor.svg?logo=github&logoColor=green&label=repo)](https://github.com/ArgoZhang/BootstrapBlazor)
[![Commit Date](https://img.shields.io/github/last-commit/ArgoZhang/BootstrapBlazor/main.svg?logo=github&logoColor=green&label=commit)](https://github.com/ArgoZhang/BootstrapBlazor)
[![Github build](https://img.shields.io/github/workflow/status/ArgoZhang/BootstrapBlazor/Build%20Project/main?label=main&logo=github&logoColor=green)](https://github.com/ArgoZhang/BootstrapBlazor/actions?query=workflow%3A%22Build+Project%22+branch%3Amain)
[![codecov](https://codecov.io/gh/dotnetcore/BootstrapBlazor/branch/main/graph/badge.svg?token=5SXIWHXZC3)](https://codecov.io/gh/dotnetcore/BootstrapBlazor)
</div>

@ -42,11 +42,7 @@ namespace BootstrapBlazor.Components
PrintService.Register(this, PrintDialogAsync);
}
private Task PrintDialogAsync(DialogOption option)
{
DialogService.Show(option);
return Task.CompletedTask;
}
private Task PrintDialogAsync(DialogOption option) => DialogService.Show(option);
/// <summary>
/// Dispose 方法

@ -42,15 +42,14 @@ namespace BootstrapBlazor.Components
protected override void OnParametersSet()
{
// 不需要走 base.OnParametersSet 方法
AdditionalAttributes ??= new Dictionary<string, object>();
if (string.IsNullOrEmpty(PreviewUrl))
{
AdditionalAttributes ??= new Dictionary<string, object>();
AdditionalAttributes.Add("onclick", "$.bb_printview(this)");
Target = null;
}
else
{
AdditionalAttributes ??= new Dictionary<string, object>();
AdditionalAttributes.Remove("onclick", out _);
Target = "_blank";
}

@ -0,0 +1,77 @@
// 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 BootstrapBlazor.Components;
using Bunit;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using System.Web;
using UnitTest.Core;
using Xunit;
namespace UnitTest.Components
{
public class PrintTest : BootstrapBlazorTestBase
{
[Fact]
public void PrintButton_Ok()
{
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
{
pb.AddChildContent<PrintButton>();
});
Assert.Contains("<a onclick=\"$.bb_printview(this)\" class=\"btn btn-primary\" role=\"button\"><i class=\"fa fa-print\"></i><span>打印</span></a>", HttpUtility.HtmlDecode(cut.Markup));
var button = cut.FindComponent<PrintButton>();
button.SetParametersAndRender(pb =>
{
pb.Add(b => b.PreviewUrl, "/PrintTest");
});
Assert.Contains("href=\"/PrintTest\"", button.Markup);
}
[Fact]
public void PrintService_Error()
{
var cut = Context.RenderComponent<BootstrapBlazorRoot>();
var printService = cut.Services.CreateScope().ServiceProvider.GetRequiredService<PrintService>();
Assert.ThrowsAsync<InvalidOperationException>(() => printService.PrintAsync<Button>(op =>
{
// 弹窗配置
op.Title = "数据查询窗口";
// 弹窗组件所需参数
return new Dictionary<string, object>();
}));
}
[Fact]
public void PrintService_Ok()
{
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
{
pb.AddChildContent<MockPrintButton>();
});
var button = cut.FindComponent<MockPrintButton>();
cut.InvokeAsync(() => button.Instance.PrintAsync());
Assert.Contains("打印按钮测试", HttpUtility.HtmlDecode(cut.Markup));
}
private class MockPrintButton : ComponentBase
{
[Inject]
[NotNull]
private PrintService? PrintService { get; set; }
public Task PrintAsync() => PrintService.PrintAsync<Button>(op =>
{
op.Title = "打印按钮测试";
return new Dictionary<string, object>();
});
}
}
}
Loading…
Cancel
Save