I have a Blazor grid for a collection of key-value pairs where the edit template for one cell has a ComboBox with a list of the possible keys. The requirement is that each key can only be used once. My grid's OnEdit handler is:
private void OnEdit(GridCommandEventArgs e)
{
MyModel model = (MyModel)e.Item;
if (e.Field == nameof(MyModel.KeyTypeId))
{
IEnumerable<byte> typeIdsInUse = Data.Select(x => x.KeyTypeId).Except([model.KeyTypeId]).ToArray();
KeyTypesForDropDown = KeyTypes!.Where(x => !typeIdsInUse.Contains(x.KeyTypeId)).ToList();
if (KeyTypesForDropDown.Count == 0)
{
Notification.Show("There are no more keys available to add.", ThemeConstants.Notification.ThemeColor.Warning);
e.IsCancelled = true;
}
}
}
My problem is that I can't figure out how to find this ComboBox in the grid cell during my test in order to validate that its data is correct. This is how far I got:
GridCommandEventArgs args = new()
{
Item = new MyModel(),
};
await grid.InvokeAsync(() => grid.Instance.OnAdd.InvokeAsync(args));
await grid.InvokeAsync(() => grid.Instance.OnCreate.InvokeAsync(args));
var state = grid.Instance.GetState();
state.OriginalEditItem = state.EditItem = (MyModel)args.Item;
await grid.Instance.SetStateAsync(state);
grid.Render();
var rows = grid.FindComponent<GridRowCollection<MyModel>>();
rows.Render();
state = grid.Instance.GetState();
state.EditField = nameof(MyModel.KeyTypeId);
await grid.Instance.SetStateAsync(state);
grid.Render();
rows.Render();
var row = rows.FindComponent<GridRow<MyModel>>();
Assert.True(row.HasComponent<GridEditCell<MyModel>>());