fix(ValidateBase): should Render UI after validate (#4255)

* fix: 调用刷新同步组件状态

* test: 更新单元测试
This commit is contained in:
Argo Zhang 2024-09-10 15:13:57 +08:00 committed by GitHub
parent 0de6cc81b4
commit 4dc4e05460
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 19 deletions

View File

@ -356,11 +356,6 @@ public abstract class ValidateBase<TValue> : DisplayBase<TValue>, IValidateCompo
&& !value.GetType().IsAssignableTo(typeof(System.Collections.IEnumerable))
&& value.GetType().IsClass;
/// <summary>
/// 获得/设置 是否执行了自定义异步验证
/// </summary>
protected bool IsAsyncValidate { get; set; }
/// <summary>
/// 属性验证方法
/// </summary>
@ -383,7 +378,6 @@ public abstract class ValidateBase<TValue> : DisplayBase<TValue>, IValidateCompo
if (validator is IValidatorAsync v)
{
await v.ValidateAsync(propertyValue, context, results);
IsAsyncValidate = true;
}
else
{
@ -404,7 +398,6 @@ public abstract class ValidateBase<TValue> : DisplayBase<TValue>, IValidateCompo
if (validator is IValidatorAsync v)
{
await v.ValidateAsync(propertyValue, context, results);
IsAsyncValidate = true;
}
else
{
@ -460,11 +453,8 @@ public abstract class ValidateBase<TValue> : DisplayBase<TValue>, IValidateCompo
OnValidate(IsValid);
}
if (IsAsyncValidate)
{
IsAsyncValidate = false;
StateHasChanged();
}
// 必须刷新一次 UI 保证状态正确
StateHasChanged();
}
private JSModule? ValidateModule { get; set; }

View File

@ -162,7 +162,7 @@ public class ValidateFormTest : BootstrapBlazorTestBase
}
[Fact]
public void SetError_Ok()
public async Task SetError_Ok()
{
var foo = new Foo();
var dummy = new Dummy();
@ -180,9 +180,9 @@ public class ValidateFormTest : BootstrapBlazorTestBase
pb.Add(a => a.ValueExpression, Utility.GenerateValueExpression(dummy, "Value", typeof(DateTime?)));
});
});
cut.Instance.SetError("Name", "Test_SetError");
cut.Instance.SetError("Test.Name", "Test_SetError");
cut.Instance.SetError<Foo>(f => f.Name, "Name_SetError");
await cut.InvokeAsync(() => cut.Instance.SetError("Name", "Test_SetError"));
await cut.InvokeAsync(() => cut.Instance.SetError("Test.Name", "Test_SetError"));
await cut.InvokeAsync(() => cut.Instance.SetError<Foo>(f => f.Name, "Name_SetError"));
// 利用反射提高代码覆盖率
var method = typeof(ValidateForm).GetMethod("TryGetValidator", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
@ -193,7 +193,7 @@ public class ValidateFormTest : BootstrapBlazorTestBase
}
[Fact]
public void SetError_UnaryExpression()
public async Task SetError_UnaryExpression()
{
var foo = new Foo();
var dummy = new Dummy();
@ -211,13 +211,13 @@ public class ValidateFormTest : BootstrapBlazorTestBase
pb.Add(a => a.ValueExpression, Utility.GenerateValueExpression(dummy, "Value", typeof(DateTime?)));
});
});
cut.Instance.SetError<Dummy>(f => f.Value, "Name_SetError");
await cut.InvokeAsync(() => cut.Instance.SetError<Dummy>(f => f.Value, "Name_SetError"));
// 利用反射提高代码覆盖率
var fieldInfo = cut.Instance.GetType().GetField("_validatorCache", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!;
var cache = (ConcurrentDictionary<(string FieldName, Type ModelType), (FieldIdentifier FieldIdentifier, IValidateComponent ValidateComponent)>)fieldInfo.GetValue(cut.Instance)!;
cache.Remove(("Value", typeof(Dummy)), out _);
cut.Instance.SetError<Dummy>(f => f.Value, "Name_SetError");
await cut.InvokeAsync(() => cut.Instance.SetError<Dummy>(f => f.Value, "Name_SetError"));
}
[Fact]