Skip to content

Commit 92779eb

Browse files
authored
Merge pull request #236 from egil/feature/155-layout
TestContext.RenderTree
2 parents 8ea8ee0 + 7168943 commit 92779eb

24 files changed

+632
-61
lines changed

CHANGELOG.md

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,47 @@ The following section list all changes in beta-11.
1212
List of new features.
1313

1414
- Two new overloads to the `RenderFragment()` and `ChildContent()` component parameter factory methods have been added that takes a `RenderFragment` as input. By [@egil](https://github.com/egil) in [#203](https://github.com/egil/bUnit/pull/203).
15+
1516
- Added a `ComponentParameterCollection` type. The `ComponentParameterCollection` is a collection of component parameters, that knows how to turn those components parameters into a `RenderFragment`, which will render a component and pass any parameters inside the collection to that component. That logic was spread out over multiple places in bUnit, and is now owned by the `ComponentParameterCollection` type. By [@egil](https://github.com/egil) in [#203](https://github.com/egil/bUnit/pull/203).
17+
1618
- Added additional placeholder services for `NavigationManager`, `HttpClient`, and `IStringLocalizer`, to make it easier for users to figure out why a test is failing due to missing service registration before rendering a component. By [@joro550](https://github.com/joro550) in [#223](https://github.com/egil/bUnit/pull/223).
19+
1720
- Added `Key` class that represents a keyboard key and helps to avoid constructing `KeyboardEventArgs` object manually. The key can be passed to `KeyPress`, `KeyDown`, or `KeyUp` helper methods to raise keyboard events. The `Key` class provides static special keys or can be obtained from character or string. Keys can be combined with key modifiers: `Key.Enter + Key.Alt`.
1821

19-
For example, this makes it easier to trigger keyboard events on an element:
22+
For example, this makes it easier to trigger keyboard events on an element:
23+
24+
```csharp
25+
var cut = ctx.RenderComponent<ComponentWithKeyboardEvents>();
26+
var element = cut.Find("input");
27+
28+
element.KeyDown(Key.Enter + Key.Control); // Triggers onkeydown event with Ctrl + Enter
29+
element.KeyUp(Key.Control + Key.Shift + 'B'); // Triggers onkeyup event with Ctrl + Shift + B
30+
element.KeyPress('1'); // Triggers onkeypress event with key 1
31+
element.KeyDown(Key.Alt + "<"); // Triggers onkeydown event with Alt + <
32+
```
2033

21-
```csharp
22-
var cut = ctx.RenderComponent<ComponentWithKeyboardEvents>();
23-
var element = cut.Find("input");
34+
By [@duracellko](https://github.com/duracellko) in [#101](https://github.com/egil/bUnit/issues/101).
2435

25-
element.KeyDown(Key.Enter + Key.Control); // Triggers onkeydown event with Ctrl + Enter
26-
element.KeyUp(Key.Control + Key.Shift + 'B'); // Triggers onkeyup event with Ctrl + Shift + B
27-
element.KeyPress('1'); // Triggers onkeypress event with key 1
28-
element.KeyDown(Key.Alt + "<"); // Triggers onkeydown event with Alt + <
29-
```
30-
By [@duracellko](https://github.com/duracellko) in [#101](https://github.com/egil/bUnit/issues/101).
36+
- Added support for registering/adding "layout" components to a test context, which components should be rendered inside. This allows you to simplify the "arrange" step of a test when a component under test requires a certain render tree as its parent, e.g. a cascading value.
37+
38+
For example, to pass a cascading string value `foo` to all components rendered with the test context, do the following:
39+
40+
```csharp
41+
ctx.AddLayoutComponent<CascadingValue<string>>(parameters => parameters.Add(p => p.Value, "foo"));
42+
var cut = ctx.RenderComponent<ComponentReceivingFoo>();
43+
```
44+
45+
By [@duracellko](https://github.com/duracellko) in [#101](https://github.com/egil/bUnit/issues/101).
3146

3247
### Changed
3348
List of changes in existing functionality.
3449

3550
- The `ComponentParameterBuilder` has been renamed to `ComponentParameterCollectionBuilder`, since it now builds the `ComponentParameterCollection` type, introduced in this release of bUnit. By [@egil](https://github.com/egil) in [#203](https://github.com/egil/bUnit/pull/203).
51+
3652
- `ComponentParameterCollectionBuilder` now allows adding cascading values that is not directly used by the component type it targets. This makes it possible to add cascading values to children of the target component. By [@egil](https://github.com/egil) in [#203](https://github.com/egil/bUnit/pull/203).
53+
3754
- The `Add(object)` has been replaced by `AddCascadingValue(object)` in `ComponentParameterCollectionBuilder`, to make it more clear that an unnamed cascading value is being passed to the target component or one of its child components. It is also possible to pass unnamed cascading values using the `Add(parameterSelector, value)` method, which now correctly detect if the selected cascading value parameter is named or unnamed. By [@egil](https://github.com/egil) in [#203](https://github.com/egil/bUnit/pull/203).
55+
3856
- It is now possible to call the `Add()`, `AddChildContent()` methods on `ComponentParameterCollectionBuilder`, and the factory methods `RenderFragment()`, `ChildContent()`, and `Template()`, _**multiple times**_ for the same parameter, if it is of type `RenderFragment` or `RenderFragment<TValue>`. Doing so previously would either result in an exception or just the last passed `RenderFragment` to be used. Now all the provided `RenderFragment` or `RenderFragment<TValue>` will be combined at runtime into a single `RenderFragment` or `RenderFragment<TValue>`.
3957

4058
For example, this makes it easier to pass e.g. both a markup string and a component to a `ChildContent` parameter:
@@ -50,7 +68,9 @@ List of changes in existing functionality.
5068
);
5169
```
5270
By [@egil](https://github.com/egil) in [#203](https://github.com/egil/bUnit/pull/203).
71+
5372
- All test doubles are now in the same namespace, `Bunit.TestDoubles`. So all import statements for `Bunit.TestDoubles.JSInterop` and `Bunit.TestDoubles.Authorization` must be changed to `Bunit.TestDoubles`. By [@egil](https://github.com/egil) in [#223](https://github.com/egil/bUnit/pull/223).
73+
5474
- Marked MarkupMatches methods as assertion methods to stop SonarSource analyzers complaining about missing assertions in tests. By [@egil](https://github.com/egil) in [#229](https://github.com/egil/bUnit/pull/229).
5575

5676
### Deprecated

docs/.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ trim_trailing_whitespace = false
1010

1111
[*.{cs,razor}]
1212
tab_size = 2
13+
indent_style = space
1314
dotnet_diagnostic.BL0001.severity = none
1415
dotnet_diagnostic.BL0002.severity = none
1516
dotnet_diagnostic.BL0003.severity = none
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Cascading value: @Value
2+
@code
3+
{
4+
[CascadingParameter]
5+
public string Value { get; set; }
6+
}

docs/samples/tests/mstest/BunitTestContext.cs

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

77
namespace Bunit.Docs.Samples
88
{
9-
public abstract class BunitTestContext : ITestContext, IDisposable
9+
public abstract class BunitTestContext : IDisposable
1010
{
1111
private Bunit.TestContext _context;
1212

docs/samples/tests/nunit/BunitTestContext.cs

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

77
namespace Bunit.Docs.Samples
88
{
9-
public abstract class BunitTestContext : ITestContext, IDisposable
9+
public abstract class BunitTestContext : IDisposable
1010
{
1111
private Bunit.TestContext _context;
1212

docs/samples/tests/razor/_Imports.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
@using Microsoft.Extensions.DependencyInjection
99
@using AngleSharp.Dom
1010
@using Bunit
11-
@using Bunit.TestDoubles.JSInterop
11+
@using Bunit.TestDoubles
1212
@using Xunit

docs/samples/tests/xunit/InjectAuthServiceTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Bunit.TestDoubles.Authorization;
1+
using Bunit.TestDoubles;
22
using System;
33
using Xunit;
44

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Xunit;
2+
using Bunit;
3+
using System.Collections.Generic;
4+
using Microsoft.AspNetCore.Components;
5+
using Microsoft.AspNetCore.Components.Web;
6+
using Microsoft.Extensions.DependencyInjection;
7+
8+
namespace Bunit.Docs.Samples
9+
{
10+
public class RenderTreeTest
11+
{
12+
[Fact]
13+
public void PrintCascadingValueTest()
14+
{
15+
using var ctx = new TestContext();
16+
17+
// Add a cascading value to the test contexts root render tree.
18+
ctx.RenderTree.Add<CascadingValue<string>>(parameters => parameters
19+
.Add(p => p.Value, "FOO")
20+
);
21+
22+
// The component will be rendered as a chld of last
23+
// component added to the RenderTree property.
24+
var cut = ctx.RenderComponent<PrintCascadingValue>();
25+
26+
// Verify that the cascading value was passed correctly.
27+
cut.MarkupMatches($"Cascading value: FOO");
28+
}
29+
30+
[Fact]
31+
public void PrintCascadingValue2Test()
32+
{
33+
using var ctx = new TestContext();
34+
35+
// Add a cascading value to the test contexts root render tree.
36+
ctx.RenderTree.TryAdd<CascadingValue<string>>(parameters => parameters
37+
.Add(p => p.Value, "BAR?")
38+
);
39+
40+
// The component will be rendered as a chld of last
41+
// component added to the RenderTree property.
42+
var cut = ctx.RenderComponent<PrintCascadingValue>();
43+
44+
// Verify that the cascading value was passed correctly.
45+
cut.MarkupMatches($"Cascading value: BAR?");
46+
}
47+
}
48+
}

docs/samples/tests/xunit/UserInfoTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Bunit.TestDoubles.Authorization;
1+
using Bunit.TestDoubles;
22
using Xunit;
33

44
namespace Bunit.Docs.Samples

docs/samples/tests/xunit/UserRightsTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Bunit.TestDoubles.Authorization;
1+
using Bunit.TestDoubles;
22
using System.Security.Claims;
33
using System.Globalization;
44
using Xunit;

0 commit comments

Comments
 (0)