Skip to content

Commit 61f98cc

Browse files
committed
fix: TasksPopulation_IsIdempotent
1 parent 33ee64c commit 61f98cc

5 files changed

Lines changed: 86 additions & 2 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using Desktop.Tasks;
4+
5+
namespace Desktop.Tests.TestAPI;
6+
7+
using SystemTask = Task;
8+
9+
public class InMemoryTaskRepository : ITaskRepository
10+
{
11+
private readonly List<Domain.Task> tasks = [];
12+
13+
public InMemoryTaskRepository(IEnumerable<Domain.Task> with)
14+
{
15+
tasks.AddRange(with);
16+
}
17+
18+
public Task<IReadOnlyList<Domain.Task>> All()
19+
{
20+
return SystemTask.FromResult<IReadOnlyList<Domain.Task>>(tasks.ToArray());
21+
}
22+
23+
public SystemTask Save(Domain.Task task)
24+
{
25+
tasks.Add(task);
26+
27+
return SystemTask.CompletedTask;
28+
}
29+
30+
public SystemTask Delete(Domain.Task toBeDeleted)
31+
{
32+
tasks.Remove(toBeDeleted);
33+
34+
return SystemTask.CompletedTask;
35+
}
36+
}

Desktop.Tests/TestAPI/Utils.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace Desktop.Tests.TestAPI;
4+
5+
public class Utils
6+
{
7+
public const int SeveralTimes = 5;
8+
9+
public static void Repeat(Action what, int times)
10+
{
11+
for (var i = 0; i < times; i++)
12+
what();
13+
}
14+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Desktop.Project;
4+
using Desktop.Tasks;
5+
using Desktop.Tests.TestAPI;
6+
using FluentAssertions;
7+
using NUnit.Framework;
8+
using static Desktop.Tests.TestAPI.TaskFactory;
9+
using static Desktop.Tests.TestAPI.Utils;
10+
11+
namespace Desktop.Tests.UnitTests;
12+
13+
public class TaskListViewModelTests
14+
{
15+
[Test]
16+
[Category("Regression")]
17+
public async Task TasksPopulation_IsIdempotent()
18+
{
19+
var taskRepository = new InMemoryTaskRepository([DesktopTask()]);
20+
var sut = TaskListViewModel(taskRepository);
21+
22+
Repeat(sut.PopulateTasks, SeveralTimes);
23+
24+
sut.Tasks.Should().BeEquivalentTo(await taskRepository.All());
25+
}
26+
27+
private static TaskListViewModel TaskListViewModel(InMemoryTaskRepository taskRepository)
28+
{
29+
return new TaskListViewModel(
30+
taskRepository,
31+
new TaskCreationViewModel(taskRepository));
32+
}
33+
}

Desktop/Project/TaskListViewModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public TaskListViewModel(
4444

4545
public void PopulateTasks()
4646
{
47+
Tasks.Clear();
48+
4749
// TODO: load this async and deferred from the ctor.
4850
var retrievedTasks = SystemTask.Run(taskRepository.All).Result;
4951
foreach (var task in retrievedTasks)

Desktop/TODO.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
- fix: Tasks are duplicated when reloading Task List view
2-
- docs: project directory structure: vertical slicing vs by tech.
1+
- docs: project directory structure: vertical slicing vs by tech.
32
- docs: the focus on UX with the accelerators/shortcuts.
43
- docs: when and why use comments & summaries.
54
- refactor: use Commands and ViewModels instead of code behinds.

0 commit comments

Comments
 (0)