Skip to content

Commit

Permalink
refactor: rename RenderComponent and SetParametersAndRender to Render
Browse files Browse the repository at this point in the history
  • Loading branch information
egil committed Jun 10, 2023
1 parent 20df97e commit 170b381
Show file tree
Hide file tree
Showing 45 changed files with 270 additions and 279 deletions.
6 changes: 5 additions & 1 deletion MIGRATION.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Migration Guide `v1` to `v2`
This document describes the changes that need to be made to migrate from bUnit 1.x to 2.x.

## Renamed all `RenderComponent` and `SetParametersAndRender` to `Render`

To make the API more consistent, `RenderComponent` and `SetParametersAndRender` methods have been renamed to `Render`.

## Removal of `ComponentParameter` and method using them

Using `ComponentParameter` and factory methods to create them is not recommend in V1 and have now been removed in V2. Instead, use the strongly typed builder pattern that enables you to pass parameters to components you render.
Using the `ComponentParameter` type and related factory methods is not recommended in V1 and have now been removed in V2. Instead, use the strongly typed builder pattern that enables you to pass parameters to components you render or write your tests in .razor files. Go to https://bunit.dev/docs/providing-input/passing-parameters-to-components to learn more.

## Removal of `GetChangesSinceFirstRender` and `GetChangesSinceLastRender` methods
The `GetChangesSinceFirstRender` and `GetChangesSinceLastRender` methods have been removed from `IRenderedComponent<TComponent>`. There is no one-to-one replacement for these methods, but the general idea is to select the HTML in question via `Find` and assert against that.
Expand Down
2 changes: 1 addition & 1 deletion benchmark/bunit.benchmarks/BenchmarkBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void Cleanup()
}

protected IRenderedComponent<TComponent> RenderComponent<TComponent>()
where TComponent : IComponent => Renderer.RenderComponent<TComponent>();
where TComponent : IComponent => Renderer.Render<TComponent>();

