Test for click event using Jasmine test suite

16,041

What I generally tend to do is create a stub and assign the event to the stub. Then trigger the click event and check if it was called

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();
        });
    });
});
Share:
16,041
Ancient
Author by

Ancient

Anything i loves or hate totally depends on my mood , working as a software engineer

Updated on June 26, 2022

Comments

  • Ancient
    Ancient almost 2 years

    I am using jasmine to test my application and right now no button exists in my code
    but i want to write a test in which i can check that whether a click event is fired or not.
    You can simply think that i want to fire click event without 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 .