Skip to content

Commit 0f5a6ee

Browse files
committed
refactor: 更新单元测试
1 parent dcb9711 commit 0f5a6ee

File tree

4 files changed

+220
-23
lines changed

4 files changed

+220
-23
lines changed

src/BootstrapBlazor/Components/Upload/AvatarUpload.razor.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -148,22 +148,6 @@ protected override void OnParametersSet()
148148
};
149149
}
150150

151-
/// <summary>
152-
/// <inheritdoc/>
153-
/// </summary>
154-
/// <returns></returns>
155-
protected override bool CheckCanUpload()
156-
{
157-
// 允许多上传
158-
if (IsMultiple)
159-
{
160-
return !MaxFileCount.HasValue || GetUploadFiles().Count < MaxFileCount;
161-
}
162-
163-
// 只允许单个上传
164-
return UploadFiles.Count == 0;
165-
}
166-
167151
/// <summary>
168152
/// 获得 数据验证客户端 ID
169153
/// </summary>

src/BootstrapBlazor/Components/Upload/CardUpload.razor

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,10 @@
8686
@code {
8787
RenderFragment RenderAdd =>
8888
@<div class="@CardItemClass">
89-
<div class="upload-item-actions btn-browser">
90-
@if (!IsDisabled)
91-
{
89+
<div class="@ActionClassString">
9290
<span class="upload-item-plus">
9391
<i class="@AddIcon"></i>
9492
</span>
95-
}
9693
</div>
9794
</div>;
9895
}

src/BootstrapBlazor/Components/Upload/UploadBase.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,14 @@ protected virtual bool CheckCanUpload()
239239
return true;
240240
}
241241

242-
if (IsMultiple == false)
242+
// 允许多上传
243+
if (IsMultiple)
243244
{
244-
return UploadFiles.Count > 0;
245+
return !MaxFileCount.HasValue || GetUploadFiles().Count < MaxFileCount;
245246
}
246247

247-
return MaxFileCount.HasValue && UploadFiles.Count > MaxFileCount.Value;
248+
// 只允许单个上传
249+
return UploadFiles.Count == 0;
248250
}
249251

