Skip to content

Commit 54067f8

Browse files
authored
feat(blazorui): add missing tests of BitButtonGroup component #11646 (#11647)
1 parent c0a0d0a commit 54067f8

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System.Collections.Generic;
2+
using Bunit;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace Bit.BlazorUI.Tests.Components.Buttons.ButtonGroup;
6+
7+
[TestClass]
8+
public class BitButtonGroupTests : BunitTestContext
9+
{
10+
[TestMethod]
11+
public void BitButtonGroupShouldRenderItemsFromItemsParameter()
12+
{
13+
var items = new List<BitButtonGroupItem>
14+
{
15+
new() { Text = "One" },
16+
new() { Text = "Two" }
17+
};
18+
19+
var comp = RenderComponent<BitButtonGroup<BitButtonGroupItem>>(parameters =>
20+
{
21+
parameters.Add(p => p.Items, items);
22+
});
23+
24+
var buttons = comp.FindAll("button");
25+
Assert.AreEqual(2, buttons.Count);
26+
Assert.IsTrue(buttons[0].TextContent.Contains("One"));
27+
Assert.IsTrue(buttons[1].TextContent.Contains("Two"));
28+
}
29+
30+
[TestMethod]
31+
public void BitButtonGroupShouldInvokeOnItemClickAndItemOnClickAction()
32+
{
33+
var actionInvokedText = string.Empty;
34+
var onItemClickCalled = false;
35+
36+
var items = new List<BitButtonGroupItem>
37+
{
38+
new() { Text = "ClickMe", OnClick = i => actionInvokedText = i.Text },
39+
new() { Text = "Other" }
40+
};
41+
42+
var component = RenderComponent<BitButtonGroup<BitButtonGroupItem>>(parameters =>
43+
{
44+
parameters.Add(p => p.Items, items);
45+
parameters.Add(p => p.OnItemClick, (BitButtonGroupItem it) => onItemClickCalled = true);
46+
});
47+
48+
var btn = component.Find(".bit-btg-itm");
49+
Assert.IsNotNull(btn);
50+
51+
btn.Click();
52+
53+
Assert.IsTrue(onItemClickCalled);
54+
Assert.AreEqual("ClickMe", actionInvokedText);
55+
}
56+
57+
[TestMethod]
58+
public void BitButtonGroupToggleDefaultKeyShouldSetToggledItemAndRaiseOnToggleChange()
59+
{
60+
string? toggledKey = null;
61+
62+
var items = new List<BitButtonGroupItem>
63+
{
64+
new() { Text = "A", Key = "a" },
65+
new() { Text = "B", Key = "b" }
66+
};
67+
68+
var comp = RenderComponent<BitButtonGroup<BitButtonGroupItem>>(parameters =>
69+
{
70+
parameters.Add(p => p.Items, items);
71+
parameters.Add(p => p.Toggle, true);
72+
parameters.Add(p => p.DefaultToggleKey, "b");
73+
parameters.Add(p => p.OnToggleChange, (BitButtonGroupItem it) => toggledKey = it?.Key);
74+
});
75+
76+
// After initialization the default toggled item should be applied
77+
Assert.AreEqual("b", toggledKey);
78+
79+
// The rendered button with toggled class
80+
var toggled = comp.FindAll(".bit-btg-chk");
81+
Assert.IsTrue(toggled.Count >= 1);
82+
Assert.IsTrue(toggled[0].TextContent.Contains("B"));
83+
84+
// Click the first button to toggle
85+
var firstBtn = comp.FindAll("button")[0];
86+
firstBtn.Click();
87+
88+
// Now toggledKey should change to 'a'
89+
Assert.AreEqual("a", toggledKey);
90+
}
91+
}

0 commit comments

Comments
 (0)