diff --git a/CHANGELOG.md b/CHANGELOG.md index 40af9edaa..53e5c85cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ List of new features. - Added `InvokeAsync(Func)` to `RenderedComponentInvokeAsyncExtensions`. By [@JeroenBos](https://github.com/JeroenBos) in [#151](https://github.com/egil/bUnit/pull/177). - Added `ITestRenderer Renderer { get ; }` to `IRenderedFragment` to make it possible to simplify the `IRenderedComponentBase` interface. By [@JeroenBos](https://github.com/JeroenBos) in [#151](https://github.com/egil/bUnit/pull/177). +- Added support for scoped CSS to `MarkupMatches` and related comparer methods. By [@egil](https://github.com/egil) in [#195](https://github.com/egil/bUnit/pull/195). ### Changed List of changes in existing functionality. diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 7d7b3007c..dc1a9ad8e 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -36,11 +36,11 @@ - - - - - + + + + + diff --git a/src/bunit.web/Rendering/Internal/Htmlizer.cs b/src/bunit.web/Rendering/Internal/Htmlizer.cs index cbf9d530c..6dcab3555 100644 --- a/src/bunit.web/Rendering/Internal/Htmlizer.cs +++ b/src/bunit.web/Rendering/Internal/Htmlizer.cs @@ -26,11 +26,13 @@ internal class Htmlizer }; private const string BLAZOR_INTERNAL_ATTR_PREFIX = "__internal_"; + private const string BLAZOR_CSS_SCOPE_ATTR_PREFIX = "b-"; public const string BLAZOR_ATTR_PREFIX = "blazor:"; public const string ELEMENT_REFERENCE_ATTR_NAME = BLAZOR_ATTR_PREFIX + "elementreference"; public static bool IsBlazorAttribute(string attributeName) - => attributeName.StartsWith(BLAZOR_ATTR_PREFIX, StringComparison.Ordinal); + => attributeName.StartsWith(BLAZOR_ATTR_PREFIX, StringComparison.Ordinal) || + attributeName.StartsWith(BLAZOR_CSS_SCOPE_ATTR_PREFIX, StringComparison.Ordinal); public static string ToBlazorAttribute(string attributeName) { diff --git a/tests/Directory.Build.props b/tests/Directory.Build.props index 396747810..533e26b94 100644 --- a/tests/Directory.Build.props +++ b/tests/Directory.Build.props @@ -15,10 +15,10 @@ - - - - + + + + diff --git a/tests/bunit.testassets/SampleComponents/ScopedCssElements.razor b/tests/bunit.testassets/SampleComponents/ScopedCssElements.razor new file mode 100644 index 000000000..6daae3e36 --- /dev/null +++ b/tests/bunit.testassets/SampleComponents/ScopedCssElements.razor @@ -0,0 +1 @@ +

Hello Pink World!

diff --git a/tests/bunit.testassets/SampleComponents/ScopedCssElements.razor.css b/tests/bunit.testassets/SampleComponents/ScopedCssElements.razor.css new file mode 100644 index 000000000..91b27d521 --- /dev/null +++ b/tests/bunit.testassets/SampleComponents/ScopedCssElements.razor.css @@ -0,0 +1,3 @@ +h1 { + color: deeppink; +} diff --git a/tests/bunit.testassets/bunit.testassets.csproj b/tests/bunit.testassets/bunit.testassets.csproj index 891556089..51e5e52bf 100644 --- a/tests/bunit.testassets/bunit.testassets.csproj +++ b/tests/bunit.testassets/bunit.testassets.csproj @@ -2,7 +2,6 @@ netstandard2.1;net5.0 - 3.0 Bunit.TestAssets Bunit.TestAssets false @@ -15,15 +14,19 @@ CS8602;CS8603;CS8625 + + 3.0 + + - - - + + + diff --git a/tests/bunit.web.tests/Asserting/MarkupMatchesAssertExtensionsTest.net5.cs b/tests/bunit.web.tests/Asserting/MarkupMatchesAssertExtensionsTest.net5.cs new file mode 100644 index 000000000..22f220228 --- /dev/null +++ b/tests/bunit.web.tests/Asserting/MarkupMatchesAssertExtensionsTest.net5.cs @@ -0,0 +1,27 @@ +#if NET5_0 + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Bunit.TestAssets.SampleComponents; + +using Xunit; + +namespace Bunit +{ + public partial class MarkupMatchesAssertExtensionsTest : TestContext + { + [Fact(DisplayName = "MarkupMatches correctly ignores scoped css attributes")] + public void Test_net5_001() + { + var cut = RenderComponent(); + + cut.MarkupMatches("

Hello Pink World!

"); + } + } +} + +#endif diff --git a/tests/bunit.web.tests/Rendering/Internal/HtmlizerTests.cs b/tests/bunit.web.tests/Rendering/Internal/HtmlizerTests.cs index 7c8d995a7..e89eedc55 100644 --- a/tests/bunit.web.tests/Rendering/Internal/HtmlizerTests.cs +++ b/tests/bunit.web.tests/Rendering/Internal/HtmlizerTests.cs @@ -1,5 +1,7 @@ using System; using Bunit.TestAssets.BlazorE2E; +using Bunit.TestAssets.SampleComponents; + using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Rendering; using Microsoft.AspNetCore.Components.Web; @@ -8,9 +10,9 @@ namespace Bunit.Rendering.Internal { - public class HtmlizerTests : TestContext + public partial class HtmlizerTests : TestContext { - [Theory(DisplayName = "The component contains correctly prefixed internal attributes.")] + [Theory(DisplayName = "Htmlizer correctly prefixed stopPropagation and preventDefault attributes")] [InlineData(false, true)] [InlineData(true, false)] [InlineData(true, true)] diff --git a/tests/bunit.web.tests/Rendering/Internal/HtmlizerTests.net5.cs b/tests/bunit.web.tests/Rendering/Internal/HtmlizerTests.net5.cs new file mode 100644 index 000000000..33d0ed460 --- /dev/null +++ b/tests/bunit.web.tests/Rendering/Internal/HtmlizerTests.net5.cs @@ -0,0 +1,27 @@ +#if NET5_0 +using System; +using Bunit.TestAssets.BlazorE2E; +using Bunit.TestAssets.SampleComponents; + +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Rendering; +using Microsoft.AspNetCore.Components.Web; +using Shouldly; +using Xunit; + +namespace Bunit.Rendering.Internal +{ + public partial class HtmlizerTests : TestContext + { + [Theory(DisplayName = "IsBlazorAttribute correctly identifies Blazor attributes")] + [InlineData("b-twl12ishk1=\"\"")] + [InlineData("blazor:onclick=\"1\"")] + [InlineData("blazor:__internal_stopPropagation_onclick=\"\"")] + [InlineData("blazor:__internal_preventDefault_onclick=\"\"")] + public void TestNET5_001(string blazorAttribute) + { + Htmlizer.IsBlazorAttribute(blazorAttribute).ShouldBeTrue(); + } + } +} +#endif