forked from MudBlazor/MudBlazor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutocompleteTests.cs
More file actions
660 lines (567 loc) · 30.3 KB
/
AutocompleteTests.cs
File metadata and controls
660 lines (567 loc) · 30.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
// Copyright (c) mudblazor 2021
// License MIT
#pragma warning disable CS1998 // async without await
#pragma warning disable BL0005 // Set parameter outside component
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using AngleSharp.Dom;
using AngleSharp.Html.Dom;
using Bunit;
using FluentAssertions;
using Microsoft.AspNetCore.Components.Web;
using MudBlazor.UnitTests.TestComponents;
using NUnit.Framework;
using static MudBlazor.UnitTests.TestComponents.AutocompleteSetParametersInitialization;
namespace MudBlazor.UnitTests.Components
{
[TestFixture]
public class AutocompleteTests : BunitTest
{
/// <summary>
/// Initial value should be shown and popup should not open.
/// </summary>
[Test]
public async Task AutocompleteTest1()
{
var comp = Context.RenderComponent<AutocompleteTest1>();
Console.WriteLine(comp.Markup);
// select elements needed for the test
var autocompletecomp = comp.FindComponent<MudAutocomplete<string>>();
var autocomplete = autocompletecomp.Instance;
//No popover-open, due it's closed
comp.Markup.Should().NotContain("mud-popover-open");
// check initial state
autocomplete.Value.Should().Be("Alabama");
autocomplete.Text.Should().Be("Alabama");
// now let's type a different state to see the popup open
autocompletecomp.Find("input").Input("Calif");
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().Contain("mud-popover-open"));
Console.WriteLine(comp.Markup);
var items = comp.FindComponents<MudListItem>().ToArray();
items.Length.Should().Be(1);
items.First().Markup.Should().Contain("California");
// click on California!
comp.Find("div.mud-list-item").Click();
// check popover class
comp.Find("div.mud-popover").ClassList.Should().Contain("autocomplete-popover-class");
// check state
comp.WaitForAssertion(() => autocomplete.Value.Should().Be("California"));
autocomplete.Text.Should().Be("California");
}
/// <summary>
/// Popup should open when 3 characters are typed and close when below.
/// </summary>
[Test]
public async Task AutocompleteTest2()
{
var comp = Context.RenderComponent<AutocompleteTest2>();
Console.WriteLine(comp.Markup);
// select elements needed for the test
var select = comp.FindComponent<MudAutocomplete<string>>();
var inputControl = comp.Find("div.mud-input-control");
// check initial state
comp.Markup.Should().NotContain("mud-popover-open");
// click and check if it has toggled the menu
inputControl.Click();
comp.WaitForAssertion(() => comp.Markup.Should().NotContain("mud-popover-open"));
// type 3 characters and check if it has toggled the menu
select.Find("input").Input("ala");
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().Contain("mud-popover-open"));
// type 2 characters and check if it has toggled the menu
select.Find("input").Input("al");
comp.WaitForAssertion(() => comp.Markup.Should().NotContain("mud-popover-open"));
}
/// <summary>
/// Autocomplete should show 'Assam' (using ToStringFunc)
/// </summary>
[Test]
public void AutocompleteTest3()
{
var comp = Context.RenderComponent<AutocompleteTest3>();
Console.WriteLine(comp.Markup);
var autocomplete = comp.FindComponent<MudAutocomplete<AutocompleteTest3.State>>().Instance;
autocomplete.Text.Should().Be("Assam");
}
/// <summary>
/// Autocomplete should show 'Assam' (using state.ToString())
/// </summary>
[Test]
public void AutocompleteTest4()
{
var comp = Context.RenderComponent<AutocompleteTest4>();
Console.WriteLine(comp.Markup);
var autocomplete = comp.FindComponent<MudAutocomplete<AutocompleteTest4.State>>().Instance;
autocomplete.Text.Should().Be("Assam");
}
/// <summary>
/// We search for a value not in list and coercion will go back to the last valid value,
/// discarding the current search text.
/// </summary>
/// <returns></returns>
[Test]
public async Task AutocompleteCoercionTest()
{
var comp = Context.RenderComponent<AutocompleteTest1>();
Console.WriteLine(comp.Markup);
var autocompletecomp = comp.FindComponent<MudAutocomplete<string>>();
var autocomplete = autocompletecomp.Instance;
autocompletecomp.SetParam(x => x.DebounceInterval, 0);
// check initial state
autocomplete.Value.Should().Be("Alabama");
autocomplete.Text.Should().Be("Alabama");
// set a value the search won't find
autocompletecomp.SetParam(a => a.Text, "Austria"); // not part of the U.S.
await comp.InvokeAsync(() => autocomplete.ToggleMenu());
// IsOpen must be true to properly simulate a user clicking outside of the component, which is what the next ToggleMenu call below does.
autocomplete.IsOpen.Should().BeTrue();
// now trigger the coercion by closing the menu
await comp.InvokeAsync(() => autocomplete.ToggleMenu());
autocomplete.Value.Should().Be("Alabama");
autocomplete.Text.Should().Be("Alabama");
}
/// <summary>
/// We search for a value not in list and value coercion will force the invalid value to be applied
/// allowing to validate the user input.
/// </summary>
/// <returns></returns>
[Test]
public async Task AutocompleteCoerceValueTest()
{
var comp = Context.RenderComponent<AutocompleteTest1>();
Console.WriteLine(comp.Markup);
var autocompletecomp = comp.FindComponent<MudAutocomplete<string>>();
var autocomplete = autocompletecomp.Instance;
autocompletecomp.SetParam(x => x.DebounceInterval, 0);
autocompletecomp.SetParam(x => x.CoerceValue, true); // if CoerceValue==true CoerceText will be ignored
// check initial state
autocomplete.Value.Should().Be("Alabama");
autocomplete.Text.Should().Be("Alabama");
// set a value the search won't find
autocompletecomp.SetParam(p => p.Text, "Austria"); // not part of the U.S.
// now trigger the coercion by toggling the the menu (it won't even open for invalid values, but it will coerce)
await comp.InvokeAsync(() => autocomplete.ToggleMenu());
comp.WaitForAssertion(() => autocomplete.Value.Should().Be("Austria"));
autocomplete.Text.Should().Be("Austria");
}
[Test]
public async Task AutocompleteCoercionOffTest()
{
var comp = Context.RenderComponent<AutocompleteTest1>();
Console.WriteLine(comp.Markup);
var autocompletecomp = comp.FindComponent<MudAutocomplete<string>>();
var autocomplete = autocompletecomp.Instance;
autocompletecomp.SetParam(x => x.DebounceInterval, 0);
autocompletecomp.SetParam(x => x.CoerceText, false);
// check initial state
autocomplete.Value.Should().Be("Alabama");
autocomplete.Text.Should().Be("Alabama");
// set a value the search won't find
await comp.InvokeAsync(() => autocomplete.ToggleMenu());
autocompletecomp.SetParam(a => a.Text, "Austria");
// now trigger the coercion by closing the menu
await comp.InvokeAsync(() => autocomplete.ToggleMenu());
autocomplete.Value.Should().Be("Alabama");
autocomplete.Text.Should().Be("Austria");
}
[Test]
public async Task Autocomplete_Should_TolerateNullFromSearchFunc()
{
var comp = Context.RenderComponent<MudAutocomplete<string>>((a) =>
{
a.Add(x => x.DebounceInterval, 0);
a.Add(x => x.SearchFunc, new Func<string, Task<IEnumerable<string>>>(async s => null)); // <--- searchfunc returns null instead of sequence
});
// enter a text so the search func will return null, and it shouldn't throw an exception
comp.SetParam(a => a.Text, "Do not throw");
comp.SetParam(x => x.SearchFunc, new Func<string, Task<IEnumerable<string>>>(s => null)); // <-- search func returns null instead of task!
comp.SetParam(a => a.Text, "Don't throw here neither");
}
[Test]
public async Task Autocomplete_ReadOnly_Should_Not_Open()
{
var comp = Context.RenderComponent<AutocompleteTest5>();
Console.WriteLine(comp.Markup);
comp.FindAll("div.mud-input-adornment")[0].Click();
Console.WriteLine(comp.Markup);
comp.WaitForAssertion(() => comp.FindAll("div.mud-popover-open").Count.Should().Be(0));
}
/// <summary>
/// After press Enter key down, the selected value should be shown in the input value
/// </summary>
[Test]
public async Task Autocomplete_after_Enter_Should_show_Selected_Value()
{
var comp = Context.RenderComponent<AutocompleteTest1>();
Console.WriteLine(comp.Markup);
// select elements needed for the test
var autocompletecomp = comp.FindComponent<MudAutocomplete<string>>();
var autocomplete = autocompletecomp.Instance;
//insert "Calif"
autocompletecomp.Find("input").Input("Calif");
await Task.Delay(100);
var args = new KeyboardEventArgs();
args.Key = "Enter";
//press Enter key
autocompletecomp.Find("input").KeyUp(args);
//The value of the input should be California
comp.WaitForAssertion(() => autocompletecomp.Find("input").GetAttribute("value").Should().Be("California"));
//and the autocomplete it's closed
autocomplete.IsOpen.Should().BeFalse();
}
/// <summary>
/// Based on this try https://try.mudblazor.com/snippet/GacPunvDUyjdUJAh
/// and this issue https://github.com/MudBlazor/MudBlazor/issues/1235
/// </summary>
[Test]
public async Task Autocomplete_Initialize_Value_on_SetParametersAsync()
{
var comp = Context.RenderComponent<AutocompleteSetParametersInitialization>();
Console.WriteLine(comp.Markup);
// select elements needed for the test
await Task.Delay(100);
var autocompletecomp = comp.FindComponent<MudAutocomplete<ExternalList>>();
var input = autocompletecomp.Find("input");
var wrappedElement = ((dynamic)input).WrappedElement;
var value = ((IHtmlInputElement)wrappedElement).Value;
//The value of the input should be California
value.Should().Be("One");
}
/// <summary>
/// Test for <seealso cref="https://github.com/MudBlazor/MudBlazor/issues/1415"/>
/// </summary>
[Test]
public async Task Autocomplete_OnBlurShouldBeCalled()
{
var calls = 0;
Action<FocusEventArgs> fn = (args) => calls++;
var comp = Context.RenderComponent<MudAutocomplete<string>>((a) =>
{
a.Add(x => x.OnBlur, fn);
});
var input = comp.Find("input");
calls.Should().Be(0);
input.Blur();
calls.Should().Be(1);
}
[Test]
public async Task AutoCompleteClearableTest()
{
var comp = Context.RenderComponent<AutocompleteTestClearable>();
// No button when initialized empty
comp.WaitForAssertion(() => comp.FindAll("button").Should().BeEmpty());
// Button shows after entering text
comp.Find("input").Input("text");
comp.WaitForAssertion(() => comp.Find("button").Should().NotBeNull());
// Text cleared and button removed after clicking clear button
comp.Find("button").Click();
comp.WaitForAssertion(() => comp.FindAll("button").Should().BeEmpty());
// Button shows again after entering text
comp.Find("input").Input("text");
comp.WaitForAssertion(() => comp.Find("button").Should().NotBeNull());
// Button removed after clearing text
comp.Find("input").Input(string.Empty);
comp.WaitForAssertion(() => comp.FindAll("button").Should().BeEmpty());
}
[Test]
public async Task Autocomplete_Should_Validate_Data_Attribute_Fail()
{
var comp = Context.RenderComponent<AutocompleteValidationDataAttrTest>();
Console.WriteLine(comp.Markup);
var autocompletecomp = comp.FindComponent<MudAutocomplete<string>>();
var autocomplete = autocompletecomp.Instance;
await comp.InvokeAsync(() => autocomplete.DebounceInterval = 0);
// Set invalid option
await comp.InvokeAsync(() => autocomplete.SelectOption("Quux"));
// check initial state
autocomplete.Value.Should().Be("Quux");
autocomplete.Text.Should().Be("Quux");
// check validity
await comp.InvokeAsync(() => autocomplete.Validate());
autocomplete.ValidationErrors.Should().NotBeEmpty();
autocomplete.ValidationErrors.Should().HaveCount(1);
autocomplete.ValidationErrors[0].Should().Equals("Should not be longer than 3");
}
[Test]
public async Task Autocomplete_Should_Validate_Data_Attribute_Success()
{
var comp = Context.RenderComponent<AutocompleteValidationDataAttrTest>();
Console.WriteLine(comp.Markup);
var autocompletecomp = comp.FindComponent<MudAutocomplete<string>>();
var autocomplete = autocompletecomp.Instance;
await comp.InvokeAsync(() => autocomplete.DebounceInterval = 0);
// Set valid option
await comp.InvokeAsync(() => autocomplete.SelectOption("Qux"));
// check initial state
autocomplete.Value.Should().Be("Qux");
autocomplete.Text.Should().Be("Qux");
// check validity
await comp.InvokeAsync(() => autocomplete.Validate());
autocomplete.ValidationErrors.Should().BeEmpty();
}
/// <summary>
/// Tests the required property.
/// </summary>
[Test]
public async Task Autocomplete_Should_SetRequiredTrue()
{
var comp = Context.RenderComponent<AutocompleteRequiredTest>();
var autocomplete = comp.FindComponent<MudAutocomplete<string>>().Instance;
autocomplete.Required.Should().BeTrue();
await comp.InvokeAsync(() => autocomplete.Validate());
autocomplete.ValidationErrors.First().Should().Be("Required");
}
/// <summary>
/// Test for <seealso cref="https://github.com/MudBlazor/MudBlazor/issues/1761"/>
/// </summary>
[Test]
public async Task Autocomplete_Should_Close_OnTab()
{
var comp = Context.RenderComponent<AutocompleteTest1>();
Console.WriteLine(comp.Markup);
// select elements needed for the test
var autocompletecomp = comp.FindComponent<MudAutocomplete<string>>();
var autocomplete = autocompletecomp.Instance;
// Should be closed
comp.WaitForAssertion(() => autocomplete.IsOpen.Should().BeFalse());
// Lets type something to cause it to open
autocompletecomp.Find("input").Input("Calif");
comp.WaitForAssertion(() => autocomplete.IsOpen.Should().BeTrue());
// Lets call blur on the input and confirm that it closed
autocompletecomp.Find("input").KeyDown(new KeyboardEventArgs() { Key = "Tab" });
comp.WaitForAssertion(() => autocomplete.IsOpen.Should().BeFalse());
// Tab closes the drop-down and does not select the selected value (California)
// because SelectValueOnTab is false by default
autocomplete.Value.Should().Be("Alabama");
}
[Test]
public async Task Autocomplete_Should_SelectValue_On_Tab_With_SelectValueOnTab()
{
var comp = Context.RenderComponent<AutocompleteTest1>();
Console.WriteLine(comp.Markup);
// select elements needed for the test
var autocompletecomp = comp.FindComponent<MudAutocomplete<string>>();
autocompletecomp.SetParam(x => x.SelectValueOnTab, true);
var autocomplete = autocompletecomp.Instance;
// Should be closed
comp.WaitForAssertion(() => autocomplete.IsOpen.Should().BeFalse());
// Lets type something to cause it to open
autocompletecomp.Find("input").Input("Calif");
comp.WaitForAssertion(() => autocomplete.IsOpen.Should().BeTrue());
// Lets call blur on the input and confirm that it closed
autocompletecomp.Find("input").KeyDown(new KeyboardEventArgs() { Key = "Tab" });
comp.WaitForAssertion(() => autocomplete.IsOpen.Should().BeFalse());
// Tab closes the drop-down and selects the selected value (California)
// because SelectValueOnTab is true
autocomplete.Value.Should().Be("California");
}
/// <summary>
/// When selecting a value by clicking on it in the list the input will blur. However, this
/// must not cause the dropdown to close or else the click on the item will not be possible!
///
/// If this test fails it means the dropdown has closed before we can even click any value in the list.
/// Such a regression happened and caused PR #1807 to be reverted
/// </summary>
[Test]
public async Task Autocomplete_Should_NotCloseDropdownOnInputBlur()
{
var comp = Context.RenderComponent<AutocompleteTest1>();
Console.WriteLine(comp.Markup);
// select elements needed for the test
var autocompletecomp = comp.FindComponent<MudAutocomplete<string>>();
var autocomplete = autocompletecomp.Instance;
//No popover-open, due it's closed
comp.Markup.Should().NotContain("mud-popover-open");
// check initial state
autocomplete.Value.Should().Be("Alabama");
autocomplete.Text.Should().Be("Alabama");
// now let's type a different state to see the popup open
autocompletecomp.Find("input").Input("Calif");
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().Contain("mud-popover-open"));
var items = comp.FindComponents<MudListItem>().ToArray();
items.Length.Should().Be(1);
items.First().Markup.Should().Contain("California");
// now, we blur the input and assert that the popover is still open.
autocompletecomp.Find("input").Blur();
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().Contain("mud-popover-open"));
}
/// <summary>
/// When calling Clear(), menu should closed, Value and Text should be cleared.
/// </summary>
[Test]
public async Task Autocomplete_CheckTextValueandOpenState_OnClear()
{
var comp = Context.RenderComponent<AutocompleteTest1>();
Console.WriteLine(comp.Markup);
// select elements needed for the test
var autocompletecomp = comp.FindComponent<MudAutocomplete<string>>();
autocompletecomp.SetParam(x => x.CoerceValue, true);
var autocomplete = autocompletecomp.Instance;
//No popover-open, due it's closed
comp.Markup.Should().NotContain("mud-popover-open");
// check initial state
autocomplete.Value.Should().Be("Alabama");
autocomplete.Text.Should().Be("Alabama");
// ToggleMenu to open menu and Clear to close it and check the text and value
await comp.InvokeAsync(() => autocomplete.ToggleMenu());
await comp.InvokeAsync(() => autocomplete.Clear().Wait());
comp.Markup.Should().NotContain("mud-popover-open");
autocomplete.Value.Should().Be("");
autocomplete.Text.Should().Be("");
// now let's type a different state
autocompletecomp.Find("input").Input("Calif");
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().Contain("mud-popover-open"));
Console.WriteLine(comp.Markup);
var items = comp.FindComponents<MudListItem>().ToArray();
items.Length.Should().Be(1);
items.First().Markup.Should().Contain("California");
// Clearing it and check the close status text and value again
await comp.InvokeAsync(() => autocomplete.Clear().Wait());
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().NotContain("mud-popover-open"));
autocomplete.Value.Should().Be("");
autocomplete.Text.Should().Be("");
}
/// <summary>
/// When calling Reset(), menu should closed, Value and Text should be null.
/// </summary>
[Test]
public async Task Autocomplete_CheckTextAndValue_OnReset()
{
var comp = Context.RenderComponent<AutocompleteTest1>();
Console.WriteLine(comp.Markup);
// select elements needed for the test
var autocompletecomp = comp.FindComponent<MudAutocomplete<string>>();
autocompletecomp.SetParam(x => x.CoerceValue, true);
var autocomplete = autocompletecomp.Instance;
//No popover-open, due it's closed
comp.Markup.Should().NotContain("mud-popover-open");
// check initial state
autocomplete.Value.Should().Be("Alabama");
autocomplete.Text.Should().Be("Alabama");
// Reset it
await comp.InvokeAsync(() => autocomplete.ToggleMenu());
await comp.InvokeAsync(() => autocomplete.Reset());
comp.Markup.Should().NotContain("mud-popover-open");
autocomplete.Value.Should().Be(null);
autocomplete.Text.Should().Be(null);
// now let's type a different state
autocompletecomp.Find("input").Input("Calif");
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().Contain("mud-popover-open"));
Console.WriteLine(comp.Markup);
var items = comp.FindComponents<MudListItem>().ToArray();
items.Length.Should().Be(1);
items.First().Markup.Should().Contain("California");
// Reseting it should close popover and set Text and Value to null again
await comp.InvokeAsync(() => autocomplete.Reset());
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().NotContain("mud-popover-open"));
autocomplete.Value.Should().Be(null);
autocomplete.Text.Should().Be(null);
}
[Test]
public async Task Autocomplete_Should_Not_Select_Disabled_Item()
{
// define some constant values
var alabamaString = "Alabama";
var alaskaString = "Alaska";
var americanSamoaString = "American Samoa";
var arkansasString = "Arkansas";
var listItemQuerySelector = "div.mud-list-item";
var selectedItemClassName = "mud-selected-item";
var selectedItemIndexPropertyInfo = typeof(MudAutocomplete<string>).GetField("_selectedListItemIndex", BindingFlags.NonPublic | BindingFlags.Instance) ?? throw new ArgumentException("Cannot find field named '_selectedListItemIndex' on type 'MudAutocomplete<T>'");
var onInputKeyUpMemberInfo = typeof(MudAutocomplete<string>).GetMethod("OnInputKeyUp", BindingFlags.Instance | BindingFlags.NonPublic) ?? throw new ArgumentException("Cannot find method named 'OnInputKeyUp' on type 'MudAutocomplete<T>'");
// create the component
var component = Context.RenderComponent<AutocompleteDisabledItemsTest>();
// get the elements needed for the test
var autocompleteComponent = component.FindComponent<MudAutocomplete<string>>();
// get the instance
var autocompleteInstance = autocompleteComponent.Instance;
// click to open the popup
autocompleteComponent.Find(TagNames.Input).Click();
// ensure popup is open
component.WaitForAssertion(() => autocompleteInstance.IsOpen.Should().BeTrue("Input has been focused and should open the popup"));
// get the matching states
var matchingStates = component.FindComponents<MudListItem>().ToArray();
// try clicking 'American Samoa'
matchingStates.Single(s => s.Markup.Contains(americanSamoaString)).Find(listItemQuerySelector).Click();
component.WaitForAssertion(() => autocompleteInstance.Value.Should().BeNullOrEmpty($"{americanSamoaString} should not be clickable."));
// try clicking 'Alaska'
matchingStates.Single(s => s.Markup.Contains(alaskaString)).Find(listItemQuerySelector).Click();
component.WaitForAssertion(() => autocompleteInstance.Value.Should().Be(alaskaString));
// reset search-string
autocompleteComponent.Find(TagNames.Input).Input(string.Empty);
// wait till popup is visible
component.WaitForAssertion(() => autocompleteInstance.IsOpen.Should().BeTrue());
// update found elements
matchingStates = component.FindComponents<MudListItem>().ToArray();
// ensure alabama is selected
component.WaitForAssertion(() => matchingStates.Single(s => s.Markup.Contains(alabamaString)).Find(listItemQuerySelector).ClassList.Should().Contain(selectedItemClassName, $"{alabamaString} should be selected/highlighted"));
// define the event-args for arrow-down
var arrowDownKeyboardEventArgs = new KeyboardEventArgs { Key = Key.Down.Value, Type = "keyup" };
// invoke directly (but twice)
onInputKeyUpMemberInfo.Invoke(autocompleteInstance, new[] { arrowDownKeyboardEventArgs });
onInputKeyUpMemberInfo.Invoke(autocompleteInstance, new[] { arrowDownKeyboardEventArgs });
// ensure that index '4' is selected
component.WaitForAssertion(() => selectedItemIndexPropertyInfo.GetValue(autocompleteInstance).Should().Be(4));
// select the highlighted value
component.Find(TagNames.Input).KeyUp(Key.Enter);
// Arkansas should be selected value
autocompleteInstance.Value.Should().Be(arkansasString);
}
/// <summary>
/// When changing the bound value, ensure the new value is displayed
/// </summary>
/// <returns></returns>
[Test]
public async Task Autocomplete_ChangeBoundValue()
{
var comp = Context.RenderComponent<AutocompleteChangeBoundObjectTest>();
Console.WriteLine(comp.Markup);
var autocompletecomp = comp.FindComponent<MudAutocomplete<string>>();
var autocomplete = autocompletecomp.Instance;
autocompletecomp.SetParametersAndRender(parameters => parameters.Add(p=> p.DebounceInterval, 0));
autocompletecomp.SetParametersAndRender(parameters => parameters.Add(p => p.CoerceText, true));
// check initial state
autocomplete.Value.Should().Be("Florida");
autocomplete.Text.Should().Be("Florida");
comp.WaitForAssertion(() => autocompletecomp.Find("input").GetAttribute("value").Should().Be("Florida"));//redundant?
//Get the button to toggle the value
var buttonElement = comp.Find("button");
await comp.InvokeAsync(() => buttonElement.Click());
autocomplete.Value.Should().Be("Georgia");
autocomplete.Text.Should().Be("Georgia");
comp.WaitForAssertion(() => autocompletecomp.Find("input").GetAttribute("value").Should().Be("Georgia"));//redundant?
//Change the value of the current bound value component
//insert "Calif"
autocompletecomp.Find("input").Input("Alabam");
await Task.Delay(100);
var args = new KeyboardEventArgs();
args.Key = "Enter";
//press Enter key
autocompletecomp.Find("input").KeyUp(args);
//The value of the input should be California
comp.WaitForAssertion(() => autocompletecomp.Find("input").GetAttribute("value").Should().Be("Alabama"));
autocomplete.Value.Should().Be("Alabama");
autocomplete.Text.Should().Be("Alabama");
//Change the bound object
var markup1 = autocompletecomp.Markup;
await comp.InvokeAsync(()=> buttonElement.Click());
//await comp.InvokeAsync(() => comp.Render());
autocomplete.Value.Should().Be("Florida"); //pass
autocomplete.Text.Should().Be("Florida"); //fail
comp.WaitForAssertion(() => autocompletecomp.Find("input").GetAttribute("value").Should().Be("Florida"));//breaks here:
var markup2 = autocompletecomp.Markup;
autocomplete.Value.Should().Be("Florida");
//autocomplete.Text.Should().Be("Florida");
//Change the bound object back and check again.
//await buttonComp.InvokeAsync(() => button.OnClick.InvokeAsync());
await comp.InvokeAsync(() => buttonElement.Click());
var t1 = autocompletecomp.Find("input").GetAttribute("value");
var t2 = autocompletecomp.Find("input").GetAttribute("text");
autocomplete.Value.Should().Be("Alabama");
autocomplete.Text.Should().Be("Alabama");
}
}
}