Jasmine Test for ng-mouseenter

女生的网名这么多〃 提交于 2019-12-08 04:45:10

问题


I'm having trouble testing my directive with an ng-mouseenter directive.

I'd like to test several things, but first off, I need to test that the method supplied to ng-mouseenter is called.

my test:

describe('hover tests', function () {
        it('the triggerPopover method should be called on hover', function() {
            spyOn($scope, 'triggerPopover');
            var ars = jQuery(view.find('article.the-class-im-looking-for'));
            jQuery(ars[0]).trigger('mouseenter');
            expect($scope.triggerPopover).toHaveBeenCalled();
        });
    });

my directive use:

<article my-directive ng-mouseenter="triggerPopover();"></article>

Result:

Expected spy triggerPopover to have been called. The ng-mouseenter stuff doesn't seem to get called


回答1:


If you are using PhantomJS for your tests, some mouse events that work in normal browsers will not work. If indeed that's the case, you can run your test with Chrome or Firefox, or alternatively implement a similar solution to this answer for a related question.




回答2:


mouseenter as a native DOM event is not supported by all browsers: http://www.quirksmode.org/dom/events/index.html

ng-mouseenter builds on the mouseover event, so you can just trigger it like this:

jQuery(ars[0]).trigger('mouseover');


来源:https://stackoverflow.com/questions/26265900/jasmine-test-for-ng-mouseenter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!