Skip to content

Commit d7395c2

Browse files
committed
feat: TaskEditing_Fails_MessageNotifiesUserAndTaskRemainsUnmodified
1 parent 16882bc commit d7395c2

6 files changed

Lines changed: 95 additions & 11 deletions

File tree

Desktop.Tests/TestAPI/TaskFactory.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ public static Task DesktopTask(string named)
1515
return DesktopTask(id: null, named, description: null);
1616
}
1717

18+
public static Task DesktopTask(
19+
string named,
20+
string description)
21+
{
22+
return DesktopTask(
23+
id: Guid.NewGuid(),
24+
named,
25+
description: description);
26+
}
27+
1828
public static Task DesktopTask(
1929
Guid? id = null,
2030
string named = null,
Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
using Desktop.Domain;
1+
using Desktop.Common;
2+
using Desktop.Domain;
23
using Desktop.Tasks;
4+
using Desktop.Tests.TestAPI;
5+
using FluentAssertions;
36
using Moq;
47
using NUnit.Framework;
58
using static Desktop.Tests.TestAPI.TaskFactory;
@@ -11,15 +14,58 @@ public class TaskEditingViewModelTests
1114
[Test]
1215
public void TasksModificationsAreCommandedToBePersisted()
1316
{
14-
var aRepositoryMock = new Mock<ITaskRepository>();
15-
var anotherRepositoryMock = new Mock<ITaskRepository>();
17+
var aRepositoryMock = RepositoryMockThatAlwaysSucceeds();
18+
var anotherRepositoryMock = RepositoryMockThatAlwaysSucceeds();
19+
var sut = TaskEditingViewModel(
20+
taskBeingEdited: AnyDesktopTask(),
21+
repositories: [aRepositoryMock.Object, anotherRepositoryMock.Object]);
1622

17-
var sut = new TaskEditingViewModel(
18-
AnyDesktopTask(),
19-
[aRepositoryMock.Object, anotherRepositoryMock.Object]);
2023
sut.Save.Execute(("New name", "New description"));
2124

2225
aRepositoryMock.Verify(m => m.Save(It.IsAny<Task>()));
2326
anotherRepositoryMock.Verify(m => m.Save(It.IsAny<Task>()));
2427
}
28+
29+
private static Mock<ITaskRepository> RepositoryMockThatAlwaysSucceeds()
30+
{
31+
var mock = new Mock<ITaskRepository>();
32+
mock
33+
.Setup(x => x.Save(It.IsAny<Task>()))
34+
.ReturnsAsync(ResultWithoutValue.Success());
35+
36+
return mock;
37+
}
38+
39+
[Test]
40+
public void TaskEditing_Fails_MessageNotifiesUserAndTaskRemainsUnmodified()
41+
{
42+
var existingTask = DesktopTask("Old name", "Old description");
43+
var repository = new InMemoryTaskRepository([existingTask]);
44+
repository.FailAlways();
45+
var messageNotifierMock = new Mock<IMessageNotifier>();
46+
var sut = TaskEditingViewModel(
47+
taskBeingEdited: existingTask,
48+
messageNotifierMock.Object,
49+
repositories: repository);
50+
51+
sut.Save.Execute(("New name", "New description"));
52+
53+
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."));
57+
existingTask.Name.Should().Be("Old name");
58+
existingTask.Description.Should().Be("Old description");
59+
}
60+
61+
private static TaskEditingViewModel TaskEditingViewModel(
62+
Task taskBeingEdited,
63+
IMessageNotifier messageNotifier = null,
64+
params ITaskRepository[] repositories)
65+
{
66+
return new TaskEditingViewModel(
67+
taskBeingEdited,
68+
messageNotifier ?? new Mock<IMessageNotifier>().Object,
69+
repositories);
70+
}
2571
}

Desktop/MainWindow.xaml.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Windows;
2+
using Desktop.Common;
23
using Desktop.Project;
34
using Desktop.Tasks;
45

