You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[](https://github.com/egil/razor-components-testing-library/releases)
Copy file name to clipboardExpand all lines: docs/csharp-examples.md
+127-8Lines changed: 127 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,10 +4,12 @@ In the following examples, the terminology **component under test** (abbreviated
4
4
5
5
All examples can be found in the [CodeOnlyTests](../sample/tests/CodeOnlyTests) folder in the [Sample project](../sample/).
6
6
7
-
1.[Creating new test classes](creating-new-test-classes)
8
-
2.[Testing components without parameters](testing-components-without-parameters)
9
-
3.[Testing components with regular parameters](testing-components-with-regular-parameters)
10
-
4.[Testing components with child content](testing-components-with-child-content)
7
+
1.[Creating new test classes](#creating-new-test-classes)
8
+
2.[Testing components without parameters](#testing-components-without-parameters)
9
+
3.[Testing components with regular parameters](#testing-components-with-regular-parameters)
10
+
4.[Testing components with child content](#testing-components-with-child-content)
11
+
5.[Testing components with `EventCallback` parameters](#testing-components-with-eventcallback-parameters)
12
+
6.[Testing components with cascading-value parameters](#testing-components-with-cascading value-parameters)
11
13
12
14
## Creating new test classes
13
15
@@ -49,7 +51,7 @@ The following unit-tests verifies that the [Counter.razor](../sample/src/Pages/C
49
51
}
50
52
```
51
53
52
-
These are the unit tests:
54
+
The [CounterTest.cs](../sample/test/CodeOnlyTests/Pages/CounterTest.cs) looks like this:
53
55
54
56
```csharp
55
57
publicclassCounterTest : ComponentTestFixture
@@ -151,7 +153,7 @@ The component under test will be the [Aside.razor](../sample/src/Components/Asid
151
153
}
152
154
```
153
155
154
-
Here is a test:
156
+
The [AsideTest.cs](../sample/test/CodeOnlyTests/Components/AsideTest.cs) looks like this:
155
157
156
158
```csharp
157
159
publicclassAsideTest : ComponentTestFixture
@@ -190,7 +192,7 @@ The second parameter, `class` is explicitly declared in the `Aside` class. It is
190
192
191
193
## Testing components with child content
192
194
193
-
The `Aside` component listed in the previous section also has a `ChildContent` parameter, so lets add a few tests that passes markup and components to it through that.
195
+
The [Aside.razor](../sample/src/Components/Aside.razor) component listed in the previous section also has a `ChildContent` parameter, so lets add a few tests that passes markup and components to it through that.
194
196
195
197
```csharp
196
198
publicclassAsideTest : ComponentTestFixture
@@ -253,4 +255,121 @@ public class AsideTest : ComponentTestFixture
253
255
}
254
256
```
255
257
256
-
In `Test002` above we use the `ChildContent(...)` helper method to create a ChildContent parameter and pass that to the `Aside` component. The overload, `ChildContent<TComponent>(component params)`, used in `Test003`, allows us to create render fragment that will render a component (of type `TComponent`) with the specified parameters. The `ChildContent<TComponent>(...)` has the same parameter options as the `RenderComponent<TComponent>` method has.
258
+
- In `Test002` above we use the `ChildContent(...)` helper method to create a ChildContent parameter and pass that to the `Aside` component.
259
+
- The overload, `ChildContent<TComponent>(...)`, used in `Test003`, allows us to create a render fragment that will render a component (of type `TComponent`) with the specified parameters.
260
+
The `ChildContent<TComponent>(...)` has the same parameter options as the `RenderComponent<TComponent>` method has.
261
+
262
+
## Testing components with `EventCallback` parameters
263
+
264
+
To show how to pass an `EventCallback` to a component under test, we will use the [ThemedButton.razor](../sample/src/Components/ThemedButton.razor), which looks like this:
265
+
266
+
```cshtml
267
+
<button @onclick="HandleOnClick"
268
+
class=@Theme?.Value
269
+
title=@Title?.Value
270
+
@attributes="Attributes">
271
+
@ChildContent
272
+
</button>
273
+
@code {
274
+
[Parameter(CaptureUnmatchedValues = true)]
275
+
public IReadOnlyDictionary<string, object>? Attributes { get; set; }
276
+
277
+
[CascadingParameter] public ThemeInfo? Theme { get; set; }
278
+
[CascadingParameter(Name = nameof(Title))] public ThemeInfo? Title { get; set; }
279
+
[Parameter] public RenderFragment? ChildContent { get; set; }
280
+
[Parameter] public EventCallback<MouseEventArgs> OnClick { get; set; }
`Test001` above uses the `EventCallback(parammeterName, callback)` helper method the generate a proper `EventCallback` object. There are many overloads, that should enable all the normal scenarios that is possible via Razor code.
314
+
315
+
## Testing components with cascading-value parameters
316
+
317
+
If a component under test accepts cascading values, like [ThemedButton.razor](../sample/src/Components/ThemedButton.razor) listed above, we can pass one or more cascading values to it like so:
0 commit comments