Setting the Value of the drop-down list through bUnit

Does any have a sample for setting the Value of a SfDropDropList?

We are binding using @bind-Value

We have tried many ways but the object/property that we've bound to never changes.

Thanks in advance


1 Reply

KP Kokila Poovendran Syncfusion Team August 7, 2023 03:53 PM UTC

Hi Nick Shaw,


Thank you for reaching out to us with your query regarding setting the value of a SfDropDownList in BUnit.


We have thoroughly reviewed your request, and we are glad to provide you with a code snippet that demonstrates how to use the @bind-Value property to change the value of the SfDropDownList. The code snippet below illustrates the implementation:



    public async Task TestMethod()

    {

 

        using var ctx = new Bunit.TestContext();

ctx.Services.AddSyncfusionBlazor();

 

        // Render your component

 

        IRenderedComponent<Component1> cut = ctx.RenderComponent<Component1>();

 

        // Find the SfDropDownList component

 

        var DropDownInput = cut.FindComponent<SfDropDownList<string, Country>>();

 

 

DropDownInput.SetParametersAndRender(param => param

 

.AddChildContent<DropDownListEvents<string, Country>>(events => events

 

            .Add(e => e.ValueChange, (ChangeEventArgs<string, Country> args) =>

 

            {

 

cut.Instance.DropVal = args.Value;

 

            })

 

            ));

 

 

        DropDownInput.SetParametersAndRender(("Value", "Cameroon"));

 

 

        // Assert the value of the SfDropDownList

 

        Assert.AreEqual(DropDownInput.Instance.Value, "Cameroon");

        Assert.AreEqual(DropDownInput.Instance.Value, cut.Instance.DropVal);

 

 

[Component1.Razor]

 

 

<SfDropDownList TValue="string"Placeholder="e.g. Australia"TItem="Country" Width="300px" @bind-Value="@DropVal" DataSource="@Countries">

    <DropDownListFieldSettings Value="Name"></DropDownListFieldSettings>

</SfDropDownList>

 

@code {

 

    public string DropVal = "Canada";

 

    public class Country

    {

        public string Name { get; set; }

 

        public string Code { get; set; }

    }

 

    List<Country> Countries = new List<Country>

   {

        new Country() { Name = "Australia", Code = "AU" },

        new Country() { Name = "Bermuda", Code = "BM" },

        new Country() { Name = "Canada", Code = "CA" },

        new Country() { Name = "Cameroon", Code = "CM" },

    };

}



Loader.
Up arrow icon