Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

490
Views
JEST how do you test event.preventDefault and event listeners in vanilla JS?

I'm rather new to JS but attentive to documentation and not lazy to read through all the stuff, so I've read Jest documentation and watched quite a few tutorials. I understand them, but then I absolutely don't understand how to approach this:

document.getElementsByTagName('form')[0].addEventListener('submit', function (event) {
    event.preventDefault();
    
    userName = document.getElementById('usrname').value
    sessionStorage.setItem('userName', document.getElementById('usrname').value);
    
    window.location.assign('game.html');
<form>
                <label for="usrname">Enter name:</label>
                <input type="text" id="usrname" name="username" maxlength="23" required>
                <input id="sbmt" type="submit" value="Play">
</form>

It is a simple game written for a school task and It was said to test it with jest to improve the project. We haven't studied any of it and no assistance is given cos it's extra and we're supposed to figure it out ourselves. I am really desperate. After so much time spent I would really like to understand. Could anyone at least guide me toward the answer? It seems like a lot of code with loads of mock functions. And, spoiler alert - idiotic existential question, if I mock everything how does it test my function and what's the point? At which point do I call it and how do I address it in this case, what do I export?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!