|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
1 | 3 | using System.Reflection; |
| 4 | +using Bunit; |
| 5 | +using FWO.Ui.Services; |
2 | 6 | using FWO.Ui.Shared; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
3 | 8 | using NUnit.Framework; |
4 | 9 |
|
5 | 10 | namespace FWO.Test |
6 | 11 | { |
7 | 12 | [TestFixture] |
8 | | - public class DropdownTest |
| 13 | + [FixtureLifeCycle(LifeCycle.InstancePerTestCase)] |
| 14 | + public class DropdownTest : BunitContext |
9 | 15 | { |
| 16 | + private static MethodInfo GetInstanceMethod(string methodName, params Type[] parameterTypes) |
| 17 | + { |
| 18 | + MethodInfo? method = typeof(Dropdown<string>).GetMethod( |
| 19 | + methodName, |
| 20 | + BindingFlags.NonPublic | BindingFlags.Instance, |
| 21 | + null, |
| 22 | + parameterTypes, |
| 23 | + null); |
| 24 | + |
| 25 | + Assert.That(method, Is.Not.Null); |
| 26 | + return method!; |
| 27 | + } |
| 28 | + |
| 29 | + private static string GetSearchValue(Dropdown<string> dropdown) |
| 30 | + { |
| 31 | + FieldInfo? searchValueField = typeof(Dropdown<string>).GetField("searchValue", BindingFlags.NonPublic | BindingFlags.Instance); |
| 32 | + Assert.That(searchValueField, Is.Not.Null); |
| 33 | + return (string?)searchValueField!.GetValue(dropdown) ?? ""; |
| 34 | + } |
| 35 | + |
| 36 | + private static void SetComponentParameter<TValue>(Dropdown<string> dropdown, string parameterName, TValue value) |
| 37 | + { |
| 38 | + PropertyInfo? parameter = typeof(Dropdown<string>).GetProperty(parameterName, BindingFlags.Public | BindingFlags.Instance); |
| 39 | + Assert.That(parameter, Is.Not.Null); |
| 40 | + parameter!.SetValue(dropdown, value); |
| 41 | + } |
| 42 | + |
10 | 43 | /// <summary> |
11 | | - /// Ensures that a global click callback tolerates a missing JS runtime after disposal. |
12 | | - /// </summary> |
| 44 | + /// Ensures that a global focus callback tolerates a missing JS runtime after disposal. |
| 45 | + /// </summary> |
13 | 46 | [Test] |
14 | | - public void OnGlobalClick_DoesNotThrow_WhenJsRuntimeIsNull() |
| 47 | + public void OnFocusChanged_DoesNotThrow_WhenJsRuntimeIsNull() |
15 | 48 | { |
16 | 49 | Dropdown<string> dropdown = new(); |
17 | | - MethodInfo? method = typeof(Dropdown<string>).GetMethod("OnGlobalClick", BindingFlags.NonPublic | BindingFlags.Instance); |
| 50 | + MethodInfo? method = typeof(Dropdown<string>).GetMethod("OnFocusChanged", BindingFlags.NonPublic | BindingFlags.Instance); |
18 | 51 |
|
19 | 52 | Assert.That(method, Is.Not.Null); |
20 | | - Assert.DoesNotThrow(() => method!.Invoke(dropdown, new object[] { "test-element" })); |
| 53 | + Assert.DoesNotThrow(() => method!.Invoke(dropdown, ["test-element"])); |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Verifies that filtering matches values without regard to input casing. |
| 58 | + /// </summary> |
| 59 | + [Test] |
| 60 | + public void Filter_IsCaseInsensitive() |
| 61 | + { |
| 62 | + Dropdown<string> dropdown = new(); |
| 63 | + SetComponentParameter(dropdown, nameof(Dropdown<string>.Elements), new[] { "Alpha", "beta", "Gamma" }); |
| 64 | + MethodInfo filterMethod = GetInstanceMethod("Filter", typeof(string)); |
| 65 | + |
| 66 | + filterMethod.Invoke(dropdown, ["AL"]); |
| 67 | + |
| 68 | + Assert.That(dropdown.FilteredElements, Is.EqualTo(["Alpha"])); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Verifies that the none-selected label is shown when there is no selection. |
| 73 | + /// </summary> |
| 74 | + [Test] |
| 75 | + public void DisplaySelection_UsesNoneSelectedText_WhenNoSelection() |
| 76 | + { |
| 77 | + Dropdown<string> dropdown = new(); |
| 78 | + SetComponentParameter(dropdown, nameof(Dropdown<string>.NoneSelectedText), "none"); |
| 79 | + MethodInfo displaySelectionMethod = GetInstanceMethod("DisplaySelection", typeof(IEnumerable<string>)); |
| 80 | + |
| 81 | + displaySelectionMethod.Invoke(dropdown, [Enumerable.Empty<string>()]); |
| 82 | + |
| 83 | + Assert.That(GetSearchValue(dropdown), Is.EqualTo("none")); |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Verifies that multiple selected values are rendered as first item plus count summary. |
| 88 | + /// </summary> |
| 89 | + [Test] |
| 90 | + public void DisplaySelection_FormatsSummary_WhenMultipleElementsSelected() |
| 91 | + { |
| 92 | + Dropdown<string> dropdown = new(); |
| 93 | + MethodInfo displaySelectionMethod = GetInstanceMethod("DisplaySelection", typeof(IEnumerable<string>)); |
| 94 | + |
| 95 | + displaySelectionMethod.Invoke(dropdown, [new[] { "first", "second", "third" }]); |
| 96 | + |
| 97 | + Assert.That(GetSearchValue(dropdown), Is.EqualTo("first, ... (+ 2)")); |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Verifies that selecting the same element twice in multiselect mode keeps a single entry. |
| 102 | + /// </summary> |
| 103 | + [Test] |
| 104 | + public async Task SelectElement_MultiSelect_AddsElementOnlyOnce() |
| 105 | + { |
| 106 | + Services.AddScoped<DomEventService>(); |
| 107 | + IRenderedComponent<Dropdown<string>> renderedDropdown = Render<Dropdown<string>>(parameters => parameters |
| 108 | + .Add(p => p.Multiselect, true) |
| 109 | + .Add(p => p.SelectedElements, [])); |
| 110 | + Dropdown<string> dropdown = renderedDropdown.Instance; |
| 111 | + MethodInfo selectMethod = GetInstanceMethod("SelectElement", typeof(string)); |
| 112 | + |
| 113 | + Task firstSelection = (Task)selectMethod.Invoke(dropdown, ["one"])!; |
| 114 | + await firstSelection; |
| 115 | + Task secondSelection = (Task)selectMethod.Invoke(dropdown, ["one"])!; |
| 116 | + await secondSelection; |
| 117 | + |
| 118 | + Assert.That(dropdown.SelectedElements, Is.EqualTo(["one"])); |
| 119 | + Assert.That(dropdown.Toggled, Is.False); |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Verifies that unselecting one item in multiselect mode removes only that item. |
| 124 | + /// </summary> |
| 125 | + [Test] |
| 126 | + public async Task UnselectElement_MultiSelect_RemovesElementFromSelection() |
| 127 | + { |
| 128 | + Services.AddScoped<DomEventService>(); |
| 129 | + IRenderedComponent<Dropdown<string>> renderedDropdown = Render<Dropdown<string>>(parameters => parameters |
| 130 | + .Add(p => p.Multiselect, true) |
| 131 | + .Add(p => p.SelectedElements, ["one", "two"])); |
| 132 | + Dropdown<string> dropdown = renderedDropdown.Instance; |
| 133 | + MethodInfo unselectMethod = GetInstanceMethod("UnselectElement", typeof(string)); |
| 134 | + |
| 135 | + Task unselection = (Task)unselectMethod.Invoke(dropdown, ["one"])!; |
| 136 | + await unselection; |
| 137 | + |
| 138 | + Assert.That(dropdown.SelectedElements, Is.EqualTo(["two"])); |
| 139 | + Assert.That(dropdown.Toggled, Is.False); |
21 | 140 | } |
22 | 141 | } |
23 | 142 | } |
0 commit comments