Skip to content

Commit 276ca59

Browse files
committed
test: 更新单元测试
1 parent 22c413e commit 276ca59

File tree

7 files changed

+265
-1130
lines changed

7 files changed

+265
-1130
lines changed

src/BootstrapBlazor.Server/Components/Samples/UploadCards.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
</section>
6969
<CardUpload TValue="string" IsMultiple="@_isMultiple" IsDirectory="@_isDirectory"
7070
IsDisabled="@_isDisabled" IsUploadButtonAtFirst="@_isUploadButtonAtFirst"
71-
ShowProgress="@_showProgress" ShowDeletedButton="@_showDeleteButton"
71+
ShowProgress="@_showProgress" ShowDeleteButton="@_showDeleteButton"
7272
ShowDownloadButton="@_showDownloadButton" ShowZoomButton="@_showZoomButton" OnChange="@OnCardUpload"></CardUpload>
7373
</DemoBlock>
7474

@@ -97,5 +97,5 @@
9797
<DemoBlock Title="@Localizer["UploadBase64Title"]"
9898
Introduction="@Localizer["UploadBase64Intro"]"
9999
Name="Base64">
100-
<CardUpload TValue="string" DefaultFileList="@Base64FormatFileList" ShowDownloadButton="false" ShowDeletedButton="false"></CardUpload>
100+
<CardUpload TValue="string" DefaultFileList="@Base64FormatFileList" ShowDownloadButton="false" ShowDeleteButton="false"></CardUpload>
101101
</DemoBlock>

