Skip to content
Prev Previous commit
Next Next commit
reworked test assertion and added a test to ensure items stay selecte…
…d even if the parent node is collapsed
  • Loading branch information
corvinsz committed May 14, 2025
commit 1c303b50b5274d65210ba44e436cec464acf5490
Original file line number Diff line number Diff line change
Expand Up @@ -1055,9 +1055,66 @@ public async Task TreeListView_SelectingMultipleItems_AddsThemToSelectedItems(Ty
// Assert inside of the remote executed method because we can't return an IList, since it's not serializable
static void AssertSelectedItems(TreeListView treeListView)
{
var selectedItems = treeListView.SelectedItems;
Assert.NotNull(selectedItems);
Assert.Equal(2, selectedItems.Count);
var dataContext = treeListView.DataContext;

if (dataContext is TreeListViewDataBinding viewModelA)
{
Assert.Equal(2, viewModelA.SelectedItems.Count);
}
if (dataContext is TreeListViewImplicitTemplate viewModelB)
{
Assert.Equal(2, viewModelB.SelectedItems.Count);
}
Comment on lines +1058 to +1065
Copy link
Member Author

@corvinsz corvinsz May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test tests 2 different UserControls with 2 different ViewModels. I somehow have to access the ViewModels. I know putting if-conditions in tests is not a good idea, but I opted to go this way.
Alternatively I could have created an interface to expose the SelectedItems property, but I think adding an interface just for the purpose of this test is a bit too much.
Other opinions/thoughts are welcome.

}
}

[Fact]
public async Task TreeListView_CollapsingParentOfSelectedChild_DoesNotRemoveChildFromSelectedItems()
{
await using var recorder = new TestRecorder(App);

IVisualElement<Grid> root = (await LoadUserControl(typeof(TreeListViewDataBinding))).As<Grid>();
IVisualElement<TreeListView> treeListView = await root.GetElement<TreeListView>();
IVisualElement<Button> addButton = await root.GetElement(ElementQuery.PropertyExpression<Button>(x => x.Content, "Add"));

IVisualElement<TreeListViewItem> item1 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[1]");
IVisualElement<TreeListViewItem> item2 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[2]");

// add three children to the second item
await AddChildren(item2, 3, addButton);

// Expand and select second item
await item2.LeftClickExpander();
await item2.LeftClick();
await Task.Delay(50);

// also select the second child
IVisualElement<TreeListViewItem> child2 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[4]");
await treeListView.SendKeyboardInput($"{ModifierKeys.Control}");
await child2.LeftClick();

// Release modifiers
await treeListView.SendKeyboardInput($"{ModifierKeys.None}");

// Assert that the parent and child are selected
await treeListView.RemoteExecute(AssertSelectedItems);

// Collapse the parent
await item2.LeftClickExpander(false);
await Task.Delay(50);

// Assert that the child is still selected, even though it was visually removed from the list
await treeListView.RemoteExecute(AssertSelectedItems);

recorder.Success();

// Assert inside of the remote executed method because we can't return an IList, since it's not serializable
static void AssertSelectedItems(TreeListView treeListView)
{
var viewModel = (TreeListViewDataBinding)treeListView.DataContext;

Assert.Equal("2", viewModel.SelectedItems[0].Value);
Assert.Equal("2_1", viewModel.SelectedItems[1].Value);
}
}

Expand Down