Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if event was not triggered on element with Jasmine spy

I am covering this part of system with tests at the moment, I have events chained in a way that stopping propagation on first must cancel second event from firing.

$.fn.extend({
        OnTheMoveBCTrigger: function (name, options) {
            if (!(this[0] instanceof OnTheMove_BusinessComponent)) {
                throw ('is not an instance of OnTheMove_BusinessComponent');
            } else {
                var event = $.Event(name);
                var promise = $(this).OnTheMoveTrigger(event, options);
                if (!event.isPropagationStopped()) {
                    $(onTheMove.PageDataRoles).OnTheMoveTrigger(name, options);
                }
            }
        }
    });

I figured how to check that second event was fired with jasmine-jquery spyOnEvent

    it("OnTheMoveBCTrigger chain fires event on onTheMove.PageDataRoles", function () {
        var spy = spyOnEvent(onTheMove.PageDataRoles, 'event2');
        $(window.bc).OnTheMoveBCTrigger("event2");
        expect('event2').toHaveBeenTriggeredOn(onTheMove.PageDataRoles);
        expect(spy).toHaveBeenTriggered();
    });

but now I need to test the case where propagation is being stopped and second event is not being fired.
How to check if event has not been fired?

like image 963
Matas Vaitkevicius Avatar asked Jun 12 '14 16:06

Matas Vaitkevicius


Video Answer


1 Answers

There is not property on Jasmine expect that allows to invert the check.
Code below worked for me.

    it("OnTheMoveBCTrigger does NOT chain fire event on onTheMove.PageDataRoles, if propogation is stopped on BC event.", function () {
        var spy = spyOnEvent(onTheMove.PageDataRoles, 'event3');
        $(window.bc).on("event3", function (e) {
            e.stopPropagation();
        });
        $(window.bc).OnTheMoveBCTrigger("event3");
        expect('event3').not.toHaveBeenTriggeredOn(onTheMove.PageDataRoles);
        expect(spy).not.toHaveBeenTriggered();
    });
like image 61
Matas Vaitkevicius Avatar answered Nov 06 '22 06:11

Matas Vaitkevicius