From 6c121f7a1d73e89d77c44c0f01d8fa1c3f0b1b99 Mon Sep 17 00:00:00 2001 From: Saleh Yusefnejad Date: Tue, 9 Dec 2025 00:28:57 +0330 Subject: [PATCH 1/3] add missing tests of BitCascadingValueProvider #11850 --- .../Params/BitCascadingValueProviderTests.cs | 214 ++++++++++++++++++ .../Utils/Params/CascadingConsumer.cs | 16 ++ .../Utils/Params/NullableConsumer.cs | 16 ++ 3 files changed, 246 insertions(+) create mode 100644 src/BlazorUI/Bit.BlazorUI.Tests/Utils/Params/BitCascadingValueProviderTests.cs create mode 100644 src/BlazorUI/Bit.BlazorUI.Tests/Utils/Params/CascadingConsumer.cs create mode 100644 src/BlazorUI/Bit.BlazorUI.Tests/Utils/Params/NullableConsumer.cs diff --git a/src/BlazorUI/Bit.BlazorUI.Tests/Utils/Params/BitCascadingValueProviderTests.cs b/src/BlazorUI/Bit.BlazorUI.Tests/Utils/Params/BitCascadingValueProviderTests.cs new file mode 100644 index 0000000000..a54fd8f706 --- /dev/null +++ b/src/BlazorUI/Bit.BlazorUI.Tests/Utils/Params/BitCascadingValueProviderTests.cs @@ -0,0 +1,214 @@ +using System.Collections.Generic; +using System.Linq; +using Bunit; +using Microsoft.AspNetCore.Components; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Bit.BlazorUI.Tests.Utils.Params; + +[TestClass] +public class BitCascadingValueProviderTests : BunitTestContext +{ + [TestMethod] + public void ShouldProvideCascadingValuesFromEnumerable() + { + var cascadingValues = new List + { + null, + new("hi", "Greeting"), + new(7) + }; + + var component = RenderComponent(parameters => + { + parameters.Add(p => p.Values, cascadingValues!); + parameters.AddChildContent(builder => + { + builder.OpenComponent(0); + builder.CloseComponent(); + }); + }); + + var consumer = component.FindComponent().Instance; + + Assert.AreEqual(7, consumer.Number); + Assert.AreEqual("hi", consumer.Greeting); + } + + [TestMethod] + public void ShouldProvideCascadingValuesFromValueList() + { + var cascadingValues = new BitCascadingValueList(); + cascadingValues.Add(3, isFixed: true); + cascadingValues.Add("message", "Greeting"); + + var component = RenderComponent(parameters => + { + parameters.Add(p => p.ValueList, cascadingValues); + parameters.AddChildContent(builder => + { + builder.OpenComponent(0); + builder.CloseComponent(); + }); + }); + + var intCascade = component.FindComponent>().Instance; + var stringCascade = component.FindComponent>().Instance; + + Assert.AreEqual(3, intCascade.Value); + Assert.IsTrue(intCascade.IsFixed); + + Assert.AreEqual("message", stringCascade.Value); + Assert.IsFalse(stringCascade.IsFixed); + Assert.AreEqual("Greeting", stringCascade.Name); + } + + [TestMethod] + public void ShouldRenderChildContentWhenNoValuesAreProvided() + { + var childContentRendered = false; + + var component = RenderComponent(parameters => + { + parameters.AddChildContent(builder => + { + childContentRendered = true; + builder.AddContent(0, "plain-child"); + }); + }); + + Assert.IsTrue(childContentRendered); + component.MarkupMatches("plain-child"); + } + + [TestMethod] + public void ShouldRenderEmptyWhenNothingProvided() + { + var component = RenderComponent(); + + component.MarkupMatches(string.Empty); + } + + [TestMethod] + public void ShouldPreferValuesParameterOverValueList() + { + var values = new List { new("from-values", "Greeting") }; + var valueList = new BitCascadingValueList + { + { "from-value-list", "Greeting" } + }; + + var component = RenderComponent(parameters => + { + parameters.Add(p => p.Values, values); + parameters.Add(p => p.ValueList, valueList); + parameters.AddChildContent(builder => + { + builder.OpenComponent(0); + builder.CloseComponent(); + }); + }); + + var consumer = component.FindComponent().Instance; + + Assert.AreEqual("from-values", consumer.Greeting); + } + + [TestMethod] + public void ShouldSkipNullEntriesAndStillCascadeOthers() + { + var cascadingValues = new List + { + new(5), + null, + new("hello", "Greeting") + }; + + var component = RenderComponent(parameters => + { + parameters.Add(p => p.Values, cascadingValues!); + parameters.AddChildContent(builder => + { + builder.OpenComponent(0); + builder.CloseComponent(); + }); + }); + + var consumer = component.FindComponent().Instance; + + Assert.AreEqual(5, consumer.Number); + Assert.AreEqual("hello", consumer.Greeting); + } + + [TestMethod] + public void ShouldRespectOrderWhenDuplicateNamesExist() + { + var cascadingValues = new List + { + new("first", "Greeting"), + new("second", "Greeting") + }; + + var component = RenderComponent(parameters => + { + parameters.Add(p => p.Values, cascadingValues!); + parameters.AddChildContent(builder => + { + builder.OpenComponent(0); + builder.CloseComponent(); + }); + }); + + var consumer = component.FindComponent().Instance; + + Assert.AreEqual("second", consumer.Greeting); + } + + [TestMethod] + public void ShouldAllowNullValuesWhenTypeProvided() + { + var cascadingValues = new BitCascadingValueList(); + cascadingValues.Add(null, "Greeting"); + cascadingValues.Add(null); + + var component = RenderComponent(parameters => + { + parameters.Add(p => p.ValueList, cascadingValues); + parameters.AddChildContent(builder => + { + builder.OpenComponent(0); + builder.CloseComponent(); + }); + }); + + var stringCascade = component.FindComponent>().Instance; + var intCascade = component.FindComponent>().Instance; + var consumer = component.FindComponent().Instance; + + Assert.IsNull(stringCascade.Value); + Assert.IsNull(intCascade.Value); + Assert.AreEqual("Greeting", stringCascade.Name); + Assert.IsNull(consumer.NullableNumber); + component.MarkupMatches("-"); + } + + [TestMethod] + public void ShouldHandleManyCascadingValues() + { + var cascadingValues = Enumerable.Range(0, 50).Select(i => new BitCascadingValue(i)).ToList(); + + var component = RenderComponent(parameters => + { + parameters.Add(p => p.Values, cascadingValues); + parameters.AddChildContent(builder => + { + builder.OpenComponent(0); + builder.CloseComponent(); + }); + }); + + var consumer = component.FindComponent().Instance; + + Assert.AreEqual(49, consumer.Number); + } +} diff --git a/src/BlazorUI/Bit.BlazorUI.Tests/Utils/Params/CascadingConsumer.cs b/src/BlazorUI/Bit.BlazorUI.Tests/Utils/Params/CascadingConsumer.cs new file mode 100644 index 0000000000..8028029715 --- /dev/null +++ b/src/BlazorUI/Bit.BlazorUI.Tests/Utils/Params/CascadingConsumer.cs @@ -0,0 +1,16 @@ +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Rendering; + +namespace Bit.BlazorUI.Tests.Utils.Params; + +public sealed class CascadingConsumer : ComponentBase +{ + [CascadingParameter] public int Number { get; set; } + + [CascadingParameter(Name = "Greeting")] public string? Greeting { get; set; } + + protected override void BuildRenderTree(RenderTreeBuilder builder) + { + builder.AddContent(0, $"{Number}-{Greeting}"); + } +} diff --git a/src/BlazorUI/Bit.BlazorUI.Tests/Utils/Params/NullableConsumer.cs b/src/BlazorUI/Bit.BlazorUI.Tests/Utils/Params/NullableConsumer.cs new file mode 100644 index 0000000000..ff634fae4b --- /dev/null +++ b/src/BlazorUI/Bit.BlazorUI.Tests/Utils/Params/NullableConsumer.cs @@ -0,0 +1,16 @@ +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Rendering; + +namespace Bit.BlazorUI.Tests.Utils.Params; + +public sealed class NullableConsumer : ComponentBase +{ + [CascadingParameter] public int? NullableNumber { get; set; } + + [CascadingParameter(Name = "Greeting")] public string? Greeting { get; set; } + + protected override void BuildRenderTree(RenderTreeBuilder builder) + { + builder.AddContent(0, $"{NullableNumber}-{Greeting}"); + } +} From 031a00126c38fd1b08753cc86848ae25f8a92062 Mon Sep 17 00:00:00 2001 From: Saleh Yusefnejad Date: Tue, 9 Dec 2025 00:41:22 +0330 Subject: [PATCH 2/3] add more tests --- .../Params/BitCascadingValueProviderTests.cs | 73 ++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/src/BlazorUI/Bit.BlazorUI.Tests/Utils/Params/BitCascadingValueProviderTests.cs b/src/BlazorUI/Bit.BlazorUI.Tests/Utils/Params/BitCascadingValueProviderTests.cs index a54fd8f706..ad419ac1a0 100644 --- a/src/BlazorUI/Bit.BlazorUI.Tests/Utils/Params/BitCascadingValueProviderTests.cs +++ b/src/BlazorUI/Bit.BlazorUI.Tests/Utils/Params/BitCascadingValueProviderTests.cs @@ -7,7 +7,7 @@ namespace Bit.BlazorUI.Tests.Utils.Params; [TestClass] -public class BitCascadingValueProviderTests : BunitTestContext +public partial class BitCascadingValueProviderTests : BunitTestContext { [TestMethod] public void ShouldProvideCascadingValuesFromEnumerable() @@ -89,6 +89,25 @@ public void ShouldRenderEmptyWhenNothingProvided() component.MarkupMatches(string.Empty); } + [TestMethod] + public void ShouldRenderChildContentWhenValueListIsEmpty() + { + var childContentRendered = false; + + var component = RenderComponent(parameters => + { + parameters.Add(p => p.ValueList, new BitCascadingValueList()); + parameters.AddChildContent(builder => + { + childContentRendered = true; + builder.AddContent(0, "empty-list-child"); + }); + }); + + Assert.IsTrue(childContentRendered); + component.MarkupMatches("empty-list-child"); + } + [TestMethod] public void ShouldPreferValuesParameterOverValueList() { @@ -140,6 +159,32 @@ public void ShouldSkipNullEntriesAndStillCascadeOthers() Assert.AreEqual("hello", consumer.Greeting); } + [TestMethod] + public void ShouldCascadeWhenFirstEntryIsNull() + { + var cascadingValues = new List + { + null, + new("greet", "Greeting"), + new(11) + }; + + var component = RenderComponent(parameters => + { + parameters.Add(p => p.Values, cascadingValues!); + parameters.AddChildContent(builder => + { + builder.OpenComponent(0); + builder.CloseComponent(); + }); + }); + + var consumer = component.FindComponent().Instance; + + Assert.AreEqual(11, consumer.Number); + Assert.AreEqual("greet", consumer.Greeting); + } + [TestMethod] public void ShouldRespectOrderWhenDuplicateNamesExist() { @@ -211,4 +256,30 @@ public void ShouldHandleManyCascadingValues() Assert.AreEqual(49, consumer.Number); } + + [TestMethod] + public void ShouldDefaultIsFixedToFalse() + { + var cascadingValues = new List + { + new(1), + new("msg", "Greeting") + }; + + var component = RenderComponent(parameters => + { + parameters.Add(p => p.Values, cascadingValues!); + parameters.AddChildContent(builder => + { + builder.OpenComponent(0); + builder.CloseComponent(); + }); + }); + + var intCascade = component.FindComponent>().Instance; + var stringCascade = component.FindComponent>().Instance; + + Assert.IsFalse(intCascade.IsFixed); + Assert.IsFalse(stringCascade.IsFixed); + } } From c28d4f8a2ea0bc79abe6b2747c3eb1ccb60ca284 Mon Sep 17 00:00:00 2001 From: msynk Date: Tue, 9 Dec 2025 09:45:30 +0330 Subject: [PATCH 3/3] resolve review comment --- .../Utils/Params/BitCascadingValueProviderTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/BlazorUI/Bit.BlazorUI.Tests/Utils/Params/BitCascadingValueProviderTests.cs b/src/BlazorUI/Bit.BlazorUI.Tests/Utils/Params/BitCascadingValueProviderTests.cs index ad419ac1a0..52d59b2fee 100644 --- a/src/BlazorUI/Bit.BlazorUI.Tests/Utils/Params/BitCascadingValueProviderTests.cs +++ b/src/BlazorUI/Bit.BlazorUI.Tests/Utils/Params/BitCascadingValueProviderTests.cs @@ -234,6 +234,7 @@ public void ShouldAllowNullValuesWhenTypeProvided() Assert.IsNull(intCascade.Value); Assert.AreEqual("Greeting", stringCascade.Name); Assert.IsNull(consumer.NullableNumber); + Assert.IsNull(consumer.Greeting); component.MarkupMatches("-"); }