Home How to trigger element.on('mouseleave') in a Uint Test to call my AngularJS function
Reply: 0

How to trigger element.on('mouseleave') in a Uint Test to call my AngularJS function

user1429
1#
user1429 Published in April 21, 2018, 3:24 pm

I want to call myFunction. Then I want to verify it was called with a Unit Test.

Javascript:

var self = this;

self.myFunction= function myFunction() {
    // do stuff
};

self.onClickChange = function onClickChange() {
    $('.checkBox').on('mouseleave', function(){
        self.myFunction();
    });
};

I have tried this test:

it('should call myFunction', inject(function ($compile, $rootScope) {
    spyOn(this.instance, 'onClickChange').and.callThrough();
    spyOn(this.instance, 'myFunction').and.callThrough();

    this.instance.onClickChange('messageBoard');

    var scope = $rootScope.$new(),
    element = $compile('<md-checkbox class="checkBox"></md-checkbox>')(scope);

    element.triggerHandler('mouseleave');
    scope.$digest();

    expect(this.instance.myFunction).toHaveBeenCalled();
}));

I have also tried using

scope.$broadcast('mouseleave');

And

var event = document.createEvent('MouseEvents');
event.initEvent('mouseleave', true, true);
$('.checkBox').dispatchEvent(event);

And this instead of the last line in the previous three lines:

document.body.dispatchEvent(event);

How can I get .on('mouseleave') to work and ferify that myFunction has been called?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305485 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO