Skip to content

Commit

Permalink
Fix GetChildElementById no access to elements inside Card (#1429)
Browse files Browse the repository at this point in the history
* Add basic tests for GetChildElementById

* Fix no access to elements inside Card. Fixes #1406
  • Loading branch information
jerone authored Oct 27, 2024
1 parent 0fddec0 commit 003e38b
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/app/dev/DevToys.Api/Tool/GUI/Components/IUICard.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace DevToys.Api;

/// <summary>
/// A component that represents a empty card and <see cref="IUIElement"/> for the option value.
/// A component that represents an empty card and <see cref="IUIElement"/> for the option value.
/// </summary>
public interface IUICard : IUIElement
{
Expand All @@ -12,7 +12,7 @@ public interface IUICard : IUIElement
}

[DebuggerDisplay($"Id = {{{nameof(Id)}}}")]
internal class UICard : UIElement, IUICard
internal class UICard : UIElementWithChildren, IUICard
{
internal UICard(string? id, IUIElement uiElement)
: base(id)
Expand All @@ -22,6 +22,11 @@ internal UICard(string? id, IUIElement uiElement)
}

public IUIElement UIElement { get; }

protected override IEnumerable<IUIElement> GetChildren()
{
yield return UIElement;
}
}

public static partial class GUI
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
namespace DevToys.UnitTests.Tool.GUI.Components;

public class UIElementWithChildrenTests
{
private class TestUIElement : UIElementWithChildren
{
private readonly IEnumerable<IUIElement> _children;

public TestUIElement(string id, IEnumerable<IUIElement> children)
: base(id)
{
_children = children;
}

protected override IEnumerable<IUIElement> GetChildren() => _children;
}

[Fact]
public void GetChildElementById_ReturnsNull_WhenIdIsNullOrEmpty()
{
var element = new TestUIElement("parent", new List<IUIElement>());
Assert.Null(element.GetChildElementById(null!));
Assert.Null(element.GetChildElementById(string.Empty));
}

[Fact]
public void GetChildElementById_ReturnsNull_WhenChildNotFound()
{
var child = new Mock<IUIElement>();
child.Setup(c => c.Id).Returns("child1");

var element = new TestUIElement("parent", new List<IUIElement> { child.Object });
Assert.Null(element.GetChildElementById("child2"));
}

[Fact]
public void GetChildElementById_ReturnsChild_WhenChildFound()
{
var child = new Mock<IUIElement>();
child.Setup(c => c.Id).Returns("child1");

var element = new TestUIElement("parent", new List<IUIElement> { child.Object });
Assert.Equal(child.Object, element.GetChildElementById("child1"));
}

[Fact]
public void GetChildElementById_ReturnsNestedChild_WhenNestedChildFound()
{
var nestedChild = new Mock<IUIElement>();
nestedChild.Setup(c => c.Id).Returns("nestedChild");

var child = new Mock<IUIElementWithChildren>();
child.Setup(c => c.Id).Returns("child1");
child.Setup(c => c.GetChildElementById("nestedChild")).Returns(nestedChild.Object);

var element = new TestUIElement("parent", new List<IUIElement> { child.Object });
Assert.Equal(nestedChild.Object, element.GetChildElementById("nestedChild"));
}

[Fact]
public void GetChildElementById_ReturnsFirstMatchingChild_WhenMultipleChildrenWithSameId()
{
var child1 = new Mock<IUIElement>();
child1.Setup(c => c.Id).Returns("child");

var child2 = new Mock<IUIElement>();
child2.Setup(c => c.Id).Returns("child");

var element = new TestUIElement(
"parent",
new List<IUIElement> { child1.Object, child2.Object }
);
Assert.Equal(child1.Object, element.GetChildElementById("child"));
}

[Fact]
public void GetChildElementById_ReturnsChild_WhenChildIsCard()
{
var child = new Mock<IUIElement>();
child.Setup(c => c.Id).Returns("child-id");

var card = new UICard("card-id", child.Object);

var element = new TestUIElement("parent", new List<IUIElement> { card });

Assert.Equal(card, element.GetChildElementById("card-id"));
Assert.Equal(child.Object, element.GetChildElementById("child-id"));
}
}

0 comments on commit 003e38b

Please sign in to comment.