我正在尝试测试 D3 指令中的 mouseover
和 mouseout
事件。这是我要测试的代码部分:
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();
})
以下是这些特定测试的相关 jasmine 代码:
it('should trigger mouse events', function() {
element.find('.node').triggerHandler('mouseover');
element.find('.node').triggerHandler('mouseout');
});
应该在这些鼠标事件上调用的函数在我的代码覆盖范围内保持红色,前提是它们从未被触发过。有人知道为什么会这样吗?
最佳答案
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');
})
关于javascript - 在 AngularJS 指令中测试 mouseover 和 mouseout 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33919908/