blazorbunit

How to wait for an element to be present in a bUnit test?


Let's say I have a test like the one below. Is there a way to wait for an element to be present in the markup of a rendered component?

[Fact]
public void Component_DoesSomething()
{
    // Arrange
    var ctx = new TestContext();
    var unitUnderTest = ctx.RenderComponent<SomeComponent>();

    var selector = "tbody";
            
    // Act
    var tableBody = unitUnderTest.Find(selector);

    // Assert
    Assert.Contains(tableBody.InnerHtml, "someStuff");
}

I've reviewed the docs below, but I don't see anything indicating how to wait for an element.


Solution

  • You can use IRenderedComponent.WaitForElement() method. See the code below:

    [Fact]
    public void Component_DoesSomething()
    {
        // Arrange
        var ctx = new TestContext();
        var unitUnderTest = ctx.RenderComponent<SomeComponent>();
    
        var selector = "tbody";
        unitUnderTest.WaitForElement(selector);
    
        // Act
        var tableBody = unitUnderTest.Find(selector);
    
        // Assert
        Assert.Contains(tableBody.InnerHtml, "someStuff");
    }
    

    See also: WaitForElements()