Skip to content

Commit afe7f4c

Browse files
committed
feat: TaskEditing_FailsForOne_NotifiesOnceAndAllowsEditingItToTheRestOfRepositories
1 parent aaa2ba2 commit afe7f4c

2 files changed

Lines changed: 62 additions & 15 deletions

File tree

Desktop.Tests/UnitTests/TaskEditingViewModelTests.cs

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using Desktop.Common;
2-
using Desktop.Domain;
1+
using System.Threading.Tasks;
2+
using Desktop.Common;
33
using Desktop.Tasks;
44
using Desktop.Tests.TestAPI;
55
using FluentAssertions;
@@ -11,6 +11,11 @@ namespace Desktop.Tests.UnitTests;
1111

1212
public class TaskEditingViewModelTests
1313
{
14+
private const string TaskEditingFailedMessage =
15+
"Task editing has failed due to " +
16+
"an internal error. The modifications will be reverted. " +
17+
"Please, try again later.";
18+
1419
[Test]
1520
public void TasksModificationsAreCommandedToBePersisted()
1621
{
@@ -22,15 +27,15 @@ public void TasksModificationsAreCommandedToBePersisted()
2227

2328
sut.Save.Execute(("New name", "New description"));
2429

25-
aRepositoryMock.Verify(m => m.Save(It.IsAny<Task>()));
26-
anotherRepositoryMock.Verify(m => m.Save(It.IsAny<Task>()));
30+
aRepositoryMock.Verify(m => m.Save(It.IsAny<Domain.Task>()));
31+
anotherRepositoryMock.Verify(m => m.Save(It.IsAny<Domain.Task>()));
2732
}
2833

2934
private static Mock<ITaskRepository> RepositoryMockThatAlwaysSucceeds()
3035
{
3136
var mock = new Mock<ITaskRepository>();
3237
mock
33-
.Setup(x => x.Save(It.IsAny<Task>()))
38+
.Setup(x => x.Save(It.IsAny<Domain.Task>()))
3439
.ReturnsAsync(ResultWithoutValue.Success());
3540

3641
return mock;
@@ -51,15 +56,47 @@ public void TaskEditing_Fails_MessageNotifiesUserAndTaskRemainsUnmodified()
5156
sut.Save.Execute(("New name", "New description"));
5257

5358
messageNotifierMock.Verify(x => x.Notify(
54-
"Task editing has failed due to " +
55-
"an internal error. The modifications will be reverted. " +
56-
"Please, try again later."));
59+
TaskEditingFailedMessage));
5760
existingTask.Name.Should().Be("Old name");
5861
existingTask.Description.Should().Be("Old description");
5962
}
6063

64+
[Test]
65+
public async Task
66+
TaskEditing_FailsForOne_NotifiesOnceAndAllowsEditingItToTheRestOfRepositories()
67+
{
68+
// Arrange
69+
var existingTask = DesktopTask("Old name", "Old description");
70+
var aRepository = new InMemoryTaskRepository([existingTask]);
71+
aRepository.FailAlways();
72+
73+
var anotherRepository = new InMemoryTaskRepository([existingTask]);
74+
75+
var messageNotifierMock = new Mock<IMessageNotifier>();
76+
var sut = TaskEditingViewModel(
77+
taskBeingEdited: existingTask,
78+
messageNotifierMock.Object,
79+
repositories: [aRepository, anotherRepository]);
80+
81+
// Act.
82+
var newName = "New name";
83+
var newDescription = "New description";
84+
sut.Save.Execute((newName, newDescription));
85+
86+
// Assert.
87+
messageNotifierMock.Verify(
88+
x => x.Notify(TaskEditingFailedMessage),
89+
Times.Once);
90+
existingTask.Name.Should().Be(newName);
91+
existingTask.Description.Should().Be(newDescription);
92+
93+
var result = await anotherRepository.All();
94+
result.Tasks.Should().Contain(
95+
DesktopTask(id: existingTask.Id, newName, newDescription));
96+
}
97+
6198
private static TaskEditingViewModel TaskEditingViewModel(
62-
Task taskBeingEdited,
99+
Domain.Task taskBeingEdited,
63100
IMessageNotifier messageNotifier = null,
64101
params ITaskRepository[] repositories)
65102
{

Desktop/Tasks/TaskEditingViewModel.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,33 @@ public TaskEditingViewModel(
2121
var originalDescription = taskBeingEdited.Description;
2222
taskBeingEdited.Description = description;
2323

24+
var anySucceeded = false;
2425
foreach (var repository in taskRepositories)
2526
{
2627
var editingResult = await repository.Save(taskBeingEdited);
2728

28-
if (!editingResult.Succeeded)
29-
{
29+
if (editingResult.Succeeded)
30+
anySucceeded = true;
31+
else
3032
messageNotifier.Notify(
3133
"Task editing has failed due to " +
3234
"an internal error. The modifications will be reverted. " +
3335
"Please, try again later.");
34-
35-
taskBeingEdited.Name = originalName;
36-
taskBeingEdited.Description = originalDescription;
37-
}
3836
}
37+
38+
if (!anySucceeded)
39+
RevertEdit(taskBeingEdited, originalName, originalDescription);
3940
});
4041
}
4142

43+
private static void RevertEdit(
44+
Task taskBeingEdited,
45+
string originalName,
46+
string originalDescription)
47+
{
48+
taskBeingEdited.Name = originalName;
49+
taskBeingEdited.Description = originalDescription;
50+
}
51+
4252
public ICommand Save { get; }
4353
}

0 commit comments

Comments
 (0)