Skip to content

Commit da92fcb

Browse files
committed
add themedbutton test
1 parent e9263fb commit da92fcb

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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.SampleApp.Components;
8+
using Xunit;
9+
using Shouldly;
10+
using Microsoft.AspNetCore.Components.Web;
11+
using Egil.RazorComponents.Testing.EventDispatchExtensions;
12+
13+
namespace Egil.RazorComponents.Testing.SampleApp.CodeOnlyTests.Components
14+
{
15+
public class ThemedButtonTest : ComponentTestFixture
16+
{
17+
[Fact(DisplayName = "When button is clicked, the OnClick event callback is triggered")]
18+
public void Test001()
19+
{
20+
var wasCalled = false;
21+
// Arrange - pass a lambda in as parameter to the OnClick parameter.
22+
//
23+
// This is equivalent to the follow Razor code:
24+
//
25+
// <ThemedButton OnClick="(_) => wasCalled = true"></ThemedButton>
26+
var cut = RenderComponent<ThemedButton>(
27+
EventCallback(nameof(ThemedButton.OnClick), (MouseEventArgs _) => wasCalled = true)
28+
);
29+
30+
// Act - click the button in CUT
31+
cut.Find("button").Click();
32+
33+
// Assert - check if callback was triggered
34+
wasCalled.ShouldBeTrue();
35+
}
36+
37+
[Fact(DisplayName = "Themed button uses provided theme info to set class attribute")]
38+
public void Test002()
39+
{
40+
// Arrange - create an instance of the ThemeInfo class to passs to the ThemedButton
41+
var theme = new ThemeInfo() { Value = "BUTTON" };
42+
43+
// Act - Render the ThemedButton component, passing in the instance of ThemeInfo
44+
// as an _unnamed_ cascading value.
45+
//
46+
// This is equivalent to the follow Razor code:
47+
//
48+
// <CascadingValue Value="theme">
49+
// <ThemedButton></ThemedButton>
50+
// </CascadingValue>
51+
var cut = RenderComponent<ThemedButton>(
52+
CascadingValue(theme)
53+
);
54+
55+
// Assert - check that the class specified in the cascading value was indeed used.
56+
cut.Find("button").ClassList.ShouldContain(theme.Value);
57+
}
58+
59+
[Fact(DisplayName = "Named cascading values are passed to components")]
60+
public void Test003()
61+
{
62+
// Arrange - create two instances of the ThemeInfo class to passs to the ThemedButton
63+
var theme = new ThemeInfo() { Value = "BUTTON" };
64+
var titleTheme = new ThemeInfo() { Value = "BAR" };
65+
66+
// Act - Render the ThemedButton component, passing in the instances of ThemeInfo
67+
// as an _unnamed_ and a _named_ cascading value.
68+
//
69+
// This is equivalent to the follow Razor code:
70+
//
71+
// <CascadingValue Value="theme">
72+
// <CascadingValue Name="Title" Value="titleTheme">
73+
// <ThemedButton></ThemedButton>
74+
// </CascadingValue>
75+
// </CascadingValue>
76+
var cut = RenderComponent<ThemedButton>(
77+
CascadingValue(theme),
78+
CascadingValue(nameof(ThemedButton.Title), titleTheme)
79+
);
80+
81+
// Assert - check that the class and title specified in the cascading values was indeed used.
82+
var elm = cut.Find("button");
83+
elm.ClassList.ShouldContain(theme.Value);
84+
elm.GetAttribute("title").ShouldContain(titleTheme.Value);
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)