Skip to content

Commit 8a2396e

Browse files
committed
feat: Remove unused abstraction
1 parent ee1ebfa commit 8a2396e

38 files changed

+151
-189
lines changed

MIGRATION.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@ public static class Extensions
2525

2626
## Merge of `bunit.core` and `bunit.web`
2727
The `bunit.core` and `bunit.web` packages have been merged into a single `bunit` package. If you used either of these packages, you should remove them and install the `bunit` package instead.
28+
29+
## Removal of unneeded abstraction
30+
31+
### `IRenderedComponentBase<TComponent>` and `IRenderedFragmentBase`
32+
`IRenderedComponentBase<TComponent>` and `IRenderedFragmentBase` have been removed. They were used to provide a common base class for `IRenderedComponent<TComponent>` and `IRenderedFragment`, but this is no longer needed (due to the merge of the project). If you used either of these interfaces, you should replace them with `IRenderedComponent<TComponent>` and `IRenderedFragment` respectively.
33+
34+
### `WebTestRender` merged into `TestRender`
35+
The `WebTestRender` class has been merged into the `TestRender` class. If you used `WebTestRender`, you should replace it with `TestRender`.

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

Lines changed: 2 additions & 2 deletions
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.IRenderedFragmentBase,System.Func{System.Boolean},System.Nullable{System.TimeSpan})) method covered on this page, and the [`WaitForAssertion()`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForAssertion(Bunit.IRenderedFragmentBase,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.RenderedFragmentWaitForHelperExtensions.WaitForState(Bunit.IRenderedFragment,Func{System.Boolean},System.Nullable{TimeSpan})) method covered on this page, and the [`WaitForAssertion()`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForAssertion(Bunit.IRenderedFragment,Action,System.Nullable{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.IRenderedFragmentBase,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.RenderedFragmentWaitForHelperExtensions.WaitForState(Bunit.IRenderedFragment,Func{System.Boolean},System.Nullable{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

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

1515
> [!WARNING]
16-
> For `IAsyncDisposable` (since .net5) relying on [`WaitForState()`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForState(Bunit.IRenderedFragmentBase,System.Func{System.Boolean},System.Nullable{System.TimeSpan})) or [`WaitForAssertion()`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForAssertion(Bunit.IRenderedFragmentBase,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.RenderedFragmentWaitForHelperExtensions.WaitForState(Bunit.IRenderedFragment,Func{System.Boolean},System.Nullable{TimeSpan})) or [`WaitForAssertion()`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForAssertion(Bunit.IRenderedFragment,Action,System.Nullable{TimeSpan})) will not work as a disposed component will not trigger a new render cycle.
1717
1818
## Checking for exceptions
1919
`Dispose` as well as `DisposeAsync` can throw exceptions which can be asserted as well. If a component under test throws an exception in `Dispose` the [`DisposeComponents`](xref:Bunit.TestContextBase.DisposeComponents) will throw the exception to the user code:

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@ In `.razor` based tests, using the <xref:Bunit.TestContext>'s <xref:Bunit.TestCo
1111

1212
If you have a <xref:Bunit.IRenderedFragment> or 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.IRenderedComponent`1>, it is possible to cause the component to render again directly through the [`Render()`](xref:Bunit.TestContext.Render``1(Microsoft.AspNetCore.Components.RenderFragment)) method or one of the [`SetParametersAndRender()`](xref:Bunit.RenderedComponentRenderExtensions.SetParametersAndRender``1(Bunit.IRenderedComponentBase{``0},System.Action{Bunit.ComponentParameterCollectionBuilder{``0}})) methods, or indirectly through the [`InvokeAsync()`](xref:Bunit.IRenderedFragmentBase.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.TestContext.Render``1(Microsoft.AspNetCore.Components.RenderFragment)) method or one of the [`SetParametersAndRender()`](xref:Bunit.RenderedComponentRenderExtensions.SetParametersAndRender``1(Bunit.IRenderedComponent{``0},System.Action{Bunit.ComponentParameterCollectionBuilder{``0}})) methods, or indirectly through the [`InvokeAsync()`](xref:Bunit.IRenderedFragment.Bunit.RenderedFragmentInvokeAsyncExtensions.InvokeAsync(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.IRenderedComponentBase{``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})) 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=16&end=22&highlight=5)]
2323

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

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

31-
The [`SetParametersAndRender(...)`](xref:Bunit.RenderedComponentRenderExtensions.SetParametersAndRender``1(Bunit.IRenderedComponentBase{``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 [`SetParametersAndRender(...)`](xref:Bunit.RenderedComponentRenderExtensions.SetParametersAndRender``1(Bunit.IRenderedComponent{``0},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=29&end=39&highlight=7-9)]
3434

35-
The highlighted line shows the call to [`SetParametersAndRender()`](xref:Bunit.RenderedComponentRenderExtensions.SetParametersAndRender``1(Bunit.IRenderedComponentBase{``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 [`SetParametersAndRender()`](xref:Bunit.RenderedComponentRenderExtensions.SetParametersAndRender``1(Bunit.IRenderedComponent{``0},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 [`SetParametersAndRender(...)`](xref:Bunit.RenderedComponentRenderExtensions.SetParametersAndRender``1(Bunit.IRenderedComponentBase{``0},System.Action{Bunit.ComponentParameterCollectionBuilder{``0}})) methods is identical to doing it with the `RenderComponent<TComponent>(...)` methods, described in detail on the <xref:passing-parameters-to-components> page.
38+
> Passing parameters to components through the [`SetParametersAndRender(...)`](xref:Bunit.RenderedComponentRenderExtensions.SetParametersAndRender``1(Bunit.IRenderedComponent{``0},Action{Bunit.ComponentParameterCollectionBuilder{``0}})) methods is identical to doing it with the `RenderComponent<TComponent>(...)` methods, described in detail on the <xref:passing-parameters-to-components> page.
3939
4040
## InvokeAsync
4141

@@ -72,4 +72,4 @@ This can also be used to assert intermediate states during an asynchronous opera
7272

7373
[!code-cshtml[CalcWithLoading.razor](../../../samples/components/CalcWithLoading.razor)]
7474

75-
[!code-csharp[](../../../samples/tests/xunit/ReRenderTest.cs?start=71&end=82&highlight=7)]
75+
[!code-csharp[](../../../samples/tests/xunit/ReRenderTest.cs?start=71&end=82&highlight=7)]

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

Lines changed: 2 additions & 2 deletions
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.IRenderedFragmentBase,System.Action,System.Nullable{System.TimeSpan})) method covered on this page, and the [`WaitForState()`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForState(Bunit.IRenderedFragmentBase,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.RenderedFragmentWaitForHelperExtensions.WaitForAssertion(Bunit.IRenderedFragment,Action,System.Nullable{TimeSpan})) method covered on this page, and the [`WaitForState()`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForState(Bunit.IRenderedFragment,Func{System.Boolean},System.Nullable{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.IRenderedFragmentBase,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.RenderedFragmentWaitForHelperExtensions.WaitForAssertion(Bunit.IRenderedFragment,Action,System.Nullable{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.

docs/site/docs/verification/verify-component-state.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: Verifying the state of a component under test
55

66
# Verifying the state of a component
77

8-
The instance of a component under test is available through the <xref:Bunit.IRenderedComponentBase`1.Instance> property on the <xref:Bunit.IRenderedComponent`1> type. When using the <xref:Bunit.TestContext>'s `RenderComponent<TComponent>()` method, this is the type returned.
8+
The instance of a component under test is available through the <xref:Bunit.IRenderedComponent`1.Instance> property on the <xref:Bunit.IRenderedComponent`1> type. When using the <xref:Bunit.TestContext>'s `RenderComponent<TComponent>()` method, this is the type returned.
99

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

@@ -14,7 +14,7 @@ In `.razor` based tests, using the <xref:Bunit.TestContext>'s <xref:Bunit.TestCo
1414
1515
## Inspecting the Component Under Test
1616

17-
The <xref:Bunit.IRenderedComponentBase`1.Instance> property on the <xref:Bunit.IRenderedComponent`1> type provides access to the component under test. For example:
17+
The <xref:Bunit.IRenderedComponent`1.Instance> property on the <xref:Bunit.IRenderedComponent`1> type provides access to the component under test. For example:
1818

1919
```csharp
2020
IRenderedComponent<Alert> cut = RenderComponent<Alert>();
@@ -25,9 +25,9 @@ Alert alert = cut.Instance;
2525
```
2626

2727
> [!WARNING]
28-
> While it is possible to set `[Parameter]` and `[CascadingParameter]` properties directly through the <xref:Bunit.IRenderedComponentBase`1.Instance> property on the <xref:Bunit.IRenderedComponent`1> type, doing so does not implicitly trigger a render and the component life-cycle methods are not called.
28+
> While it is possible to set `[Parameter]` and `[CascadingParameter]` properties directly through the <xref:Bunit.IRenderedComponent`1.Instance> property on the <xref:Bunit.IRenderedComponent`1> type, doing so does not implicitly trigger a render and the component life-cycle methods are not called.
2929
>
30-
> The correct approach is to set parameters through the [`SetParametersAndRender()`](xref:Bunit.RenderedComponentRenderExtensions.SetParametersAndRender``1(Bunit.IRenderedComponentBase{``0},System.Action{Bunit.ComponentParameterCollectionBuilder{``0}})) methods. See the <xref:trigger-renders> page for more on this.
30+
> The correct approach is to set parameters through the [`SetParametersAndRender()`](xref:Bunit.RenderedComponentRenderExtensions.SetParametersAndRender``1(Bunit.IRenderedComponent{``0},Action{Bunit.ComponentParameterCollectionBuilder{``0}})) methods. See the <xref:trigger-renders> page for more on this.
3131
3232
## Finding Components in the Render Tree
3333

src/bunit/Asserting/CompareToExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Bunit;
1010
public static class CompareToExtensions
1111
{
1212
/// <summary>
13-
/// Compare the rendered markup in the <paramref name="actual"/> <see cref="IRenderedFragmentBase"/>
13+
/// Compare the rendered markup in the <paramref name="actual"/> <see cref="IRenderedFragment"/>
1414
/// with that in the <paramref name="expected"/> markup string.
1515
/// </summary>
1616
/// <param name="actual">Source of rendered markup to check.</param>
@@ -28,8 +28,8 @@ public static IReadOnlyList<IDiff> CompareTo(this IRenderedFragment actual, stri
2828
}
2929

3030
/// <summary>
31-
/// Compare the rendered markup in the <paramref name="actual"/> <see cref = "IRenderedFragmentBase" /> to
32-
/// the rendered markup in the <paramref name="expected"/> <see cref = "IRenderedFragmentBase" />.
31+
/// Compare the rendered markup in the <paramref name="actual"/> <see cref = "IRenderedFragment" /> to
32+
/// the rendered markup in the <paramref name="expected"/> <see cref = "IRenderedFragment" />.
3333
/// </summary>
3434
/// <param name="actual">Source of rendered markup to check.</param>
3535
/// <param name="expected">Source of rendered markup to compare with.</param>

src/bunit/Asserting/MarkupMatchesAssertExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public static void MarkupMatches(this IRenderedFragment actual, RenderFragment e
278278
actual.Services.GetRequiredService<IRenderedComponentActivator>(),
279279
actual.Services.GetRequiredService<TestServiceProvider>(),
280280
actual.Services.GetRequiredService<ILoggerFactory>());
281-
var renderedFragment = (IRenderedFragment)renderer.RenderFragment(expected);
281+
var renderedFragment = renderer.RenderFragment(expected);
282282
MarkupMatches(actual, renderedFragment, userMessage);
283283
}
284284

@@ -414,7 +414,7 @@ public static void MarkupMatches(this IEnumerable<IElement> actual, IEnumerable<
414414
private static IRenderedFragment AdhocRenderRenderFragment(this RenderFragment renderFragment)
415415
{
416416
using var ctx = new TestContext();
417-
return (IRenderedFragment)ctx.RenderInsideRenderTree(renderFragment);
417+
return ctx.RenderInsideRenderTree(renderFragment);
418418
}
419419

420420
private static INodeList ToNodeList(this string markup, BunitHtmlParser? htmlParser)

src/bunit/ComponentFactories/GenericComponentFactory{TComponent,TSubstituteComponent}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ namespace Bunit.ComponentFactories;
22

33
internal sealed class GenericComponentFactory<TComponent, TSubstituteComponent> : IComponentFactory
44
where TComponent : IComponent
5-
where TSubstituteComponent : IComponent
5+
where TSubstituteComponent : IComponent, new()
66
{
77
public bool CanCreate(Type componentType) => componentType == typeof(TComponent);
88

9-
public IComponent Create(Type componentType) => Activator.CreateInstance<TSubstituteComponent>()!;
9+
public IComponent Create(Type componentType) => new TSubstituteComponent();
1010
}

src/bunit/Extensions/ComponentFactoryCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static class ComponentFactoryCollectionExtensions
1818
/// <returns>A <see cref="ComponentFactoryCollection"/>.</returns>
1919
public static ComponentFactoryCollection Add<TComponent, TSubstituteComponent>(this ComponentFactoryCollection factories)
2020
where TComponent : IComponent
21-
where TSubstituteComponent : IComponent
21+
where TSubstituteComponent : IComponent, new()
2222
{
2323
ArgumentNullException.ThrowIfNull(factories);
2424

0 commit comments

Comments
 (0)