1
0
mirror of https://github.com/chylex/.NET-Community-Toolkit.git synced 2025-04-10 11:15:45 +02:00

Add unit tests for Execute raising CanExecuteChanged if needed

This commit is contained in:
Sergio Pedri 2022-02-14 21:03:34 +01:00
parent 3c71d4b401
commit d776a1ecda
2 changed files with 184 additions and 0 deletions
tests/CommunityToolkit.Mvvm.UnitTests

View File

@ -325,4 +325,97 @@ async void TestCallback(Action throwAction, Action completeAction)
Assert.IsTrue(success);
}
// See https://github.com/CommunityToolkit/dotnet/issues/108
[TestMethod]
public void Test_AsyncRelayCommand_ExecuteDoesNotRaisesCanExecuteChanged()
{
TaskCompletionSource<object?> tcs = new();
AsyncRelayCommand command = new(() => tcs.Task, allowConcurrentExecutions: true);
(object? Sender, EventArgs? Args) args = default;
command.CanExecuteChanged += (s, e) => args = (s, e);
Assert.IsTrue(command.CanExecute(null));
command.Execute(null);
Assert.IsNull(args.Sender);
Assert.IsNull(args.Args);
Assert.IsTrue(command.CanExecute(null));
tcs.SetResult(null);
}
[TestMethod]
public void Test_AsyncRelayCommand_ExecuteWithoutConcurrencyRaisesCanExecuteChanged()
{
TaskCompletionSource<object?> tcs = new();
AsyncRelayCommand command = new(() => tcs.Task, allowConcurrentExecutions: false);
(object? Sender, EventArgs? Args) args = default;
command.CanExecuteChanged += (s, e) => args = (s, e);
Assert.IsTrue(command.CanExecute(null));
command.Execute(null);
Assert.AreSame(command, args.Sender);
Assert.AreSame(EventArgs.Empty, args.Args);
Assert.IsFalse(command.CanExecute(null));
tcs.SetResult(null);
}
[TestMethod]
public void Test_AsyncRelayCommand_ExecuteDoesNotRaisesCanExecuteChanged_WithCancellation()
{
TaskCompletionSource<object?> tcs = new();
AsyncRelayCommand command = new(token => tcs.Task, allowConcurrentExecutions: true);
(object? Sender, EventArgs? Args) args = default;
command.CanExecuteChanged += (s, e) => args = (s, e);
Assert.IsTrue(command.CanExecute(null));
command.Execute(null);
Assert.IsNull(args.Sender);
Assert.IsNull(args.Args);
Assert.IsTrue(command.CanExecute(null));
tcs.SetResult(null);
}
[TestMethod]
public void Test_AsyncRelayCommand_ExecuteWithoutConcurrencyRaisesCanExecuteChanged_WithToken()
{
TaskCompletionSource<object?> tcs = new();
AsyncRelayCommand command = new(token => tcs.Task, allowConcurrentExecutions: false);
(object? Sender, EventArgs? Args) args = default;
command.CanExecuteChanged += (s, e) => args = (s, e);
Assert.IsTrue(command.CanExecute(null));
command.Execute(null);
Assert.AreSame(command, args.Sender);
Assert.AreSame(EventArgs.Empty, args.Args);
Assert.IsFalse(command.CanExecute(null));
tcs.SetResult(null);
}
}

View File

@ -223,4 +223,95 @@ async void TestCallback(Action throwAction, Action completeAction)
Assert.IsTrue(success);
}
public void Test_AsyncRelayCommand_ExecuteDoesNotRaisesCanExecuteChanged()
{
TaskCompletionSource<object?> tcs = new();
AsyncRelayCommand<string> command = new(s => tcs.Task, allowConcurrentExecutions: true);
(object? Sender, EventArgs? Args) args = default;
command.CanExecuteChanged += (s, e) => args = (s, e);
Assert.IsTrue(command.CanExecute(""));
command.Execute("");
Assert.IsNull(args.Sender);
Assert.IsNull(args.Args);
Assert.IsTrue(command.CanExecute(""));
tcs.SetResult(null);
}
[TestMethod]
public void Test_AsyncRelayCommand_ExecuteWithoutConcurrencyRaisesCanExecuteChanged()
{
TaskCompletionSource<object?> tcs = new();
AsyncRelayCommand<string> command = new(s => tcs.Task, allowConcurrentExecutions: false);
(object? Sender, EventArgs? Args) args = default;
command.CanExecuteChanged += (s, e) => args = (s, e);
Assert.IsTrue(command.CanExecute(""));
command.Execute("");
Assert.AreSame(command, args.Sender);
Assert.AreSame(EventArgs.Empty, args.Args);
Assert.IsFalse(command.CanExecute(""));
tcs.SetResult(null);
}
[TestMethod]
public void Test_AsyncRelayCommand_ExecuteDoesNotRaisesCanExecuteChanged_WithCancellation()
{
TaskCompletionSource<object?> tcs = new();
AsyncRelayCommand<string> command = new((s, token) => tcs.Task, allowConcurrentExecutions: true);
(object? Sender, EventArgs? Args) args = default;
command.CanExecuteChanged += (s, e) => args = (s, e);
Assert.IsTrue(command.CanExecute(""));
command.Execute("");
Assert.IsNull(args.Sender);
Assert.IsNull(args.Args);
Assert.IsTrue(command.CanExecute(""));
tcs.SetResult(null);
}
[TestMethod]
public void Test_AsyncRelayCommand_ExecuteWithoutConcurrencyRaisesCanExecuteChanged_WithToken()
{
TaskCompletionSource<object?> tcs = new();
AsyncRelayCommand<string> command = new((s, token) => tcs.Task, allowConcurrentExecutions: false);
(object? Sender, EventArgs? Args) args = default;
command.CanExecuteChanged += (s, e) => args = (s, e);
Assert.IsTrue(command.CanExecute(""));
command.Execute("");
Assert.AreSame(command, args.Sender);
Assert.AreSame(EventArgs.Empty, args.Args);
Assert.IsFalse(command.CanExecute(""));
tcs.SetResult(null);
}
}