Skip to content

Commit 92b8ed0

Browse files
committed
CascadingValue tests.
1 parent 4370a85 commit 92b8ed0

38 files changed

+763
-248
lines changed

sample/src/Components/NoArgs.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Hello world</h1>

sample/src/Components/ThemeInfo.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Egil.RazorComponents.Testing.Library.SampleApp.Components
8+
{
9+
public class ThemeInfo
10+
{
11+
private const string DEFAULT_BUTTON_CLASS = "btn btn-primary";
12+
private string _buttonClass = DEFAULT_BUTTON_CLASS;
13+
14+
public string Class { get => _buttonClass; set => _buttonClass = value ?? DEFAULT_BUTTON_CLASS; }
15+
}
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<button class=@(Theme?.Class ?? "") @attributes="Attributes">
2+
@ChildContent
3+
</button>
4+
@code {
5+
[Parameter(CaptureUnmatchedValues = true)]
6+
public IReadOnlyDictionary<string, object> Attributes { get; set; }
7+
8+
[CascadingParameter]
9+
public ThemeInfo Theme { get; set; }
10+
11+
[Parameter]
12+
public RenderFragment ChildContent { get; set; }
13+
14+
protected override void OnAfterRender(bool firstRender)
15+
{
16+
base.OnAfterRender(firstRender);
17+
}
18+
}

sample/src/Components/TodoItem.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<li class="list-group-item list-group-item-action" @onclick="Complete">
1+
<li id="todo-@(Todo.Id)" class="list-group-item list-group-item-action" @onclick="Complete">
22
<span>@Todo.Text</span>
33
<span class="float-right text-danger">(click to complete)</span>
44
</li>

sample/src/Components/TodoList.razor

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
@code {
1919
private ElementReference _inputRef;
2020
private string NewTaskValue { get; set; } = string.Empty;
21+
private IEnumerable<Todo> _todos = Array.Empty<Todo>();
2122

2223
[Parameter] public string Label { get; set; } = "Task description";
2324
[Parameter] public RenderFragment<Todo> ItemsTemplate { get; set; } = default!;
24-
[Parameter] public IEnumerable<Todo> Items { get; set; } = Array.Empty<Todo>();
25+
[Parameter] public IEnumerable<Todo> Items { get => _todos; set => _todos = value ?? Array.Empty<Todo>(); }
2526
[Parameter] public EventCallback<Todo> OnAddingTodo { get; set; }
2627

2728
protected override async Task OnAfterRenderAsync(bool firstRender)

sample/src/Pages/Index.razor

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,12 @@
33
<h1>Hello, world!</h1>
44

55
Welcome to your new app.
6+
7+
<CascadingValue Value="theme" IsFixed="true">
8+
<ThemedButton>TEST TEST TEST</ThemedButton>
9+
</CascadingValue>
10+
11+
12+
@code {
13+
private ThemeInfo theme = new ThemeInfo { Class = "btn btn-primary" };
14+
}

sample/src/Pages/Todos.razor

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
<TodoList OnAddingTodo="AddingTodo" Items="_todos">
88
<ItemsTemplate>
99
<TodoItem Todo=@context OnCompleted="CompleteTodo" />
10-
<TodoItem Todo=@context OnCompleted=@(id => CompleteTodo(id)) />
1110
</ItemsTemplate>
1211
</TodoList>
1312

sample/tests/CodeOnlyTests/CounterTest.cs renamed to sample/tests/CodeOnlyTests/Components/CounterTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using Egil.RazorComponents.Testing.Asserting;
67
using Egil.RazorComponents.Testing.Library.SampleApp.Pages;
78
using Shouldly;
89
using Xunit;
910

1011
namespace Egil.RazorComponents.Testing.Library.SampleApp.CodeOnlyTests
1112
{
12-
public class CounterTest : ComponentTestBase
13+
public class CounterTest : TestContext
1314
{
1415
[Fact]
1516
public void InitialHtmlIsCorrect()

sample/tests/CodeOnlyTests/FetchDataTest.cs renamed to sample/tests/CodeOnlyTests/Components/FetchDataTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
using Xunit;
99
using Egil.RazorComponents.Testing.Library.SampleApp.Pages;
1010
using Shouldly;
11+
using Egil.RazorComponents.Testing.Asserting;
1112

1213
namespace Egil.RazorComponents.Testing.Library.SampleApp.CodeOnlyTests
1314
{
14-
public class FetchDataTest : ComponentTestBase
15+
public class FetchDataTest : TestContext
1516
{
1617
class MockForecastService : IWeatherForecastService
1718
{
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Components;
7+
using Egil.RazorComponents.Testing.Library.SampleApp.Components;
8+
using Xunit;
9+
using Shouldly;
10+
11+
namespace Egil.RazorComponents.Testing.Library.SampleApp.CodeOnlyTests.Components
12+
{
13+
public class ThemedButtonTest : TestContext
14+
{
15+
[Fact(DisplayName = "Themed button uses provided theme info to set class attribute")]
16+
public void Test001()
17+
{
18+
// arrange
19+
var theme = new ThemeInfo() { Class = "BUTTON" };
20+
21+
var cut = RenderComponent<ThemedButton>(
22+
(nameof(ThemedButton.Theme), theme, true),
23+
("id", "testid")
24+
);
25+
26+
cut.Find("button").ClassList.ShouldContain(theme.Class);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)