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

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

Check if Element is focused - Blazor B-Unit

问题

  1. [Fact]
  2. public async Task ClickComponent_FocusInputField()
  3. {
  4. string testIdentifier = "MyTestIdentifier";
  5. List<string> testValue = new() { "MyTestValue1", "MyTestValue3", "MyTestValue3" };
  6. using var ctx = new TestContext();
  7. var cut = ctx.RenderComponent<DataRowTagSelectComponent<string>>(parameters => parameters
  8. .Add(p => p.Values, testValue)
  9. .Add(p => p.DataRowIdentifier, testIdentifier)
  10. .Add(p => p.MainDataEditMode, true)
  11. );
  12. IElement mainContainerTarget = cut.Find(".main-data-data-row");
  13. // Mocking click of container
  14. mainContainerTarget.Click();
  15. // Important stuff is happening here - how do I access the currently active item?
  16. // Or how do I check if an element is focused?
  17. // Use the `await` keyword to ensure FocusAsync() completes before proceeding.
  18. await Task.Delay(100); // Add a small delay to let FocusAsync complete (adjust as needed).
  19. // Check if the input element is focused.
  20. IElement inputTarget = cut.Find(".main-data-data-row__chip-container__input");
  21. Assert.NotNull(inputTarget);
  22. Assert.True(inputTarget.IsFocused);
  23. }

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
  1. &lt;div class=&quot;main-data-data-row&quot; @onclick=&quot;@HandleDataRowClick&quot;&gt;
  2. ...
  3. &lt;input class=&quot;main-data-data-row__chip-container__input&quot; ref=&quot;InputRef&quot;/&gt;
  4. &lt;/div&gt;
component.razor.cs
  1. public partial class DataRowTagSelectComponent&lt;T&gt; : DataRowComponentBase
  2. {
  3. ...
  4. public ElementReference InputRef { get; set; }
  5. ...
  6. public void HandleDataRowClick()
  7. {
  8. if (!Disabled)
  9. {
  10. InputRef.FocusAsync();
  11. }
  12. }
  13. }

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:

  1. [Fact]
  2. public void ClickComponent_FocusInputField()
  3. {
  4. string testIdentifier = &quot;MyTestIdentifier&quot;;
  5. List&lt;string&gt; testValue = new() { &quot;MyTestValue1&quot;, &quot;MyTestValue3&quot;, &quot;MyTestValue3&quot;, };
  6. using var ctx = new TestContext();
  7. var cut = ctx.RenderComponent&lt;DataRowTagSelectComponent&lt;string&gt;&gt;(parameters =&gt; parameters
  8. .Add(p =&gt; p.Values, testValue)
  9. .Add(p =&gt; p.DataRowIdentifier, testIdentifier)
  10. .Add(p =&gt; p.MainDataEditMode, true)
  11. );
  12. IElement mainContainerTarget = cut.Find(&quot;.main-data-data-row&quot;);
  13. //mocking click of container
  14. mainContainerTarget.Click();
  15. // important stuff is happening here - how do i access the currently active item?
  16. // or how do i check if a element is focused?
  17. IElement inputTarget = cut.Find(&quot;.main-data-data-row__chip-container__input&quot;);
  18. Assert.NotNull(inputTarget);
  19. Assert.Equal(inputTarget, ???)
  20. }

答案1

得分: 1

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

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

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

  1. IElement inputTarget = cut.Find(&quot;.main-data-data-row__chip-container__input&quot;);
  2. 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:

确定