Skip to content

Commit 02fc237

Browse files
committed
Added various tools and methods for testing Todos
1 parent fa8c511 commit 02fc237

36 files changed

+505
-208
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22

33
# CA1822: Mark members as static
44
dotnet_diagnostic.CA1822.severity = suggestion
5+
6+
# CS8603: Possible null reference return.
7+
dotnet_diagnostic.CS8603.severity = none

Directory.Build.props

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,10 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.0-preview3.19555.2" />
10-
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.0-preview3.19555.2" />
119
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
1210
<PrivateAssets>all</PrivateAssets>
1311
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1412
</PackageReference>
1513
</ItemGroup>
1614

17-
<!--<PropertyGroup>
18-
<AnnotatedReferenceAssemblyVersion>3.0.0</AnnotatedReferenceAssemblyVersion>
19-
</PropertyGroup>
20-
<ItemGroup>
21-
<PackageReference Include="TunnelVisionLabs.ReferenceAssemblyAnnotator" Version="1.0.0-alpha.90" PrivateAssets="all" />
22-
<PackageDownload Include="Microsoft.NETCore.App.Ref" Version="[$(AnnotatedReferenceAssemblyVersion)]" />
23-
</ItemGroup>-->
24-
2515
</Project>
Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
1-
<li class="list-group-item list-group-item-action">
2-
<div class="custom-control custom-checkbox">
3-
<input type="checkbox" class="custom-control-input" @bind=@Todo.IsDone />
4-
<label class="custom-control-label">@Todo.Text</label>
5-
<a class="float-right" href="#">(x)</a>
6-
</div>
1+
<li class="list-group-item list-group-item-action" @onclick="Complete">
2+
<span>@Todo.Text</span>
3+
<span class="float-right text-danger">(click to complete)</span>
74
</li>
85
@code {
9-
[Parameter] public Todo Todo { get; set; } = default!;
6+
private Todo _todo = Todo.Empty;
7+
8+
[Parameter]
9+
public Todo Todo
10+
{
11+
get => _todo;
12+
set => _todo = value ?? throw new ArgumentNullException(nameof(Todo));
13+
}
14+
15+
[Parameter] public EventCallback<int> OnCompleted { get; set; }
16+
17+
protected override void OnParametersSet()
18+
{
19+
if (Todo == Todo.Empty)
20+
throw new ArgumentException($"The {nameof(Todo)} parameter was not set.");
21+
}
22+
23+
private void Complete()
24+
{
25+
OnCompleted.InvokeAsync(Todo.Id);
26+
}
1027
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
@inject IJSRuntime jsRuntime
2+
3+
<form @onsubmit="AddTask">
4+
<div class="input-group">
5+
<input type="text" class="form-control" placeholder=@Label aria-label=@Label
6+
@bind="NewTaskValue" @ref="_inputRef" />
7+
<div class="input-group-append">
8+
<button class="btn btn-secondary" type="submit">Add task</button>
9+
</div>
10+
</div>
11+
</form>
12+
<ol class="list-group">
13+
@foreach (var item in Items)
14+
{
15+
@ItemsTemplate(item);
16+
}
17+
</ol>
18+
19+
@code{
20+
private ElementReference _inputRef;
21+
private string NewTaskValue { get; set; } = string.Empty;
22+
23+
[Parameter] public string Label { get; set; } = "Task description";
24+
[Parameter] public RenderFragment<Todo> ItemsTemplate { get; set; } = default!;
25+
[Parameter] public IEnumerable<Todo> Items { get; set; } = Array.Empty<Todo>();
26+
[Parameter] public EventCallback<Todo> OnAddingTodo { get; set; }
27+
28+
protected override async Task OnAfterRenderAsync(bool firstRender)
29+
{
30+
if (firstRender)
31+
{
32+
await jsRuntime.InvokeVoidAsync("document.body.focus.call", _inputRef);
33+
}
34+
}
35+
36+
private void AddTask()
37+
{
38+
if (!string.IsNullOrWhiteSpace(NewTaskValue))
39+
{
40+
OnAddingTodo.InvokeAsync(new Todo { Text = NewTaskValue });
41+
}
42+
NewTaskValue = string.Empty;
43+
}
44+
}

sample/src/Data/ITodoService.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
4+
namespace Egil.RazorComponents.Testing.Library.SampleApp.Data
5+
{
6+
public interface ITodoService
7+
{
8+
void Add(Todo todo);
9+
void Complete(int todoId);
10+
Task<IReadOnlyList<Todo>> Get();
11+
}
12+
}

sample/src/Data/Todo.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public class Todo
44
{
55
public int Id { get; set; }
66
public string Text { get; set; } = string.Empty;
7-
public bool IsDone { get; set; }
7+
8+
public static readonly Todo Empty = new Todo();
89
}
910
}

sample/src/Data/TodoService.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,24 @@
66

77
namespace Egil.RazorComponents.Testing.Library.SampleApp.Data
88
{
9-
public class TodoService
9+
public class TodoService : ITodoService
1010
{
11-
public Task<Todo[]> Get()
11+
private readonly List<Todo> _todos = new List<Todo>();
12+
13+
public Task<IReadOnlyList<Todo>> Get()
14+
{
15+
return Task.FromResult((IReadOnlyList<Todo>)_todos);
16+
}
17+
18+
public void Add(Todo todo)
1219
{
13-
return Task.FromResult(Array.Empty<Todo>());
20+
todo.Id = _todos.Count;
21+
_todos.Add(todo);
1422
}
1523

16-
public void Add(Todo todo) { }
17-
public void Update(Todo todo) { }
18-
public void Delete(Todo todo) { }
24+
public void Complete(int todoId)
25+
{
26+
_todos.RemoveAll(x => x.Id == todoId);
27+
}
1928
}
2029
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.0</TargetFramework>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
55
</PropertyGroup>
66

77
</Project>

sample/src/Pages/TodoList.razor

Lines changed: 0 additions & 33 deletions
This file was deleted.

sample/src/Pages/Todos.razor

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
@page "/todos"
2+
@inject ITodoService todoService
3+
4+
<h1>Todos</h1>
5+
<p>Be smart - todo!</p>
6+
7+
<TodoList OnAddingTodo="AddingTodo" Items="_todos">
8+
<ItemsTemplate>
9+
<TodoItem Todo=@context OnCompleted="CompleteTodo" />
10+
<TodoItem Todo=@context OnCompleted=@(id => CompleteTodo(id)) />
11+
</ItemsTemplate>
12+
</TodoList>
13+
14+
@code {
15+
IReadOnlyList<Todo> _todos = Array.Empty<Todo>();
16+
17+
protected override async Task OnInitializedAsync()
18+
{
19+
_todos = await todoService.Get();
20+
}
21+
22+
private async Task AddingTodo(Todo todo)
23+
{
24+
todoService.Add(todo);
25+
_todos = await todoService.Get();
26+
}
27+
28+
private async Task CompleteTodo(int todoId)
29+
{
30+
todoService.Complete(todoId);
31+
_todos = await todoService.Get();
32+
}
33+
}

0 commit comments

Comments
 (0)