Skip to content

Commit 4b0b0a5

Browse files
committed
feat: TaskCreation_Fails_MessageNotifiesUserAndTasksIsNotAddedToTheList
1 parent d7395c2 commit 4b0b0a5

3 files changed

Lines changed: 55 additions & 8 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Desktop.Common;
2+
using Desktop.Tasks;
3+
using Desktop.Tests.TestAPI;
4+
using FluentAssertions;
5+
using Moq;
6+
using NUnit.Framework;
7+
using static Desktop.Tests.TestAPI.TaskFactory;
8+
9+
namespace Desktop.Tests.UnitTests;
10+
11+
public class TaskCreationViewModelTests
12+
{
13+
[Test]
14+
public void TaskCreation_Fails_MessageNotifiesUserAndTasksIsNotAddedToTheList()
15+
{
16+
var repository = new InMemoryTaskRepository([AnyDesktopTask()]);
17+
repository.FailAlways();
18+
var messageNotifierMock = new Mock<IMessageNotifier>();
19+
var sut = new TaskCreationViewModel(
20+
messageNotifierMock.Object,
21+
[repository]);
22+
var closeable = new Mock<ICloseable>();
23+
24+
sut.SaveTask.Execute((closeable.Object, "Any name", "Any description"));
25+
26+
messageNotifierMock.Verify(x => x.Notify(
27+
"Task creation has failed due to " +
28+
"an internal error. The Task won't be created. " +
29+
"Please, try again later."));
30+
sut.CreatedTask.Should().BeNull();
31+
}
32+
}

Desktop.Tests/UnitTests/TaskListViewModelTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ public void TaskDeletion_Fails_MessageNotifiesUserAndTaskRemains()
6969
var existingTask = DesktopTask();
7070
var backendRepository =
7171
new InMemoryTaskRepository([existingTask]);
72-
72+
7373
var messageNotifierMock = new Mock<IMessageNotifier>();
74-
74+
7575
var sut = TaskListViewModel(backendRepository, messageNotifierMock.Object);
7676
sut.PopulateTasks();
7777
backendRepository.FailAlways();
78-
78+
7979
// Act.
8080
sut.Delete.Execute(existingTask);
8181

@@ -90,10 +90,11 @@ public void TaskDeletion_Fails_MessageNotifiesUserAndTaskRemains()
9090
private static TaskListViewModel TaskListViewModel(
9191
params InMemoryTaskRepository[] taskRepositories)
9292
{
93+
var messageNotifier = new Mock<IMessageNotifier>().Object;
9394
return new TaskListViewModel(
9495
taskRepositories,
95-
new TaskCreationViewModel(taskRepositories),
96-
new Mock<IMessageNotifier>().Object);
96+
new TaskCreationViewModel(messageNotifier, taskRepositories),
97+
messageNotifier);
9798
}
9899

99100
private static TaskListViewModel TaskListViewModel(
@@ -102,7 +103,7 @@ private static TaskListViewModel TaskListViewModel(
102103
{
103104
return new TaskListViewModel(
104105
[taskRepository],
105-
new TaskCreationViewModel([taskRepository]),
106+
new TaskCreationViewModel(messageNotifier, [taskRepository]),
106107
messageNotifier);
107108
}
108109
}

Desktop/Tasks/TaskCreationViewModel.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ namespace Desktop.Tasks;
66

77
public class TaskCreationViewModel : ViewModelBase
88
{
9-
public TaskCreationViewModel(IEnumerable<ITaskRepository> taskRepositories)
9+
public TaskCreationViewModel(
10+
IMessageNotifier messageNotifier,
11+
IEnumerable<ITaskRepository> taskRepositories)
1012
{
1113
SaveTask =
1214
new AsyncRelayCommand<(ICloseable, string, string)>(async args =>
@@ -17,7 +19,19 @@ public TaskCreationViewModel(IEnumerable<ITaskRepository> taskRepositories)
1719

1820
foreach (var repository in taskRepositories)
1921
{
20-
await repository.Save(CreatedTask);
22+
var creationResult = await repository.Save(CreatedTask);
23+
24+
if (!creationResult.Succeeded)
25+
{
26+
messageNotifier.Notify(
27+
"Task creation has failed due to " +
28+
"an internal error. The Task won't be created. " +
29+
"Please, try again later.");
30+
31+
CreatedTask = null;
32+
33+
break;
34+
}
2135
}
2236

2337
closeable!.Close();

0 commit comments

Comments
 (0)