Test for click event using the Jasmine test suite

I am using jasmine to test my application, and right now there is no button in my code

but I want to write a test in which I can check if a click event is fired or not.
You might just think that I want to fire a click event without a button.

Here is what i did

  scenario('checking that click event is triggered or not', function () { given('Sigin form is filled', function () { }); when('signin button is clicked ', function () { spyOn($, "click"); $.click(); }); then('Should click event is fired or not" ', function () { expect($.click).toHaveBeenCalled(); }); }); 

Thanks in advance.

+4
source share
1 answer

What I usually do is create a stub and assign an event to the stub. Then fire the click event and check if it was triggered

 describe('view interactions', function () { beforeEach(function () { this.clickEventStub = sinon.stub(this, 'clickEvent'); }); afterEach(function () { this.clickEvent.restore(); }); describe('when item is clicked', function () { it('event is fired', function () { this.elem.trigger('click'); expect(this.clickEventStub).toHaveBeenCalled(); }); }); }); 
+5
source

Source: https://habr.com/ru/post/1484128/


All Articles