In bUnit I want to make a call like this:
var textbox = renderedComponent.FindComponent<DxTextBox>("#myid");
However, FindComponent<T>()
does not have a method that accepts any filtering. Is there any way to do this other than call IRenderedComponent.FindComponents<DxTextBox>()
and then walk the list?
In short, no, there is no other way (at the moment). You could write your own extension method to achieve that (just a rough outline, I did not test that code):
public static class Extensions
{
public static T FindComponentWithSelector<T>(this IRenderedFragment renderedFragment, string selector) where T : IComponent
{
var elements = renderedFragment.FindAll(selector);
foreach (var element in elements)
{
var component = renderedFragment.FindComponent<T>(element);
if (component != null)
{
return component.Instance;
}
}
throw new InvalidOperationException($"Component of type {typeof(T).FullName} with selector '{selector}' not found.");
}
private static IRenderedComponent<TComponent>? FindComponent<TComponent>(this IRenderedFragment renderedFragment, IElement element)
where TComponent : IComponent
{
return renderedFragment.FindComponents<TComponent>()
.FirstOrDefault(component => component.Markup.Contains(element.OuterHtml, StringComparison.OrdinalIgnoreCase));
}
}
We have this on our radar to some extent: https://github.com/bUnit-dev/bUnit/issues/153
This would allow you to retrieve the IRenderedComponent
from an IElement
.