【问题标题】:Jasmine testing mouseover events in d3Jasmine 在 d3 中测试鼠标悬停事件
【发布时间】: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


    【解决方案1】:

    所以我想出了一个通过测试,很大程度上受到此处提供的答案Testing Mouseover event in D3 with Sinon 的启发。唯一真正的区别是使用 d3/jasmine 语法代替 Sinon。以下正确通过:

    it("#path_one should change fill colour to rgb(255,0,0)", function() {
        var oldColor = path.style('fill');
        document.getElementById('path_one').dispatchEvent(new MouseEvent('mouseover'));
        expect(d3.select('#path_one').style('fill')).not.toEqual(oldColor);
    });
    

    【讨论】:

      【解决方案2】:

      在代码中设置样式,在测试中设置属性。尝试更改为两者之一。此外,浏览器很可能会将十六进制颜色字符串强制转换为 rgb 颜色字符串。因此,如果将颜色设置为 '#ff0000',它可能会被强制转换为 'rgb(255, 0, 0)'。最后,jQuery 的 trigger 不会触发使用 d3 的 on 注册的事件。详情请见https://github.com/mbostock/d3/issues/100

      【讨论】:

      • 我已经做出了建议的更改,但它似乎并没有让我得到正确的答案。使用 $('path') 的测试现在给出: Expected 'rgb(31, 119, 180)' to equal '#ff0000' 使用 $('#1') 的测试现在给出: TypeError: undefined is not a function
      • 我刚刚注意到您在输入后立即设置属性填充。这与您现在的鼠标悬停和测试是否一致?
      • 我不确定您的确切意思。我可以看到您指的是 DonutChartFixture.html 的第六行,但不确定这与测试有何关系。代码如给出。
      • 因为你是先设置属性fill,然后当你鼠标悬停时,你是通过css使用style设置它。然后您正在测试 atttribue fill 是否等于您通过 style 设置的内容。这不会过去的。此外,d3 会将十六进制颜色字符串强制转换为 rgb 字符串,因此您必须编写测试来检查颜色的 rgb 字符串。结帐:plnkr.co/edit/XX1VGNBqcnE7haocUxEB?p=preview
      • 感谢您的示例。我进行了建议的更改,并进行了一些更改以确保选择正确的元素:我已将第一个路径元素标记为 d3.select('.path') 默认返回 id 为 '#path_one'所以我可以通过 id 更正选择该元素。但是,虽然我得到了一个像 expect('mouseover').toHaveBeenTriggeredOn('#path_one') 这样的测试才能正确通过,但我仍然得到了不正确的颜色比较: Expected 'rgb(31, 119, 180)' to equal 'rgb (255,0,0)'。
      猜你喜欢
      • 2013-09-30
      • 1970-01-01
      • 2018-10-28
      • 2015-04-22
      • 1970-01-01
      • 2015-03-11
      • 1970-01-01
      • 2022-01-20
      • 2018-08-13
      相关资源
      最近更新 更多