Explore Network Navigate Register Login
  • Sign Up
  • Sign In
  • Home
  • ──
  • View
1

How to call onclick events written on div through jest (salesforce)?

  • Ayaan
  • Jul 04, 2025
  • 0 answers
  • 2 times viewed

I have created an LWC in Salesforce and am trying to creat Jest tests for it. I have implemented a div in html file and calling the js function using the onclick event as shown below.

<div class="csa-utility-svg" onclick={handleShowMoreClick} data-id="showMoreButton">
  <label class="card-text-cursor-pointer">{labels.showMore_button} </label>
</div>
How do I test onclick={handleShowMoreClick} through jest?
 
With both approaches I've tried I get TypeError: Cannot read properties of null (button value is coming as null).
 
approach 1:
element.shadowRoot.querySelector('div[data-id="showMoreButton"]').dispatchEvent(new CustomEvent('click'));

approach 2:

const showMoreButton = element.shadowRoot.querySelector('div\[data-id="showMoreButton"\]');
expect(showMoreBtn.length).toBeGreaterThan(0);   // real existence check
showMoreBtn\[0\].click();
    await flushPromises();
});

Add a comment

Tags javascript python salesforce lightning salesforce.com
Share

0 Answer

Write your Answer

Categories

Education Websites, IT & Software Accounting & Human Resources Health & Medicine

Recent Posts

Update built-in NER model of Spacy instead of overwrite

Jan 04, 2025

Sign In

Welcome back!

To stay connected with the education world, login with your personal details.

Forgot Password?
Trouble logging in?

Enter your email, phone, or username and we'll send you a link to get back into your account..

Back to Login
New User? Register

Sign Up

Learner Master Boffin
Use 8+ characters with at least one number.
Use 8+ characters with at least one number.
Use 8+ characters with at least one number.
Already User? Login

SUPPORT

Privacy & Policy Refund Policy Terms & Conditions Coins & Pricing

For Users

How It Works - Boffin How It Works - Master How It Works - Learner

ABOUT

About Us FAQs
© 2025 Cyfinex