Test if event was not triggered on element with Jasmine spy
Asked Answered
B

1

6

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?

Basseterre answered 12/6, 2014 at 16:51 Comment(0)
B
2

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();
    });
Basseterre answered 13/6, 2014 at 8:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.