Skip to content

Commit 0f7dfe6

Browse files
committed
Add more unit tests for [AlsoNotifyCanExecuteFor]
1 parent ccb8a9d commit 0f7dfe6

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

tests/CommunityToolkit.Mvvm.UnitTests/Test_ObservablePropertyAttribute.cs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,60 @@ public void Test_AlsoNotifyChangeFor()
273273
CollectionAssert.AreEqual(new[] { nameof(model.Surname), nameof(model.FullName), nameof(model.Alias) }, propertyNames);
274274
}
275275

276+
[TestMethod]
277+
public void Test_AlsoNotifyChangeFor_GeneratedCommand()
278+
{
279+
DependentPropertyModel2 model = new();
280+
281+
List<string?> propertyNames = new();
282+
int canExecuteRequests = 0;
283+
284+
model.PropertyChanged += (s, e) => propertyNames.Add(e.PropertyName);
285+
model.MyCommand.CanExecuteChanged += (s, e) => canExecuteRequests++;
286+
287+
model.Text = "Ross";
288+
289+
Assert.AreEqual(1, canExecuteRequests);
290+
291+
CollectionAssert.AreEqual(new[] { nameof(model.Text), nameof(model.FullName), nameof(model.Alias) }, propertyNames);
292+
}
293+
294+
[TestMethod]
295+
public void Test_AlsoNotifyChangeFor_IRelayCommandProperty()
296+
{
297+
DependentPropertyModel3 model = new();
298+
299+
List<string?> propertyNames = new();
300+
int canExecuteRequests = 0;
301+
302+
model.PropertyChanged += (s, e) => propertyNames.Add(e.PropertyName);
303+
model.MyCommand.CanExecuteChanged += (s, e) => canExecuteRequests++;
304+
305+
model.Text = "Ross";
306+
307+
Assert.AreEqual(1, canExecuteRequests);
308+
309+
CollectionAssert.AreEqual(new[] { nameof(model.Text), nameof(model.FullName), nameof(model.Alias) }, propertyNames);
310+
}
311+
312+
[TestMethod]
313+
public void Test_AlsoNotifyChangeFor_IAsyncRelayCommandOfTProperty()
314+
{
315+
DependentPropertyModel4 model = new();
316+
317+
List<string?> propertyNames = new();
318+
int canExecuteRequests = 0;
319+
320+
model.PropertyChanged += (s, e) => propertyNames.Add(e.PropertyName);
321+
model.MyCommand.CanExecuteChanged += (s, e) => canExecuteRequests++;
322+
323+
model.Text = "Ross";
324+
325+
Assert.AreEqual(1, canExecuteRequests);
326+
327+
CollectionAssert.AreEqual(new[] { nameof(model.Text), nameof(model.FullName), nameof(model.Alias) }, propertyNames);
328+
}
329+
276330
[TestMethod]
277331
public void Test_OnPropertyChangingAndChangedPartialMethods()
278332
{
@@ -344,6 +398,56 @@ public sealed partial class DependentPropertyModel
344398
public RelayCommand MyCommand { get; } = new(() => { });
345399
}
346400

401+
[INotifyPropertyChanged]
402+
public sealed partial class DependentPropertyModel2
403+
{
404+
[ObservableProperty]
405+
[AlsoNotifyChangeFor(nameof(FullName), nameof(Alias))]
406+
[AlsoNotifyCanExecuteFor(nameof(TestFromMethodCommand))]
407+
private string? text;
408+
409+
public string FullName => "";
410+
411+
public string Alias => "";
412+
413+
public RelayCommand MyCommand { get; } = new(() => { });
414+
415+
[ICommand]
416+
private void TestFromMethod()
417+
{
418+
}
419+
}
420+
421+
[INotifyPropertyChanged]
422+
public sealed partial class DependentPropertyModel3
423+
{
424+
[ObservableProperty]
425+
[AlsoNotifyChangeFor(nameof(FullName), nameof(Alias))]
426+
[AlsoNotifyCanExecuteFor(nameof(MyCommand))]
427+
private string? text;
428+
429+
public string FullName => "";
430+
431+
public string Alias => "";
432+
433+
public IRelayCommand MyCommand { get; } = null!;
434+
}
435+
436+
[INotifyPropertyChanged]
437+
public sealed partial class DependentPropertyModel4
438+
{
439+
[ObservableProperty]
440+
[AlsoNotifyChangeFor(nameof(FullName), nameof(Alias))]
441+
[AlsoNotifyCanExecuteFor(nameof(MyCommand))]
442+
private string? text;
443+
444+
public string FullName => "";
445+
446+
public string Alias => "";
447+
448+
public IAsyncRelayCommand<string> MyCommand { get; } = null!;
449+
}
450+
347451
public partial class MyFormViewModel : ObservableValidator
348452
{
349453
[ObservableProperty]

0 commit comments

Comments
 (0)