forked from CodeBeamOrg/CodeBeam.MudBlazor.Extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComboboxTests.cs
More file actions
469 lines (403 loc) · 29.2 KB
/
ComboboxTests.cs
File metadata and controls
469 lines (403 loc) · 29.2 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
#pragma warning disable CS1998 // async without await
#pragma warning disable IDE1006 // leading underscore
#pragma warning disable BL0005 // Set parameter outside component
using Bunit;
using MudExtensions.UnitTests.TestComponents;
using AwesomeAssertions;
using Microsoft.AspNetCore.Components.Web;
using MudBlazor;
using MudExtensions.UnitTests.Extensions;
using MudBlazor.Extensions;
namespace MudExtensions.UnitTests.Components
{
[TestFixture]
public class ComboBoxTests : BunitTest
{
// Note: MudSelect doesn't guaranteed the consequences of changing Value if MultiSelection is true for now.
// When this feature will add, just uncomment the testcase to test it. No need to write new test.
[Test]
[TestCase(false)]
//[TestCase(true)]
public void ComboBox_InitialValueTest(bool multiSelection)
{
var comp = Context.Render<ComboBoxInitialValueTest>();
comp.SetParametersAndRenderAsync(p => p.Add(x => x.SelectedValue, "1"));
comp.SetParametersAndRenderAsync(p => p.Add(x => x.MultiSelection, multiSelection));
var combobox = comp.FindComponent<MudComboBox<string?>>();
combobox.Instance.GetState(x => x.Value).Should().Be("1");
//combobox.Instance.SelectedValues.Should().BeEquivalentTo(new HashSet<string?> { "1" }); // TODO: Fix behavior
combobox.Instance.GetState(x => x.Text).Should().Be("1");
}
// Note: MudSelect doesn't guaranteed the consequences of changing SelectedValues if MultiSelection is false for now.
// When this feature will add, just uncomment the testcase to test it. No need to write new test.
[Test]
//[TestCase(false)]
[TestCase(true)]
public void ComboBox_InitialValuesTest(bool multiSelection)
{
var comp = Context.Render<ComboBoxInitialValueTest>(x =>
{
x.Add(c => c.SelectedValues, new HashSet<string>() { "1" });
x.Add(c => c.MultiSelection, multiSelection);
});
var combobox = comp.FindComponent<MudComboBox<string>>();
combobox.Instance.GetState(x => x.Value).Should().Be("1");
combobox.Instance.SelectedValues.Should().BeEquivalentTo(new HashSet<string>() { "1" });
combobox.Instance.GetState(x => x.Text).Should().Be("1");
}
[Test]
public void ComboBox_ValueBubblingTest_MultiSelection()
{
var comp = Context.Render<ComboBoxInitialValueTest>(x =>
{
x.Add(c => c.MultiSelection, true);
});
var combobox = comp.FindComponent<MudComboBox<string>>();
combobox.Instance.GetState(x => x.Value).Should().BeNull();
combobox.Instance.GetState(x => x.Text).Should().BeNullOrEmpty();
comp.Render(p => p.Add(x => x.SelectedValues, new HashSet<string>() { "1" }));
combobox.Instance.GetState(x => x.Value).Should().Be(null);
combobox.Instance.SelectedValues.Should().BeEquivalentTo(new HashSet<string>() { "1" });
combobox.Instance.GetState(x => x.Text).Should().Be(null);
comp.Render(p => p.Add(x => x.SelectedValues, new HashSet<string>() { "2", "1" }));
combobox.Instance.GetState(x => x.Value).Should().Be(null);
combobox.Instance.SelectedValues.Should().BeEquivalentTo(new HashSet<string>() { "2", "1" });
combobox.Instance.GetState(x => x.Text).Should().Be(null);
}
[Test]
public async Task ComboBox_ValueChangeEventCountTest_MultiSelection()
{
var comp = Context.Render<ComboBoxEventCountTest>(x =>
{
x.Add(c => c.MultiSelection, true);
});
var combobox = comp.FindComponent<MudComboBox<string>>();
comp.Instance.ValueChangeCount.Should().Be(0);
comp.Instance.ValuesChangeCount.Should().Be(0);
await comp.InvokeAsync(() => combobox.Render(p => p.Add(x => x.SelectedValues, new HashSet<string>() { "1" })));
comp.WaitForAssertion(() => comp.Instance.ValueChangeCount.Should().Be(0));
comp.WaitForAssertion(() => comp.Instance.ValuesChangeCount.Should().Be(1));
// Setting same value should not fire events
await comp.InvokeAsync(() => combobox.Render(p => p.Add(x => x.SelectedValues, new HashSet<string>() { "1" })));
comp.WaitForAssertion(() => comp.Instance.ValueChangeCount.Should().Be(0));
comp.Instance.ValuesChangeCount.Should().Be(1);
}
/// <summary>
/// Click should open the Menu and selecting a value should update the bindable value.
/// </summary>
[Test]
public async Task ComboBoxTest1()
{
var comp = Context.Render<ComboBoxTest1>();
// print the generated html
//Console.WriteLine(comp.Markup);
// select elements needed for the test
var combobox = comp.FindComponent<MudComboBox<string>>();
var menu = comp.Find("div.mud-popover");
var input = comp.Find("div.mud-input-control");
// check popover class
menu.ClassList.Should().Contain("combobox-popover-class");
// check initial state
combobox.Instance.GetState(x => x.Value).Should().BeNullOrEmpty();
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().Contain("d-none"));
// click and check if it has toggled the menu
input.Click();
menu.ClassList.Should().NotContain("d-none");
// now click an item and see the value change
comp.WaitForAssertion(() => comp.FindAll("div.mud-combobox-item").Count.Should().BeGreaterThan(0));
var items = comp.FindAll("div.mud-combobox-item").ToArray();
items[1].Click();
// menu should be closed now
comp.WaitForAssertion(() => menu.ClassList.Should().Contain("d-none"));
combobox.Instance.GetState(x => x.Value).Should().Be("2");
// now we cheat and click the list without opening the menu ;)
input.Click();
comp.WaitForAssertion(() => comp.FindAll("div.mud-combobox-item").Count.Should().BeGreaterThan(0));
items = comp.FindAll("div.mud-combobox-item").ToArray();
items[0].Click();
comp.WaitForAssertion(() => combobox.Instance.GetState(x => x.Value).Should().Be("1"));
//Check user on blur implementation works
var @switch = comp.FindComponent<MudSwitch<bool>>();
@switch.Instance.Value = true;
await comp.InvokeAsync(() => combobox.Instance.HandleOnBlur(new FocusEventArgs()));
comp.WaitForAssertion(() => @switch.Instance.GetState(x => x.Value).Should().Be(false));
}
[Test]
public async Task ComboBoxClearableTest()
{
var comp = Context.Render<ComboBoxClearableTest>();
var combobox = comp.FindComponent<MudComboBox<string>>();
var input = comp.Find("div.mud-input-control");
// No button when initialized
comp.FindAll("button").Should().BeEmpty();
input.Click();
comp.WaitForAssertion(() => comp.FindAll("div.mud-combobox-item").Count.Should().BeGreaterThan(0));
// Button shows after selecting item
var items = comp.FindAll("div.mud-combobox-item").ToArray();
items[1].Click();
//comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().NotContain("mud-popover-open"));
comp.WaitForAssertion(() => combobox.Instance.GetState(x => x.Value).Should().Be("2"));
comp.Find("button").Should().NotBeNull();
// Selection cleared and button removed after clicking clear button
comp.Find("button").MouseDown();
comp.WaitForAssertion(() => combobox.Instance.GetState(x => x.Value).Should().BeNullOrEmpty());
comp.FindAll("button").Should().BeEmpty();
// Clear button click handler should have been invoked
comp.Instance.ClearButtonClicked.Should().BeTrue();
}
[Test]
public void MultiSelect_SelectAll()
{
var comp = Context.Render<ComboBoxMultiSelectTest2>();
// select element needed for the test
var combobox = comp.FindComponent<MudComboBox<string>>();
var menu = comp.Find("div.mud-popover");
var input = combobox.Find("div.mud-input-control");
// Open the menu
input.Click();
comp.WaitForAssertion(() => menu.ClassList.Should().Contain("mud-popover-open"));
comp.FindAll("div.mud-combobox-item")[0].Click();
// validate the result. all items should be selected
comp.WaitForAssertion(() => combobox.Instance.GetPresenterText().Should().Be("FirstA^SecondA^ThirdA"));
combobox.Instance.SelectedValues.Should().BeEquivalentTo(new HashSet<string>() { "FirstA", "SecondA", "ThirdA" });
comp.FindAll("div.mud-combobox-item")[0].Click();
comp.WaitForAssertion(() => combobox.Instance.GetPresenterText().Should().Be(""));
combobox.Instance.SelectedValues.Should().BeEquivalentTo(new HashSet<string>());
}
[Test]
public async Task ComboBox_MultiSelectEditable()
{
var comp = Context.Render<ComboBoxMultiSelectEditableTest>();
// print the generated html
Console.WriteLine(comp.Markup);
// select elements needed for the test
var combobox = comp.FindComponent<MudComboBox<string>>();
var menu = comp.Find("div.mud-popover");
var input = combobox.Find("div.mud-input-control");
// check initial state
combobox.Instance.GetState(x => x.Value).Should().BeNullOrEmpty();
comp.WaitForAssertion(() =>
comp.Find("div.mud-popover").ClassList.Should().Contain("d-none"));
// click and check if it has toggled the menu
await comp.InvokeAsync(() => input.Click());
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().NotContain("d-none"));
// now click an item and see the value change
comp.WaitForAssertion(() => comp.FindAll("div.mud-combobox-item").Count.Should().Be(4));
comp.WaitForAssertion(() => combobox.Instance.GetEnabledAndEligibleItems().Count.Should().Be(4));
var items = comp.FindAll("div.mud-combobox-item").ToArray();
await comp.InvokeAsync(() => combobox.Instance.HandleInternalValueChanged("t"));
await comp.InvokeAsync(() => combobox.Instance.ForceRenderItems());
items = comp.FindAll("div.mud-combobox-item").ToArray();
comp.WaitForAssertion(() => combobox.Instance.GetEnabledAndEligibleItems().Count.Should().Be(2));
}
/// <summary>
/// Click should not close the menu and selecting multiple values should update the bindable value with a comma separated list.
/// </summary>
[Test]
public async Task ComboBox_MultiSelectTest1()
{
var comp = Context.Render<ComboBoxMultiSelectTest1>();
// print the generated html
Console.WriteLine(comp.Markup);
// select elements needed for the test
var combobox = comp.FindComponent<MudComboBox<string>>();
var menu = comp.Find("div.mud-popover");
var input = combobox.Find("div.mud-input-control");
// check initial state
combobox.Instance.GetState(x => x.Value).Should().BeNullOrEmpty();
comp.WaitForAssertion(() =>
comp.Find("div.mud-popover").ClassList.Should().Contain("d-none"));
// click and check if it has toggled the menu
await comp.InvokeAsync(() => input.Click());
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().NotContain("d-none"));
// now click an item and see the value change
comp.WaitForAssertion(() => comp.FindAll("div.mud-combobox-item").Count.Should().BeGreaterThan(0));
var items = comp.FindAll("div.mud-combobox-item").ToArray();
items[1].Click();
// menu should still be open now!!
comp.Find("div.mud-popover").ClassList.Should().NotContain("d-none");
comp.WaitForAssertion(() => combobox.Instance.GetPresenterText().Should().Be("2"));
items[0].Click();
comp.WaitForAssertion(() => combobox.Instance.GetPresenterText().Should().Be("2, 1"));
items[2].Click();
comp.WaitForAssertion(() => combobox.Instance.GetPresenterText().Should().Be("2, 1, 3"));
items[0].Click();
comp.WaitForAssertion(() => combobox.Instance.GetPresenterText().Should().Be("2, 3"));
combobox.Instance.SelectedValues?.Count().Should().Be(2);
combobox.Instance.SelectedValues.Should().Contain("2");
combobox.Instance.SelectedValues.Should().Contain("3");
//Console.WriteLine(comp.Markup);
const string @unchecked =
"M19 5v14H5V5h14m0-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2z";
const string @checked =
"M19 3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.11 0 2-.9 2-2V5c0-1.1-.89-2-2-2zm-9 14l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z";
// check that the correct items are checked
comp.WaitForAssertion(() =>
comp.FindAll("div.mud-combobox-item path")[1]?.Attributes["d"]?.Value.Should().Be(@unchecked));
comp.FindAll("div.mud-combobox-item path")[3]?.Attributes["d"]?.Value.Should().Be(@checked);
comp.FindAll("div.mud-combobox-item path")[5]?.Attributes["d"]?.Value.Should().Be(@checked);
// now check how setting the SelectedValues makes items checked or unchecked
// Note: If popover is open, selecting values programmatically doesn't work for now.
await comp.InvokeAsync(() => combobox.Instance.CloseMenu());
await comp.InvokeAsync(() =>
{
combobox.Instance.SelectedValues = new HashSet<string>() { "1", "2" };
});
await comp.InvokeAsync(() => input.Click());
comp.WaitForAssertion(() => comp.FindAll("div.mud-combobox-item path")[1]?.Attributes["d"]?.Value.Should().Be(@checked));
comp.FindAll("div.mud-combobox-item path")[3]?.Attributes["d"]?.Value.Should().Be(@checked);
combobox.Instance.SelectedValues.Should().NotContain("3");
comp.WaitForAssertion(() => comp.FindAll("div.mud-combobox-item path")[5]?.Attributes["d"]?.Value.Should().Be(@unchecked));
//Console.WriteLine(comp.Markup);
}
[Test]
public async Task ComboBoxTest_KeyboardNavigation_SingleSelect()
{
var comp = Context.Render<ComboBoxTest1>();
// print the generated html
//Console.WriteLine(comp.Markup);
// select elements needed for the test
var combobox = comp.FindComponent<MudComboBox<string>>().Instance;
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
comp.Render();
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().NotContain("d-none"));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Escape", Type = "keydown", }));
comp.Render();
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().Contain("d-none"));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = " ", Type = "keydown", }));
comp.Render();
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().NotContain("d-none"));
//If we didn't select an item with mouse or arrow keys yet, value should remains null.
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = " ", Type = "keydown", }));
comp.Render();
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().Contain("d-none"));
comp.WaitForAssertion(() => combobox.GetState(x => x.Value).Should().Be(null));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "ArrowDown", AltKey = true, Type = "keydown", }));
comp.Render();
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().NotContain("d-none"));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "ArrowUp", AltKey = true, Type = "keydown", }));
comp.Render();
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().Contain("d-none"));
//If dropdown is closed, arrow key should not set a value.
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "ArrowDown", Type = "keydown", }));
comp.Render();
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().NotContain("d-none"));
comp.WaitForAssertion(() => combobox.GetState(x => x.Value).Should().Be(null));
// If no item is hiligted, enter should only close popover, not select any item and value
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Escape", Type = "keydown", }));
comp.Render();
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().Contain("d-none"));
comp.WaitForAssertion(() => combobox.GetState(x => x.Value).Should().Be(null));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
comp.Render();
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().NotContain("d-none"));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "ArrowDown", Type = "keydown", }));
comp.WaitForAssertion(() => combobox.GetState(x => x.Value).Should().BeNull());
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "ArrowDown", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
comp.WaitForAssertion(() => combobox.GetState(x => x.Value).Should().Be("3"));
//End key should not select the last disabled item
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "End", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
comp.WaitForAssertion(() => combobox.GetState(x => x.Value).Should().Be("3"));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().Contain("mud-popover-open"));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "ArrowDown", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
comp.Render();
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().Contain("d-none"));
comp.WaitForAssertion(() => combobox.GetState(x => x.Value).Should().Be("1"));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "ArrowDown", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
comp.WaitForAssertion(() => combobox.GetState(x => x.Value).Should().Be("2"));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Home", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
comp.WaitForAssertion(() => combobox.GetState(x => x.Value).Should().Be("1"));
//Arrow up should select still the first item
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "ArrowUp", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
comp.WaitForAssertion(() => combobox.GetState(x => x.Value).Should().Be("3"));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "End", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "ArrowDown", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
comp.WaitForAssertion(() => combobox.GetState(x => x.Value).Should().Be("1"));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "2", Type = "keydown", }));
comp.WaitForAssertion(() => combobox.GetState(x => x.Value).Should().Be("1"));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
comp.WaitForAssertion(() => combobox.GetState(x => x.Value).Should().Be("2"));
}
[Test]
public async Task ComboBoxTest_KeyboardNavigation_ToggleSelect()
{
var comp = Context.Render<ComboBoxTest1>();
var combobox = comp.FindComponent<MudComboBox<string>>().Instance;
combobox.ToggleSelection = true;
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
comp.Render();
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().NotContain("d-none"));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "ArrowDown", Type = "keydown", }));
comp.WaitForAssertion(() => combobox.GetState(x => x.Value).Should().BeNull());
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "ArrowDown", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
comp.WaitForAssertion(() => combobox.GetState(x => x.Value).Should().Be("3"));
//End key should not select the last disabled item
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
comp.WaitForAssertion(() => combobox.GetState(x => x.Value).Should().BeNull());
}
[Test]
public async Task ComboBoxTest_KeyboardNavigation_MultiSelect()
{
var comp = Context.Render<ComboBoxMultiSelectTest3>();
var combobox = comp.FindComponent<MudComboBox<string>>();
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().Contain("d-none"));
await comp.InvokeAsync(() => combobox.Instance.HandleKeyDownAsync(new KeyboardEventArgs() { Key = " ", Type = "keydown", }));
comp.Render();
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().NotContain("d-none"));
await comp.InvokeAsync(() => combobox.Instance.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "a", CtrlKey = true, Type = "keydown", }));
comp.WaitForAssertion(() => combobox.Instance.GetPresenterText().Should().Be("7 felines have been selected"));
await comp.InvokeAsync(() => combobox.Instance.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "A", CtrlKey = true, Type = "keydown", }));
comp.WaitForAssertion(() => combobox.Instance.GetPresenterText().Should().BeNull());
await comp.InvokeAsync(() => combobox.Instance.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "ArrowDown", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.Instance.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
comp.WaitForAssertion(() => combobox.Instance.GetPresenterText().Should().Be("1 feline has been selected"));
await comp.InvokeAsync(() => combobox.Instance.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "A", CtrlKey = true, Type = "keydown", }));
comp.WaitForAssertion(() => combobox.Instance.GetPresenterText().Should().Be("7 felines have been selected"));
await comp.InvokeAsync(() => combobox.Instance.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Escape", Type = "keydown", }));
comp.Render();
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().Contain("d-none"));
await comp.InvokeAsync(() => combobox.Instance.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
comp.Render();
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().NotContain("d-none"));
await comp.InvokeAsync(() => combobox.Instance.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "ArrowDown", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.Instance.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
comp.WaitForAssertion(() => combobox.Instance.SelectedValues.Should().NotContain("Jaguar"));
await comp.InvokeAsync(() => combobox.Instance.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Home", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.Instance.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "NumpadEnter", Type = "keydown", }));
comp.WaitForAssertion(() => combobox.Instance.SelectedValues.Should().Contain("Jaguar"));
await comp.InvokeAsync(() => combobox.Instance.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "ArrowDown", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.Instance.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
comp.WaitForAssertion(() => combobox.Instance.SelectedValues.Should().NotContain("Leopard"));
await comp.InvokeAsync(() => combobox.Instance.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "End", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.Instance.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
comp.WaitForAssertion(() => combobox.Instance.SelectedValues.Should().NotContain("Tiger"));
await comp.InvokeAsync(() => combobox.Render(p => p.Add(x => x.Disabled, true)));
await comp.InvokeAsync(() => combobox.Instance.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keydown", }));
comp.WaitForAssertion(() => combobox.Instance.SelectedValues.Should().NotContain("Tiger"));
await comp.InvokeAsync(() => combobox.Render(p => p.Add(x => x.Disabled, false)));
//Test the keyup event
await comp.InvokeAsync(() => combobox.Instance.HandleKeyUpAsync(new KeyboardEventArgs() { Key = "Enter", Type = "keyup", }));
comp.WaitForAssertion(() => combobox.Instance.SelectedValues.Should().NotContain("Tiger"));
await comp.InvokeAsync(() => combobox.Instance.HandleKeyDownAsync(new KeyboardEventArgs() { Key = "Tab", Type = "keydown", }));
await comp.InvokeAsync(() => combobox.Instance.OnKeyUp.InvokeAsync(new KeyboardEventArgs() { Key = "Tab" }));
comp.Render(); // <-- this is necessary for reliable passing of the test
comp.WaitForAssertion(() => comp.Find("div.mud-popover").ClassList.Should().Contain("d-none"));
}
}
}