-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix GetChildElementById no access to elements inside Card (#1429)
* Add basic tests for GetChildElementById * Fix no access to elements inside Card. Fixes #1406
- Loading branch information
Showing
2 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/app/tests/DevToys.UnitTests/Tool/GUI/Components/UIElementWithChildrenTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |