V
V
VovaSakay2021-06-03 13:07:18
Unit testing
VovaSakay, 2021-06-03 13:07:18

How to simulate a click on an element in Jest (not react)?

My code

describe('Events', () => {
  afterEach(() => {
    anchor.innerHTML = '';
  });

  test('click element', () => {
    const view = new View(intervalConfig, anchor);

    const spy = jest.spyOn(view, 'notify');

    const element = anchor.querySelector('.slider__handle') as HTMLElement;
    element.dispatchEvent(new Event('click', { bubbles: true }));

    expect(spy).toHaveBeenCalled();
    spy.mockRestore();
  });
});


In console: ReferenceError: Event is not defined

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Lewandowski, 2021-06-03
@vovaspace

https://testing-library.com/docs/dom-testing-libra...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question