@@ -7,14 +8,17 @@ namespace Desktop;
78
public partial class MainWindow : Window
89
{
910
private readonly TaskListViewModel taskListViewModel;
11+
private readonly IMessageNotifier messageNotifier;
1012
private readonly IEnumerable<ITaskRepository> taskRepositories;
1113

1214
public MainWindow(
1315
TaskListViewModel taskListViewModel,
1416
MainWindowViewModel viewModel,
17+
IMessageNotifier messageNotifier,
1518
IEnumerable<ITaskRepository> taskRepositories)
1619
{
1720
this.taskListViewModel = taskListViewModel;
21+
this.messageNotifier = messageNotifier;
1822
this.taskRepositories = taskRepositories;
1923
DataContext = viewModel;
2024

@@ -24,7 +28,7 @@ public MainWindow(
2428

2529
private void NavigateToTaskList()
2630
{
27-
var tasksList = new TasksList(taskListViewModel, taskRepositories);
31+
var tasksList = new TasksList(taskListViewModel, messageNotifier, taskRepositories);
2832
Content.Content = tasksList;
2933
}
3034

Desktop/Project/TasksList.xaml.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
using System.Windows.Controls;
22
using System.Windows.Input;
3+
using Desktop.Common;
34
using Desktop.Tasks;
45
using Task = Desktop.Domain.Task;
56

67
namespace Desktop.Project;
78

89
public partial class TasksList : UserControl
910
{
11+
private readonly IMessageNotifier messageNotifier;
1012
private readonly IEnumerable<ITaskRepository> taskRepositories;
1113

1214
public TasksList(
1315
TaskListViewModel viewModel,
16+
IMessageNotifier messageNotifier,
1417
IEnumerable<ITaskRepository> taskRepositories)
1518
{
19+
this.messageNotifier = messageNotifier;
1620
this.taskRepositories = taskRepositories;
1721
DataContext = viewModel;
1822

@@ -37,7 +41,7 @@ private void Edit(object sender, MouseButtonEventArgs e)
3741
if (sender is not ListBoxItem { DataContext: Task task })
3842
return;
3943

40-
var window = new TaskEditing(task, taskRepositories);
44+
var window = new TaskEditing(task, messageNotifier, taskRepositories);
4145
window.ShowDialog();
4246
}
4347
}

Desktop/Tasks/TaskEditing.xaml.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Windows;
2+
using Desktop.Common;
23
using Task = Desktop.Domain.Task;
34

45
namespace Desktop.Tasks;
@@ -9,11 +10,14 @@ public partial class TaskEditing : Window
910
{
1011
private readonly TaskEditingViewModel viewModel;
1112

12-
public TaskEditing(Task original, IEnumerable<ITaskRepository> taskRepositories)
13+
public TaskEditing(
14+
Task original,
15+
IMessageNotifier messageNotifier,
16+
IEnumerable<ITaskRepository> taskRepositories)
1317
{
1418
InitializeComponent();
1519

16-
viewModel = new TaskEditingViewModel(original, taskRepositories);
20+
viewModel = new TaskEditingViewModel(original, messageNotifier, taskRepositories);
1721

1822
Name.Text = original.Name;
1923
Description.Text = original.Description;

Desktop/Tasks/TaskEditingViewModel.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,33 @@ public class TaskEditingViewModel : ViewModelBase
99
{
1010
public TaskEditingViewModel(
1111
Task taskBeingEdited,
12+
IMessageNotifier messageNotifier,
1213
IEnumerable<ITaskRepository> taskRepositories)
1314
{
1415
Save = new AsyncRelayCommand<(string, string)>(async args =>
1516
{
1617
var (name, description) = args;
1718

19+
var originalName = taskBeingEdited.Name;
1820
taskBeingEdited.Name = name;
21+
var originalDescription = taskBeingEdited.Description;
1922
taskBeingEdited.Description = description;
2023

2124
foreach (var repository in taskRepositories)
22-
await repository.Save(taskBeingEdited);
25+
{
26+
var editingResult = await repository.Save(taskBeingEdited);
27+
28+
if (!editingResult.Succeeded)
29+
{
30+
messageNotifier.Notify(
31+
"Task editing has failed due to " +
32+
"an internal error. The modifications will be reverted. " +
33+
"Please, try again later.");
34+
35+
taskBeingEdited.Name = originalName;
36+
taskBeingEdited.Description = originalDescription;
37+
}
38+
}
2339
});
2440
}
2541

0 commit comments

Comments
 (0)