检查元素是否处于焦点状态 – Blazor B-Unit

huangapple go评论100阅读模式
英文:

Check if Element is focused - Blazor B-Unit

问题

[Fact]
public async Task ClickComponent_FocusInputField()
{
    string testIdentifier = "MyTestIdentifier";
    List<string> testValue = new() { "MyTestValue1", "MyTestValue3", "MyTestValue3" };

    using var ctx = new TestContext();

    var cut = ctx.RenderComponent<DataRowTagSelectComponent<string>>(parameters => parameters
        .Add(p => p.Values, testValue)
        .Add(p => p.DataRowIdentifier, testIdentifier)
        .Add(p => p.MainDataEditMode, true)
    );

    IElement mainContainerTarget = cut.Find(".main-data-data-row");

    // Mocking click of container
    mainContainerTarget.Click();

    // Important stuff is happening here - how do I access the currently active item?
    // Or how do I check if an element is focused?
    
    // Use the `await` keyword to ensure FocusAsync() completes before proceeding.
    await Task.Delay(100); // Add a small delay to let FocusAsync complete (adjust as needed).

    // Check if the input element is focused.
    IElement inputTarget = cut.Find(".main-data-data-row__chip-container__input");
    Assert.NotNull(inputTarget);
    Assert.True(inputTarget.IsFocused);
}

Note: In the updated code, I added await Task.Delay(100); to ensure that the asynchronous FocusAsync() method has a chance to complete before you check if the input element is focused. You may need to adjust the delay duration as needed based on the actual behavior of FocusAsync().

英文:

I am writing unittests for my component. This component looks like this:

component.razor
&lt;div class=&quot;main-data-data-row&quot; @onclick=&quot;@HandleDataRowClick&quot;&gt;
   ...
   &lt;input class=&quot;main-data-data-row__chip-container__input&quot; ref=&quot;InputRef&quot;/&gt;
&lt;/div&gt;
component.razor.cs
public partial class DataRowTagSelectComponent&lt;T&gt; : DataRowComponentBase
{
   ...

   public ElementReference InputRef { get; set; }
   
   ...

   public void HandleDataRowClick()
   {
      if (!Disabled)
      {
         InputRef.FocusAsync();
      }
   }
}

Now to the question

As you can see, on click of the container, the input field is focused. How would one test this in a B-Unit test?

This is how my unit test looks so far:

[Fact]
public void ClickComponent_FocusInputField()
{
   string testIdentifier = &quot;MyTestIdentifier&quot;;
   List&lt;string&gt; testValue = new() { &quot;MyTestValue1&quot;, &quot;MyTestValue3&quot;, &quot;MyTestValue3&quot;, };

   using var ctx = new TestContext();

   var cut = ctx.RenderComponent&lt;DataRowTagSelectComponent&lt;string&gt;&gt;(parameters =&gt; parameters
         .Add(p =&gt; p.Values, testValue)
         .Add(p =&gt; p.DataRowIdentifier, testIdentifier)
         .Add(p =&gt; p.MainDataEditMode, true)
         );

   IElement mainContainerTarget = cut.Find(&quot;.main-data-data-row&quot;);

   //mocking click of container
   mainContainerTarget.Click();

   // important stuff is happening here - how do i access the currently active item?
   // or how do i check if a element is focused?
   IElement inputTarget = cut.Find(&quot;.main-data-data-row__chip-container__input&quot;);
   Assert.NotNull(inputTarget);
        
   Assert.Equal(inputTarget, ???)
}

答案1

得分: 1

IElement有一个IsFocused属性,您可以按如下方式使用:

IElement inputTarget = cut.Find(".main-data-data-row__chip-container__input");
Assert.True(inputTarget.IsFocused);
英文:

IElement has an IsFocused property that you can use as follows:

IElement inputTarget = cut.Find(&quot;.main-data-data-row__chip-container__input&quot;);
Assert.True(inputTarget.IsFocused);

huangapple
  • 本文由 发表于 2023年8月10日 16:41:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定