Salesforce, Python, SQL, & other ways to put your data where you need it

Need event music? 🎸

Live and recorded jazz, pop, and meditative music for your virtual conference / Zoom wedding / yoga class / private party with quality sound and a smooth technical experience

💬 EN

#


How do most developers deal with JSDom not being able to successfully recognize that an Event object’s “defaultPrevented” property should be “true” after that event was dispatched onto an EventTarget, whereupon an EventListener ran “preventDefault()”? I need to be able to write a build-time test that confirms that I didn’t make a mistake around my implementation of “preventDefault().” I know I didn’t, because it works great in the browser, but I want a build-time test for regression testing peace of mind

To address the issue of JSDom not correctly recognizing the defaultPrevented property as true after calling preventDefault(), developers typically employ alternative testing strategies. Here are some approaches to create a build-time test for your preventDefault() implementation:

  1. Mock the preventDefault() method: Instead of relying on JSDom’s implementation, you can mock the preventDefault() method and verify that it’s called when expected12. ```javascript const mockPreventDefault = jest.fn(); const event = { preventDefault: mockPreventDefault };

// Your event handler function handleEvent(e) { e.preventDefault(); }

// Test handleEvent(event); expect(mockPreventDefault).toHaveBeenCalled();


2. Use a custom event implementation:
Create a custom event object that correctly sets the `defaultPrevented` property when `preventDefault()` is called[^1_2].
```javascript
class CustomEvent {
  constructor() {
    this.defaultPrevented = false;
  }
  preventDefault() {
    this.defaultPrevented = true;
  }
}

// Your event handler
function handleEvent(e) {
  e.preventDefault();
}

// Test
const event = new CustomEvent();
handleEvent(event);
expect(event.defaultPrevented).toBe(true);
  1. Test the side effects: Instead of directly checking defaultPrevented, test the expected behavior or side effects of calling preventDefault()3. ```javascript // Your component or function that uses preventDefault function handleLinkClick(e) { e.preventDefault(); // Perform some action instead of navigation performAction(); }

// Test test(‘handleLinkClick prevents default and performs action’, () => { const mockPerformAction = jest.fn(); const mockPreventDefault = jest.fn(); const event = { preventDefault: mockPreventDefault };

handleLinkClick(event);

expect(mockPreventDefault).toHaveBeenCalled(); expect(mockPerformAction).toHaveBeenCalled(); });


4. Use a headless browser:
For more accurate browser behavior testing, consider using a headless browser like Puppeteer or Playwright in your build process[^1_5].
```javascript
const puppeteer = require('puppeteer');

test('preventDefault works correctly', async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  
  await page.setContent(`
    <a href="#" id="testLink">Test Link</a>
    <script>
      document.getElementById('testLink').addEventListener('click', (e) => {
        e.preventDefault();
      });
    </script>
  `);

  const defaultPrevented = await page.evaluate(() => {
    const link = document.getElementById('testLink');
    const event = new MouseEvent('click', { bubbles: true, cancelable: true });
    link.dispatchEvent(event);
    return event.defaultPrevented;
  });

  expect(defaultPrevented).toBe(true);

  await browser.close();
});

By implementing one or more of these strategies, you can create reliable build-time tests for your preventDefault() implementation, ensuring proper functionality and providing regression testing peace of mind.


  1. https://developer.mozilla.org/en-US/docs/Web/API/Event/defaultPrevented 

  2. https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault 

  3. https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener