Skip to content

Commit

Permalink
fix: Fixes after merge
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet authored and bUnitBot committed Apr 19, 2024
1 parent 04576c1 commit 3ad4fa1
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 46 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ List of new features.

- Added `Render(RenderFragment)` and `Render<TComponent>(RenderFragment)` methods to `TestContext`, as well as various overloads to the `MarkupMatches` methods, that also takes a `RenderFragment` as the expected value.

The difference between the generic `Render` method and the non-generic one is that the generic returns an `IRenderedComponent<TComponent>`, whereas the non-generic one returns a `IRenderedFragment`.
The difference between the generic `Render` method and the non-generic one is that the generic returns an `IRenderedComponent<TComponent>`, whereas the non-generic one returns a `RenderedFragment`.

Calling `Render<TComponent>(RenderFragent)` is equivalent to calling `Render(RenderFragment).FindComponent<TComponent>()`, e.g. it returns the first component in the render tree of type `TComponent`. This is different from the `RenderComponent<TComponent>()` method, where `TComponent` _is_ the root component of the render tree.

Expand Down
4 changes: 2 additions & 2 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This document describes the changes that need to be made to migrate from bUnit 1
## Removal of `GetChangesSinceFirstRender` and `GetChangesSinceLastRender` methods
The `GetChangesSinceFirstRender` and `GetChangesSinceLastRender` methods have been removed from `RenderedComponent<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.

Alternatively, the `IRenderedFragment` still offers the `OnMarkupUpdated` event, which can be used to assert against the markup after a render.
Alternatively, the `RenderedFragment` still offers the `OnMarkupUpdated` event, which can be used to assert against the markup after a render.

## Removal of `IsNullOrEmpty` extension method on `IEnumerable<T>` and `CreateLogger` on `IServiceProvider`
The `IsNullOrEmpty` extension method on `IEnumerable<T>` has been removed, as well as the `CreateLogger` extension method on `IServiceProvider`. These extension methods are pretty common and conflict with other libraries. These methods can be recreated like this:
Expand All @@ -29,7 +29,7 @@ The `bunit.core` and `bunit.web` packages have been merged into a single `bunit`
## Removal of unneeded abstraction

### `IRenderedComponentBase<TComponent>` and `RenderedFragmentBase`
`IRenderedComponentBase<TComponent>`, `IRenderedComponent<TComponent>`, `IRenderedFragmentBase`, `IRenderedFragment` and `RenderedFragmentBase` have been removed.
`IRenderedComponentBase<TComponent>`, `IRenderedComponent<TComponent>`, `RenderedFragmentBase`, `RenderedFragment` and `RenderedFragmentBase` have been removed.
If you used either of these types, you should replace them with `RenderedComponent<TComponent>` or `RenderedFragment` respectively.

### `WebTestRender` merged into `BunitTestRender`
Expand Down
20 changes: 10 additions & 10 deletions docs/site/docs/verification/verify-markup.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ The following sections will cover each of these.

## Result of rendering components

When a component is rendered in a test, the result is a <xref:Bunit.IRenderedFragment> or a <xref:Bunit.IRenderedComponent`1>. Through these, it is possible to access the rendered markup (HTML) of the component and, in the case of <xref:Bunit.IRenderedComponent`1>, the instance of the component.
When a component is rendered in a test, the result is a <xref:Bunit.RenderedFragment> or a <xref:Bunit.RenderedComponent`1>. Through these, it is possible to access the rendered markup (HTML) of the component and, in the case of <xref:Bunit.RenderedComponent`1>, the instance of the component.

> [!NOTE]
> An <xref:Bunit.RenderedComponent`1> inherits from <xref:Bunit.RenderedFragment>. This page will only cover features of the <xref:Bunit.RenderedFragment> type. <xref:Bunit.RenderedComponent`1> is covered on the <xref:verify-component-state> page.
## Inspecting DOM nodes

The rendered markup from a component is available as a DOM node through the <xref:Bunit.IRenderedFragment.Nodes> property on <xref:Bunit.IRenderedFragment>. The nodes and element types comes from [AngleSharp](https://anglesharp.github.io/) that follows the W3C DOM API specifications and gives you the same results as a state-of-the-art browser’s implementation of the DOM API in JavaScript. Besides the official DOM API, AngleSharp and bUnit add some useful extension methods on top. This makes working with DOM nodes convenient.
The rendered markup from a component is available as a DOM node through the <xref:Bunit.RenderedFragment.Nodes> property on <xref:Bunit.RenderedFragment>. The nodes and element types comes from [AngleSharp](https://anglesharp.github.io/) that follows the W3C DOM API specifications and gives you the same results as a state-of-the-art browser’s implementation of the DOM API in JavaScript. Besides the official DOM API, AngleSharp and bUnit add some useful extension methods on top. This makes working with DOM nodes convenient.

### Finding DOM elements

bUnit supports multiple different ways of searching and querying the rendered HTML elements:

- `FindByLabelText(string labelText)` that takes a text string used to label an input element and returns an `IElement` as output, or throws an exception if none are found (this is included in the experimental library [bunit.web.query](https://www.nuget.org/packages/bunit.web.query)). Use this method when possible compared to the generic `Find` and `FindAll` methods.
- [`Find(string cssSelector)`](xref:Bunit.RenderedFragmentExtensions.Find(Bunit.IRenderedFragment,System.String)) takes a "CSS selector" as input and returns an `IElement` as output, or throws an exception if none are found.
- [`FindAll(string cssSelector)`](xref:Bunit.RenderedFragmentExtensions.FindAll(Bunit.IRenderedFragment,System.String,System.Boolean)) takes a "CSS selector" as input and returns a list of `IElement` elements.
- [`Find(string cssSelector)`](xref:Bunit.RenderedFragmentExtensions.Find(Bunit.RenderedFragment,System.String)) takes a "CSS selector" as input and returns an `IElement` as output, or throws an exception if none are found.
- [`FindAll(string cssSelector)`](xref:Bunit.RenderedFragmentExtensions.FindAll(Bunit.RenderedFragment,System.String,System.Boolean)) takes a "CSS selector" as input and returns a list of `IElement` elements.

Let's see some examples of using the [`Find(string cssSelector)`](xref:Bunit.RenderedFragmentExtensions.Find(Bunit.IRenderedFragment,System.String)) and [`FindAll(string cssSelector)`](xref:Bunit.RenderedFragmentExtensions.FindAll(Bunit.IRenderedFragment,System.String,System.Boolean)) methods to query the `<FancyTable>` component listed below.
Let's see some examples of using the [`Find(string cssSelector)`](xref:Bunit.RenderedFragmentExtensions.Find(Bunit.RenderedFragment,System.String)) and [`FindAll(string cssSelector)`](xref:Bunit.RenderedFragmentExtensions.FindAll(Bunit.RenderedFragment,System.String,System.Boolean)) methods to query the `<FancyTable>` component listed below.

[!code-razor[FancyTable.razor](../../../samples/components/FancyTable.razor)]

Expand All @@ -53,15 +53,15 @@ Once you have one or more elements, you verify against them, such as by inspec

#### Auto-refreshing Find() queries

An element found with the [`Find(string cssSelector)`](xref:Bunit.RenderedFragmentExtensions.Find(Bunit.IRenderedFragment,System.String)) method will be updated if the component it came from is re-rendered.
An element found with the [`Find(string cssSelector)`](xref:Bunit.RenderedFragmentExtensions.Find(Bunit.RenderedFragment,System.String)) method will be updated if the component it came from is re-rendered.

However, that does not apply to elements that are found by traversing the DOM tree via the <xref:Bunit.IRenderedFragment.Nodes> property on <xref:Bunit.IRenderedFragment>, for example, as those nodes do not know when their root component is re-rendered. Consequently, they don’t know when they should be updated.
However, that does not apply to elements that are found by traversing the DOM tree via the <xref:Bunit.RenderedFragment.Nodes> property on <xref:Bunit.RenderedFragment>, for example, as those nodes do not know when their root component is re-rendered. Consequently, they don’t know when they should be updated.

As a result of this, it is always recommended to use the [`Find(string cssSelector)`](xref:Bunit.RenderedFragmentExtensions.Find(Bunit.IRenderedFragment,System.String)) method when searching for a single element. Alternatively, always reissue the query whenever you need the element.
As a result of this, it is always recommended to use the [`Find(string cssSelector)`](xref:Bunit.RenderedFragmentExtensions.Find(Bunit.RenderedFragment,System.String)) method when searching for a single element. Alternatively, always reissue the query whenever you need the element.

#### Auto-refreshable FindAll() queries

The [`FindAll(string cssSelector, bool enableAutoRefresh = false)`](xref:Bunit.RenderedFragmentExtensions.FindAll(Bunit.IRenderedFragment,System.String,System.Boolean)) method has an optional parameter, `enableAutoRefresh`, which when set to `true` will return a collection of `IElement`. This automatically refreshes itself when the component the elements came from is re-rendered.
The [`FindAll(string cssSelector, bool enableAutoRefresh = false)`](xref:Bunit.RenderedFragmentExtensions.FindAll(Bunit.RenderedFragment,System.String,System.Boolean)) method has an optional parameter, `enableAutoRefresh`, which when set to `true` will return a collection of `IElement`. This automatically refreshes itself when the component the elements came from is re-rendered.

## Semantic comparison of markup

Expand Down Expand Up @@ -122,7 +122,7 @@ Learn more about the customization options on the <xref:semantic-html-comparison

## Verification of raw markup

To access the rendered markup of a component, just use the <xref:Bunit.IRenderedFragment.Markup> property on <xref:Bunit.IRenderedFragment>. This holds the *raw* HTML from the component as a `string`.
To access the rendered markup of a component, just use the <xref:Bunit.RenderedFragment.Markup> property on <xref:Bunit.RenderedFragment>. This holds the *raw* HTML from the component as a `string`.

> [!WARNING]
> Be aware that all indentions and whitespace in your components (`.razor` files) are included in the raw rendered markup, so it is often wise to normalize the markup string a little. For example, via the string `Trim()` method to make the tests more stable. Otherwise, a change to the formatting in your components might break the tests unnecessarily when it does not need to.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private static void GenerateOrdinaryMethod(StringBuilder source, IMethodSymbol m
var methodParts = method.ToDisplayParts(GeneratorConfig.SymbolFormat);

// It seems that the ToDisplayParts will return ...
//
//
// public global::AngleSharp.Dom.IShadowRoot AttachShadow(global::AngleSharp.Dom.ShadowRootMode mode = 0)
//
// when called on a method with a default enum parameters specified.
Expand Down
4 changes: 2 additions & 2 deletions src/bunit.web.query/ByLabelTextElementFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ namespace Bunit;

internal sealed class ByLabelTextElementFactory : IElementWrapperFactory
{
private readonly IRenderedFragment testTarget;
private readonly RenderedFragment testTarget;
private readonly string labelText;
private readonly ByLabelTextOptions options;

public Action? OnElementReplaced { get; set; }

public ByLabelTextElementFactory(IRenderedFragment testTarget, string labelText, ByLabelTextOptions options)
public ByLabelTextElementFactory(RenderedFragment testTarget, string labelText, ByLabelTextOptions options)
{
this.testTarget = testTarget;
this.labelText = labelText;
Expand Down
6 changes: 3 additions & 3 deletions src/bunit.web.query/Labels/LabelQueryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Bunit;

/// <summary>
/// Extension methods for querying IRenderedFragments by Label
/// Extension methods for querying RenderedFragments by Label
/// </summary>
public static class LabelQueryExtensions
{
Expand All @@ -23,7 +23,7 @@ public static class LabelQueryExtensions
/// <param name="renderedFragment">The rendered fragment to search.</param>
/// <param name="labelText">The text of the label to search (i.e. the InnerText of the Label, such as "First Name" for a `<label>First Name</label>`)</param>
/// <param name="configureOptions">Method used to override the default behavior of FindByLabelText.</param>
public static IElement FindByLabelText(this IRenderedFragment renderedFragment, string labelText, Action<ByLabelTextOptions>? configureOptions = null)
public static IElement FindByLabelText(this RenderedFragment renderedFragment, string labelText, Action<ByLabelTextOptions>? configureOptions = null)
{
var options = ByLabelTextOptions.Default;
if (configureOptions is not null)
Expand All @@ -35,7 +35,7 @@ public static IElement FindByLabelText(this IRenderedFragment renderedFragment,
return FindByLabelTextInternal(renderedFragment, labelText, options) ?? throw new LabelNotFoundException(labelText);
}

internal static IElement? FindByLabelTextInternal(this IRenderedFragment renderedFragment, string labelText, ByLabelTextOptions options)
internal static IElement? FindByLabelTextInternal(this RenderedFragment renderedFragment, string labelText, ByLabelTextOptions options)
{
foreach (var strategy in LabelTextQueryStrategies)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace Bunit.Labels.Strategies;

internal interface ILabelTextQueryStrategy
{
IElement? FindElement(IRenderedFragment renderedFragment, string labelText, ByLabelTextOptions options);
IElement? FindElement(RenderedFragment renderedFragment, string labelText, ByLabelTextOptions options);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Bunit.Labels.Strategies;

internal sealed class LabelTextUsingAriaLabelStrategy : ILabelTextQueryStrategy
{
public IElement? FindElement(IRenderedFragment renderedFragment, string labelText, ByLabelTextOptions options)
public IElement? FindElement(RenderedFragment renderedFragment, string labelText, ByLabelTextOptions options)
{
var caseSensitivityQualifier = options.ComparisonType switch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Bunit.Labels.Strategies;

internal sealed class LabelTextUsingAriaLabelledByStrategy : ILabelTextQueryStrategy
{
public IElement? FindElement(IRenderedFragment renderedFragment, string labelText, ByLabelTextOptions options)
public IElement? FindElement(RenderedFragment renderedFragment, string labelText, ByLabelTextOptions options)
{
var elementsWithAriaLabelledBy = renderedFragment.Nodes.TryQuerySelectorAll("[aria-labelledby]");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Bunit.Labels.Strategies;

internal sealed class LabelTextUsingForAttributeStrategy : ILabelTextQueryStrategy
{
public IElement? FindElement(IRenderedFragment renderedFragment, string labelText, ByLabelTextOptions options)
public IElement? FindElement(RenderedFragment renderedFragment, string labelText, ByLabelTextOptions options)
{
var matchingLabel = renderedFragment.Nodes.TryQuerySelectorAll("label")
.SingleOrDefault(l => l.TextContent.Trim().Equals(labelText, options.ComparisonType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Bunit.Labels.Strategies;

internal sealed class LabelTextUsingWrappedElementStrategy : ILabelTextQueryStrategy
{
public IElement? FindElement(IRenderedFragment renderedFragment, string labelText, ByLabelTextOptions options)
public IElement? FindElement(RenderedFragment renderedFragment, string labelText, ByLabelTextOptions options)
{
var matchingLabel = renderedFragment.Nodes.TryQuerySelectorAll("label")
.SingleOrDefault(l => l.GetInnerText().Trim().StartsWith(labelText, options.ComparisonType));
Expand Down
2 changes: 1 addition & 1 deletion src/bunit.web.query/bunit.web.query.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\bunit.web\bunit.web.csproj" />
<ProjectReference Include="..\bunit\bunit.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 3ad4fa1

Please sign in to comment.