Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<BitButtonGroupItem>
{
new() { Text = "One" },
new() { Text = "Two" }
};

var comp = RenderComponent<BitButtonGroup<BitButtonGroupItem>>(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<BitButtonGroupItem>
{
new() { Text = "ClickMe", OnClick = i => actionInvokedText = i.Text },
new() { Text = "Other" }
};

var component = RenderComponent<BitButtonGroup<BitButtonGroupItem>>(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<BitButtonGroupItem>
{
new() { Text = "A", Key = "a" },
new() { Text = "B", Key = "b" }
};

var comp = RenderComponent<BitButtonGroup<BitButtonGroupItem>>(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);
}
}
Loading