Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button click throws Bunit.ElementNotFoundException #251

Closed
nickmuller opened this issue Nov 5, 2020 · 3 comments
Closed

Button click throws Bunit.ElementNotFoundException #251

nickmuller opened this issue Nov 5, 2020 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@nickmuller
Copy link

Describe the bug or question
Triggering a click event on a button results in a 'Bunit.ElementNotFoundException' exception. I have a form component with an edit button, after a click on the button it then is removed from the DOM. The form then shows a cancel and submit button. I can see that the click is being handled, so I think it's being referenced after being removed from DOM.

I've made a small project to reproduce the exception: XUnitTestProject1.zip

Example:
Testing this component:

<form>
    @if (State.IsReadonly)
    {
        <button class="editbutton" @onclick="EditHandler">Edit</button>
    }
    <div>
        @ChildContent
    </div>
    @if (!State.IsReadonly)
    {
        <button class="cancelbutton">Cancel</button>
        <button class="submitbutton">Submit</button>
    }
</form>

@code {
    [CascadingParameter] public FormState State { get; set; }
    [Parameter] public RenderFragment ChildContent { get; set; }

    private void EditHandler()
    {
        State.Edit();
    }
}

With this test:

public class UnitTest1 : TestContext
{
    [Fact]
    public void Test1()
    {
        var state = new FormState();
        var cut = RenderComponent<Form>(parameters => parameters
            .AddCascadingValue(state));

        cut.Find("button.editbutton").Click();

        Assert.False(cut.Instance.State.IsReadonly);
    }
}

Results in this output:

Bunit.ElementNotFoundException
Exception of type 'Bunit.ElementNotFoundException' was thrown.
   at Bunit.ElementFactory`1.AngleSharpWrappers.IElementFactory<TElement>.GetElement() in /_/src/bunit.web/Extensions/Internal/ElementFactory.cs:line 30
   at AngleSharpWrappers.Wrapper`1.get_WrappedElement()
   at AngleSharpWrappers.ElementWrapper.HasAttribute(String name)
   at Bunit.GeneralEventDispatchExtensions.TriggerEventAsync(IElement element, String eventName, EventArgs eventArgs) in /_/src/bunit.web/EventDispatchExtensions/GeneralEventDispatchExtensions.cs:line 49
   at Bunit.MouseEventDispatchExtensions.ClickAsync(IElement element, MouseEventArgs eventArgs) in /_/src/bunit.web/EventDispatchExtensions/MouseEventDispatchExtensions.cs:line 304
   at Bunit.MouseEventDispatchExtensions.Click(IElement element, Int64 detail, Double screenX, Double screenY, Double clientX, Double clientY, Int64 button, Int64 buttons, Boolean ctrlKey, Boolean shiftKey, Boolean altKey, Boolean metaKey, String type) in /_/src/bunit.web/EventDispatchExtensions/MouseEventDispatchExtensions.cs:line 286
   at XUnitTestProject1.UnitTest1.Test1

Expected behavior:
Click of the button should work without an error. As a workaround I now use cut.FindAll("button.editbutton")[0].Click();

Version info:

  • Library version: v1.0.0-beta-11
@egil egil added the bug Something isn't working label Nov 5, 2020
@egil egil self-assigned this Nov 5, 2020
@egil egil added the investigate This issue require further investigation before closing. label Nov 5, 2020
@egil
Copy link
Member

egil commented Nov 5, 2020

Ohh my.. two in one day. Thanks for reporting.

@egil
Copy link
Member

egil commented Nov 5, 2020

I figured out the root cause and there is a fix on the way to the nightly channel now. I really appreciate the effort you put in to creating small repro samples. It makes my bug finding much easier. Thanks again!

@egil egil closed this as completed Nov 5, 2020
@egil egil removed the investigate This issue require further investigation before closing. label Nov 5, 2020
@nickmuller
Copy link
Author

Sure no problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants