How to test mousedown and keydown elements when there is no onchange handler for specific element #450
-
Hey, I try to test Radzen Dropdown with AllowFilter, but I am not able to use
As a simple workaround I would just try to change the bind value for the element instead of choosing it with code but it seems to be only a workaround. When I look at the markup I can just find an empty onclick: <p> <div onclick="" class="rz-dropdown valid rz-clear" blazor:onmousedown="4" style="width:300px" tabindex="0" blazor:onkeydown="5" id="RootCategorySelect" blazor:elementReference=""> So it has an empty onclick for it. How can I make it work with using onmousedown and onkeydown? I was not able to find reasonable examples with bunit and these methods. Or would it make sense to put something into onclick instead and make work somehow? My test code: [Test]
public void EditSkill_ItCreatesRootCategoryChildHardkill()
{
using var ctx = new Bunit.TestContext();
ctx.Services.AddSingleton(_skillService);
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
var cut = ctx.RenderComponent<Team01.Pages.Skill.EditSkill>();
cut.Find("#TypeInput").Change("Hardskill");
cut.Find("#NameInput").Change("NewRootChild");
//cut.Find("#RootCategorySelect").Change("RootCategorySkill");
//cut.Instance.chosenRootCategory =
cut.Find("form").Submit();
Assert.AreEqual(new Skill() {Id = 5, Type = SkillType.Hardskill, Name = "NewRootChild", ParentSkill = SkillData.SkillsWithCategories(2)[0], ParentId = 1},
DataHelper.QueryData<Skill>("Skill", 5, _config));
} TypeInput and NameInput belong to and In the HandleValidSubmit I just set chosenRootCagory for the currentSkill. in my component I have: [...]
<RadzenDropDown Id="RootCategorySelect" AllowClear="true" TValue="Team01.Models.Skill" Style="width:300px"
Data=@_rootCategorySkillList TextProperty="Name"
@bind-Value=@chosenRootCategory
Change=@(args => OnRootCategorySelected(args))/>
[...]
private async void OnRootCategorySelected(object selectedSkill)
{
//has to be set to null every time so second DropDown is reloaded
chosenRootChildCategory = null;
//get all child skills from root category
if (selectedSkill != null)
{
_childCategorySkillList = await SkillServiceAsync.GetAllChildSkillsFormParent(chosenRootCategory.Id);
//if first root category is already a skillList without further categories (i.e. Datenbanken)
if (_allSkillList.Any(skill => skill.Id == _childCategorySkillList[0].Id))
_childCategorySkillList = null;
StateHasChanged();
}
} Please let me know if you need further code and thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
Hi @FelixOutta,
Are you the developer of Radzen Dropdown or are you using it in a component of your own? If its the latter, then you might just want to use the (current in preview) feature in bUnit that allows you to replace a component with a dummy, to not have it be part of a test. E.g.: // arrange
using var ctx = new Bunit.TestContext();
ctx.ComponentFactories.UseDummyFor<RadzenDropDown>();
var cut = ctx.RenderComponent<Team01.Pages.Skill.EditSkill>();
var selectedSkill = ...;
// Act
var dummyDropdown = cut.FindComponent<Dummy<RadzenDropDown>>(); // This finds the dummy that replaces
var changeMethod = dummyDropdown.Instance.GetParameter(p => p.Change); // Finds the method you have passed as parameter to the RadzenDropDown Change parameter, i.e. the OnRootCategorySelected method
changeMethod.InvokeAsync(selectedSkill); // invoke the method to emulate that users selected a value through the RadzenDropDown in the browser.
// Assert
... The example code above requires the preview version of bUnit available on nuget. Also, I wrote the code from memory, so it might not compile, but it should give you an idea of how to proceed.
The empty onclick is not added by a blazor event handler binding. If it was, it would be prefixed with
All Hope this helps. |
Beta Was this translation helpful? Give feedback.
-
using a fresh connection for the failing line solved error. My method was called two times within the initializing process of the page and tried to use the same connection. using IDbConnection dbConnection = new SqlConnection(DatabaseUtils.ConnectionString);
var results = (await dbConnection.QueryAsync<Skill>(sql, new { ids = Ids })).ToList(); |
Beta Was this translation helpful? Give feedback.
using a fresh connection for the failing line solved error. My method was called two times within the initializing process of the page and tried to use the same connection.