Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Problems with spying on custom event names and/or target document. #271

Open
jschill opened this issue Nov 18, 2015 · 4 comments
Open

Problems with spying on custom event names and/or target document. #271

jschill opened this issue Nov 18, 2015 · 4 comments

Comments

@jschill
Copy link

jschill commented Nov 18, 2015

Hello,

I'm trying to upgrade our tests to jasmine 2.x from 1.x. When upgrading jasmine-jquery to latest from 1.x i get trouble spying on custom event names and/or target document. Has this feature been removed?

Works:

    // Working test -- target: '#some_element', event name: 'click'
    it('working test 1', function () {
        var eventSpy = spyOnEvent('#some_element', 'click');
        $('#some_element').trigger('click');
        expect('click').toHaveBeenTriggeredOn('#some_element');
        expect(eventSpy).toHaveBeenTriggered();
    });

Doesn't work 1:

    // Non-working test 1 -- target: '#some_element', event name: 'clickz'
    it('non-working test 1', function () {
        var eventSpy = spyOnEvent('#some_element', 'clickz');
        $('#some_element').trigger('clickz');
        expect('clickz').toHaveBeenTriggeredOn('#some_element'); // Expected event [object Object] to have been triggered on #some_element
        expect(eventSpy).toHaveBeenTriggered();                  // Expected event clickz to have been triggered on #some_element
    });

Doesn't work 2:

    // Non-working test 2 -- target: document, event name: 'click'
    it('no-working test 2', function () {
        var eventSpy = spyOnEvent(document, 'click');
        $(document).trigger('click');
        expect('click').toHaveBeenTriggeredOn(document); // Expected event [object Object] to have been triggered on [object HTMLDocument]
        expect(eventSpy).toHaveBeenTriggered();          // Expected event click to have been triggered on [object HTMLDocument]
    });
@skip405
Copy link

skip405 commented Feb 18, 2016

Agree with @jschill in that it would be great to use toHaveBeenTriggered() or toHaveBeenTriggeredOn() with custom events.

What does work OK is the toHandle() matcher, as in expect($('.someElemClass')).toHandle("someCustomEvent");

@chesedo
Copy link

chesedo commented Jul 8, 2016

I also just found this to be 'broken'

What works

    setFixtures( '<input />' );
    var input = $( 'input' );
    var spyEvent = spyOnEvent( input, 'click' );
    $( input ).click();
    expect( spyEvent ).toHaveBeenTriggered();

Does not work

    setFixtures( '<input />' );
    var input = $( 'input' );
    var spyEvent = spyOnEvent( input, 'blur' );
    $( input ).blur();
    expect( spyEvent ).toHaveBeenTriggered();

But toHandle() works perfectly as @skip405 mentioned

@mpelzsherman
Copy link

But toHandle() seems to only check that the event is "handled" somewhere in the code. It doesn't check that the event is actually triggered... right?

@nkonev
Copy link

nkonev commented Mar 22, 2017

What I did wrong ?

not works

    it("Input of string value makes nilled=false and it reflects to object", function() {
        expect($el).toBeInDOM();

        const spyEvent = spyOnEvent(colorId, 'change');
        $(colorId).val('green').change();
        $(colorId).trigger('change');
        expect(spyEvent).toHaveBeenTriggered(); 

        expect(attributesObj[SEARCH_CRITERIAS][colorStringIndex][NILLED]).toBeFalsy();
        expect(attributesObj[SEARCH_CRITERIAS][colorStringIndex][STRING_VALUE]).toBe('green');

    });

works

    it("Input of string value makes nilled=false and it reflects to object", function() {
        expect($el).toBeInDOM();

        const spyEvent = spyOnEvent(colorId, 'click');
        $(colorId).val('green').change();
        $(colorId).trigger('click');
        expect(spyEvent).toHaveBeenTriggered(); 

        expect(attributesObj[SEARCH_CRITERIAS][colorStringIndex][NILLED]).toBeFalsy();
        expect(attributesObj[SEARCH_CRITERIAS][colorStringIndex][STRING_VALUE]).toBe('green');

    });

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants