Skip to content

Commit 1152fa8

Browse files
committed
test: 更新单元测试
1 parent 64ce6b2 commit 1152fa8

4 files changed

Lines changed: 26 additions & 11 deletions

File tree

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,10 @@ public override async Task ToggleMessage(IReadOnlyCollection<ValidationResult> r
187187

188188
ValidateModule ??= await LoadValidateModule();
189189

190-
var invalidItems = IsInValidOnAddItem
191-
? [new { Id = AddId, _results.First().ErrorMessage }]
192-
: _results.Select(i => new { Id = i.MemberNames.FirstOrDefault(), i.ErrorMessage }).ToList();
193-
194190
var items = IsInValidOnAddItem
195191
? [AddId]
196192
: Files.Select(i => i.ValidateId).ToList();
197-
193+
var invalidItems = _results.GetInvalidItems(IsInValidOnAddItem, AddId);
198194
var addId = IsInValidOnAddItem ? null : AddId;
199195
await ValidateModule.InvokeVoidAsync("executeUpload", items, invalidItems, addId);
200196
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,10 @@ public override async Task ToggleMessage(IReadOnlyCollection<ValidationResult> r
176176

177177
ValidateModule ??= await LoadValidateModule();
178178

179-
var invalidItems = IsInValidOnAddItem
180-
? [new { Id = AddId, _results.First().ErrorMessage }]
181-
: _results.Select(i => new { Id = i.MemberNames.FirstOrDefault(), i.ErrorMessage }).ToList();
182-
183179
var items = IsInValidOnAddItem
184180
? [AddId]
185181
: Files.Select(i => i.ValidateId).ToList();
186-
182+
var invalidItems = _results.GetInvalidItems(IsInValidOnAddItem, AddId);
187183
var addId = IsInValidOnAddItem ? null : AddId;
188184
await ValidateModule.InvokeVoidAsync("executeUpload", items, invalidItems, addId);
189185
}

src/BootstrapBlazor/Extensions/ValidateContextExtensions.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
@@ -42,4 +42,16 @@ public static ValidationResult GetValidationResult(this ValidationContext contex
4242
var memberNames = string.IsNullOrEmpty(context.MemberName) ? null : new string[] { context.MemberName };
4343
return new ValidationResult(errorMessage, memberNames);
4444
}
45+
46+
internal static List<ValidateItem> GetInvalidItems(this IReadOnlyCollection<ValidationResult> source, bool isInValidOnAddItem, string? newId) => isInValidOnAddItem
47+
? [new ValidateItem() { Id = newId, ErrorMessage = source.First().ErrorMessage }]
48+
: source.Select(i => new ValidateItem() { Id = i.MemberNames.FirstOrDefault(), ErrorMessage = i.ErrorMessage }).ToList();
49+
50+
[ExcludeFromCodeCoverage]
51+
internal readonly record struct ValidateItem
52+
{
53+
public string? Id { get; init; }
54+
55+
public string? ErrorMessage { get; init; }
56+
}
4557
}

test/UnitTest/Components/UploadCardTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ public async Task CardUpload_ValidateForm_Ok()
171171
pb.Add(a => a.Value, foo.Files);
172172
pb.Add(a => a.ValueExpression, Utility.GenerateValueExpression(foo, "Files", typeof(List<UploadFile>)));
173173
pb.Add(a => a.AllowExtensions, [".jpg"]);
174+
pb.Add(a => a.ShowDeleteButton, true);
174175
});
175176
pb.Add(a => a.OnValidSubmit, context =>
176177
{
@@ -208,6 +209,16 @@ await input.Instance.OnChange.InvokeAsync(new InputFileChangeEventArgs(new List<
208209
});
209210

210211
Assert.DoesNotContain("is-invalid", upload.Markup);
212+
213+
upload.Render(pb =>
214+
{
215+
pb.Add(a => a.IsDisabled, false);
216+
});
217+
// 清空所有文件
218+
var items = cut.FindAll(".btn-outline-danger");
219+
Assert.Single(items);
220+
await cut.InvokeAsync(() => items[0].Click());
221+
form.Submit();
211222
}
212223

213224
[Fact]

0 commit comments

Comments
 (0)