Skip to content

Commit 6439290

Browse files
committed
test: 增加单元测试
1 parent 63d52f8 commit 6439290

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/BootstrapBlazor/Components/DropdownWidget/DropdownWidget.razor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public sealed partial class DropdownWidget
4343
/// <inheritdoc/>
4444
/// </summary>
4545
/// <returns></returns>
46-
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new { Method = nameof(OnWidgetItemClosed) });
46+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new { Method = nameof(TriggerStateChanged) });
4747

4848
/// <summary>
4949
/// 添加 DropdownWidgetItem 方法
@@ -63,7 +63,7 @@ internal void Add(DropdownWidgetItem item)
6363
/// <param name="shown"></param>
6464
/// <returns></returns>
6565
[JSInvokable]
66-
public async Task OnWidgetItemClosed(int index, bool shown)
66+
public async Task TriggerStateChanged(int index, bool shown)
6767
{
6868
var items = GetItems().ToList();
6969
var item = index < items.Count ? items[index] : null;

test/UnitTest/Components/DropdownWidgetTest.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,45 @@ public void HeaderColor_OK()
181181
Assert.NotNull(ele);
182182
}
183183

184-
private static IEnumerable<DropdownWidgetItem> GetItems()
184+
[Fact]
185+
public async Task OnItemAsync_OK()
186+
{
187+
var shown = false;
188+
var closed = false;
189+
var cut = Context.RenderComponent<DropdownWidget>(builder =>
190+
{
191+
builder.Add(a => a.OnItemShownAsync, item =>
192+
{
193+
shown = true;
194+
return Task.CompletedTask;
195+
});
196+
builder.Add(a => a.OnItemCloseAsync, item =>
197+
{
198+
closed = true;
199+
return Task.CompletedTask;
200+
});
201+
builder.Add(s => s.ChildContent, new RenderFragment(builder =>
202+
{
203+
builder.OpenComponent<DropdownWidgetItem>(0);
204+
builder.AddAttribute(1, nameof(DropdownWidgetItem.HeaderColor), Color.Success);
205+
builder.AddAttribute(2, nameof(DropdownWidgetItem.Title), "Test1");
206+
builder.CloseComponent();
207+
208+
builder.OpenComponent<DropdownWidgetItem>(0);
209+
builder.AddAttribute(10, nameof(DropdownWidgetItem.HeaderColor), Color.Success);
210+
builder.AddAttribute(11, nameof(DropdownWidgetItem.Title), "Test2");
211+
builder.CloseComponent();
212+
}));
213+
});
214+
215+
await cut.InvokeAsync(() => cut.Instance.TriggerStateChanged(0, true));
216+
Assert.True(shown);
217+
218+
await cut.InvokeAsync(() => cut.Instance.TriggerStateChanged(1, false));
219+
Assert.True(closed);
220+
}
221+
222+
private static List<DropdownWidgetItem> GetItems()
185223
{
186224
var ret = new List<DropdownWidgetItem>();
187225
var widget = new DropdownWidgetItem();

0 commit comments

Comments
 (0)