From a287b9a1b89f2be1e031d7721690299c345b13bd Mon Sep 17 00:00:00 2001 From: msynk Date: Sun, 16 Nov 2025 13:06:14 +0330 Subject: [PATCH] add missing tests of BitButtonGroup component #11646 --- .../Components/Buttons/BitButtonGroupTests.cs | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/BlazorUI/Bit.BlazorUI.Tests/Components/Buttons/BitButtonGroupTests.cs diff --git a/src/BlazorUI/Bit.BlazorUI.Tests/Components/Buttons/BitButtonGroupTests.cs b/src/BlazorUI/Bit.BlazorUI.Tests/Components/Buttons/BitButtonGroupTests.cs new file mode 100644 index 0000000000..d49fe98370 --- /dev/null +++ b/src/BlazorUI/Bit.BlazorUI.Tests/Components/Buttons/BitButtonGroupTests.cs @@ -0,0 +1,91 @@ +using System.Collections.Generic; +using Bunit; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Bit.BlazorUI.Tests.Components.Buttons.ButtonGroup; + +[TestClass] +public class BitButtonGroupTests : BunitTestContext +{ + [TestMethod] + public void BitButtonGroupShouldRenderItemsFromItemsParameter() + { + var items = new List + { + new() { Text = "One" }, + new() { Text = "Two" } + }; + + var comp = RenderComponent>(parameters => + { + parameters.Add(p => p.Items, items); + }); + + var buttons = comp.FindAll("button"); + Assert.AreEqual(2, buttons.Count); + Assert.IsTrue(buttons[0].TextContent.Contains("One")); + Assert.IsTrue(buttons[1].TextContent.Contains("Two")); + } + + [TestMethod] + public void BitButtonGroupShouldInvokeOnItemClickAndItemOnClickAction() + { + var actionInvokedText = string.Empty; + var onItemClickCalled = false; + + var items = new List + { + new() { Text = "ClickMe", OnClick = i => actionInvokedText = i.Text }, + new() { Text = "Other" } + }; + + var component = RenderComponent>(parameters => + { + parameters.Add(p => p.Items, items); + parameters.Add(p => p.OnItemClick, (BitButtonGroupItem it) => onItemClickCalled = true); + }); + + var btn = component.Find(".bit-btg-itm"); + Assert.IsNotNull(btn); + + btn.Click(); + + Assert.IsTrue(onItemClickCalled); + Assert.AreEqual("ClickMe", actionInvokedText); + } + + [TestMethod] + public void BitButtonGroupToggleDefaultKeyShouldSetToggledItemAndRaiseOnToggleChange() + { + string? toggledKey = null; + + var items = new List + { + new() { Text = "A", Key = "a" }, + new() { Text = "B", Key = "b" } + }; + + var comp = RenderComponent>(parameters => + { + parameters.Add(p => p.Items, items); + parameters.Add(p => p.Toggle, true); + parameters.Add(p => p.DefaultToggleKey, "b"); + parameters.Add(p => p.OnToggleChange, (BitButtonGroupItem it) => toggledKey = it?.Key); + }); + + // After initialization the default toggled item should be applied + Assert.AreEqual("b", toggledKey); + + // The rendered button with toggled class + var toggled = comp.FindAll(".bit-btg-chk"); + Assert.IsTrue(toggled.Count >= 1); + Assert.IsTrue(toggled[0].TextContent.Contains("B")); + + // Click the first button to toggle + var firstBtn = comp.FindAll("button")[0]; + firstBtn.Click(); + + // Now toggledKey should change to 'a' + Assert.AreEqual("a", toggledKey); + } +}