Skip to content

Commit 448ce78

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

4 files changed

Lines changed: 36 additions & 12 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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(argo@live.ca) Website: https://www.blazor.zone
5+
6+
namespace BootstrapBlazor.Components;
7+
8+
readonly record struct UploadValidateItem(string? Id, string? ErrorMessage);

src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,11 @@ private async Task ValidateAsync(IValidateComponent validator, ValidationContext
509509
else
510510
{
511511
// 未选择文件
512-
if (propertyValue is IEnumerable)
512+
if (propertyValue is string)
513+
{
514+
515+
}
516+
else if (propertyValue is IEnumerable)
513517
{
514518
propertyValue = null;
515519
}

src/BootstrapBlazor/Extensions/ValidateContextExtensions.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,7 @@ public static ValidationResult GetValidationResult(this ValidationContext contex
4343
return new ValidationResult(errorMessage, memberNames);
4444
}
4545

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-
}
46+
internal static List<UploadValidateItem> GetInvalidItems(this IReadOnlyCollection<ValidationResult> source, bool isInValidOnAddItem, string? newId) => isInValidOnAddItem
47+
? [new UploadValidateItem() { Id = newId, ErrorMessage = source.First().ErrorMessage }]
48+
: source.Select(i => new UploadValidateItem() { Id = i.MemberNames.FirstOrDefault(), ErrorMessage = i.ErrorMessage }).ToList();
5749
}

test/UnitTest/Components/UploadAvatarTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,26 @@ await cut.InvokeAsync(() => input.Instance.OnChange.InvokeAsync(new InputFileCha
286286
await cut.InvokeAsync(() => items[1].Click());
287287
}
288288

289+
[Fact]
290+
public void UploadValidateItem_Ok()
291+
{
292+
var type = Type.GetType("BootstrapBlazor.Components.UploadValidateItem, BootstrapBlazor");
293+
Assert.NotNull(type);
294+
295+
var instance = Activator.CreateInstance(type, ["addId", "mock_ErrorMessage"]);
296+
var propertyInfo = type.GetProperty("Id");
297+
Assert.NotNull(propertyInfo);
298+
299+
var v = propertyInfo.GetValue(instance, null);
300+
Assert.Equal("addId", v);
301+
302+
propertyInfo = type.GetProperty("ErrorMessage");
303+
Assert.NotNull(propertyInfo);
304+
305+
v = propertyInfo.GetValue(instance, null);
306+
Assert.Equal("mock_ErrorMessage", v);
307+
}
308+
289309
private class Person
290310
{
291311
[Required]

0 commit comments

Comments
 (0)