Selecting items in RadzenDropDown with BUNIT

Hi folks,

I have a question that I'm not sure you'll have the answer to, but I'm hoping someone here might be able to point me in a decent direction. I've changed my actual values here as it's company source.

I have a Radzen Dropdown with a few items in it.

   <div class="row mb-4">
                        <div class="col-md-4 align-items-center d-flex">
                            <RadzenLabel Text="Test Label:" id="test-type-label"/>
                        <div class="col">
                                TextProperty="TestTypeName "
                                style="width: 100%;"
                            <RadzenRequiredValidator Component="TestType" Style="position: absolute" id="test-type-validation"/>

Here's an example of a similar C# class it's bound to.

   public TestTypeModel(TestType testType)
        TestType = testType;
        TestTypeName = Enum.GetName(TestType);

    public TestType TestType { get; }

    public string TestTypeName { get; }

The page I'm using toggles the visibility of components beneath this dropdown when the value of the dropdown changes and re-renders the component.

The component definition looks like:

                        Visible="@(SelectedTestTypeModel.TestType == TestType.New)">

This works great through the UI.

I have a unit test that renders the page with BUNIT.

        using TestContext context = new TestContext();
        // does some common registrations for arrows/input file/popup
        // adds a mock client to the component
        IRenderedComponent<TestPage> testPage = context.RenderComponent<TestPage>();
       // I then can find the dropdown by the id tag I've applied to the control or by finding the li with the appropriate text. 

       // here's what it looks like rendered  
       <div class="row mb-4">
           <div class="col-md-4 align-items-center d-flex">    
               <label class="rz-label" id="test-type-label" blazor:elementReference="cfde00ae-7391-42f3-9ddb-bf88a108838c">Test Type:</label>
            <div class="col">   
                <div class="rz-dropdown valid" onmousedown="Radzen.activeElement = null" blazor:onclick="6" style="width: 100%;" tabindex="0" blazor:onkeydown="7" id="test-type-input" blazor:onfocus="8" blazor:elementReference="">
        <div class="rz-helper-hidden-accessible">
            <input style="width:100%" aria-haspopup="listbox" readonly type="text" tabindex="-1" name="TestType" value="NamespacedOmmited.TestTypeModel" aria-label="New" />

            <label class="rz-dropdown-label rz-inputtext ">

        <div class="rz-dropdown-trigger  rz-corner-right">
            <span class="rz-dropdown-trigger-icon  rzi rzi-chevron-down"></span>

        <div id="popup-test-type-input" class="rz-dropdown-panel    " style="display:none; box-sizing: border-box">
            <div class="rz-dropdown-items-wrapper" style="max-height: 200px;overflow-x: hidden">
                <ul class="rz-dropdown-items rz-dropdown-list    " role="listbox" blazor:elementReference="">
    <li role="option" class="rz-dropdown-item  rz-state-highlight" aria-label="&gt;New" blazor:onmousedown:preventDefault blazor:onmousedown="9" blazor:onclick:preventDefault blazor:onclick="10">
    <li role="option" class="rz-dropdown-item " aria-label="&gt;Update" blazor:onmousedown:preventDefault blazor:onmousedown="11" blazor:onclick:preventDefault blazor:onclick="12">
Update        </span>
     // test code: 
     IRefreshableElementCollection<IElement> refreshableElementCollection = inlayPage.FindAll("li");
    IElement element = refreshableElementCollection
            .First(x => x.Text().StripLeadingTrailingSpaces().StripLineBreaks() == "New");

    // no change to rendered css even if you manually re-render with 

I probably should of provided a sample app for this looking at it all now....

Anyways I looked through the existing support tickets especially this one... How to test Radzen Dropdown with bunit?

Which lead me to hunt through the existing unit tests for the components/rendering and I didn't find any for selecting items in the dropdowns. I found some that select buttons for the date time picker which uses a dropdown of sorts.

Does anyone have a recommendation on how to approach this problem? Is it possible to programatically select an item in the dropdown at the moment?

I mostly want to test to make sure that the proper component gets rendered without making the property that we use to determine visibility public if it at all possible.

Thanks for your help,

Hi @ctrent12,

Does it have to be through the UI? Can't use use the Value or SelectedItem property of RadzenDropDown? Otherwise you can try "clicking" an element with class class="rz-dropdown-item".

I'll try shortly and follow up. Really appreciate the help.

Sadly none of these worked for me.

The good news is I did end up figuring a way to set it.
Apparently the Dropdown Instance has a SelectItem method in it.
I've demonstrated it below for someone else who comes along looking for this answer. Note the test assertions are from NUNIT.

 using Bunit.TestContext testContext = new Bunit.TestContext();
        testContext.JSInterop.SetupVoid("Radzen.preventArrows", _ => true);
        testContext.JSInterop.SetupVoid("Radzen.togglePopup", _ => true);

        IRenderedComponent<RadzenPage> radzenPage = testContext.RenderComponent<RadzenPage>();
        IRenderedComponent<RadzenDropDown<TestModel>> dropdown = radzenPage.FindComponent<RadzenDropDown<TestModel>>();

        radzenPage.InvokeAsync(() =>
            IEnumerable<TestModel>? instanceData = dropdown.Instance.Data as IEnumerable<TestModel>;
            dropdown.Instance.SelectItem(instanceData.Last(x => x.TestType == TestType.Update));
            return Task.CompletedTask;

        IRenderedComponent<TestComponent> testComponent = radzenPage.FindComponent<TestComponent>();
        Assert.That(testComponent.Instance.Visible, Is.True);


1 Like