From 3ad4fa1160752e9fa9225aeabf563a3ebac7119d Mon Sep 17 00:00:00 2001 From: Steven Giesel Date: Fri, 22 Mar 2024 15:29:40 +0100 Subject: [PATCH] fix: Fixes after merge --- CHANGELOG.md | 2 +- MIGRATION.md | 4 +- docs/site/docs/verification/verify-markup.md | 20 ++++----- .../Web.AngleSharp/WrapperElementGenerator.cs | 2 +- .../ByLabelTextElementFactory.cs | 4 +- .../Labels/LabelQueryExtensions.cs | 6 +-- .../Strategies/ILabelTextQueryStrategy.cs | 2 +- .../LabelTextUsingAriaLabelStrategy.cs | 2 +- .../LabelTextUsingAriaLabelledByStrategy.cs | 2 +- .../LabelTextUsingForAttributeStrategy.cs | 2 +- .../LabelTextUsingWrappedElementStrategy.cs | 2 +- src/bunit.web.query/bunit.web.query.csproj | 2 +- .../Labels/LabelQueryExtensionsTests.cs | 42 +++++++++---------- 13 files changed, 46 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e23662bae..4f8c1f7f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -664,7 +664,7 @@ List of new features. - Added `Render(RenderFragment)` and `Render(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`, 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`, whereas the non-generic one returns a `RenderedFragment`. Calling `Render(RenderFragent)` is equivalent to calling `Render(RenderFragment).FindComponent()`, e.g. it returns the first component in the render tree of type `TComponent`. This is different from the `RenderComponent()` method, where `TComponent` _is_ the root component of the render tree. diff --git a/MIGRATION.md b/MIGRATION.md index 275a9c313..02f944288 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -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`. 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` and `CreateLogger` on `IServiceProvider` The `IsNullOrEmpty` extension method on `IEnumerable` 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: @@ -29,7 +29,7 @@ The `bunit.core` and `bunit.web` packages have been merged into a single `bunit` ## Removal of unneeded abstraction ### `IRenderedComponentBase` and `RenderedFragmentBase` -`IRenderedComponentBase`, `IRenderedComponent`, `IRenderedFragmentBase`, `IRenderedFragment` and `RenderedFragmentBase` have been removed. +`IRenderedComponentBase`, `IRenderedComponent`, `RenderedFragmentBase`, `RenderedFragment` and `RenderedFragmentBase` have been removed. If you used either of these types, you should replace them with `RenderedComponent` or `RenderedFragment` respectively. ### `WebTestRender` merged into `BunitTestRender` diff --git a/docs/site/docs/verification/verify-markup.md b/docs/site/docs/verification/verify-markup.md index 147777e2e..4f6d4b1e6 100644 --- a/docs/site/docs/verification/verify-markup.md +++ b/docs/site/docs/verification/verify-markup.md @@ -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 or a . Through these, it is possible to access the rendered markup (HTML) of the component and, in the case of , the instance of the component. +When a component is rendered in a test, the result is a or a . Through these, it is possible to access the rendered markup (HTML) of the component and, in the case of , the instance of the component. > [!NOTE] > An inherits from . This page will only cover features of the type. is covered on the page. ## Inspecting DOM nodes -The rendered markup from a component is available as a DOM node through the property on . 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 property on . 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 `` 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 `` component listed below. [!code-razor[FancyTable.razor](../../../samples/components/FancyTable.razor)] @@ -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 property on , 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 property on , 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 @@ -122,7 +122,7 @@ Learn more about the customization options on the property on . This holds the *raw* HTML from the component as a `string`. +To access the rendered markup of a component, just use the property on . 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. diff --git a/src/bunit.generators.internal/Web.AngleSharp/WrapperElementGenerator.cs b/src/bunit.generators.internal/Web.AngleSharp/WrapperElementGenerator.cs index b8b294e0a..ddbbd8db2 100644 --- a/src/bunit.generators.internal/Web.AngleSharp/WrapperElementGenerator.cs +++ b/src/bunit.generators.internal/Web.AngleSharp/WrapperElementGenerator.cs @@ -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. diff --git a/src/bunit.web.query/ByLabelTextElementFactory.cs b/src/bunit.web.query/ByLabelTextElementFactory.cs index eef786319..ca5cb63f8 100644 --- a/src/bunit.web.query/ByLabelTextElementFactory.cs +++ b/src/bunit.web.query/ByLabelTextElementFactory.cs @@ -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; diff --git a/src/bunit.web.query/Labels/LabelQueryExtensions.cs b/src/bunit.web.query/Labels/LabelQueryExtensions.cs index 8d7c42fad..7355987bc 100644 --- a/src/bunit.web.query/Labels/LabelQueryExtensions.cs +++ b/src/bunit.web.query/Labels/LabelQueryExtensions.cs @@ -4,7 +4,7 @@ namespace Bunit; /// -/// Extension methods for querying IRenderedFragments by Label +/// Extension methods for querying RenderedFragments by Label /// public static class LabelQueryExtensions { @@ -23,7 +23,7 @@ public static class LabelQueryExtensions /// The rendered fragment to search. /// The text of the label to search (i.e. the InnerText of the Label, such as "First Name" for a ``) /// Method used to override the default behavior of FindByLabelText. - public static IElement FindByLabelText(this IRenderedFragment renderedFragment, string labelText, Action? configureOptions = null) + public static IElement FindByLabelText(this RenderedFragment renderedFragment, string labelText, Action? configureOptions = null) { var options = ByLabelTextOptions.Default; if (configureOptions is not null) @@ -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) { diff --git a/src/bunit.web.query/Labels/Strategies/ILabelTextQueryStrategy.cs b/src/bunit.web.query/Labels/Strategies/ILabelTextQueryStrategy.cs index 3c1ba8ea5..64290af77 100644 --- a/src/bunit.web.query/Labels/Strategies/ILabelTextQueryStrategy.cs +++ b/src/bunit.web.query/Labels/Strategies/ILabelTextQueryStrategy.cs @@ -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); } diff --git a/src/bunit.web.query/Labels/Strategies/LabelTextUsingAriaLabelStrategy.cs b/src/bunit.web.query/Labels/Strategies/LabelTextUsingAriaLabelStrategy.cs index 2ef029809..6db805f36 100644 --- a/src/bunit.web.query/Labels/Strategies/LabelTextUsingAriaLabelStrategy.cs +++ b/src/bunit.web.query/Labels/Strategies/LabelTextUsingAriaLabelStrategy.cs @@ -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 { diff --git a/src/bunit.web.query/Labels/Strategies/LabelTextUsingAriaLabelledByStrategy.cs b/src/bunit.web.query/Labels/Strategies/LabelTextUsingAriaLabelledByStrategy.cs index 3c81e47ac..8364c7454 100644 --- a/src/bunit.web.query/Labels/Strategies/LabelTextUsingAriaLabelledByStrategy.cs +++ b/src/bunit.web.query/Labels/Strategies/LabelTextUsingAriaLabelledByStrategy.cs @@ -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]"); diff --git a/src/bunit.web.query/Labels/Strategies/LabelTextUsingForAttributeStrategy.cs b/src/bunit.web.query/Labels/Strategies/LabelTextUsingForAttributeStrategy.cs index af3eb91f8..1b02afe5c 100644 --- a/src/bunit.web.query/Labels/Strategies/LabelTextUsingForAttributeStrategy.cs +++ b/src/bunit.web.query/Labels/Strategies/LabelTextUsingForAttributeStrategy.cs @@ -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)); diff --git a/src/bunit.web.query/Labels/Strategies/LabelTextUsingWrappedElementStrategy.cs b/src/bunit.web.query/Labels/Strategies/LabelTextUsingWrappedElementStrategy.cs index 43f47503c..67ecff9a7 100644 --- a/src/bunit.web.query/Labels/Strategies/LabelTextUsingWrappedElementStrategy.cs +++ b/src/bunit.web.query/Labels/Strategies/LabelTextUsingWrappedElementStrategy.cs @@ -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)); diff --git a/src/bunit.web.query/bunit.web.query.csproj b/src/bunit.web.query/bunit.web.query.csproj index 0fded8cba..c4ef80695 100644 --- a/src/bunit.web.query/bunit.web.query.csproj +++ b/src/bunit.web.query/bunit.web.query.csproj @@ -27,7 +27,7 @@ - + diff --git a/tests/bunit.web.query.tests/Labels/LabelQueryExtensionsTests.cs b/tests/bunit.web.query.tests/Labels/LabelQueryExtensionsTests.cs index 9ee2c2e6c..f4c3a5d4c 100644 --- a/tests/bunit.web.query.tests/Labels/LabelQueryExtensionsTests.cs +++ b/tests/bunit.web.query.tests/Labels/LabelQueryExtensionsTests.cs @@ -2,7 +2,7 @@ namespace Bunit.Labels; -public class LabelQueryExtensionsTests : TestContext +public class LabelQueryExtensionsTests : BunitContext { public static TheoryData HtmlElementsThatCanHaveALabel { get; } = new() { @@ -19,7 +19,7 @@ public class LabelQueryExtensionsTests : TestContext public void Test001(string htmlElementWithLabel) { var labelText = $"Label for {htmlElementWithLabel} 1"; - var cut = RenderComponent(ps => + var cut = Render(ps => ps.AddChildContent($""" <{htmlElementWithLabel} id="{htmlElementWithLabel}-with-label" /> @@ -36,7 +36,7 @@ public void Test001(string htmlElementWithLabel) public void Test002() { var expectedLabelText = Guid.NewGuid().ToString(); - var cut = RenderComponent(ps => + var cut = Render(ps => ps.AddChildContent($""" {Guid.NewGuid()} """)); @@ -50,7 +50,7 @@ public void Test002() public void Test003(string htmlElementWithLabel) { var labelText = $"{htmlElementWithLabel} Wrapped Label"; - var cut = RenderComponent(ps => + var cut = Render(ps => ps.AddChildContent($"""