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

raise onclick for div element #160

Closed
aschaefer94 opened this issue Jun 29, 2020 · 13 comments
Closed

raise onclick for div element #160

aschaefer94 opened this issue Jun 29, 2020 · 13 comments
Labels
question Further information is requested

Comments

@aschaefer94
Copy link

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.

@aschaefer94 aschaefer94 added the question Further information is requested label Jun 29, 2020
@egil
Copy link
Member

egil commented Jun 29, 2020

For some reason there is no event handler attached to the element when you get that error.

To debug this, look at the cut.Markup property and look for blazor:onclick attributes. That is how the event handlers are identified when attached to elements.

@aschaefer94
Copy link
Author

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.

@egil
Copy link
Member

egil commented Jun 29, 2020

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.

@egil
Copy link
Member

egil commented Jun 29, 2020

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?

@aschaefer94
Copy link
Author

aschaefer94 commented Jun 29, 2020

Here's the html of the component under test.

<div class="row justify-content-center">
	<div id="openGroupDeleteModal" class="mb-3 col-lg-6 col-sm-10 row justify-content-end" @onclick="GetDeleteGroupModalMessage">
		<div class="oi oi-trash pt-3 mt-1 mr-2" style="color:white"></div>
	</div>
</div>

In my test I am calling:
cut.Find("#openGroupDeleteModal").Click()

In debugging, I can see that it is getting the element I want, but isn't binding the click to it.

@egil
Copy link
Member

egil commented Jun 29, 2020

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 @onclick handler. I assume your example looks like this?

@aschaefer94
Copy link
Author

aschaefer94 commented Jun 29, 2020

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.

@egil
Copy link
Member

egil commented Jun 29, 2020

@aschaefer94 can you post a minimal example that has this problem, if you cannot share your full component?

@aschaefer94
Copy link
Author

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.

@egil
Copy link
Member

egil commented Jun 29, 2020

Sounds right. If the @if(){} block is toggled during the test using async code, you might have to look at cut.WaitForState (https://bunit.egilhansen.com/docs/interaction/awaiting-async-state.html) to get your test stable.

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.

@aschaefer94
Copy link
Author

Sure.

--Component Under Test (GroupDelete.razor)--
@if(Show)
{
< h1>Show this message< /h1>
}
< div class="row justify-content-end">
< div class="col-lg-6 row justify-content-end" @OnClick="GetDeleteGroupModalMessage">
< div class="icon">< /div>
< /div>
< /div>

@code{
[Parameter]
public EventCallback GetDeleteGroupModalMessage { get; set; }

-- Working Test--
var cut = RenderComponent(EventCallback("GetDeleteGroupModalMessage", 0 => {}));

cut.Find("#openGroupDeleteModal").Click();

cut.Render();

cut.MarkupMatches(expectedMarkup);

--Broken Test--
var cut = RenderComponent();

cut.Find("#openGroupDeleteModal").Click();

cut.Render();

cut.MarkupMatches(expectedMarkup);

@egil
Copy link
Member

egil commented Jun 29, 2020

Yeah, no, Blazor wont render that onclick handler to the DOM when there is nothing to call. Btw. that additional call to cut.Render() should not be necessary in this case. This runs fine on my machine:

<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

@aschaefer94
Copy link
Author

Thanks Egil, I appreciate how quick you are to respond to these threads!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants