Skip to content

Commit 07c7c9c

Browse files
committed
Docs: eventcallback parameters
1 parent af5ad4d commit 07c7c9c

2 files changed

Lines changed: 46 additions & 24 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@inherits TestComponentBase
2+
3+
<Fixture Test="f => {}">
4+
<ComponentUnderTest>
5+
<AllKindsOfParams Numbers="42" Lines=@(new List<string> { "Hello", "World" })>
6+
</AllKindsOfParams>
7+
</ComponentUnderTest>
8+
</Fixture>
9+
10+
<Fixture Test="f => {}">
11+
<ComponentUnderTest>
12+
<AllKindsOfParams OnClick=@(args => { /* handle callback */ })
13+
OnSomething=@(() => { /* handle callback */ })>
14+
</AllKindsOfParams>
15+
</ComponentUnderTest>
16+
</Fixture>

docs/site/docs/providing-input/passing-parameters-to-components.md

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ bUnit comes with a bunch of ways to pass parameters to a component under test.
99

1010
In Razor-based tests, those written in `.razor` files, passing parameters is exactly the same as in your normal Blazor pages and components.
1111

12-
For C#-based test code, you can:
12+
For C#-based test code, help is needed. This comes as:
1313

14-
- use loosely typed factory methods
15-
- use a simple tuple-based syntax, i.e. `(name, value)`
16-
- use a strongly typed builder (still experimental)
14+
- Loosely typed factory methods
15+
- Simple tuple-based syntax, i.e. `(name, value)`
16+
- Strongly typed builder (still experimental)
1717

18-
There are two methods in bUnit that allows passing parameters:
18+
There are two methods in bUnit that allows passing parameters in C#-based test code:
1919

2020
- `RenderComponent` on the test context
2121
- `SetParametersAndRender` on a rendered component
@@ -26,15 +26,15 @@ In the following sub sections we will show both C# and Razor-based test code, ju
2626
> In all examples below, the <xref:Bunit.ComponentParameterFactory> is imported into the test class using `using static Bunit.ComponentParameterFactory;`. This results in a lot less boilerplate code, which improves test readability.
2727
>
2828
> With `using static` import, we can use the factory methods like this:
29-
>
29+
>
3030
> ```csharp
3131
> using static Bunit.ComponentParameterFactory;
3232
> ...
3333
> var componentParameter = Parameter("paramName", someValue);
3434
> ```
35-
>
35+
>
3636
> With a regular `using` import, we have to prefix the static factory methods like this:
37-
>
37+
>
3838
> ```csharp
3939
> using Bunit.ComponentParameterFactory;
4040
> ...
@@ -55,21 +55,9 @@ Let's look at an example of passing parameter that takes types which or _not_ sp
5555
5656
[!code-csharp[AllKindsOfParams.razor](../../../samples/components/AllKindsOfParams.razor#L3-L7)]
5757
58-
# [C# test code](#tab/xunit)
59-
60-
```dotnetcli
61-
dotnet new xunit -o <NAME OF TEST PROJECT>
62-
```
63-
64-
# [Razor test code](#tab/nunit)
58+
Using either C# or Razor test code, this can be done like this:
6559
66-
```dotnetcli
67-
dotnet new nunit -o <NAME OF TEST PROJECT>
68-
```
69-
70-
***
71-
72-
Using either C# tuples, a factory method or the parameter builder, this can be done like this:
60+
# [C# test code](#tab/csharp)
7361
7462
[!code-csharp[](../../../samples/tests/xunit/AllKindsOfParamsTest.cs#L17-L39)]
7563
@@ -80,21 +68,39 @@ All of these examples does the same thing, here is what is going on:
8068
3. The third example uses the <xref:Bunit.ComponentParameterFactory.Parameter(System.String,System.Object)> factory method.
8169
4. The last example uses the <xref:Bunit.ComponentParameterBuilder`1>'s `Add` method, which takes a parameter selector expression that selects the parameter using a lambda, and forces you to provide the correct type for the value. This makes the builders methods strongly typed and refactor safe.
8270
71+
# [Razor test code](#tab/razor)
72+
73+
[!code-html[](../../../samples/tests/razor/AllKindsOfParamsTest.razor#L3-L8)]
74+
75+
This is just regular Blazor parameter passing, which is the same for both `Fixture` and `SnapshotTest` razor tests.
76+
77+
***
78+
8379
### EventCallback Parameters
8480
8581
This example will pass parameters to the follow two `EventCallback` parameters:
8682
8783
[!code-csharp[AllKindsOfParams.razor](../../../samples/components/AllKindsOfParams.razor#L9-L13)]
8884
89-
Using either one of the `EventCallback` factory methods or the parameter builder `Add` method, this can be done like this:
85+
Using either C# or Razor test code, this can be done like this:
9086
91-
[!code-csharp[](../../../samples/tests/xunit/AllKindsOfParamsTest.cs#L48-L64)]
87+
# [C# test code](#tab/csharp)
88+
89+
[!code-csharp[](../../../samples/tests/xunit/AllKindsOfParamsTest.cs#L48-L63)]
9290
9391
All of these examples does the same thing, here is what is going on:
9492
9593
1. The first and second example uses the `EventCallback` factory method in <xref:Bunit.ComponentParameterFactory> (there are many overloads that take different kinds of `Action` and `Func` delegates), to pass a lambda as the event callback to the specified parameter.
9694
2. The last example uses the <xref:Bunit.ComponentParameterBuilder`1>'s `Add` method, which takes a parameter selector expression that selects the parameter using a lambda, and forces you to provide the correct type of callback method. This makes the builders methods strongly typed and refactor safe.
9795
96+
# [Razor test code](#tab/razor)
97+
98+
[!code-html[](../../../samples/tests/razor/AllKindsOfParamsTest.razor#L10-L16)]
99+
100+
This is just regular Blazor parameter passing, which is the same for both `Fixture` and `SnapshotTest` razor tests.
101+
102+
***
103+
98104
### ChildContent Parameters
99105
100106
### RenderFragment Parameters

0 commit comments

Comments
 (0)