Skip to content

Commit 9727ea6

Browse files
committed
lots of updates to docs and child content/params handling
1 parent 92b8ed0 commit 9727ea6

37 files changed

+1128
-228
lines changed

sample/src/Components/ThemeInfo.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ namespace Egil.RazorComponents.Testing.Library.SampleApp.Components
88
{
99
public class ThemeInfo
1010
{
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; }
11+
public string? Value { get; set; }
1512
}
1613
}

sample/src/Components/ThemedButton.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<button class=@(Theme?.Class ?? "") @attributes="Attributes">
1+
<button class=@(Theme?.Value ?? "NO-THEME") @attributes="Attributes">
22
@ChildContent
33
</button>
44
@code {
55
[Parameter(CaptureUnmatchedValues = true)]
66
public IReadOnlyDictionary<string, object> Attributes { get; set; }
77

88
[CascadingParameter]
9-
public ThemeInfo Theme { get; set; }
9+
public ThemeInfo? Theme { get; set; }
1010

1111
[Parameter]
1212
public RenderFragment ChildContent { get; set; }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<div class=@(Class?.Value ?? "") title=@(Title?.Value ?? "")>
2+
@ChildContent
3+
</div>
4+
@code {
5+
[CascadingParameter(Name = nameof(Class))]
6+
public ThemeInfo? Class { get; set; }
7+
8+
[CascadingParameter(Name = nameof(Title))]
9+
public ThemeInfo? Title { 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/Pages/Index.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ Welcome to your new app.
1010

1111

1212
@code {
13-
private ThemeInfo theme = new ThemeInfo { Class = "btn btn-primary" };
13+
private ThemeInfo theme = new ThemeInfo { Value = "btn btn-primary" };
1414
}

sample/tests/CodeOnlyTests/Components/CounterTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Egil.RazorComponents.Testing.Library.SampleApp.CodeOnlyTests
1212
{
13-
public class CounterTest : TestContext
13+
public class CounterTest : ComponentTestFixture
1414
{
1515
[Fact]
1616
public void InitialHtmlIsCorrect()

sample/tests/CodeOnlyTests/Components/FetchDataTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Egil.RazorComponents.Testing.Library.SampleApp.CodeOnlyTests
1414
{
15-
public class FetchDataTest : TestContext
15+
public class FetchDataTest : ComponentTestFixture
1616
{
1717
class MockForecastService : IWeatherForecastService
1818
{
@@ -44,12 +44,12 @@ public void AfterDataLoadsItIsDisplayedInAForecastTable()
4444
var forecasts = new[] { new WeatherForecast { Date = DateTime.Now, Summary = "Testy", TemperatureC = 42 } };
4545
var cut = RenderComponent<FetchData>();
4646
var initialHtml = cut.GetMarkup();
47-
var expectedDataTable = RenderComponent<ForecastDataTable>((nameof(ForecastDataTable.Forecasts), forecasts));
4847

4948
// act
5049
WaitForNextRender(() => mockForecastService.Task.SetResult(forecasts));
5150

5251
// assert
52+
var expectedDataTable = RenderComponent<ForecastDataTable>((nameof(ForecastDataTable.Forecasts), forecasts));
5353
cut.CompareTo(initialHtml)
5454
.ShouldHaveChanges(
5555
diff => diff.ShouldBeRemoval("<p><em>Loading...</em></p>"),
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Egil.RazorComponents.Testing.Library.SampleApp.Components;
7+
using Egil.RazorComponents.Testing.Asserting;
8+
using Shouldly;
9+
using Xunit;
10+
11+
namespace Egil.RazorComponents.Testing.Library.SampleApp.CodeOnlyTests.Components
12+
{
13+
public class PassingChildContentTest : ComponentTestFixture
14+
{
15+
[Fact(DisplayName = "Markup passed to child content is rendered as expected")]
16+
public void Test001()
17+
{
18+
var markup = "<h1>HELLO WORLD</h1>";
19+
20+
var cut = RenderComponent<ThemedButton>(ChildContent(markup));
21+
22+
cut.Find("button").ChildNodes.ShouldBe(markup);
23+
}
24+
25+
[Fact(DisplayName = "A component render fragment passed to child content is rendered as expected")]
26+
public void Test002()
27+
{
28+
// arrange
29+
var expectedChildContent = RenderComponent<ThemedButton>(("id", "test"));
30+
31+
// act
32+
var cut = RenderComponent<ThemedElement>(
33+
// creates child content parameter that renders a
34+
// fragment that creates a ThemedButton component with the provided params
35+
ChildContent<ThemedButton>(("id", "test"))
36+
);
37+
38+
// assert by going through ThemedElement's root DIV
39+
cut.Find("div").ChildNodes.ShouldBe(expectedChildContent);
40+
// assert by finding the child content by its ID tag and comparing to the expected
41+
cut.Find("#test").ShouldBe(expectedChildContent);
42+
}
43+
}
44+
}

sample/tests/CodeOnlyTests/Components/ThemedButtonTest.cs

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 ThemedXXTest : ComponentTestFixture
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() { Value = "BUTTON" };
20+
21+
// act
22+
var cut = RenderComponent<ThemedButton>(
23+
CascadingValue(theme)
24+
);
25+
26+
// assert
27+
cut.Find("button").ClassList.ShouldContain(theme.Value);
28+
}
29+
30+
[Fact(DisplayName = "Named cascading values are passed to components")]
31+
public void Test002()
32+
{
33+
// arrange
34+
var classTheme = new ThemeInfo() { Value = "FOO" };
35+
var titleTheme = new ThemeInfo() { Value = "BAR" };
36+
37+
// act
38+
var cut = RenderComponent<ThemedElement>(
39+
CascadingValue(nameof(ThemedElement.Class), classTheme),
40+
CascadingValue(nameof(ThemedElement.Title), titleTheme)
41+
);
42+
43+
// assert
44+
var elm = cut.Find("div");
45+
elm.ClassList.ShouldContain(classTheme.Value);
46+
elm.GetAttribute("title").ShouldContain(titleTheme.Value);
47+
}
48+
}
49+
}

sample/tests/CodeOnlyTests/Components/TodoItemTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Egil.RazorComponents.Testing.Library.SampleApp.CodeOnlyTests
1414
{
15-
public class TodoItemTest : TestContext
15+
public class TodoItemTest : ComponentTestFixture
1616
{
1717
[Fact(DisplayName = "When no Todo is passed to item an exception is thrown")]
1818
public void Test001()
@@ -38,10 +38,9 @@ public void Test003()
3838
{
3939
var todo = new Todo { Id = 42, Text = "Hello world" };
4040
var completedId = 0;
41-
var onCompleteHandler = EventCallback.Factory.Create(this, (int id) => completedId = id);
4241
var cut = RenderComponent<TodoItem>(
4342
(nameof(TodoItem.Todo), todo),
44-
(nameof(TodoItem.OnCompleted), onCompleteHandler)
43+
EventCallback(nameof(TodoItem.OnCompleted), (int id) => completedId = id)
4544
);
4645

4746
cut.Find("li").Click();

0 commit comments

Comments
 (0)