Skip to content

Commit 3a0857f

Browse files
committed
feat: TaskDeletion_FailsForOne_NotifiesOnceAndAllowsDeletingItToTheRestOfRepositories
1 parent 9114f04 commit 3a0857f

2 files changed

Lines changed: 59 additions & 14 deletions

File tree

Desktop.Tests/UnitTests/TaskListViewModelTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,41 @@ public void TaskDeletion_Fails_MessageNotifiesUserAndTaskRemains()
8787
sut.Tasks.Should().Contain(existingTask);
8888
}
8989

90+
[Test]
91+
public async Task
92+
TaskDeletion_FailsForOne_NotifiesOnceAndAllowsDeletingItToTheRestOfRepositories()
93+
{
94+
// Arrange.
95+
var existingTask = DesktopTask();
96+
var aRepository =
97+
new InMemoryTaskRepository([existingTask]);
98+
var anotherRepository =
99+
new InMemoryTaskRepository([existingTask]);
100+
101+
var messageNotifierMock = new Mock<IMessageNotifier>();
102+
103+
var sut = TaskListViewModel(
104+
[aRepository, anotherRepository],
105+
messageNotifierMock.Object);
106+
sut.PopulateTasks();
107+
aRepository.FailAlways();
108+
109+
// Act.
110+
sut.Delete.Execute(existingTask);
111+
112+
// Assert.
113+
messageNotifierMock.Verify(
114+
x => x.Notify(
115+
"Task deletion has failed due to " +
116+
"an internal error. The Task won't be deleted. " +
117+
"Please, try again later."),
118+
Times.Once);
119+
sut.Tasks.Should().BeEmpty();
120+
121+
var result = await anotherRepository.All();
122+
result.Tasks.Should().BeEmpty();
123+
}
124+
90125
private static TaskListViewModel TaskListViewModel(
91126
params InMemoryTaskRepository[] taskRepositories)
92127
{
@@ -106,4 +141,14 @@ private static TaskListViewModel TaskListViewModel(
106141
new TaskCreationViewModel(messageNotifier, [taskRepository]),
107142
messageNotifier);
108143
}
144+
145+
private static TaskListViewModel TaskListViewModel(
146+
InMemoryTaskRepository[] taskRepositories,
147+
IMessageNotifier messageNotifier)
148+
{
149+
return new TaskListViewModel(
150+
taskRepositories,
151+
new TaskCreationViewModel(messageNotifier, taskRepositories),
152+
messageNotifier);
153+
}
109154
}

Desktop/Project/TaskListViewModel.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Desktop.Project;
1010

1111
public class TaskListViewModel : ViewModelBase
1212
{
13-
private readonly IEnumerable<ITaskRepository> taskRepositories;
13+
private readonly IReadOnlyList<ITaskRepository> taskRepositories;
1414
private readonly IMessageNotifier messageNotifier;
1515
public ObservableCollection<Task> Tasks { get; } = [];
1616

@@ -21,7 +21,7 @@ public TaskListViewModel(
2121
TaskCreationViewModel taskCreationViewModel,
2222
IMessageNotifier messageNotifier)
2323
{
24-
this.taskRepositories = taskRepositories;
24+
this.taskRepositories = taskRepositories.ToArray();
2525
this.messageNotifier = messageNotifier;
2626

2727
Add = new RelayCommand(() =>
@@ -36,18 +36,18 @@ public TaskListViewModel(
3636

3737
Delete = new AsyncRelayCommand<Task>(async taskToRemove =>
3838
{
39-
foreach (var repository in this.taskRepositories)
40-
{
41-
var deletion = await repository.Delete(taskToRemove!);
42-
43-
if (deletion.Succeeded)
44-
Tasks.Remove(taskToRemove!);
45-
else
46-
messageNotifier.Notify(
47-
"Task deletion has failed due to " +
48-
"an internal error. The Task won't be deleted. " +
49-
"Please, try again later.");
50-
}
39+
var deletingTasks = this.taskRepositories
40+
.Select(x => x.Delete(taskToRemove!));
41+
var results = await SystemTask.WhenAll(deletingTasks);
42+
43+
if (results.Any(x => !x.Succeeded))
44+
messageNotifier.Notify(
45+
"Task deletion has failed due to " +
46+
"an internal error. The Task won't be deleted. " +
47+
"Please, try again later.");
48+
49+
if (results.Any(x => x.Succeeded))
50+
Tasks.Remove(taskToRemove!);
5151
});
5252
}
5353

0 commit comments

Comments
 (0)