How to test an event triggered on document? (with jasmine-flight)

1,750 views
Skip to first unread message

Gabriel Pugliese

unread,
Oct 23, 2014, 8:15:33 AM10/23/14
to twitter...@googlegroups.com
I'm trying to test this example:

    var beforeAjaxEvent = 'dataBeforeShippingAjax';
    $(document).trigger(beforeAjaxEvent);
    expect(beforeAjaxEvent).toHaveBeenTriggeredOn(document);

But it raises an error that I can't understand:

        Expected event dataBeforeShippingAjax to have been triggered on <#document>,Expected event dataBeforeShippingAjax not to have been triggered on <#document>
        Error: Expected event dataBeforeShippingAjax to have been triggered on <#document>,Expected event dataBeforeShippingAjax not to have been triggered on <#document>
            at Object.<anonymous> (/home/gabriel/Workspace/ml-site/src/test/jasmine-flight/spec/ui/zip_code_form_spec.js:33:29)

 I've also tried to do a spyOnEvent without success. What's going on here?

Gabriel Pugliese
CodersTV.com
@coderstv

Tom Ashworth

unread,
Oct 23, 2014, 9:03:51 AM10/23/14
to Gabriel Pugliese, twitter...@googlegroups.com
Hi Gabriel,

You need to create a spy and pass it to expect(...):

    var beforeAjaxEvent = 'dataBeforeShippingAjax';
    var spy = spyOnEvent(beforeAjaxEvent);
    $(document).trigger(beforeAjaxEvent);
    expect(spy).toHaveBeenTriggeredOn(document);

Does that help?

Tom


--
Tom

Gabriel Pugliese

unread,
Oct 23, 2014, 9:19:37 AM10/23/14
to Tom Ashworth, twitter...@googlegroups.com
Nope. I've tried that before but it raises a similar error. It says the event is undefined:


        Expected event undefined to have been triggered on <#document>,Expected event undefined not to have been triggered on <#document>
        Error: Expected event undefined to have been triggered on <#document>,Expected event undefined not to have been triggered on <#document>
            at Object.<anonymous> (/home/gabriel/Workspace/ml-site/src/test/jasmine-flight/spec/ui/zip_code_form_spec.js:33:27)
Chrome 38.0.2125 (Linux): Executed 4 of 4 (1 FAILED) (0.163 secs / 0.155 secs)



Gabriel Pugliese
CodersTV.com
@coderstv

Tom Ashworth

unread,
Oct 23, 2014, 9:25:12 AM10/23/14
to Gabriel Pugliese, twitter...@googlegroups.com
Try: var spy = spyOnEvent(document, beforeAjaxEvent);
--
Tom

Tom Ashworth

unread,
Oct 23, 2014, 10:50:47 AM10/23/14
to Gabriel Pugliese, twitter...@googlegroups.com
In the first case, I think you again need to save the spy and call expect on it:
    
    var spy = spyOn(this.component, 'handleError');
    $(document).trigger('dataCalculateShippingError', { code: 2 });
    expect(spy).toHaveBeenCalled();

In the second, Jasmine will call the function you pass to expect which might not be what you want. If you want to check handleError throws in specific case, then you should probably cause that inside a new function passed to expect:

    expect(function () {
        this.component.handleError(someEvent, someBadData);
    }).toThrow()


On Thu, Oct 23, 2014 at 3:02 PM, Gabriel Pugliese <gabrielh...@gmail.com> wrote:
This question also enters on this case:

expect(this.component.handleError).toThrow();



Gabriel Pugliese
CodersTV.com
@coderstv

On Thu, Oct 23, 2014 at 11:54 AM, Gabriel Pugliese <gabrielh...@gmail.com> wrote:
Let me make another question:

Is it possible to spyOn methods of the component that are bound with this.on ? For example:

On component:
    this.on(document, 'dataCalculateShippingError', this.handleError);

Test:

    spyOn(this.component, 'handleError');
    $(document).trigger('dataCalculateShippingError', { code: 2 });
    expect(this.component.handleError).toHaveBeenCalled();




Gabriel Pugliese
CodersTV.com
@coderstv

On Thu, Oct 23, 2014 at 11:33 AM, Gabriel Pugliese <gabrielh...@gmail.com> wrote:
Oh, that worked!
Thank you very much



Gabriel Pugliese
CodersTV.com
@coderstv





--
Tom

Cameron Stitt

unread,
Oct 24, 2014, 4:36:30 AM10/24/14
to twitter...@googlegroups.com, gabrielh...@gmail.com
This is correct. Note that for flight events, and according to the jasmine-flight docs, you have to follow the pattern:

var eventSpy = spyOnEvent(this.component, 'eventName');
$(document).trigger('eventName');
expect(eventSpy).toHaveBeenTriggeredOn(this.component);
Reply all
Reply to author
Forward
0 new messages