Skip to content

Commit 2309b71

Browse files
linkdotnetegil
andcommitted
refactor: Merge RenderedFragment and RenderedComponent into IRenderedComponent with single internal RenderedComponent<TComponent> type
* refactor: Merge RenderedFragment and RenderedComponent into IRenderedComponent * fix: use generic parameters correctly * revert: rename BunitRootComponent to ContainerFragment * refactor: rename RootComponent to BunitRootComponent --------- Co-authored-by: Egil Hansen <egil@assimilated.dk>
1 parent bdd525c commit 2309b71

File tree

94 files changed

+826
-912
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+826
-912
lines changed

.editorconfig

+1
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ dotnet_diagnostic.S112.severity = none # S112: General exceptions should never b
453453
dotnet_diagnostic.S1075.severity = suggestion # S1075: URIs should not be hardcoded
454454
dotnet_diagnostic.S1186.severity = suggestion # S1186: Methods should not be empty
455455
dotnet_diagnostic.S2292.severity = suggestion # S2292: Trivial properties should be auto-implemented
456+
dotnet_diagnostic.S2743.severity = suggestion # S2743: A static field in a generic type is not shared among instances
456457
dotnet_diagnostic.S4158.severity = none # BUGGY with C#9 code - doesnt understand local methods
457458

458459
# Razor specific rules

docs/samples/tests/xunit/ReRenderTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public async Task RendersViaInvokeAsyncWithLoading()
7373
// Indirectly re-renders and returns the task returned by Calculate().
7474
// The explicit <Task> here is important, otherwise the call to Calculate()
7575
// will be awaited automatically.
76-
var task = await cut.InvokeAsync<Task>(() => cut.Instance.Calculate(1, 2));
76+
var task = await cut.InvokeAsync<CalcWithLoading, Task>(() => cut.Instance.Calculate(1, 2));
7777
cut.MarkupMatches("<output>Loading</output>");
7878

7979
// Wait for the task to complete.

docs/site/docs/interaction/awaiting-async-state.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ A test can fail if a component performs asynchronous renders. This may be due to
99

1010
You need to handle this specifically in your tests because tests execute in the test framework's synchronization context and the test renderer executes renders in its own synchronization context. If you do not, you will likely experience tests that sometimes pass and sometimes fail.
1111

12-
bUnit comes with two methods that help to deal with this issue: the [`WaitForState()`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForState(Bunit.RenderedFragment,System.Func{System.Boolean},System.Nullable{System.TimeSpan})) method covered on this page, and the [`WaitForAssertion()`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForAssertion(Bunit.RenderedFragment,System.Action,System.Nullable{System.TimeSpan})) method covered on the <xref:async-assertion> page.
12+
bUnit comes with two methods that help to deal with this issue: the [`WaitForState()`](xref:Bunit.RenderedComponentWaitForHelperExtensions.WaitForState``1(Bunit.IRenderedComponent{``0},System.Func{System.Boolean},System.Nullable{System.TimeSpan})) method covered on this page, and the [`WaitForAssertion()`](xref:Bunit.RenderedComponentWaitForHelperExtensions.WaitForAssertion``1(Bunit.IRenderedComponent{``0},System.Action,System.Nullable{System.TimeSpan})) method covered on the <xref:async-assertion> page.
1313

1414
Let's start by taking a look at the `WaitForState` method in more detail.
1515

1616
## Waiting for state using `WaitForState`
1717

18-
The [`WaitForState(Func<Boolean>, TimeSpan?)`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForState(Bunit.RenderedFragment,System.Func{System.Boolean},System.Nullable{System.TimeSpan})) method can be used to block and wait in a test method, until the provided predicate returns true or the timeout is reached. (The default timeout is one second.)
18+
The [`WaitForState(Func<Boolean>, TimeSpan?)`](xref:Bunit.RenderedComponentWaitForHelperExtensions.WaitForState``1(Bunit.IRenderedComponent{``0},System.Func{System.Boolean},System.Nullable{System.TimeSpan})) method can be used to block and wait in a test method, until the provided predicate returns true or the timeout is reached. (The default timeout is one second.)
1919