src/BootstrapBlazor/Components/Upload/CardUpload.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
</button>
5252
}
5353
</div>
54-
@if (ShowDeletedButton)
54+
@if (ShowDeleteButton)
5555
{
5656
<DynamicElement TagName="button" type="button" class="btn btn-sm btn-outline-danger"
5757
disabled="@GetDeleteButtonDisabledString(item)" aria-label="delete"

src/BootstrapBlazor/Extensions/UploadFileExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ public static class UploadFileExtensions
2121
/// <param name="maxHeight"></param>
2222
/// <param name="maxAllowedSize"></param>
2323
/// <param name="token"></param>
24+
[ExcludeFromCodeCoverage]
2425
public static async Task RequestBase64ImageFileAsync(this UploadFile upload, string? format = null, int maxWidth = 320, int maxHeight = 240, long? maxAllowedSize = null, CancellationToken token = default)
2526
{
2627
if (upload.File != null)
2728
{
2829
try
2930
{
30-
format ??= upload.File.ContentType;
31+
format ??= upload.File.ContentType;
3132
var imageFile = await upload.File.RequestImageFileAsync(format, maxWidth, maxHeight);
3233

3334
maxAllowedSize ??= upload.File.Size;
@@ -54,6 +55,7 @@ public static async Task RequestBase64ImageFileAsync(this UploadFile upload, str
5455
/// <param name="maxAllowedSize"></param>
5556
/// <param name="token"></param>
5657
/// <returns></returns>
58+
[ExcludeFromCodeCoverage]
5759
public static async Task<bool> SaveToFileAsync(this UploadFile upload, string fileName, long maxAllowedSize = 512000, CancellationToken token = default)
5860
{
5961
var ret = false;
@@ -129,6 +131,7 @@ public static async Task<bool> SaveToFileAsync(this UploadFile upload, string fi
129131
/// <param name="maxAllowedSize"></param>
130132
/// <param name="token"></param>
131133
/// <returns></returns>
134+
[ExcludeFromCodeCoverage]
132135
public static async Task<byte[]?> GetBytesAsync(this UploadFile upload, string format, int maxWidth, int maxHeight, long maxAllowedSize = 512000, CancellationToken token = default)
133136
{
134137
byte[]? ret = null;

test/UnitTest/Components/UploadAvatarTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
55

66
using Microsoft.AspNetCore.Components.Forms;
7+
using System.Runtime.CompilerServices;
78

89
namespace UnitTest.Components;
910

@@ -161,6 +162,22 @@ await input.Instance.OnChange.InvokeAsync(new InputFileChangeEventArgs(new List<
161162
Assert.DoesNotContain("is-invalid", upload.Markup);
162163
}
163164

165+
[Fact]
166+
public void AvatarUpload_ShowProgress_Ok()
167+
{
168+
var cut = Context.RenderComponent<AvatarUpload<string>>(pb =>
169+
{
170+
pb.Add(a => a.ShowProgress, true);
171+
pb.Add(a => a.OnChange, async file =>
172+
{
173+
await Task.Delay(100);
174+
await file.SaveToFileAsync("1.txt");
175+
SetUploaded(file, false);
176+
});
177+
});
178+
var input = cut.FindComponent<InputFile>();
179+
}
180+
164181
private class MockBrowserFile(string name = "UploadTestFile", string contentType = "text") : IBrowserFile
165182
{
166183
public string Name { get; } = name;
@@ -176,4 +193,7 @@ public Stream OpenReadStream(long maxAllowedSize = 512000, CancellationToken can
176193
return new MemoryStream([0x01, 0x02]);
177194
}
178195
}
196+
197+
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "set_Uploaded")]
198+
static extern void SetUploaded(UploadFile @this, bool v);
179199
}
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
using Microsoft.AspNetCore.Components.Forms;
7+
using System.ComponentModel.DataAnnotations;
8+
using System.Threading.Tasks;
9+
10+
namespace UnitTest.Components;
11+
12+
public class UploadDropTest : BootstrapBlazorTestBase
13+
{
14+
[Fact]
15+
public void DropUpload_BodyTemplate_Ok()
16+
{
17+
var cut = Context.RenderComponent<DropUpload>(pb =>
18+
{
19+
pb.Add(a => a.BodyTemplate, b => b.AddContent(0, "drop-upload-body-template"));
20+
});
21+
cut.MarkupMatches("<div class=\"upload is-drop\" diff:ignore><div class=\"upload-drop-body\">drop-upload-body-template</div><ul class=\"upload-drop-list\"></ul><input hidden=\"hidden\" type=\"file\" diff:ignore></input></div>");
22+
}
23+
24+
[Fact]
25+
public void DropUpload_IconTemplate_Ok()
26+
{
27+
var cut = Context.RenderComponent<DropUpload>(pb =>
28+
{
29+
pb.Add(a => a.IconTemplate, b => b.AddContent(0, "drop-upload-icon-template"));
30+
});
31+
cut.Contains("<div class=\"upload-drop-icon\">drop-upload-icon-template</div>");
32+
}
33+
34+
[Fact]
35+
public void DropUpload_TextTemplate_Ok()
36+
{
37+
var cut = Context.RenderComponent<DropUpload>(pb =>
38+
{
39+
pb.Add(a => a.TextTemplate, b => b.AddContent(0, "drop-upload-text-template"));
40+
});
41+
cut.Contains("<div class=\"upload-drop-text\">drop-upload-text-template</div>");
42+
}
43+
44+
[Fact]
45+
public void DropUpload_Footer_Ok()
46+
{
47+
var cut = Context.RenderComponent<DropUpload>(pb =>
48+
{
49+
pb.Add(a => a.ShowFooter, true);
50+
});
51+
cut.Contains("<div class=\"upload-drop-footer\"></div>");
52+
53+
cut.SetParametersAndRender(pb =>
54+
{
55+
pb.Add(a => a.FooterTemplate, b => b.AddContent(0, "drop-upload-footer-text"));
56+
});
57+
cut.Contains("<div class=\"upload-drop-footer\">drop-upload-footer-text</div>");
58+
}
59+
60+
[Fact]
61+
public async Task DropUpload_OnChanged_Ok()
62+
{
63+
var cut = Context.RenderComponent<DropUpload>(pb =>
64+
{
65+
pb.Add(a => a.ShowLabel, true);
66+
pb.Add(a => a.ShowProgress, true);
67+
pb.Add(a => a.OnChange, async files =>
68+
{
69+
await files.SaveToFileAsync("1.text");
70+
});
71+
});
72+
73+
var input = cut.FindComponent<InputFile>();
74+
await cut.InvokeAsync(() => input.Instance.OnChange.InvokeAsync(new InputFileChangeEventArgs(new List<MockBrowserFile>()
75+
{
76+
new()
77+
})));
78+
}
79+
80+
[Fact]
81+
public async Task ShowUploadList_Ok()
82+
{
83+
UploadFile? file = null;
84+
var cut = Context.RenderComponent<DropUpload>(pb =>
85+
{
86+
pb.Add(a => a.ShowUploadFileList, true);
87+
pb.Add(a => a.ShowDownloadButton, true);
88+
pb.Add(a => a.OnDownload, f =>
89+
{
90+
file = f;
91+
return Task.CompletedTask;
92+
});
93+
pb.Add(a => a.DefaultFileList,
94+
[
95+
new() { FileName = "Test-File1.text" },
96+
new() { FileName = "Test-File2.jpg" },
97+
]);
98+
});
99+
cut.Contains("upload-body is-list");
100+
101+
var button = cut.Find(".download-icon");
102+
await cut.InvokeAsync(() => button.Click());
103+
Assert.NotNull(file);
104+
}
105+
106+
[Fact]
107+
public void ButtonUpload_OnGetFileFormat_Ok()
108+
{
109+
var cut = Context.RenderComponent<DropUpload>(pb =>
110+
{
111+
pb.Add(a => a.LoadingIcon, "fa-loading");
112+
pb.Add(a => a.DeleteIcon, "fa-delte");
113+
pb.Add(a => a.CancelIcon, "fa-cancel");
114+
pb.Add(a => a.DownloadIcon, "fa-download");
115+
pb.Add(a => a.InvalidStatusIcon, "fa-invalid");
116+
pb.Add(a => a.ValidStatusIcon, "fa-valid");
117+
118+
pb.Add(a => a.FileIconArchive, "fa-file-text");
119+
pb.Add(a => a.FileIconExcel, "fa-file-excel");
120+
pb.Add(a => a.FileIconFile, "fa-file");
121+
pb.Add(a => a.FileIconDocx, "fa-file-word");
122+
pb.Add(a => a.FileIconPPT, "fa-file-powerpoint");
123+
pb.Add(a => a.FileIconAudio, "fa-file-audio");
124+
pb.Add(a => a.FileIconVideo, "fa-file-video");
125+
pb.Add(a => a.FileIconCode, "fa-file-code");
126+
pb.Add(a => a.FileIconPdf, "fa-file-pdf");
127+
pb.Add(a => a.FileIconImage, "fa-file-image");
128+
pb.Add(a => a.FileIconZip, "fa-file-archive");
129+
pb.Add(a => a.DefaultFileList,
130+
[
131+
new() { FileName = "1.csv" },
132+
new() { FileName = "1.xls" },
133+
new() { FileName = "1.xlsx" },
134+
new() { FileName = "1.doc" },
135+
new() { FileName = "1.docx" },
136+
new() { FileName = "1.dot" },
137+
new() { FileName = "1.ppt" },
138+
new() { FileName = "1.pptx" },
139+
new() { FileName = "1.wav" },
140+
new() { FileName = "1.mp3" },
141+
new() { FileName = "1.mp4" },
142+
new() { FileName = "1.mov" },
143+
new() { FileName = "1.mkv" },
144+
new() { FileName = "1.cs" },
145+
new() { FileName = "1.html" },
146+
new() { FileName = "1.vb" },
147+
new() { FileName = "1.pdf" },
148+
new() { FileName = "1.zip" },
149+
new() { FileName = "1.rar" },
150+
new() { FileName = "1.iso" },
151+
new() { FileName = "1.txt" },
152+
new() { FileName = "1.log" },
153+
new() { FileName = "1.jpg" },
154+
new() { FileName = "1.jpeg" },
155+
new() { FileName = "1.png" },
156+
new() { FileName = "1.bmp" },
157+
new() { FileName = "1.gif" },
158+
new() { FileName = "1.test" },
159+
new() { FileName = "1" }
160+
]);
161+
162+
});
163+
cut.Contains("fa-file-excel");
164+
cut.Contains("fa-file-word");
165+
cut.Contains("fa-file-powerpoint");
166+
cut.Contains("fa-file-audio");
167+
cut.Contains("fa-file-video");
168+
cut.Contains("fa-file-code");
169+
cut.Contains("fa-file-pdf");
170+
cut.Contains("fa-file-archive");
171+
cut.Contains("fa-file-text");
172+
cut.Contains("fa-file-image");
173+
cut.Contains("fa-file-archive");
174+
cut.Contains("fa-file");
175+
176+
cut.SetParametersAndRender(pb =>
177+
{
178+
pb.Add(a => a.OnGetFileFormat, extensions =>
179+
{
180+
return "fa-format-test";
181+
});
182+
});
183+
cut.Contains("fa-format-test");
184+
}
185+
186+
private class MockBrowserFile(string name = "UploadTestFile", string contentType = "text") : IBrowserFile
187+
{
188+
public string Name { get; } = name;
189+
190+
public DateTimeOffset LastModified { get; } = DateTimeOffset.Now;
191+
192+
public long Size { get; } = 10;
193+
194+
public string ContentType { get; } = contentType;
195+
196+
public Stream OpenReadStream(long maxAllowedSize = 512000, CancellationToken cancellationToken = default)
197+
{
198+
return new MemoryStream([0x01, 0x02]);
199+
}
200+
}
201+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
using Microsoft.AspNetCore.Components.Forms;
7+
8+
namespace UnitTest.Components;
9+
10+
public class UploadFileTest
11+
{
12+
[Fact]
13+
public async Task RequestBase64ImageFileAsync_Ok()
14+
{
15+
UploadFile uploadFile = new()
16+
{
17+
File = new MockBrowserFile("UploadTestFile", "image/png")
18+
};
19+
await uploadFile.RequestBase64ImageFileAsync(format: "image/png", maxWidth: 320, maxHeight: 240, maxAllowedSize: 512000, token: default);
20+
}
21+
22+
private class MockBrowserFile(string name = "UploadTestFile", string contentType = "text") : IBrowserFile
23+
{
24+
public string Name { get; } = name;
25+
26+
public DateTimeOffset LastModified { get; } = DateTimeOffset.Now;
27+
28+
public long Size { get; } = 10;
29+
30+
public string ContentType { get; } = contentType;
31+
32+
public Stream OpenReadStream(long maxAllowedSize = 512000, CancellationToken cancellationToken = default)
33+
{
34+
return new MemoryStream([0x01, 0x02]);
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)