【发布时间】:2014-05-27 19:25:13
【问题描述】:
我正在研究使用 d3.js 生成交互式图表元素
我正在尝试测试由于茉莉花的鼠标悬停事件而导致的填充颜色变化。理想情况下,我希望路径元素的颜色在鼠标悬停时更改为“#ff0000”,但我的 jasmine 终端告诉我这没有发生 - 元素似乎保持与最初设置的颜色相同,例如我的元素 id #1 产生:预期 '#1f77b4' 是 '#ff0000'。
我也在使用 jquery 和 jasmine-jquery 库。
来自我的圆环图夹具的相关代码,DonutChartFixture.html
var color = d3.scale.category20();
var path = svg.selectAll("path")
.data(pie(data))
.enter().append("path").attr("class", "path")
.attr("fill", function(d, i) { return color(i); })
// removed id declaration here
.attr("d", arc)
.on("mouseover", function(){d3.select(this).style("fill", "#ff0000");})
.on("mouseout" , function(){d3.select(this).style("fill", function(d) {
return d.color;
});});
// want to highlight the first path element
var path_one = d3.select('.path').attr("id", "path_one");
还有一个来自我的规范文件 DonutChart.js 的测试
function loadDonutChart() {
loadFixtures('DonutChartFixture.html');
}
describe("Mouseover events", function() {
var spyEvent;
beforeEach(function() {
loadDonutChart();
});
// updated test for element d3.select('.path').attr("id", "path_one")
it("#path_one should change fill colour to rgb(255,0,0)", function() {
spyEvent = spyOnEvent('#path_one', 'mouseover');
$('#path_one').trigger("mouseover");
// expect('mouseover').toHaveBeenTriggeredOn('#path_one');
expect(path_one.style('fill')).toEqual('rgb(255,0,0)');
});
});
如您所见,我尝试按 id 和标签名称选择元素均无济于事,两者都产生与上述相同的读数。干杯。
【问题讨论】:
标签: javascript jquery d3.js jasmine jasmine-jquery