-
-
Notifications
You must be signed in to change notification settings - Fork 110
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
raise onclick for div element #160
Comments
For some reason there is no event handler attached to the element when you get that error. To debug this, look at the |
Okay. I'm not seeing any attributes with the blazor: prefix. Is there a specific way to make BUnit see the blazor attributes that I may be missing? The click is actually working when running the app, so I know my syntax is working in real functionality. |
It should just work, nothing special required. Can you share your component under test (or just the part that is causing the problem) and your test code. Then we should be able to figure it out. |
Btw. It could be related to #119. Are you sure the event is bound to the element you expect and not an element higher up the DOM tree? |
Here's the html of the component under test.
In my test I am calling: In debugging, I can see that it is getting the element I want, but isn't binding the click to it. |
Hmm that's weird. Is that the entire component? I'll have to try it locally later when the kids are in bed and get back to you. In the mean time, check out the example on the https://bunit.egilhansen.com/ front-page. That shows a basic use of triggering am |
Yes, my code looks just like the example, except that I am clicking on a < div> element instead of a < button> element. Sorry, I realized I missed the first part of your response there. No, it is not the entire component. There is markup wrapped in an @if(){} razor code block above what I pasted into the comment above. |
@aschaefer94 can you post a minimal example that has this problem, if you cannot share your full component? |
We just hit a breakthrough here. The click was calling an EventCallback, which we were not properly passing to our cut when it was instantiated. Apparently, if the target of the @OnClick is null, Blazor doesn't register the onclick event on the element. |
Sounds right. If the Ps. I would really appreciate a minimal example of this scenario, so I can see if this is something that can be made easier through bUnit. |
Sure. --Component Under Test (GroupDelete.razor)-- @code{ -- Working Test-- cut.Find("#openGroupDeleteModal").Click(); cut.Render(); cut.MarkupMatches(expectedMarkup); --Broken Test-- cut.Find("#openGroupDeleteModal").Click(); cut.Render(); cut.MarkupMatches(expectedMarkup); |
Yeah, no, Blazor wont render that onclick handler to the DOM when there is nothing to call. Btw. that additional call to <div>
<div id="elmId"
@onclick="ClickHandler">
CONTENT
</div>
</div>
@code
{
[Parameter]
public EventCallback ClickHandler { get; set; }
} var called = false;
var cut = RenderComponent<Modal>(parameters => parameters
.Add(p => p.ClickHandler, () => called = true)
);
cut.Find("#elmId").Click();
Assert.True(called); Note I am using the strongly type way of passing parameters to a component, covered on this page: https://bunit.egilhansen.com/docs/providing-input/passing-parameters-to-components.html |
Thanks Egil, I appreciate how quick you are to respond to these threads! |
I have a div with an @OnClick event that is calling an EventCallback. In testing, when I say cut.Find().Click() on the element, the test runner says "The element does not have an event handler for the event 'onclick', nor any other events." I have verified that I am correctly finding the element, so it is not that I am using the Find() method incorrectly.
Any ideas on how I can force the onclick event handler on a div?
Thank you.
The text was updated successfully, but these errors were encountered: