I'm having trouble trying to get my test to pass. I would like to be able to use a spy to check whether or not the mouseover event was correctly called. Currently I am getting the following error, "Error: expected on to have been called at least once but was never called". Part of the confusion on my part relates to the differences between d3 and jQuery selectors, I'm more than happy to use the latter, but I'm not sure how to correctly use the former in a test to get the result I want.
My dependencies are d3, jQuery, mocha, chai, sinon, and sinon-chai.
Relevant code from my index.html file,
<script src="fixtures.js"></script>
<div id="mocha"></div>
<script src="mocha.js"></script>
<script src="chai.js"></script>
<script src="sinon-chai.js"></script>
<script src="sinon-1.10.2.js"></script>
<script>
mocha.ui('bdd');
mocha.reporter('html');
var expect = chai.expect;
</script>
<script src="tests.js"></script>
<script>
mocha.run();
</script>
fixtures.js,
var path = svg.selectAll("path")
.data(pie(data))
.enter().append("path").attr("class", "path")
.style("fill", function(d, i) { return color(i); })
.attr("d", arc)
.on("mouseover", function() { d3.select(this).style("fill", "#ff0000"); })
.on("mouseout" , function() { d3.selectAll("path").style("fill", function(d, i) { return color(i); }); });
// I wanted to test my understanding of d3 selectors
var path_one = d3.select('.path').attr("id", "path_one");
tests.js,
describe('Donut Chart', function() {
describe("Mouseover events", function() {
it("should call on('mouseover')", function () {
var spy = sinon.spy(path, "on");
$('path').mouseenter();
sinon.assert.called(spy);
});
});
});
<path>elements in your DOM? What do you get if you call$('path')in the JS console? - Pavling$('path').mouseover();? (since your test is talking about mouseover rather than mouseenter) - Pavling$('path').mouseover();- Pavling