Skip to content

Commit 9a742b3

Browse files
authored
Merge pull request #444 from nventive/dev/thla/fix-add-vm-disposal-tests
fix: Add VM Disposal in Tests
2 parents dc34a54 + 13cc4c3 commit 9a742b3

File tree

5 files changed

+53
-4
lines changed

5 files changed

+53
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Prefix your items with `(Template)` if the change is about the template and not
1515
- Updated the Segoe MDL2 Assets font to fix an issue with the Flip View icons on Windows.
1616
- Optimized the .NET workloads install process.
1717
- Fixed the iOS application icon size.
18+
- Added VM Disposal in Functional Tests.
1819

1920
## 3.8.X
2021
- Updated from .NET 8 to .NET 9.

src/app/ApplicationTemplate.Presentation/ViewModels/Extensions/ISectionsNavigator.Extensions.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Linq;
33
using System.Reactive.Linq;
4+
using System.Threading.Tasks;
5+
using System.Threading;
46
using Chinook.StackNavigation;
57

68
namespace Chinook.SectionsNavigation;
@@ -51,4 +53,43 @@ public static IObservable<string> ObserveActiveSectionName(this ISectionsNavigat
5153
var state = args.EventArgs.CurrentState;
5254
return state.ActiveSection.Name;
5355
});
56+
57+
/// <summary>
58+
/// Clears the sections and sets the active section to <paramref name="activeSectionName"/> if not null or empty.
59+
/// </summary>
60+
/// <param name="sectionsNavigator"><see cref="ISectionsNavigator"/>.</param>
61+
/// <param name="ct">The cancellation token.</param>
62+
/// <param name="activeSectionName">The active section name to set after clearing them all.</param>
63+
/// <remarks>
64+
/// There are good chances that the page requesting this operation gets disposed, so you should use <see cref="CancellationToken.None"/>.
65+
/// <br/>
66+
/// If you don't want any section set as active don't provide <paramref name="activeSectionName"/>.
67+
/// </remarks>
68+
public static async Task ClearSections(this ISectionsNavigator sectionsNavigator, CancellationToken ct, string activeSectionName = default)
69+
{
70+
// Clearing those pages prevents bindings from updating in background.
71+
// For better performance, it's better to re-create the pages when we'll need them.
72+
foreach (var section in sectionsNavigator.State.Sections.Values)
73+
{
74+
await section.Clear(ct);
75+
}
76+
77+
if (!string.IsNullOrEmpty(activeSectionName))
78+
{
79+
await sectionsNavigator.SetActiveSection(ct, activeSectionName);
80+
}
81+
}
82+
83+
/// <summary>
84+
/// Closes all modals.
85+
/// </summary>
86+
/// <param name="sectionsNavigator"><see cref="ISectionsNavigator"/>.</param>
87+
/// <param name="ct">The cancellation token.</param>
88+
public static async Task CloseModals(this ISectionsNavigator sectionsNavigator, CancellationToken ct)
89+
{
90+
foreach (var modal in sectionsNavigator.State.Modals)
91+
{
92+
await sectionsNavigator.CloseModal(ct, SectionsNavigatorRequest.GetCloseModalRequest(modalName: modal.Name));
93+
}
94+
}
5495
}

src/app/ApplicationTemplate.Tests.Functional/ForceUpdate/ForceUpdatesShould.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
using System.Reactive.Linq;
33
using System.Threading.Tasks;
44
using ApplicationTemplate.DataAccess;
5+
using ApplicationTemplate.Tests;
56

6-
namespace ApplicationTemplate.Tests;
7+
namespace ForceUpdate;
78

89
/// <summary>
910
/// Tests for the forced update flow.

src/app/ApplicationTemplate.Tests.Functional/FunctionalTestBase.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,16 @@ async Task IAsyncLifetime.InitializeAsync()
241241
ViewModelBase.DefaultServiceProvider.Should().BeSameAs(_coreStartup.ServiceProvider, because: "We want the ViewModels of this test to use the services that we just initialized.");
242242
}
243243

244-
Task IAsyncLifetime.DisposeAsync()
244+
async Task IAsyncLifetime.DisposeAsync()
245245
{
246+
// To prevent scheduled tasks in view models from running after the test is completed.
247+
// Once the test is finished, the view models should no longer be active.
248+
// Therefore, we clear the stack navigator, which disposes of all view models.
249+
await GetService<ISectionsNavigator>().CloseModals(CancellationToken.None);
250+
await GetService<ISectionsNavigator>().ClearSections(CancellationToken.None);
251+
246252
_coreStartup.Dispose();
247253
_backButtonSource.Dispose();
248-
return Task.CompletedTask;
249254
}
250255

251256
private sealed class FunctionalTestBackButtonSource : IBackButtonSource

src/app/ApplicationTemplate.Tests.Functional/Posts/EditPostPageViewModelShould.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
using System.Linq;
33
using System.Threading;
44
using System.Threading.Tasks;
5+
using ApplicationTemplate.Tests;
56
using Xunit.Abstractions;
67

7-
namespace ApplicationTemplate.Tests;
8+
namespace Posts;
89

910
public sealed class EditPostPageViewModelShould : FunctionalTestBase
1011
{

0 commit comments

Comments
 (0)