250252
/// <summary>
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
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+
9+
namespace UnitTest.Components;
10+
11+
public class UploadCardTest : BootstrapBlazorTestBase
12+
{
13+
[Fact]
14+
public async Task CardUpload_Ok()
15+
{
16+
var zoom = false;
17+
var deleted = false;
18+
var cut = Context.RenderComponent<CardUpload<string>>(pb =>
19+
{
20+
pb.Add(a => a.OnDelete, file =>
21+
{
22+
deleted = true;
23+
return Task.FromResult(true);
24+
});
25+
pb.Add(a => a.DefaultFileList,
26+
[
27+
new() { FileName = "Test-File1.text" },
28+
new() { FileName = "Test-File2.jpg" },
29+
new() { PrevUrl = "Test-File3.png" },
30+
new() { PrevUrl = "Test-File4.bmp" },
31+
new() { PrevUrl = "Test-File5.jpeg" },
32+
new() { PrevUrl = "Test-File6.gif" },
33+
new() { PrevUrl = "data:image/png;base64,iVBORw0KGgoAAAANS=" },
34+
new() { FileName = null! }
35+
]);
36+
});
37+
cut.Contains("bb-previewer collapse active");
38+
39+
cut.SetParametersAndRender(pb =>
40+
{
41+
pb.Add(a => a.IconTemplate, file => builder =>
42+
{
43+
builder.AddContent(0, "custom-file-icon-template");
44+
});
45+
});
46+
cut.Contains("custom-file-icon-template");
47+
48+
// OnZoom
49+
await cut.InvokeAsync(() => cut.Find(".btn-zoom").Click());
50+
Assert.False(zoom);
51+
52+
cut.SetParametersAndRender(pb =>
53+
{
54+
pb.Add(a => a.OnZoomAsync, file =>
55+
{
56+
zoom = true;
57+
return Task.CompletedTask;
58+
});
59+
});
60+
await cut.InvokeAsync(() => cut.Find(".btn-zoom").Click());
61+
Assert.True(zoom);
62+
63+
zoom = false;
64+
await cut.InvokeAsync(() => cut.Find(".upload-item-body-image").Click());
65+
Assert.True(zoom);
66+
67+
// ShowDownload
68+
var clicked = false;
69+
cut.SetParametersAndRender(pb =>
70+
{
71+
pb.Add(a => a.ShowDownloadButton, true);
72+
pb.Add(a => a.OnDownload, file =>
73+
{
74+
clicked = true;
75+
return Task.CompletedTask;
76+
});
77+
78+
});
79+
Assert.Contains("btn-download", cut.Markup);
80+
var button = cut.Find(".btn-download");
81+
await cut.InvokeAsync(() => button.Click());
82+
Assert.True(clicked);
83+
84+
// OnDelete
85+
await cut.InvokeAsync(() => cut.Find(".btn-outline-danger").Click());
86+
Assert.True(deleted);
87+
88+
// CanPreviewCallback
89+
cut.SetParametersAndRender(pb =>
90+
{
91+
pb.Add(a => a.CanPreviewCallback, p =>
92+
{
93+
return false;
94+
});
95+
});
96+
await cut.InvokeAsync(() => cut.Find(".btn-zoom").Click());
97+
98+
// ShowProgress
99+
cut.SetParametersAndRender(pb =>
100+
{
101+
pb.Add(a => a.ShowProgress, true);
102+
pb.Add(a => a.OnChange, async file =>
103+
{
104+
await file.SaveToFileAsync("1.txt");
105+
});
106+
});
107+
var input = cut.FindComponent<InputFile>();
108+
await cut.InvokeAsync(() => input.Instance.OnChange.InvokeAsync(new InputFileChangeEventArgs(new List<MockBrowserFile>()
109+
{
110+
new("test.txt", "Image-Png")
111+
})));
112+
await cut.InvokeAsync(() => input.Instance.OnChange.InvokeAsync(new InputFileChangeEventArgs(new List<MockBrowserFile>()
113+
{
114+
new("test.png")
115+
})));
116+
117+
// IsUploadButtonAtFirst
118+
cut.SetParametersAndRender(pb =>
119+
{
120+
pb.Add(a => a.IsUploadButtonAtFirst, true);
121+
pb.Add(a => a.IsMultiple, true);
122+
});
123+
}
124+
125+
[Fact]
126+
public void CardUpload_ValidateForm_Ok()
127+
{
128+
var foo = new Foo();
129+
var cut = Context.RenderComponent<ValidateForm>(pb =>
130+
{
131+
pb.Add(a => a.Model, foo);
132+
pb.AddChildContent<CardUpload<string>>(pb =>
133+
{
134+
pb.Add(a => a.Value, foo.Name);
135+
pb.Add(a => a.ValueExpression, foo.GenerateValueExpression());
136+
});
137+
});
138+
cut.Contains("form-label");
139+
}
140+
141+
[Fact]
142+
public async Task CardUpload_ShowProgress_Ok()
143+
{
144+
var cancel = false;
145+
var cut = Context.RenderComponent<CardUpload<string>>(pb =>
146+
{
147+
pb.Add(a => a.ShowProgress, true);
148+
pb.Add(a => a.OnChange, async file =>
149+
{
150+
await Task.Delay(100);
151+
await file.SaveToFileAsync("1.txt");
152+
});
153+
pb.Add(a => a.OnCancel, file =>
154+
{
155+
cancel = true;
156+
return Task.CompletedTask;
157+
});
158+
});
159+
var input = cut.FindComponent<InputFile>();
160+
await cut.InvokeAsync(() =>
161+
{
162+
input.Instance.OnChange.InvokeAsync(new InputFileChangeEventArgs(new List<MockBrowserFile>()
163+
{
164+
new()
165+
}));
166+
var button = cut.Find(".btn-cancel");
167+
Assert.NotNull(button);
168+
button.Click();
169+
});
170+
Assert.True(cancel);
171+
}
172+
173+
//[Fact]
174+
//public async Task CardUpload_Max_Ok()
175+
//{
176+
// var cut = Context.RenderComponent<CardUpload<string>>(pb =>
177+
// {
178+
// pb.Add(a => a.ShowProgress, true);
179+
// pb.Add(a => a.Max, 1);
180+
// });
181+
// var input = cut.FindComponent<InputFile>();
182+
// await cut.InvokeAsync(async () =>
183+
// {
184+
// await input.Instance.OnChange.InvokeAsync(new InputFileChangeEventArgs(new List<MockBrowserFile>()
185+
// {
186+
// new()
187+
// }));
188+
// });
189+
//}
190+
191+
private class Person
192+
{
193+
[Required]
194+
[FileValidation(Extensions = [".png", ".jpg", ".jpeg"])]
195+
196+
public IBrowserFile? Picture { get; set; }
197+
}
198+
199+
private class MockBrowserFile(string name = "UploadTestFile", string contentType = "text") : IBrowserFile
200+
{
201+
public string Name { get; } = name;
202+
203+
public DateTimeOffset LastModified { get; } = DateTimeOffset.Now;
204+
205+
public long Size { get; } = 10;
206+
207+
public string ContentType { get; } = contentType;
208+
209+
public Stream OpenReadStream(long maxAllowedSize = 512000, CancellationToken cancellationToken = default)
210+
{
211+
return new MemoryStream([0x01, 0x02]);
212+
}
213+
}
214+
}

0 commit comments

Comments
 (0)