在AngularJS指令中测试mouseover和mouseout事件

问题描述 投票:3回答:1

我试图在D3指令中测试mouseovermouseout事件。这是我试图测试的代码部分:

var nodeEnter = node.enter().append('svg:g')
  .attr('class', 'node')
  .attr('transform', function(d) {
    return 'translate(' + d.x + ',' + d.y + ')';
  })
  .attr('filter', 'url(' + $location.path() + '#drop-shadow)')
  .on('mouseover', function(d) {
    tooltip.transition()
      .duration(200)
      .style('opacity', 0.75);
    tooltip.html(d.email)
      .style('left', (d3.event.pageX - 50) + 'px')
      .style('top', (d3.event.pageY - 50) + 'px');
    d.scale = 1.5;
    tick();
  })
  .on('mouseout', function(d) {
    tooltip.transition()
      .duration(200)
      .style('opacity', 0);
    d.scale = 1;
    tick();
  })

以下是这些特定测试的相关茉莉花代码:

it('should trigger mouse events', function() {
  element.find('.node').triggerHandler('mouseover');
  element.find('.node').triggerHandler('mouseout');
});

应该在这些鼠标事件上调用的函数在我的代码覆盖中保持红色,如果它们从未被触发过。任何人都知道为什么会这样?

javascript angularjs d3.js jasmine karma-runner
1个回答
1
投票

This为我效劳:

使用jQuery:

beforeEach(function(){
    $.fn.triggerSVGEvent = function(eventName) {
        var event = document.createEvent('SVGEvents');
        event.initEvent(eventName, true, true);
        this[0].dispatchEvent(event);
        return $(this)
    }
})

然后在测试中:

it('should trigger mouse events', function() {
    $(yourSelector).triggerSVGEvent('mouseover');
    $(yourSelector).triggerSVGEvent('mouseout');
})
© www.soinside.com 2019 - 2024. All rights reserved.