2020
> [!NOTE]
2121
> The `WaitForState()` method will try the predicate passed to it when the `WaitForState()` method is called, and every time the component under test renders.

docs/site/docs/interaction/dispose-components.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The following example of this:
1313
[!code-csharp[DisposeComponentsTest.cs](../../../samples/tests/xunit/DisposeComponentsTest.cs#L13-L22)]
1414

1515
> [!WARNING]
16-
> For `IAsyncDisposable` (since .net5) relying on [`WaitForState()`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForState(Bunit.RenderedFragment,System.Func{System.Boolean},System.Nullable{System.TimeSpan})) or [`WaitForAssertion()`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForAssertion(Bunit.RenderedFragment,System.Action,System.Nullable{System.TimeSpan})) will not work as a disposed component will not trigger a new render cycle.
16+
> For `IAsyncDisposable` (since .net5) relying on [`WaitForState()`](xref:Bunit.RenderedComponentWaitForHelperExtensions.WaitForState``1(Bunit.IRenderedComponent{``0},System.Func{System.Boolean},System.Nullable{System.TimeSpan})) or [`WaitForAssertion()`](xref:Bunit.RenderedComponentWaitForHelperExtensions.WaitForAssertion``1(Bunit.IRenderedComponent{``0},System.Action,System.Nullable{System.TimeSpan})) will not work as a disposed component will not trigger a new render cycle.
1717
1818
## Disposing components asynchronously
1919
If a component implements `IAsyncDisposable`, `DisposeComponentsAsync` can be awaited to wait for all asynchronous `DisposeAsync` methods. Sometimes interacting with JavaScript in Blazor WebAssembly requires disposing or resetting state in `DisposeAsync`.

docs/site/docs/interaction/trigger-event-handlers.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ bUnit comes with event dispatch helper methods that makes it possible to invoke
2323
- [Progress events](xref:Bunit.ProgressEventDispatchExtensions)
2424
- [Touch event](xref:Bunit.TouchEventDispatchExtensions)
2525

26-
To use these, first find the element in the component under test where the event handler is bound. This is usually done with the [`Find(string cssSelector)`](xref:Bunit.RenderedFragmentExtensions.Find(Bunit.RenderedFragment,System.String)) method. Next, invoke the event dispatch helper method of choice.
26+
To use these, first find the element in the component under test where the event handler is bound. This is usually done with the [`Find(string cssSelector)`](xref:Bunit.RenderedComponentExtensions.Find``1(Bunit.IRenderedComponent{``0},System.String)) method. Next, invoke the event dispatch helper method of choice.
2727

2828
The following section demonstrates how to do this...
2929

@@ -49,7 +49,7 @@ To trigger the `@onclick` `ClickHandler` event handler method in the `<ClickMe>`
4949

5050
This is what happens in the test:
5151

52-
1. In the arrange step of the test, the `<ClickMe>` component is rendered and the `<button>` element is found using the [`Find(string cssSelector)`](xref:Bunit.RenderedFragmentExtensions.Find(Bunit.RenderedFragment,System.String)) method.
52+
1. In the arrange step of the test, the `<ClickMe>` component is rendered and the `<button>` element is found using the [`Find(string cssSelector)`](xref:Bunit.RenderedComponentExtensions.Find``1(Bunit.IRenderedComponent{``0},System.String)) method.
5353
2. The act step of the test is the `<button>`'s click event handler. In this case, the `ClickHandler` event handler method is invoked in three different ways:
5454
- The first and second invocations use the same [`Click`](xref:Bunit.MouseEventDispatchExtensions.Click(AngleSharp.Dom.IElement,System.Int64,System.Double,System.Double,System.Double,System.Double,System.Double,System.Double,System.Double,System.Double,System.Int64,System.Int64,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.String)) method. It has a number of optional arguments, some of which are passed in the second invocation. If any arguments are provided, they are added to an instance of the `MouseEventArgs` type, which is passed to the event handler if it has it as an argument.
5555
- The last invocation uses the [`Click`](xref:Bunit.MouseEventDispatchExtensions.Click(AngleSharp.Dom.IElement,Microsoft.AspNetCore.Components.Web.MouseEventArgs)) method. This takes an instance of the `MouseEventArgs` type, which is passed to the event handler if it has it as an argument.

docs/site/docs/interaction/trigger-renders.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,37 @@ title: Triggering a render life cycle on a component
55

66
# Triggering a render life cycle on a component
77

8-
To trigger a re-render of a component under test, a reference to it through a <xref:Bunit.RenderedComponent`1> type is needed. When using the <xref:Bunit.BunitContext>'s `Render<TComponent>()` method, this is the type returned.
8+
To trigger a re-render of a component under test, a reference to it through a <xref:Bunit.IRenderedComponent`1> type is needed. When using the <xref:Bunit.BunitContext>'s `Render<TComponent>()` method, this is the type returned.
99

10-
In `.razor` based tests, using the <xref:Bunit.BunitContext>'s <xref:Bunit.BunitContext.Render(Microsoft.AspNetCore.Components.RenderFragment)> method also returns an <xref:Bunit.RenderedComponent`1> (as opposed to the <xref:Bunit.BunitContext.Render(Microsoft.AspNetCore.Components.RenderFragment)> method which returns the more simple <xref:Bunit.RenderedFragment>).
10+
In `.razor` based tests, using the <xref:Bunit.BunitContext>'s <xref:Bunit.BunitContext.Render(Microsoft.AspNetCore.Components.RenderFragment)> method also returns an <xref:Bunit.IRenderedComponent`1> (as opposed to the <xref:Bunit.BunitContext.Render(Microsoft.AspNetCore.Components.RenderFragment)> method which returns the more simple <xref:Bunit.IRenderedComponent`1>).
1111

12-
If you have a <xref:Bunit.RenderedFragment> or a <xref:Bunit.RenderedComponent`1> in a test, but need a child component's <xref:Bunit.RenderedComponent`1>, then use the `FindComponent<TComponent>()` or the `FindComponents<TComponent>()` methods, which traverse down the render tree and finds rendered components.
12+
If you have a <xref:Bunit.IRenderedComponent`1> in a test, but need a child component's <xref:Bunit.IRenderedComponent`1>, then use the `FindComponent<TComponent>()` or the `FindComponents<TComponent>()` methods, which traverse down the render tree and finds rendered components.
1313

14-
With a <xref:Bunit.RenderedComponent`1>, it is possible to cause the component to render again directly through the [`Render()`](xref:Bunit.BunitContext.Render``1(Microsoft.AspNetCore.Components.RenderFragment)) method or one of the [`Render()`](xref:Bunit.RenderedComponentRenderExtensions.Render``1(Bunit.RenderedComponent{``0},System.Action{Bunit.ComponentParameterCollectionBuilder{``0}})) methods, or indirectly through the [`InvokeAsync()`](xref:Bunit.RenderedFragment.Bunit.RenderedFragmentInvokeAsyncExtensions.InvokeAsync(System.Action)) method.
14+
With a <xref:Bunit.IRenderedComponent`1>, it is possible to cause the component to render again directly through the [`Render()`](xref:Bunit.BunitContext.Render``1(Microsoft.AspNetCore.Components.RenderFragment)) method or one of the [`Render()`](xref:Bunit.RenderedComponentRenderExtensions.Render``1(Bunit.IRenderedComponent{``0},System.Action{Bunit.ComponentParameterCollectionBuilder{``0}})) methods, or indirectly through the [`InvokeAsync()`](xref:Bunit.RenderedComponentInvokeAsyncExtensions.InvokeAsync``1(Bunit.IRenderedComponent{``0},System.Action)) method.
1515

1616
Let's look at how to use each of these methods to cause a re-render.
1717

1818
## Render
1919

20-
The [`Render()`](xref:Bunit.RenderedComponentRenderExtensions.Render``1(Bunit.RenderedComponent{``0},System.Action{Bunit.ComponentParameterCollectionBuilder{``0}})) method tells the renderer to re-render the component, i.e. go through its life-cycle methods (except for `OnInitialized()` and `OnInitializedAsync()` methods). To use it, do the following:
20+
The [`Render()`](xref:Bunit.RenderedComponentRenderExtensions.Render``1(Bunit.IRenderedComponent{``0},System.Action{Bunit.ComponentParameterCollectionBuilder{``0}})) method tells the renderer to re-render the component, i.e. go through its life-cycle methods (except for `OnInitialized()` and `OnInitializedAsync()` methods). To use it, do the following:
2121

2222
[!code-csharp[](../../../samples/tests/xunit/ReRenderTest.cs?start=15&end=21&highlight=5)]
2323

24-
The highlighted line shows the call to [`Render()`](xref:Bunit.RenderedComponentRenderExtensions.Render``1(Bunit.RenderedComponent{``0},System.Action{Bunit.ComponentParameterCollectionBuilder{``0}})).
24+
The highlighted line shows the call to [`Render()`](xref:Bunit.RenderedComponentRenderExtensions.Render``1(Bunit.IRenderedComponent{``0},System.Action{Bunit.ComponentParameterCollectionBuilder{``0}})).
2525

2626
> [!TIP]
27-
> The number of renders a component has been through can be inspected and verified using the <xref:Bunit.RenderedFragment.RenderCount> property.
27+
> The number of renders a component has been through can be inspected and verified using the <xref:Bunit.IRenderedComponent`1.RenderCount> property.
2828
2929
## Render
3030

31-
The [`Render(...)`](xref:Bunit.RenderedComponentRenderExtensions.Render``1(Bunit.RenderedComponent{``0},System.Action{Bunit.ComponentParameterCollectionBuilder{``0}})) methods tells the renderer to re-render the component with new parameters, i.e. go through its life-cycle methods (except for `OnInitialized()` and `OnInitializedAsync()` methods), passing the new parameters &mdash; _but only the new parameters_ &mdash; to the `SetParametersAsync()` method. To use it, do the following:
31+
The [`Render(...)`](xref:Bunit.RenderedComponentRenderExtensions.Render``1(Bunit.IRenderedComponent{``0},System.Action{Bunit.ComponentParameterCollectionBuilder{``0}})) methods tells the renderer to re-render the component with new parameters, i.e. go through its life-cycle methods (except for `OnInitialized()` and `OnInitializedAsync()` methods), passing the new parameters &mdash; _but only the new parameters_ &mdash; to the `SetParametersAsync()` method. To use it, do the following:
3232

3333
[!code-csharp[](../../../samples/tests/xunit/ReRenderTest.cs?start=28&end=38&highlight=7-9)]
3434

35-
The highlighted line shows the call to [`Render()`](xref:Bunit.RenderedComponentRenderExtensions.Render``1(Bunit.RenderedComponent{``0},System.Action{Bunit.ComponentParameterCollectionBuilder{``0}})), which is also available as a version that takes the zero or more component parameters, e.g. created through the component parameter factory helper methods, if you prefer that method of passing parameters.
35+
The highlighted line shows the call to [`Render()`](xref:Bunit.RenderedComponentRenderExtensions.Render``1(Bunit.IRenderedComponent{``0},System.Action{Bunit.ComponentParameterCollectionBuilder{``0}})), which is also available as a version that takes the zero or more component parameters, e.g. created through the component parameter factory helper methods, if you prefer that method of passing parameters.
3636

3737
> [!NOTE]
38-
> Passing parameters to components through the [`Render(...)`](xref:Bunit.RenderedComponentRenderExtensions.Render``1(Bunit.RenderedComponent{``0},System.Action{Bunit.ComponentParameterCollectionBuilder{``0}})) methods is identical to doing it with the `Render<TComponent>(...)` methods, described in detail on the <xref:passing-parameters-to-components> page.
38+
> Passing parameters to components through the [`Render(...)`](xref:Bunit.RenderedComponentRenderExtensions.Render``1(Bunit.IRenderedComponent{``0},System.Action{Bunit.ComponentParameterCollectionBuilder{``0}})) methods is identical to doing it with the `Render<TComponent>(...)` methods, described in detail on the <xref:passing-parameters-to-components> page.
3939
4040
## InvokeAsync
4141

@@ -56,7 +56,7 @@ To invoke the `Calculate()` method on the component instance, do the following:
5656
The highlighted line shows the call to `InvokeAsync()`, which is passed an `Action` delegate that calls the `Calculate` method.
5757

5858
> [!TIP]
59-
> The instance of a component under test is available through the <xref:Bunit.RenderedComponent`1.Instance> property.
59+
> The instance of a component under test is available through the <xref:Bunit.IRenderedComponent`1.Instance> property.
6060
6161
### Advanced use cases
6262

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ The solution is to inherit from bUnits `BunitContext` instead, i.e.:
444444

445445
## Limitations of rendering a `RenderFragment` inside a test
446446

447-
When rendering a `RenderFragment` using the <xref:Bunit.BunitContext.Render(Microsoft.AspNetCore.Components.RenderFragment)> method, the created <xref:Bunit.RenderedFragment> is static. This means that it will not re-render even if events are triggered.
447+
When rendering a `RenderFragment` using the <xref:Bunit.BunitContext.Render(Microsoft.AspNetCore.Components.RenderFragment)> method, the created <xref:Bunit.IRenderedComponent`1> is static. This means that it will not re-render even if events are triggered.
448448
```razor
449449
@inherits BunitContext
450450

docs/site/docs/verification/async-assertion.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ A test can fail if a component performs asynchronous renders. This may be due to
99

1010
You need to handle this specifically in your tests because tests execute in the test framework's synchronization context and the test renderer executes renders in its own synchronization context. If you do not, you will likely experience tests that sometimes pass, and sometimes fail.
1111

12-
bUnit comes with two methods that help to deal with this issue: the [`WaitForAssertion()`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForAssertion(Bunit.RenderedFragment,System.Action,System.Nullable{System.TimeSpan})) method covered on this page, and the [`WaitForState()`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForState(Bunit.RenderedFragment,System.Func{System.Boolean},System.Nullable{System.TimeSpan})) method covered on the <xref:awaiting-async-state> page.
12+
bUnit comes with two methods that help to deal with this issue: the [`WaitForAssertion()`](xref:Bunit.RenderedComponentWaitForHelperExtensions.WaitForAssertion``1(Bunit.IRenderedComponent{``0},System.Action,System.Nullable{System.TimeSpan})) method covered on this page, and the [`WaitForState()`](xref:Bunit.RenderedComponentWaitForHelperExtensions.WaitForState``1(Bunit.IRenderedComponent{``0},System.Func{System.Boolean},System.Nullable{System.TimeSpan})) method covered on the <xref:awaiting-async-state> page.
1313

1414
Let's start by taking a look at the ` WaitForAssertion` method in more detail.
1515

1616
## Waiting for assertion to pass using `WaitForAssertion`
1717

18-
The [`WaitForAssertion(Action, TimeSpan?)`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForAssertion(Bunit.RenderedFragment,System.Action,System.Nullable{System.TimeSpan})) method can be used to block and wait in a test method until the provided assert action does not throw an exception, or until the timeout is reached (the default timeout is one second).
18+
The [`WaitForAssertion(Action, TimeSpan?)`](xref:Bunit.RenderedComponentWaitForHelperExtensions.WaitForAssertion``1(Bunit.IRenderedComponent{``0},System.Action,System.Nullable{System.TimeSpan})) method can be used to block and wait in a test method until the provided assert action does not throw an exception, or until the timeout is reached (the default timeout is one second).
1919

2020
> [!NOTE]
2121
> The `WaitForAssertion()` method will try the assert action passed to it when the `WaitForAssertion()` method is called and every time the component under test renders.

0 commit comments

Comments
 (0)