protected virtual void InternalCleanup()
{
Expand Down
12 changes: 6 additions & 6 deletions src/bunit.template/template/Company.BlazorTests1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

<ItemGroup>
<PackageReference Include="bunit" Version="#{NBGV_NuGetPackageVersion}#" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand All @@ -33,13 +33,13 @@
</ItemGroup>

<ItemGroup Condition="'$(testFramework_nunit)' == 'true'">
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>

<ItemGroup Condition="'$(testFramework_mstest)' == 'true'">
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions src/bunit.template/template/CounterCSharpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class CounterCSharpTests : BunitTestContext
public void CounterStartsAtZero()
{
// Arrange
var cut = RenderComponent<Counter>();
var cut = Render<Counter>();

// Assert that content of the paragraph shows counter at zero
cut.Find("p").MarkupMatches("<p>Current count: 0</p>");
Expand All @@ -39,7 +39,7 @@ public void CounterStartsAtZero()
public void ClickingButtonIncrementsCounter()
{
// Arrange
var cut = RenderComponent<Counter>();
var cut = Render<Counter>();

// Act - click button to increment counter
cut.Find("button").Click();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Bunit.Extensions;
/// Helper methods that make it easier to work directly with a <see cref="BunitRenderer"/>
/// in bUnit web.
/// </summary>
public static class TestRendererExtensions
public static class BunitRendererExtensions
{
/// <summary>
/// Renders a <typeparamref name="TComponent"/> with the parameters build with the <paramref name="parameterBuilder"/> passed to it.
Expand All @@ -15,7 +15,7 @@ public static class TestRendererExtensions
/// <param name="renderer">The renderer to use.</param>
/// <param name="parameterBuilder">The a builder to create parameters to pass to the component.</param>
/// <returns>A <see cref="IRenderedComponent{TComponent}"/> that provides access to the rendered component.</returns>
public static IRenderedComponent<TComponent> RenderComponent<TComponent>(this BunitRenderer renderer, Action<ComponentParameterCollectionBuilder<TComponent>>? parameterBuilder = null)
public static IRenderedComponent<TComponent> Render<TComponent>(this BunitRenderer renderer, Action<ComponentParameterCollectionBuilder<TComponent>>? parameterBuilder = null)
where TComponent : IComponent
{
ArgumentNullException.ThrowIfNull(renderer);
Expand Down
23 changes: 5 additions & 18 deletions src/bunit/Extensions/RenderedComponentRenderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,13 @@ namespace Bunit;
/// </summary>
public static class RenderedComponentRenderExtensions
{
/// <summary>
/// Render the component under test again.
/// </summary>
/// <param name="renderedComponent">The rendered component to re-render.</param>
/// <typeparam name="TComponent">The type of the component.</typeparam>
public static void Render<TComponent>(this IRenderedComponent<TComponent> renderedComponent)
where TComponent : IComponent
=> SetParametersAndRender(renderedComponent, ParameterView.Empty);

/// <summary>
/// Render the component under test again with the provided <paramref name="parameters"/>.
/// </summary>
/// <param name="renderedComponent">The rendered component to re-render with new parameters.</param>
/// <param name="parameters">Parameters to pass to the component upon rendered.</param>
/// <typeparam name="TComponent">The type of the component.</typeparam>
public static void SetParametersAndRender<TComponent>(this IRenderedComponent<TComponent> renderedComponent, ParameterView parameters)
public static void Render<TComponent>(this IRenderedComponent<TComponent> renderedComponent, ParameterView parameters)
where TComponent : IComponent
{
ArgumentNullException.ThrowIfNull(renderedComponent);
Expand Down Expand Up @@ -53,13 +44,13 @@ public static void SetParametersAndRender<TComponent>(this IRenderedComponent<TC
/// <param name="renderedComponent">The rendered component to re-render with new parameters.</param>
/// <param name="parameterBuilder">An action that receives a <see cref="ComponentParameterCollectionBuilder{TComponent}"/>.</param>
/// <typeparam name="TComponent">The type of the component.</typeparam>
public static void SetParametersAndRender<TComponent>(this IRenderedComponent<TComponent> renderedComponent, Action<ComponentParameterCollectionBuilder<TComponent>>? parameterBuilder = null)
public static void Render<TComponent>(this IRenderedComponent<TComponent> renderedComponent, Action<ComponentParameterCollectionBuilder<TComponent>>? parameterBuilder = null)
where TComponent : IComponent
{
ArgumentNullException.ThrowIfNull(renderedComponent);

var builder = new ComponentParameterCollectionBuilder<TComponent>(parameterBuilder);
SetParametersAndRender(renderedComponent, ToParameterView(builder.Build()));
Render(renderedComponent, ToParameterView(builder.Build()));
}

private static ParameterView ToParameterView(IReadOnlyCollection<ComponentParameter> parameters)
Expand All @@ -68,23 +59,19 @@ private static ParameterView ToParameterView(IReadOnlyCollection<ComponentParame

if (parameters.Count > 0)
{
var paramDict = new Dictionary<string, object?>(StringComparer.Ordinal);
var paramDict = new Dictionary<string, object?>(parameters.Count, StringComparer.Ordinal);

foreach (var param in parameters)
{
if (param.IsCascadingValue)
throw new InvalidOperationException($"You cannot provide a new cascading value through the {nameof(SetParametersAndRender)} method.");
throw new InvalidOperationException($"You cannot provide a new cascading value through the {nameof(Render)} method.");
if (param.Name is null)
throw new InvalidOperationException("A parameters name is required.");

paramDict.Add(param.Name, param.Value);
}

// Nullable is disabled to get around the issue with different annotations
// between .netstandard2.1 and net6.
#nullable disable
parameterView = ParameterView.FromDictionary(paramDict);
#nullable restore
}

return parameterView;
Expand Down
2 changes: 1 addition & 1 deletion src/bunit/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public TestContext()
/// <typeparam name="TComponent">Type of the component to render.</typeparam>
/// <param name="parameterBuilder">The ComponentParameterBuilder action to add type safe parameters to pass to the component when it is rendered.</param>
/// <returns>The rendered <typeparamref name="TComponent"/>.</returns>
public virtual IRenderedComponent<TComponent> RenderComponent<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>? parameterBuilder = null)
public virtual IRenderedComponent<TComponent> Render<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>? parameterBuilder = null)
where TComponent : IComponent
{
var renderFragment = new ComponentParameterCollectionBuilder<TComponent>(parameterBuilder)
Expand Down
4 changes: 2 additions & 2 deletions src/bunit/TestContextWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public abstract class TestContextWrapper
/// <typeparam name="TComponent">Type of the component to render.</typeparam>
/// <param name="parameterBuilder">The ComponentParameterBuilder action to add type safe parameters to pass to the component when it is rendered.</param>
/// <returns>The rendered <typeparamref name="TComponent"/>.</returns>
public virtual IRenderedComponent<TComponent> RenderComponent<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>> parameterBuilder)
public virtual IRenderedComponent<TComponent> Render<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>? parameterBuilder = null)
where TComponent : IComponent
=> TestContext?.RenderComponent<TComponent>(parameterBuilder) ?? throw new InvalidOperationException("The TestContext has not been initialized.");
=> TestContext?.Render<TComponent>(parameterBuilder) ?? throw new InvalidOperationException("The TestContext has not been initialized.");

/// <summary>
/// Renders the <paramref name="renderFragment"/> and returns the first <typeparamref name="TComponent"/> in the resulting render tree.
Expand Down
16 changes: 8 additions & 8 deletions tests/bunit.tests/Asserting/CompareToDiffingExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,26 @@ public void Test001(MethodInfo methodInfo, string argName, object[] args)
[Fact(DisplayName = "CompareTo with rendered fragment and string")]
public void Test002()
{
var rf1 = RenderComponent<Simple1>(ps => ps.Add(p => p.Header, "FOO"));
var rf2 = RenderComponent<Simple1>(ps => ps.Add(p => p.Header, "BAR"));
var rf1 = Render<Simple1>(ps => ps.Add(p => p.Header, "FOO"));
var rf2 = Render<Simple1>(ps => ps.Add(p => p.Header, "BAR"));

rf1.CompareTo(rf2.Markup).Count.ShouldBe(1);
}

[Fact(DisplayName = "CompareTo with rendered fragment and rendered fragment")]
public void Test003()
{
var rf1 = RenderComponent<Simple1>(ps => ps.Add(p => p.Header, "FOO"));
var rf2 = RenderComponent<Simple1>(ps => ps.Add(p => p.Header, "BAR"));
var rf1 = Render<Simple1>(ps => ps.Add(p => p.Header, "FOO"));
var rf2 = Render<Simple1>(ps => ps.Add(p => p.Header, "BAR"));

rf1.CompareTo(rf2).Count.ShouldBe(1);
}

[Fact(DisplayName = "CompareTo with INode and INodeList")]
public void Test004()
{
var rf1 = RenderComponent<Simple1>(ps => ps.Add(p => p.Header, "FOO"));
var rf2 = RenderComponent<Simple1>(ps => ps.Add(p => p.Header, "BAR"));
var rf1 = Render<Simple1>(ps => ps.Add(p => p.Header, "FOO"));
var rf2 = Render<Simple1>(ps => ps.Add(p => p.Header, "BAR"));

var elm = rf1.Find("h1");
elm.CompareTo(rf2.Nodes).Count.ShouldBe(1);
Expand All @@ -68,8 +68,8 @@ public void Test004()
[Fact(DisplayName = "CompareTo with INodeList and INode")]
public void Test005()
{
var rf1 = RenderComponent<Simple1>(ps => ps.Add(p => p.Header, "FOO"));
var rf2 = RenderComponent<Simple1>(ps => ps.Add(p => p.Header, "BAR"));
var rf1 = Render<Simple1>(ps => ps.Add(p => p.Header, "FOO"));
var rf2 = Render<Simple1>(ps => ps.Add(p => p.Header, "BAR"));

var elm = rf1.Find("h1");
rf2.Nodes.CompareTo(elm).Count.ShouldBe(1);
Expand Down
12 changes: 6 additions & 6 deletions tests/bunit.tests/Asserting/MarkupMatchesAssertExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,39 +103,39 @@ public void Test011()
[Fact(DisplayName = "MarkupMatches combination works with FindAll and FindComponents<T>")]
public void Test012()
{
var cut = RenderComponent<RefToSimple1Child>();
var cut = Render<RefToSimple1Child>();

cut.FindAll("h1").MarkupMatches(cut.FindComponents<Simple1>());
}

[Fact(DisplayName = "MarkupMatches combination works with FindAll and a markup string")]
public void Test013()
{
var cut = RenderComponent<NoArgs>();
var cut = Render<NoArgs>();

cut.FindAll("h1").MarkupMatches("<h1>Hello world</h1>");
}

[Fact(DisplayName = "MarkupMatches combination works with Find and FindAll")]
public void Test014()
{
var cut = RenderComponent<TwoChildren>();
var cut = Render<TwoChildren>();

cut.Find("div").MarkupMatches(cut.FindAll("div"));
}

[Fact(DisplayName = "MarkupMatches correctly ignores scoped CSS attributes")]
public void Test_net5_001()
{
var cut = RenderComponent<ScopedCssElements>();
var cut = Render<ScopedCssElements>();

cut.MarkupMatches("<h1>Hello Pink World!</h1>");
}

[Fact(DisplayName = "Handles HtmlUnknownElement when comparing elements")]
public void Test015()
{
var chart = RenderComponent<SimpleSvg>();
var chart = Render<SimpleSvg>();

// the path will be returned as a SvgElement since it is
// parsed in the context of a <svg> element.
Expand All @@ -155,7 +155,7 @@ public void Test016()
<zui-button diff:ignoreAttributes></zui-button>
</div>";

var cut = RenderComponent<CustomElement>();
var cut = Render<CustomElement>();

cut.MarkupMatches(expectedMarkup);
}
Expand Down
Loading

0 comments on commit 170b381

Please sign in to comment.