Skip to content

Commit 26c8664

Browse files
committed
test: 更新单元测试
1 parent 9a98a3c commit 26c8664

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

test/UnitTest/Components/AutoFillTest.cs

Lines changed: 64 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.Extensions.Localization;
7+
using System.ComponentModel.DataAnnotations;
78

89
namespace UnitTest.Components;
910

@@ -422,4 +423,67 @@ public void Clearable_Ok()
422423
});
423424
cut.Contains("clear-icon");
424425
}
426+
427+
428+
[Fact]
429+
public async Task Validate_Ok()
430+
{
431+
var valid = false;
432+
var invalid = false;
433+
var model = new MockModel() { Value = new Foo() { Name = "Test-Select1" } };
434+
var items = new List<Foo>()
435+
{
436+
new() { Name = "test1" },
437+
new() { Name = "test2" }
438+
};
439+
var cut = Context.RenderComponent<ValidateForm>(builder =>
440+
{
441+
builder.Add(a => a.OnValidSubmit, context =>
442+
{
443+
valid = true;
444+
return Task.CompletedTask;
445+
});
446+
builder.Add(a => a.OnInvalidSubmit, context =>
447+
{
448+
invalid = true;
449+
return Task.CompletedTask;
450+
});
451+
builder.Add(a => a.Model, model);
452+
builder.AddChildContent<AutoFill<Foo>>(pb =>
453+
{
454+
pb.Add(a => a.Items, items);
455+
pb.Add(a => a.Value, model.Value);
456+
pb.Add(a => a.IsClearable, true);
457+
pb.Add(a => a.OnGetDisplayText, f => f?.Name);
458+
pb.Add(a => a.OnValueChanged, v =>
459+
{
460+
model.Value = v;
461+
return Task.CompletedTask;
462+
});
463+
pb.Add(a => a.ValueExpression, Utility.GenerateValueExpression(model, "Value", typeof(Foo)));
464+
});
465+
});
466+
467+
await cut.InvokeAsync(() =>
468+
{
469+
var form = cut.Find("form");
470+
form.Submit();
471+
});
472+
Assert.True(valid);
473+
Assert.Equal("Test-Select1", model.Value.Name);
474+
475+
// 点击 Clear 按钮
476+
var button = cut.Find(".clear-icon");
477+
await cut.InvokeAsync(() => button.Click());
478+
479+
var form = cut.Find("form");
480+
form.Submit();
481+
Assert.True(invalid);
482+
}
483+
484+
class MockModel
485+
{
486+
[Required]
487+
public Foo? Value { get; set; }
488+
}
425489
}

0 commit comments

Comments
 (0)