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

1.4k Views Asked by At

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.

1

There are 1 best solutions below

1
On BEST ANSWER

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()