Skip to content

Commit 4a3f919

Browse files
committed
Add unit test for inheriting [INotifyDataErrorInfo]
1 parent 69ddcfd commit 4a3f919

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

tests/CommunityToolkit.Mvvm.UnitTests/Test_ObservablePropertyAttribute.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,32 @@ public void Test_ObservablePropertyWithValueNamedField_WithValidationAttributesA
290290
Assert.AreEqual(errors[1].PropertyName, nameof(ModelWithValuePropertyWithAutomaticValidationWithClassLevelAttribute.Value));
291291
}
292292

293+
[TestMethod]
294+
public void Test_ObservablePropertyWithValueNamedField_WithValidationAttributesAndValidation_InheritingClassLevelAttribute()
295+
{
296+
ModelWithValuePropertyWithAutomaticValidationInheritingClassLevelAttribute model = new();
297+
298+
List<string?> propertyNames = new();
299+
300+
model.PropertyChanged += (s, e) => propertyNames.Add(e.PropertyName);
301+
302+
List<DataErrorsChangedEventArgs> errors = new();
303+
304+
model.ErrorsChanged += (s, e) => errors.Add(e);
305+
306+
model.Value2 = "Bo";
307+
308+
Assert.IsTrue(model.HasErrors);
309+
Assert.AreEqual(errors.Count, 1);
310+
Assert.AreEqual(errors[0].PropertyName, nameof(ModelWithValuePropertyWithAutomaticValidationInheritingClassLevelAttribute.Value2));
311+
312+
model.Value2 = "Hello world";
313+
314+
Assert.IsFalse(model.HasErrors);
315+
Assert.AreEqual(errors.Count, 2);
316+
Assert.AreEqual(errors[1].PropertyName, nameof(ModelWithValuePropertyWithAutomaticValidationInheritingClassLevelAttribute.Value2));
317+
}
318+
293319
// See https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/4184
294320
[TestMethod]
295321
public void Test_GeneratedPropertiesWithValidationAttributesOverFields()
@@ -1058,6 +1084,14 @@ public partial class ModelWithValuePropertyWithAutomaticValidationWithClassLevel
10581084
private string? value;
10591085
}
10601086

1087+
public partial class ModelWithValuePropertyWithAutomaticValidationInheritingClassLevelAttribute : ModelWithValuePropertyWithAutomaticValidationWithClassLevelAttribute
1088+
{
1089+
[ObservableProperty]
1090+
[Required]
1091+
[MinLength(5)]
1092+
private string? value2;
1093+
}
1094+
10611095
public partial class ViewModelWithValidatableGeneratedProperties : ObservableValidator
10621096
{
10631097
[Required]

0 commit comments

Comments
 (0)