|
1 | 1 | using Bunit; |
2 | 2 | using AwesomeAssertions; |
3 | 3 | using Microsoft.AspNetCore.Components; |
| 4 | +using Microsoft.AspNetCore.Components.Web; |
4 | 5 | using MudBlazor; |
5 | 6 | using MudBlazor.Extensions; |
6 | 7 | using MudExtensions.UnitTests.TestComponents; |
@@ -54,5 +55,144 @@ public async Task ChipField_ParentFormReadOnly_ShouldPreventRemoval() |
54 | 55 | await comp.InvokeAsync(() => chipField.Instance.Closed(chips[0].Instance)); |
55 | 56 | chipField.Instance.GetState(x => x.Values).Should().BeEquivalentTo(new List<string> { "first", "second" }); |
56 | 57 | } |
| 58 | + |
| 59 | + [Test] |
| 60 | + public async Task ChipField_Required_WithChips_ShouldBeValid() |
| 61 | + { |
| 62 | + var comp = Context.Render<ChipFieldRequiredTest>(opt => |
| 63 | + { |
| 64 | + opt.Add(a => a.Values, new List<string> { "first" }); |
| 65 | + }); |
| 66 | + |
| 67 | + await comp.InvokeAsync(() => comp.Instance.Form!.Validate()); |
| 68 | + |
| 69 | + comp.Instance.Form!.IsValid.Should().BeTrue(); |
| 70 | + comp.Instance.Form.Errors.Should().BeEmpty(); |
| 71 | + comp.FindAll(".mud-input-error").Should().BeEmpty(); |
| 72 | + comp.Markup.Should().NotContain("Add at least one chip"); |
| 73 | + } |
| 74 | + |
| 75 | + [Test] |
| 76 | + public async Task ChipField_Required_WithoutChips_ShouldBeInvalid() |
| 77 | + { |
| 78 | + var comp = Context.Render<ChipFieldRequiredTest>(opt => |
| 79 | + { |
| 80 | + opt.Add(a => a.Values, new List<string>()); |
| 81 | + }); |
| 82 | + |
| 83 | + await comp.InvokeAsync(() => comp.Instance.Form!.Validate()); |
| 84 | + |
| 85 | + comp.Instance.Form!.IsValid.Should().BeFalse(); |
| 86 | + comp.Instance.Form.Errors.Should().ContainSingle().Which.Should().Be("Add at least one chip"); |
| 87 | + comp.Markup.Should().Contain("Add at least one chip"); |
| 88 | + } |
| 89 | + |
| 90 | + [Test] |
| 91 | + public async Task ChipField_Required_WithTextButNoChips_ShouldBeInvalid() |
| 92 | + { |
| 93 | + var comp = Context.Render<ChipFieldRequiredTest>(opt => |
| 94 | + { |
| 95 | + opt.Add(a => a.Values, new List<string>()); |
| 96 | + }); |
| 97 | + |
| 98 | + comp.Find("input").Input(new ChangeEventArgs { Value = "not a chip yet" }); |
| 99 | + await comp.InvokeAsync(() => comp.Instance.Form!.Validate()); |
| 100 | + |
| 101 | + comp.Instance.Form!.IsValid.Should().BeFalse(); |
| 102 | + } |
| 103 | + |
| 104 | + [Test] |
| 105 | + public async Task ChipField_Required_TypingAfterError_ShouldKeepShowingError() |
| 106 | + { |
| 107 | + var comp = Context.Render<ChipFieldRequiredTest>(opt => |
| 108 | + { |
| 109 | + opt.Add(a => a.Values, new List<string>()); |
| 110 | + }); |
| 111 | + await comp.InvokeAsync(() => comp.Instance.Form!.Validate()); |
| 112 | + comp.Markup.Should().Contain("Add at least one chip"); |
| 113 | + |
| 114 | + comp.Find("input").Input(new ChangeEventArgs { Value = "not a chip yet" }); |
| 115 | + comp.Markup.Should().Contain("Add at least one chip"); |
| 116 | + |
| 117 | + await comp.InvokeAsync(() => comp.Instance.Form!.Validate()); |
| 118 | + |
| 119 | + comp.Instance.Form!.IsValid.Should().BeFalse(); |
| 120 | + comp.Markup.Should().Contain("Add at least one chip"); |
| 121 | + comp.Find("input").GetAttribute("aria-invalid").Should().Be("true"); |
| 122 | + } |
| 123 | + |
| 124 | + [Test] |
| 125 | + public async Task ChipField_Required_AddingChipAfterError_ShouldBecomeValid() |
| 126 | + { |
| 127 | + var comp = Context.Render<ChipFieldRequiredTest>(opt => |
| 128 | + { |
| 129 | + opt.Add(a => a.Values, new List<string>()); |
| 130 | + }); |
| 131 | + await comp.InvokeAsync(() => comp.Instance.Form!.Validate()); |
| 132 | + comp.Instance.Form!.IsValid.Should().BeFalse(); |
| 133 | + |
| 134 | + var chipField = comp.FindComponent<MudChipField<string>>(); |
| 135 | + comp.Find("input").Input(new ChangeEventArgs { Value = "first" }); |
| 136 | + await comp.InvokeAsync(() => chipField.Instance.HandleBeforeInput(new BeforeInputEventArgs { Data = " ", InputType = "insert" })); |
| 137 | + chipField.Instance.GetState(x => x.Values).Should().BeEquivalentTo(new List<string> { "first" }); |
| 138 | + |
| 139 | + await comp.InvokeAsync(() => comp.Instance.Form!.Validate()); |
| 140 | + |
| 141 | + comp.Instance.Form!.IsValid.Should().BeTrue(); |
| 142 | + comp.FindAll(".mud-input-error").Should().BeEmpty(); |
| 143 | + comp.Markup.Should().NotContain("Add at least one chip"); |
| 144 | + } |
| 145 | + |
| 146 | + [Test] |
| 147 | + public async Task ChipField_Required_BlurWithChips_ShouldNotShowError() |
| 148 | + { |
| 149 | + var comp = Context.Render<ChipFieldRequiredTest>(opt => |
| 150 | + { |
| 151 | + opt.Add(a => a.Values, new List<string> { "first" }); |
| 152 | + }); |
| 153 | + |
| 154 | + await comp.Find("input").BlurAsync(new FocusEventArgs()); |
| 155 | + |
| 156 | + comp.FindAll(".mud-input-error").Should().BeEmpty(); |
| 157 | + comp.Instance.Form!.Errors.Should().BeEmpty(); |
| 158 | + } |
| 159 | + |
| 160 | + [Test] |
| 161 | + public void ChipField_Required_ShouldKeepRequiredMarker() |
| 162 | + { |
| 163 | + var comp = Context.Render<ChipFieldRequiredTest>(opt => |
| 164 | + { |
| 165 | + opt.Add(a => a.Values, new List<string> { "first" }); |
| 166 | + }); |
| 167 | + |
| 168 | + comp.FindAll(".mud-input-control.mud-input-required").Should().NotBeEmpty(); |
| 169 | + } |
| 170 | + |
| 171 | + [Test] |
| 172 | + public async Task ChipField_Required_WithNullValues_ShouldBeInvalid() |
| 173 | + { |
| 174 | + var comp = Context.Render<ChipFieldRequiredTest>(); |
| 175 | + |
| 176 | + await comp.InvokeAsync(() => comp.Instance.Form!.Validate()); |
| 177 | + |
| 178 | + comp.Instance.Form!.IsValid.Should().BeFalse(); |
| 179 | + comp.Markup.Should().Contain("Add at least one chip"); |
| 180 | + } |
| 181 | + |
| 182 | + [Test] |
| 183 | + public async Task ChipFieldTextField_WithoutHasChips_ShouldValidateRequiredAgainstText() |
| 184 | + { |
| 185 | + var comp = Context.Render<ChipFieldTextField<string>>(opt => |
| 186 | + { |
| 187 | + opt.Add(a => a.Required, true); |
| 188 | + }); |
| 189 | + |
| 190 | + await comp.InvokeAsync(() => comp.Instance.ValidateAsync()); |
| 191 | + comp.Instance.HasErrors.Should().BeTrue(); |
| 192 | + |
| 193 | + comp.Find("input").Change("text"); |
| 194 | + await comp.InvokeAsync(() => comp.Instance.ValidateAsync()); |
| 195 | + comp.Instance.HasErrors.Should().BeFalse(); |
| 196 | + } |
57 | 197 | } |
58 | 198 | } |
0 commit comments