Skip to content

Commit

Permalink
Added unittests for new events
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandKoenig committed Jan 21, 2024
1 parent 5dbc56f commit df299ae
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 4 deletions.
66 changes: 64 additions & 2 deletions src/RolandK.AvaloniaExtensions.Tests/Mvvm/MvvmUserControlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
{
/// <inheritdoc />
public event EventHandler<CloseWindowRequestEventArgs>? CloseWindowRequest;
Expand All @@ -186,6 +245,9 @@ private class TestViewModel : ObservableObject, IAttachableViewModel
/// <inheritdoc />
public object? AssociatedView { get; set; }

[ObservableProperty]
private string _dummyProperty = string.Empty;

public TViewService? TryGetViewService<TViewService>()
where TViewService : class
{
Expand Down
67 changes: 65 additions & 2 deletions src/RolandK.AvaloniaExtensions.Tests/Mvvm/MvvmWindowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
{
/// <inheritdoc />
public event EventHandler<CloseWindowRequestEventArgs>? CloseWindowRequest;
Expand All @@ -248,6 +308,9 @@ private class TestViewModel : ObservableObject, IAttachableViewModel
/// <inheritdoc />
public object? AssociatedView { get; set; }

[ObservableProperty]
private string _dummyProperty = string.Empty;

public TViewService? TryGetViewService<TViewService>()
where TViewService : class
{
Expand Down

0 comments on commit df299ae

Please sign in to comment.