|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the Apache 2.0 License |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | +// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone |
| 5 | + |
| 6 | +using System.ComponentModel.DataAnnotations; |
| 7 | + |
| 8 | +namespace UnitTest.Components; |
| 9 | + |
| 10 | +public class RadioListGenericTest : BootstrapBlazorTestBase |
| 11 | +{ |
| 12 | + [Fact] |
| 13 | + public void Enum_NoItems() |
| 14 | + { |
| 15 | + var v = EnumEducation.Middle; |
| 16 | + var cut = Context.RenderComponent<RadioListGeneric<EnumEducation>>(pb => |
| 17 | + { |
| 18 | + pb.Add(a => a.Value, v); |
| 19 | + }); |
| 20 | + Assert.Contains("radio-list form-control", cut.Markup); |
| 21 | + Assert.Contains("form-check-input", cut.Markup); |
| 22 | + } |
| 23 | + |
| 24 | + [Fact] |
| 25 | + public void EnumValue_Ok() |
| 26 | + { |
| 27 | + var v = EnumEducation.Middle; |
| 28 | + var cut = Context.RenderComponent<RadioListGeneric<EnumEducation>>(pb => |
| 29 | + { |
| 30 | + pb.Add(a => a.Items, typeof(EnumEducation).ToSelectList<EnumEducation>()); |
| 31 | + pb.Add(a => a.Value, v); |
| 32 | + }); |
| 33 | + Assert.Contains("form-check-input", cut.Markup); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void EnumNullValue_Ok() |
| 38 | + { |
| 39 | + EnumEducation? v = null; |
| 40 | + var cut = Context.RenderComponent<RadioListGeneric<EnumEducation?>>(pb => |
| 41 | + { |
| 42 | + pb.Add(a => a.Value, v); |
| 43 | + pb.Add(a => a.NullItemText, "Test-Null"); |
| 44 | + pb.Add(a => a.IsAutoAddNullItem, true); |
| 45 | + }); |
| 46 | + var items = cut.FindAll("[type='radio']"); |
| 47 | + Assert.Equal(3, items.Count); |
| 48 | + Assert.Contains("Test-Null", cut.Markup); |
| 49 | + Assert.Contains("form-check-input", cut.Markup); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public async Task SelectedItem_Ok() |
| 54 | + { |
| 55 | + var cut = Context.RenderComponent<RadioListGeneric<Foo>>(pb => |
| 56 | + { |
| 57 | + pb.Add(a => a.Items, new List<SelectedItem<Foo>> |
| 58 | + { |
| 59 | + new(new Foo() { Id = 1, Name = "张三 001" }, "Test1"), |
| 60 | + new(new Foo() { Id = 2, Name = "张三 002" }, "Test2") |
| 61 | + }); |
| 62 | + pb.Add(a => a.IsVertical, true); |
| 63 | + }); |
| 64 | + cut.Contains("is-vertical"); |
| 65 | + var item = cut.Find(".form-check-input"); |
| 66 | + await cut.InvokeAsync(() => item.Click()); |
| 67 | + Assert.Equal("张三 001", cut.Instance.Value.Name); |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public void IsDisabled_Ok() |
| 72 | + { |
| 73 | + var cut = Context.RenderComponent<RadioListGeneric<Foo>>(pb => |
| 74 | + { |
| 75 | + pb.Add(a => a.Items, new List<SelectedItem<Foo>> |
| 76 | + { |
| 77 | + new(new Foo() { Id = 1, Name = "张三 001" }, "Test1"), |
| 78 | + new(new Foo() { Id = 2, Name = "张三 002" }, "Test2") { IsDisabled = true } |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + // 候选项被禁用 |
| 83 | + cut.Contains("disabled=\"disabled\""); |
| 84 | + |
| 85 | + // 组件被禁用 |
| 86 | + cut.SetParametersAndRender(pb => |
| 87 | + { |
| 88 | + pb.Add(a => a.IsDisabled, true); |
| 89 | + }); |
| 90 | + cut.Contains("form-check disabled"); |
| 91 | + } |
| 92 | + |
| 93 | + [Fact] |
| 94 | + public void ShowLabel_Ok() |
| 95 | + { |
| 96 | + var cut = Context.RenderComponent<RadioListGeneric<Foo>>(pb => |
| 97 | + { |
| 98 | + pb.Add(a => a.Items, new List<SelectedItem<Foo>> |
| 99 | + { |
| 100 | + new(new Foo() { Id = 1, Name = "张三 001" }, "Test1"), |
| 101 | + new(new Foo() { Id = 2, Name = "张三 002" }, "Test2") |
| 102 | + }); |
| 103 | + pb.Add(a => a.ShowLabel, true); |
| 104 | + pb.Add(a => a.DisplayText, "test-label"); |
| 105 | + }); |
| 106 | + cut.Contains("test-label"); |
| 107 | + cut.Contains("form-label"); |
| 108 | + } |
| 109 | + |
| 110 | + [Fact] |
| 111 | + public void NotInItems_Ok() |
| 112 | + { |
| 113 | + var cut = Context.RenderComponent<RadioListGeneric<Foo>>(); |
| 114 | + Assert.Null(cut.Instance.Value); |
| 115 | + Assert.Equal("<div class=\"radio-list form-control\" role=\"group\"></div>", cut.Markup); |
| 116 | + |
| 117 | + // 组件值为 test |
| 118 | + // 组件给的候选 Items 中无 test 选项 |
| 119 | + var v = new Foo() { Id = 3, Name = "test" }; |
| 120 | + cut.SetParametersAndRender(pb => |
| 121 | + { |
| 122 | + pb.Add(a => a.Items, new List<SelectedItem<Foo>> |
| 123 | + { |
| 124 | + new(new Foo() { Id = 1, Name = "张三 001" }, "Test1"), |
| 125 | + new(new Foo() { Id = 2, Name = "张三 002" }, "Test2") |
| 126 | + }); |
| 127 | + pb.Add(a => a.Value, v); |
| 128 | + pb.Add(a => a.CustomKeyAttribute, typeof(KeyAttribute)); |
| 129 | + pb.Add(a => a.ModelEqualityComparer, new Func<Foo, Foo, bool>((x, y) => x.Id == y.Id)); |
| 130 | + }); |
| 131 | + Assert.Null(cut.Instance.Value); |
| 132 | + |
| 133 | + v = new Foo() { Id = 3, Name = "test" }; |
| 134 | + cut.SetParametersAndRender(pb => |
| 135 | + { |
| 136 | + pb.Add(a => a.Items, new List<SelectedItem<Foo>>()); |
| 137 | + pb.Add(a => a.Value, v); |
| 138 | + }); |
| 139 | + Assert.Null(cut.Instance.Value); |
| 140 | + } |
| 141 | + |
| 142 | + [Fact] |
| 143 | + public async Task OnSelectedChanged_Ok() |
| 144 | + { |
| 145 | + var selected = false; |
| 146 | + var v = EnumEducation.Middle; |
| 147 | + var cut = Context.RenderComponent<RadioListGeneric<EnumEducation>>(pb => |
| 148 | + { |
| 149 | + pb.Add(a => a.Items, typeof(EnumEducation).ToSelectList<EnumEducation>()); |
| 150 | + pb.Add(a => a.Value, v); |
| 151 | + pb.Add(a => a.OnSelectedChanged, v => |
| 152 | + { |
| 153 | + selected = true; |
| 154 | + return Task.CompletedTask; |
| 155 | + }); |
| 156 | + }); |
| 157 | + var item = cut.Find(".form-check-input"); |
| 158 | + await cut.InvokeAsync(() => item.Click()); |
| 159 | + Assert.True(selected); |
| 160 | + } |
| 161 | + |
| 162 | + [Fact] |
| 163 | + public async Task OnSelectedChanged_SelectedItem_Ok() |
| 164 | + { |
| 165 | + var selected = false; |
| 166 | + var v = EnumEducation.Middle; |
| 167 | + var cut = Context.RenderComponent<RadioListGeneric<EnumEducation>>(pb => |
| 168 | + { |
| 169 | + pb.Add(a => a.Items, typeof(EnumEducation).ToSelectList<EnumEducation>()); |
| 170 | + pb.Add(a => a.Value, v); |
| 171 | + pb.Add(a => a.OnSelectedChanged, v => |
| 172 | + { |
| 173 | + selected = true; |
| 174 | + return Task.CompletedTask; |
| 175 | + }); |
| 176 | + }); |
| 177 | + var item = cut.Find(".form-check-input"); |
| 178 | + await cut.InvokeAsync(() => item.Click()); |
| 179 | + Assert.True(selected); |
| 180 | + Assert.Equal(EnumEducation.Primary, cut.Instance.Value); |
| 181 | + } |
| 182 | + |
| 183 | + [Fact] |
| 184 | + public void ItemTemplate_Ok() |
| 185 | + { |
| 186 | + var v = new Foo() { Id = 3, Name = "test" }; |
| 187 | + var cut = Context.RenderComponent<RadioListGeneric<Foo>>(pb => |
| 188 | + { |
| 189 | + pb.Add(a => a.Items, new List<SelectedItem<Foo>> |
| 190 | + { |
| 191 | + new(new Foo() { Id = 1, Name = "张三 001" }, "Test1"), |
| 192 | + new(new Foo() { Id = 2, Name = "张三 002" }, "Test2") |
| 193 | + }); |
| 194 | + pb.Add(a => a.Value, v); |
| 195 | + pb.Add(a => a.ItemTemplate, v => builder => |
| 196 | + { |
| 197 | + builder.AddContent(0, $"<div>item-template-{v.Value?.Name}</div>"); |
| 198 | + }); |
| 199 | + }); |
| 200 | + cut.Contains("item-template-张三 001"); |
| 201 | + cut.Contains("item-template-张三 002"); |
| 202 | + } |
| 203 | + |
| 204 | + [Fact] |
| 205 | + public async Task IsButton_Ok() |
| 206 | + { |
| 207 | + var cut = Context.RenderComponent<RadioListGeneric<EnumEducation>>(pb => |
| 208 | + { |
| 209 | + pb.Add(a => a.IsButton, true); |
| 210 | + pb.Add(a => a.Items, typeof(EnumEducation).ToSelectList<EnumEducation>()); |
| 211 | + pb.Add(a => a.Value, EnumEducation.Middle); |
| 212 | + }); |
| 213 | + cut.Contains("radio-list btn-group"); |
| 214 | + |
| 215 | + cut.SetParametersAndRender(pb => |
| 216 | + { |
| 217 | + pb.Add(a => a.Color, Color.Danger); |
| 218 | + }); |
| 219 | + cut.Contains("btn active bg-danger"); |
| 220 | + |
| 221 | + var btn = cut.Find(".btn"); |
| 222 | + await cut.InvokeAsync(() => |
| 223 | + { |
| 224 | + btn.Click(); |
| 225 | + }); |
| 226 | + cut.Contains("btn active bg-danger"); |
| 227 | + } |
| 228 | + |
| 229 | + [Fact] |
| 230 | + public void ShowBorder_Ok() |
| 231 | + { |
| 232 | + var cut = Context.RenderComponent<RadioListGeneric<EnumEducation>>(pb => |
| 233 | + { |
| 234 | + pb.Add(a => a.ShowBorder, false); |
| 235 | + pb.Add(a => a.Items, typeof(EnumEducation).ToSelectList<EnumEducation>()); |
| 236 | + pb.Add(a => a.Value, EnumEducation.Middle); |
| 237 | + }); |
| 238 | + cut.Contains("no-border"); |
| 239 | + } |
| 240 | +} |
0 commit comments