From df299ae2bf75609d4b1f8633eca1e450e2afd29d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20K=C3=B6nig?= Date: Sun, 21 Jan 2024 14:50:23 +0100 Subject: [PATCH] Added unittests for new events --- .../Mvvm/MvvmUserControlTests.cs | 66 +++++++++++++++++- .../Mvvm/MvvmWindowTests.cs | 67 ++++++++++++++++++- 2 files changed, 129 insertions(+), 4 deletions(-) diff --git a/src/RolandK.AvaloniaExtensions.Tests/Mvvm/MvvmUserControlTests.cs b/src/RolandK.AvaloniaExtensions.Tests/Mvvm/MvvmUserControlTests.cs index da82fa1..7bfa2d9 100644 --- a/src/RolandK.AvaloniaExtensions.Tests/Mvvm/MvvmUserControlTests.cs +++ b/src/RolandK.AvaloniaExtensions.Tests/Mvvm/MvvmUserControlTests.cs @@ -10,7 +10,7 @@ namespace RolandK.AvaloniaExtensions.Tests.Mvvm; [Collection(nameof(ApplicationTestCollection))] -public class MvvmUserControlTests +public partial class MvvmUserControlTests { [Fact] public Task Attach_MvvmUserControl_to_ViewModel() @@ -172,10 +172,69 @@ public Task Attach_ViewModel_to_multiple_views_throws_InvalidOperationException( }); } + [Fact] + public Task Attach_MvvmUserControl_to_ViewModel_then_detach_and_check_events_fired() + { + return UnitTestApplication.RunInApplicationContextAsync(() => + { + // Arrange + var testMvvmControl = new MvvmUserControl(); + var testViewModel = new TestViewModel(); + + var viewModelAttachedEventCount = 0; + var viewModelDetachedEventCount = 0; + testMvvmControl.ViewModelAttached += (_, _) => viewModelAttachedEventCount++; + testMvvmControl.ViewModelDetached += (_, _) => viewModelDetachedEventCount++; + + // Act + testMvvmControl.DataContext = testViewModel; + testMvvmControl.ViewFor = typeof(TestViewModel); + var testRoot = new TestRootWindow(testMvvmControl); + testRoot.Content = new Grid(); + + // Assert + Assert.Equal(1, viewModelAttachedEventCount); + Assert.Equal(1, viewModelDetachedEventCount); + + GC.KeepAlive(testRoot); + }); + } + + [Fact] + public Task Attach_MvvmUserControl_to_ViewModel_then_check_ViewModelPropertyChanged_event() + { + return UnitTestApplication.RunInApplicationContextAsync(() => + { + // Arrange + var testMvvmControl = new MvvmUserControl(); + var testViewModel = new TestViewModel(); + + var propertyChangedEventCount = 0; + var lastPropertyChangedEventPropertyName = string.Empty; + testMvvmControl.ViewModelPropertyChanged += (_, args) => + { + propertyChangedEventCount++; + lastPropertyChangedEventPropertyName = args.PropertyName; + }; + + // Act + testMvvmControl.DataContext = testViewModel; + testMvvmControl.ViewFor = typeof(TestViewModel); + var testRoot = new TestRootWindow(testMvvmControl); + testViewModel.DummyProperty = "Some other value.."; + + // Assert + Assert.Equal(1, propertyChangedEventCount); + Assert.Equal(nameof(TestViewModel.DummyProperty), lastPropertyChangedEventPropertyName); + + GC.KeepAlive(testRoot); + }); + } + //************************************************************************* //************************************************************************* //************************************************************************* - private class TestViewModel : ObservableObject, IAttachableViewModel + private partial class TestViewModel : ObservableObject, IAttachableViewModel { /// public event EventHandler? CloseWindowRequest; @@ -186,6 +245,9 @@ private class TestViewModel : ObservableObject, IAttachableViewModel /// public object? AssociatedView { get; set; } + [ObservableProperty] + private string _dummyProperty = string.Empty; + public TViewService? TryGetViewService() where TViewService : class { diff --git a/src/RolandK.AvaloniaExtensions.Tests/Mvvm/MvvmWindowTests.cs b/src/RolandK.AvaloniaExtensions.Tests/Mvvm/MvvmWindowTests.cs index aed7a0f..7cbea91 100644 --- a/src/RolandK.AvaloniaExtensions.Tests/Mvvm/MvvmWindowTests.cs +++ b/src/RolandK.AvaloniaExtensions.Tests/Mvvm/MvvmWindowTests.cs @@ -10,7 +10,7 @@ namespace RolandK.AvaloniaExtensions.Tests.Mvvm; [Collection(nameof(ApplicationTestCollection))] -public class MvvmWindowTests +public partial class MvvmWindowTests { [Fact] public async Task Attach_MvvmWindow_to_ViewModel() @@ -234,10 +234,70 @@ public Task Attach_ViewModel_to_multiple_views_throws_InvalidOperationException( }); } + [Fact] + public Task Attach_MvvmWindow_to_ViewModel_then_detach_and_check_events_fired() + { + return UnitTestApplication.RunInApplicationContextAsync(() => + { + // Arrange + var testMvvmWindow = new MvvmWindow(); + var testViewModel = new TestViewModel(); + + var viewModelAttachedEventCount = 0; + var viewModelDetachedEventCount = 0; + testMvvmWindow.ViewModelAttached += (_, _) => viewModelAttachedEventCount++; + testMvvmWindow.ViewModelDetached += (_, _) => viewModelDetachedEventCount++; + + // Act + testMvvmWindow.DataContext = testViewModel; + testMvvmWindow.ViewFor = typeof(TestViewModel); + testMvvmWindow.Show(); + testViewModel.TriggerCloseWindowRequest(); + + // Assert + Assert.Equal(1, viewModelAttachedEventCount); + Assert.Equal(1, viewModelDetachedEventCount); + + // Cleanup + testMvvmWindow.Close(); + }); + } + + [Fact] + public Task Attach_MvvmWindow_to_ViewModel_then_check_ViewModelPropertyChanged_event() + { + return UnitTestApplication.RunInApplicationContextAsync(() => + { + // Arrange + var testMvvmWindow = new MvvmWindow(); + var testViewModel = new TestViewModel(); + + var propertyChangedEventCount = 0; + var lastPropertyChangedEventPropertyName = string.Empty; + testMvvmWindow.ViewModelPropertyChanged += (_, args) => + { + propertyChangedEventCount++; + lastPropertyChangedEventPropertyName = args.PropertyName; + }; + + // Act + testMvvmWindow.DataContext = testViewModel; + testMvvmWindow.ViewFor = typeof(TestViewModel); + testMvvmWindow.Show(); + testViewModel.DummyProperty = "Some other value.."; + + // Assert + Assert.Equal(1, propertyChangedEventCount); + Assert.Equal(nameof(TestViewModel.DummyProperty), lastPropertyChangedEventPropertyName); + + testMvvmWindow.Close(); + }); + } + //************************************************************************* //************************************************************************* //************************************************************************* - private class TestViewModel : ObservableObject, IAttachableViewModel + private partial class TestViewModel : ObservableObject, IAttachableViewModel { /// public event EventHandler? CloseWindowRequest; @@ -248,6 +308,9 @@ private class TestViewModel : ObservableObject, IAttachableViewModel /// public object? AssociatedView { get; set; } + [ObservableProperty] + private string _dummyProperty = string.Empty; + public TViewService? TryGetViewService() where TViewService : class {