Skip to content

Commit a3012a3

Browse files
committed
test: 更新单元测试
1 parent eebd3c4 commit a3012a3

File tree

1 file changed

+206
-0
lines changed

1 file changed

+206
-0
lines changed

test/UnitTest/Components/MultiSelectTest.cs

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
55

6+
using Microsoft.AspNetCore.Components.Web.Virtualization;
7+
using System.Reflection;
8+
69
namespace UnitTest.Components;
710

811
public class MultiSelectTest : BootstrapBlazorTestBase
@@ -661,4 +664,207 @@ public void IsMarkupString_Ok()
661664
});
662665
Assert.Contains("<div>Test1</div>", cut.Markup);
663666
}
667+
668+
[Fact]
669+
public void IsVirtualize_Items()
670+
{
671+
var cut = Context.RenderComponent<MultiSelect<string>>(pb =>
672+
{
673+
pb.Add(a => a.Items, new SelectedItem[]
674+
{
675+
new("1", "Test1"),
676+
new("2", "Test2")
677+
});
678+
pb.Add(a => a.Value, "2");
679+
pb.Add(a => a.IsVirtualize, true);
680+
pb.Add(a => a.RowHeight, 33f);
681+
pb.Add(a => a.OverscanCount, 4);
682+
});
683+
684+
cut.SetParametersAndRender(pb => pb.Add(a => a.ShowSearch, true));
685+
cut.InvokeAsync(async () =>
686+
{
687+
// 搜索 T
688+
cut.Find(".search-text").Input("T");
689+
await cut.Instance.ConfirmSelectedItem(0);
690+
});
691+
}
692+
693+
[Fact]
694+
public async Task IsVirtualize_Items_Clearable_Ok()
695+
{
696+
var cut = Context.RenderComponent<MultiSelect<string>>(pb =>
697+
{
698+
pb.Add(a => a.Items, new SelectedItem[]
699+
{
700+
new("1", "Test1"),
701+
new("2", "Test2")
702+
});
703+
pb.Add(a => a.Value, "2");
704+
pb.Add(a => a.IsVirtualize, true);
705+
pb.Add(a => a.RowHeight, 33f);
706+
pb.Add(a => a.OverscanCount, 4);
707+
pb.Add(a => a.IsClearable, true);
708+
pb.Add(a => a.ShowSearch, true);
709+
});
710+
711+
// 覆盖有搜索条件时,点击清空按钮
712+
// 期望 UI 显示值为默认值
713+
// 期望 下拉框为全数据
714+
var input = cut.Find(".search-text");
715+
await cut.InvokeAsync(() => cut.Instance.TriggerOnSearch("2"));
716+
717+
// 下拉框仅显示一个选项 Test2
718+
var items = cut.FindAll(".dropdown-item");
719+
Assert.Single(items);
720+
721+
// 点击 Clear 按钮
722+
var button = cut.Find(".clear-icon");
723+
await cut.InvokeAsync(() => button.Click());
724+
725+
// 下拉框显示所有选项
726+
items = cut.FindAll(".dropdown-item");
727+
Assert.Equal(2, items.Count);
728+
}
729+
730+
[Fact]
731+
public async Task IsVirtualize_OnQueryAsync_Clearable_Ok()
732+
{
733+
var query = false;
734+
var startIndex = 0;
735+
var requestCount = 0;
736+
var searchText = string.Empty;
737+
var cut = Context.RenderComponent<MultiSelect<string>>(pb =>
738+
{
739+
pb.Add(a => a.OnQueryAsync, option =>
740+
{
741+
query = true;
742+
startIndex = option.StartIndex;
743+
requestCount = option.Count;
744+
searchText = option.SearchText;
745+
return Task.FromResult(new QueryData<SelectedItem>()
746+
{
747+
Items = string.IsNullOrEmpty(searchText)
748+
? [new("", "All"), new("1", "Test1"), new("2", "Test2")]
749+
: [new("2", "Test2")],
750+
TotalCount = string.IsNullOrEmpty(searchText) ? 2 : 1
751+
});
752+
});
753+
pb.Add(a => a.Value, "");
754+
pb.Add(a => a.IsVirtualize, true);
755+
pb.Add(a => a.IsClearable, true);
756+
pb.Add(a => a.ShowSearch, true);
757+
});
758+
759+
// 覆盖有搜索条件时,点击清空按钮
760+
// 期望 UI 显示值为默认值
761+
// 期望 下拉框为全数据
762+
var input = cut.Find(".search-text");
763+
await cut.InvokeAsync(() => cut.Instance.TriggerOnSearch("2"));
764+
765+
// 下拉框仅显示一个选项 Test2
766+
var items = cut.FindAll(".dropdown-item");
767+
Assert.Single(items);
768+
769+
query = false;
770+
// 点击 Clear 按钮
771+
var button = cut.Find(".clear-icon");
772+
await cut.InvokeAsync(() => button.Click());
773+
774+
// 下拉框显示所有选项
775+
Assert.True(query);
776+
}
777+
778+
[Fact]
779+
public async Task IsVirtualize_BindValue()
780+
{
781+
var value = "3";
782+
var cut = Context.RenderComponent<MultiSelect<string>>(pb =>
783+
{
784+
pb.Add(a => a.Value, value);
785+
pb.Add(a => a.IsVirtualize, true);
786+
pb.Add(a => a.ValueChanged, EventCallback.Factory.Create(this, new Action<string?>(item =>
787+
{
788+
value = item;
789+
})));
790+
pb.Add(a => a.OnQueryAsync, option =>
791+
{
792+
return Task.FromResult(new QueryData<SelectedItem>()
793+
{
794+
Items = new SelectedItem[]
795+
{
796+
new("1", "Test1"),
797+
new("2", "Test2")
798+
},
799+
TotalCount = 2
800+
});
801+
});
802+
});
803+
804+
// 3 不在集合内,但是由于是虚拟集合,只能显示
805+
var select = cut.Instance;
806+
Assert.Equal("3", select.Value);
807+
808+
var item = cut.Find(".dropdown-item");
809+
await cut.InvokeAsync(() =>
810+
{
811+
item.Click();
812+
});
813+
Assert.Equal("3,1", value);
814+
}
815+
816+
[Fact]
817+
public void IsVirtualize_DefaultVirtualizeItemText()
818+
{
819+
string? value = "3";
820+
var cut = Context.RenderComponent<MultiSelect<string>>(pb =>
821+
{
822+
pb.Add(a => a.IsVirtualize, true);
823+
pb.Add(a => a.DefaultVirtualizeItemText, "Test 3");
824+
pb.Add(a => a.Value, value);
825+
pb.Add(a => a.ValueChanged, EventCallback.Factory.Create(this, new Action<string?>(item =>
826+
{
827+
value = item;
828+
})));
829+
pb.Add(a => a.OnQueryAsync, option =>
830+
{
831+
return Task.FromResult(new QueryData<SelectedItem>()
832+
{
833+
Items = new SelectedItem[]
834+
{
835+
new("1", "Test1"),
836+
new("2", "Test2")
837+
},
838+
TotalCount = 2
839+
});
840+
});
841+
});
842+
843+
cut.InvokeAsync(() =>
844+
{
845+
var input = cut.Find(".form-select");
846+
Assert.Equal("Test 3", input.GetAttribute("value"));
847+
});
848+
}
849+
850+
[Fact]
851+
public void LoadItems_Ok()
852+
{
853+
var cut = Context.RenderComponent<MultiSelect<string>>(pb =>
854+
{
855+
pb.Add(a => a.OnQueryAsync, option =>
856+
{
857+
return Task.FromResult(new QueryData<SelectedItem>());
858+
});
859+
pb.Add(a => a.Value, "2");
860+
pb.Add(a => a.IsVirtualize, true);
861+
});
862+
var select = cut.Instance;
863+
var mi = select.GetType().GetMethod("LoadItems", BindingFlags.NonPublic | BindingFlags.Instance);
864+
mi?.Invoke(select, [new ItemsProviderRequest(0, 1, CancellationToken.None)]);
865+
866+
var totalCountProperty = select.GetType().GetProperty("TotalCount", BindingFlags.NonPublic | BindingFlags.Instance);
867+
totalCountProperty?.SetValue(select, 2);
868+
mi?.Invoke(select, [new ItemsProviderRequest(0, 1, CancellationToken.None)]);
869+
}
664870
}

0 commit comments

Comments
 (0)