|
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 | // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone |
5 | 5 |
|
6 | | -using Microsoft.AspNetCore.Components.Web; |
| 6 | +using AngleSharp.Html.Dom; |
| 7 | +using Microsoft.AspNetCore.Components.Web.Virtualization; |
7 | 8 | using Microsoft.Extensions.Localization; |
| 9 | +using System.Reflection; |
8 | 10 |
|
9 | 11 | namespace UnitTest.Components; |
10 | 12 |
|
@@ -306,4 +308,121 @@ public void ShowDropdownListOnFocus_Ok() |
306 | 308 | }); |
307 | 309 | cut.DoesNotContain("data-bb-auto-dropdown-focus"); |
308 | 310 | } |
| 311 | + |
| 312 | + [Fact] |
| 313 | + public async Task IsVirtualize_Items() |
| 314 | + { |
| 315 | + var items = new List<Foo>() { new() { Name = "test1" }, new() { Name = "test2" } }; |
| 316 | + var cut = Context.RenderComponent<AutoFill<Foo>>(pb => |
| 317 | + { |
| 318 | + pb.Add(a => a.IsLikeMatch, true); |
| 319 | + pb.Add(a => a.Items, items); |
| 320 | + pb.Add(a => a.Value, items[0]); |
| 321 | + pb.Add(a => a.IsVirtualize, true); |
| 322 | + pb.Add(a => a.RowHeight, 58f); |
| 323 | + pb.Add(a => a.OverscanCount, 4); |
| 324 | + pb.Add(a => a.OnGetDisplayText, f => f?.Name); |
| 325 | + }); |
| 326 | + |
| 327 | + await cut.InvokeAsync(() => cut.Instance.TriggerFilter("2")); |
| 328 | + cut.Contains("<div>test2</div>"); |
| 329 | + } |
| 330 | + |
| 331 | + [Fact] |
| 332 | + public async Task IsVirtualize_Items_Clearable_Ok() |
| 333 | + { |
| 334 | + var items = new List<Foo>() { new() { Name = "test1" }, new() { Name = "test2" } }; |
| 335 | + var cut = Context.RenderComponent<AutoFill<Foo>>(pb => |
| 336 | + { |
| 337 | + pb.Add(a => a.Items, items); |
| 338 | + pb.Add(a => a.Value, items[0]); |
| 339 | + pb.Add(a => a.IsVirtualize, true); |
| 340 | + pb.Add(a => a.IsClearable, true); |
| 341 | + pb.Add(a => a.OnGetDisplayText, f => f?.Name); |
| 342 | + }); |
| 343 | + await cut.InvokeAsync(() => cut.Instance.TriggerFilter("2")); |
| 344 | + |
| 345 | + // 点击 Clear 按钮 |
| 346 | + var button = cut.Find(".clear-icon"); |
| 347 | + await cut.InvokeAsync(() => button.Click()); |
| 348 | + cut.SetParametersAndRender(); |
| 349 | + |
| 350 | + var input = cut.Find(".form-control"); |
| 351 | + Assert.Null(input.NodeValue); |
| 352 | + } |
| 353 | + |
| 354 | + [Fact] |
| 355 | + public async Task IsVirtualize_OnQueryAsync_Clearable_Ok() |
| 356 | + { |
| 357 | + var query = false; |
| 358 | + var startIndex = 0; |
| 359 | + var requestCount = 0; |
| 360 | + var searchText = string.Empty; |
| 361 | + var cleared = false; |
| 362 | + var items = new List<Foo>() { new() { Name = "test1" }, new() { Name = "test2" } }; |
| 363 | + var cut = Context.RenderComponent<AutoFill<Foo>>(pb => |
| 364 | + { |
| 365 | + pb.Add(a => a.OnQueryAsync, option => |
| 366 | + { |
| 367 | + query = true; |
| 368 | + startIndex = option.StartIndex; |
| 369 | + requestCount = option.Count; |
| 370 | + searchText = option.SearchText; |
| 371 | + return Task.FromResult(new QueryData<Foo>() |
| 372 | + { |
| 373 | + Items = string.IsNullOrEmpty(searchText) ? items : items.Where(i => i.Name!.Contains(searchText, StringComparison.OrdinalIgnoreCase)), |
| 374 | + TotalCount = string.IsNullOrEmpty(searchText) ? 2 : 1 |
| 375 | + }); |
| 376 | + }); |
| 377 | + pb.Add(a => a.Value, items[0]); |
| 378 | + pb.Add(a => a.IsVirtualize, true); |
| 379 | + pb.Add(a => a.IsClearable, true); |
| 380 | + pb.Add(a => a.OnGetDisplayText, f => f?.Name); |
| 381 | + pb.Add(a => a.OnClearAsync, () => |
| 382 | + { |
| 383 | + cleared = true; |
| 384 | + return Task.CompletedTask; |
| 385 | + }); |
| 386 | + }); |
| 387 | + await cut.InvokeAsync(() => cut.Instance.TriggerFilter("2")); |
| 388 | + Assert.Equal("2", searchText); |
| 389 | + Assert.Contains("<div>test2</div>", cut.Markup); |
| 390 | + |
| 391 | + query = false; |
| 392 | + // 点击 Clear 按钮 |
| 393 | + var button = cut.Find(".clear-icon"); |
| 394 | + await cut.InvokeAsync(() => button.Click()); |
| 395 | + |
| 396 | + Assert.True(query); |
| 397 | + Assert.True(cleared); |
| 398 | + |
| 399 | + // OnQueryAsync 返回空集合 |
| 400 | + cut.SetParametersAndRender(pb => |
| 401 | + { |
| 402 | + pb.Add(a => a.OnQueryAsync, option => |
| 403 | + { |
| 404 | + return Task.FromResult(new QueryData<Foo>() |
| 405 | + { |
| 406 | + Items = null, |
| 407 | + TotalCount = 0 |
| 408 | + }); |
| 409 | + }); |
| 410 | + }); |
| 411 | + await cut.InvokeAsync(() => button.Click()); |
| 412 | + } |
| 413 | + |
| 414 | + [Fact] |
| 415 | + public void Clearable_Ok() |
| 416 | + { |
| 417 | + var items = new List<int?>() { 1, 2 }; |
| 418 | + var cut = Context.RenderComponent<AutoFill<int?>>(pb => |
| 419 | + { |
| 420 | + pb.Add(a => a.Items, items); |
| 421 | + pb.Add(a => a.Value, items[0]); |
| 422 | + pb.Add(a => a.IsVirtualize, true); |
| 423 | + pb.Add(a => a.IsClearable, true); |
| 424 | + pb.Add(a => a.OnGetDisplayText, f => f?.ToString()); |
| 425 | + }); |
| 426 | + cut.Contains("clear-icon"); |
| 427 | + } |
309 | 428 | } |
0 commit comments